硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2519|回复: 9
收起左侧

[有问必答] STM32F205 SPI DMA 发送不出数据

[复制链接]

10

主题

38

回帖

68

积分

初级会员

积分
68
发表于 2018-1-10 14:22:42 | 显示全部楼层 |阅读模式
STM32F205 SPI DMA 发送不出数据,代码如下,请帮忙看下,谢谢 !
  1. GPIO_InitTypeDef GPIO_InitStructure;
  2.         SPI_InitTypeDef  SPI_InitStructure;
  3.         DMA_InitTypeDef       DMA_InitStructure;
  4.     //int ii =0;
  5.         /*!< Enable SPI2 clocks */
  6.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
  7.         /*!< Enable GPIO clocks */
  8.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  9.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
  10.         /*!< Connect SPI pins to AF5 */
  11.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource13,GPIO_AF_SPI2);
  12.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource14,GPIO_AF_SPI2);
  13.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource15,GPIO_AF_SPI2);

  14.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  15.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  16.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  17.         GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
  18.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  19.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  20.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  21.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  22.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  23.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  24.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_NOPULL;
  25.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  26.         //Cdfinger_BspCs_HIGH();
  27.         //Cdfinger_Bspdelay_ms(1);
  28.     Cdfinger_BspCs_LOW();

  29.         SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  30.         SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  31.         SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  32.         SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  33.         SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  34.         SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  35.         SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  36.         SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  37.         SPI_InitStructure.SPI_CRCPolynomial = 7;
  38.         SPI_Init(SPI2, &SPI_InitStructure);
  39.         SPI_Cmd(SPI2, ENABLE);


  40.         /* DMA1 Stream0 channel4 spi tx configuration **************************************/
  41.         DMA_DeInit(DMA1_Stream4);
  42.         DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  43.         DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR);
  44.         DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&cdfingerimgtxbuf[0];
  45.         DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  46.         DMA_InitStructure.DMA_BufferSize = 28*1024;
  47.         DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//;
  48.         DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  49.         DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  50.         DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  51.         DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// ;
  52.         DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//DMA_Priority_High;
  53.         DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//DMA_FIFOMode_Enable;
  54.         DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  55.         DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  56.         DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  57.         DMA_Init(DMA1_Stream4, &DMA_InitStructure);
  58.         memset(cdfingerimgtxbuf,0x33,28*1024);

  59.     SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
  60.     DMA_Cmd(DMA1_Stream4, ENABLE);
  61.     while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
复制代码



回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106856
QQ
发表于 2018-1-11 00:56:58 | 显示全部楼层
这个有个官方做的F4板子SPI DMA双板通信例子,你对比下看看。F2的DMA配置库跟F4差不多。
main.h
  1. /**
  2.   ******************************************************************************
  3.   * @file    SPI/SPI_TwoBoards/SPI_DataExchangeDMA/main.h
  4.   * @author  MCD Application Team
  5.   * @version V1.7.0
  6.   * @date    22-April-2016
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>&#169; COPYRIGHT 2016 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */

  27. /* Define to prevent recursive inclusion -------------------------------------*/
  28. #ifndef __MAIN_H
  29. #define __MAIN_H

  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif

  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f4xx.h"

  35. #if defined (USE_STM324xG_EVAL)
  36.   #include "stm324xg_eval.h"

  37. #elif defined (USE_STM324x7I_EVAL)
  38.   #include "stm324x7i_eval.h"
  39.    
  40. #else
  41. #error "Please select first the Evaluation board used in your application (in Project Options)"
  42. #endif

  43. /* Exported typedef ----------------------------------------------------------*/
  44. #define countof(a)   (sizeof(a) / sizeof(*(a)))
  45. typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;

  46. /* Exported define -----------------------------------------------------------*/
  47. /* Uncomment the line below if you will use the SPI peripheral as a Master */
  48. /* #define SPI_MASTER */
  49. /* Uncomment the line below if you will use the SPI peripheral as a Slave */
  50. #define SPI_SLAVE  

  51. /* USER_TIMEOUT value for waiting loops. This timeout is just guarantee that the
  52.    application will not remain stuck if the USART communication is corrupted.
  53.    You may modify this timeout value depending on CPU frequency and application
  54.    conditions (interrupts routines, number of data to transfer, baudrate, CPU
  55.    frequency...). */
  56. #define USER_TIMEOUT                    ((uint32_t)0x64) /* Waiting 1s */

  57. /* SPIx Communication boards Interface */
  58. #if defined (USE_STM324xG_EVAL)
  59.   #define SPIx                           SPI2
  60.   #define SPIx_CLK                       RCC_APB1Periph_SPI2
  61.   #define SPIx_CLK_INIT                  RCC_APB1PeriphClockCmd
  62.   #define SPIx_IRQn                      SPI2_IRQn
  63.   #define SPIx_IRQHANDLER                SPI2_IRQHandler

  64.   #define SPIx_SCK_PIN                   GPIO_Pin_1
  65.   #define SPIx_SCK_GPIO_PORT             GPIOI
  66.   #define SPIx_SCK_GPIO_CLK              RCC_AHB1Periph_GPIOI
  67.   #define SPIx_SCK_SOURCE                GPIO_PinSource1
  68.   #define SPIx_SCK_AF                    GPIO_AF_SPI2

  69.   #define SPIx_MISO_PIN                  GPIO_Pin_2
  70.   #define SPIx_MISO_GPIO_PORT            GPIOI
  71.   #define SPIx_MISO_GPIO_CLK             RCC_AHB1Periph_GPIOI
  72.   #define SPIx_MISO_SOURCE               GPIO_PinSource2
  73.   #define SPIx_MISO_AF                   GPIO_AF_SPI2

  74.   #define SPIx_MOSI_PIN                  GPIO_Pin_3
  75.   #define SPIx_MOSI_GPIO_PORT            GPIOI
  76.   #define SPIx_MOSI_GPIO_CLK             RCC_AHB1Periph_GPIOI
  77.   #define SPIx_MOSI_SOURCE               GPIO_PinSource3
  78.   #define SPIx_MOSI_AF                   GPIO_AF_SPI2

  79.   #define SPIx_DMA                       DMA1
  80.   #define SPIx_DMA_CLK                   RCC_AHB1Periph_DMA1
  81.   #define SPIx_TX_DMA_CHANNEL            DMA_Channel_0
  82.   #define SPIx_TX_DMA_STREAM             DMA1_Stream4
  83.   #define SPIx_TX_DMA_FLAG_TCIF          DMA_FLAG_TCIF4
  84.   #define SPIx_RX_DMA_CHANNEL            DMA_Channel_0
  85.   #define SPIx_RX_DMA_STREAM             DMA1_Stream3
  86.   #define SPIx_RX_DMA_FLAG_TCIF          DMA_FLAG_TCIF3

  87. #endif /* USE_STM324xG_EVAL */

  88. #if defined (USE_STM324x7I_EVAL)
  89.   #define SPIx                           SPI2
  90.   #define SPIx_CLK                       RCC_APB1Periph_SPI2
  91.   #define SPIx_CLK_INIT                  RCC_APB1PeriphClockCmd
  92.   #define SPIx_IRQn                      SPI2_IRQn
  93.   #define SPIx_IRQHANDLER                SPI2_IRQHandler

  94.   #define SPIx_SCK_PIN                   GPIO_Pin_1
  95.   #define SPIx_SCK_GPIO_PORT             GPIOI
  96.   #define SPIx_SCK_GPIO_CLK              RCC_AHB1Periph_GPIOI
  97.   #define SPIx_SCK_SOURCE                GPIO_PinSource1
  98.   #define SPIx_SCK_AF                    GPIO_AF_SPI2

  99.   #define SPIx_MISO_PIN                  GPIO_Pin_2
  100.   #define SPIx_MISO_GPIO_PORT            GPIOI
  101.   #define SPIx_MISO_GPIO_CLK             RCC_AHB1Periph_GPIOI
  102.   #define SPIx_MISO_SOURCE               GPIO_PinSource2
  103.   #define SPIx_MISO_AF                   GPIO_AF_SPI2

  104.   #define SPIx_MOSI_PIN                  GPIO_Pin_3
  105.   #define SPIx_MOSI_GPIO_PORT            GPIOI
  106.   #define SPIx_MOSI_GPIO_CLK             RCC_AHB1Periph_GPIOI
  107.   #define SPIx_MOSI_SOURCE               GPIO_PinSource3
  108.   #define SPIx_MOSI_AF                   GPIO_AF_SPI2

  109.   #define SPIx_DMA                       DMA1
  110.   #define SPIx_DMA_CLK                   RCC_AHB1Periph_DMA1
  111.   #define SPIx_TX_DMA_CHANNEL            DMA_Channel_0
  112.   #define SPIx_TX_DMA_STREAM             DMA1_Stream4
  113.   #define SPIx_TX_DMA_FLAG_TCIF          DMA_FLAG_TCIF4
  114.   #define SPIx_RX_DMA_CHANNEL            DMA_Channel_0
  115.   #define SPIx_RX_DMA_STREAM             DMA1_Stream3
  116.   #define SPIx_RX_DMA_FLAG_TCIF          DMA_FLAG_TCIF3

  117. #endif /* USE_STM324x7I_EVAL */

  118. #define BUFFERSIZE                       100

  119. /* Exported types ------------------------------------------------------------*/
  120. /* Exported constants --------------------------------------------------------*/
  121. /* Exported macro ------------------------------------------------------------*/
  122. /* Exported functions ------------------------------------------------------- */

  123. #ifdef __cplusplus
  124. }
  125. #endif

  126. #endif /* __MAIN_H */

  127. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
main.C
  1. /**
  2.   ******************************************************************************
  3.   * @file    SPI/SPI_TwoBoards/SPI_DataExchangeDMA/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.7.0
  6.   * @date    22-April-2016
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>&#169; COPYRIGHT 2016 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */

  27. /* Includes ------------------------------------------------------------------*/
  28. #include "main.h"

  29. /** @addtogroup STM32F4xx_StdPeriph_Examples
  30.   * @{
  31.   */

  32. /** @addtogroup SPI_DataExchangeDMA
  33.   * @{
  34.   */

  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. uint8_t aTxBuffer[BUFFERSIZE] = "SPI Master/Slave : Communication between two SPI using DMA";
  40. __IO uint8_t aRxBuffer [BUFFERSIZE];
  41. __IO uint8_t ubRxIndex = 0;
  42. __IO uint8_t ubTxIndex = 0;
  43. __IO uint32_t TimeOut = 0;

  44. SPI_InitTypeDef  SPI_InitStructure;

  45. /* Private function prototypes -----------------------------------------------*/
  46. static void SPI_Config(void);
  47. static void SysTickConfig(void);
  48. static TestStatus Buffercmp(uint8_t* pBuffer1, __IO uint8_t* pBuffer2, uint16_t BufferLength);

  49. /* Private functions ---------------------------------------------------------*/

  50. /**
  51.   * @brief  Main program
  52.   * @param  None
  53.   * @retval None
  54.   */
  55. int main(void)
  56. {
  57.   /*!< At this stage the microcontroller clock setting is already configured,
  58.        this is done through SystemInit() function which is called from startup
  59.        files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
  60.        before to branch to application main.
  61.      */
  62.   
  63.   /* SPI configuration */
  64.   SPI_Config();
  65.   
  66.   /* SysTick configuration */
  67.   SysTickConfig();
  68.   
  69.   /* Initialize LEDs mounted on EVAL board */     
  70.   STM_EVAL_LEDInit(LED1);
  71.   STM_EVAL_LEDInit(LED2);
  72.   STM_EVAL_LEDInit(LED3);
  73.   STM_EVAL_LEDInit(LED4);
  74.   
  75. #ifdef SPI_MASTER
  76.   /* Master board configuration */   
  77.   /* Initializes the SPI communication */
  78.   SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  79.   SPI_Init(SPIx, &SPI_InitStructure);
  80.   
  81.   /* The Data transfer is performed in the SPI using Direct Memory Access */

  82.   /* Enable DMA SPI TX Stream */
  83.   DMA_Cmd(SPIx_TX_DMA_STREAM,ENABLE);

  84.   /* Enable DMA SPI RX Stream */
  85.   DMA_Cmd(SPIx_RX_DMA_STREAM,ENABLE);

  86.   /* Enable SPI DMA TX Requsts */
  87.   SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE);

  88.   /* Enable SPI DMA RX Requsts */
  89.   SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE);
  90.   
  91.   /* Configure the Tamper Button */
  92.   STM_EVAL_PBInit(BUTTON_TAMPER,BUTTON_MODE_GPIO);
  93.   
  94.   /* Wait until Tamper Button is pressed */
  95.   while (STM_EVAL_PBGetState(BUTTON_TAMPER));
  96.   
  97.   /* Enable the SPI peripheral */
  98.   SPI_Cmd(SPIx, ENABLE);
  99.   
  100. #endif /* SPI_MASTER */
  101.   
  102. #ifdef SPI_SLAVE
  103.   /* Slave board configuration */
  104.   /* Initializes the SPI communication */
  105.   SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  106.   SPI_Init(SPIx, &SPI_InitStructure);

  107.   /* Enable DMA SPI TX Stream */
  108.   DMA_Cmd(SPIx_TX_DMA_STREAM,ENABLE);

  109.   /* Enable DMA SPI RX Stream */
  110.   DMA_Cmd(SPIx_RX_DMA_STREAM,ENABLE);  
  111.   
  112.   /* Enable SPI DMA TX Requsts */
  113.   SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE);

  114.   /* Enable SPI DMA RX Requsts */
  115.   SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE);

  116.   /* Enable the SPI peripheral */
  117.   SPI_Cmd(SPIx, ENABLE);

  118. #endif /* SPI_SLAVE */

  119.   /* Waiting the end of Data transfer */
  120.   while (DMA_GetFlagStatus(SPIx_TX_DMA_STREAM,SPIx_TX_DMA_FLAG_TCIF)==RESET);
  121.   while (DMA_GetFlagStatus(SPIx_RX_DMA_STREAM,SPIx_RX_DMA_FLAG_TCIF)==RESET);
  122.   
  123.   /* Clear DMA Transfer Complete Flags */
  124.   DMA_ClearFlag(SPIx_TX_DMA_STREAM,SPIx_TX_DMA_FLAG_TCIF);
  125.   DMA_ClearFlag(SPIx_RX_DMA_STREAM,SPIx_RX_DMA_FLAG_TCIF);  
  126.   
  127.   /* Disable DMA SPI TX Stream */
  128.   DMA_Cmd(SPIx_TX_DMA_STREAM,DISABLE);

  129.   /* Disable DMA SPI RX Stream */
  130.   DMA_Cmd(SPIx_RX_DMA_STREAM,DISABLE);  
  131.   
  132.   /* Disable SPI DMA TX Requsts */
  133.   SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, DISABLE);

  134.   /* Disable SPI DMA RX Requsts */
  135.   SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE);

  136.   /* Disable the SPI peripheral */
  137.   SPI_Cmd(SPIx, DISABLE);  
  138.   
  139.   if (Buffercmp(aTxBuffer, aRxBuffer, BUFFERSIZE) != FAILED)
  140.   {
  141.     /* Turn ON LED1 and LED3 */
  142.     STM_EVAL_LEDOn(LED1);
  143.     STM_EVAL_LEDOn(LED3);
  144.     /* Turn OFF LED2 and LED4 */
  145.     STM_EVAL_LEDOff(LED2);
  146.     STM_EVAL_LEDOff(LED4);
  147.   }
  148.   else
  149.   {
  150.     /* Turn OFF LED1 and LED3 */
  151.     STM_EVAL_LEDOff(LED1);
  152.     STM_EVAL_LEDOff(LED3);
  153.     /* Turn ON LED2 and LED4 */
  154.     STM_EVAL_LEDOn(LED2);
  155.     STM_EVAL_LEDOn(LED4);   
  156.   }
  157.   
  158.   /* Infinite Loop */
  159.   while (1)
  160.   {
  161.   }  
  162. }

  163. /**
  164.   * @brief  Configures the SPI Peripheral.
  165.   * @param  None
  166.   * @retval None
  167.   */
  168. static void SPI_Config(void)
  169. {
  170.   GPIO_InitTypeDef GPIO_InitStructure;
  171.   DMA_InitTypeDef DMA_InitStructure;

  172.   /* Peripheral Clock Enable -------------------------------------------------*/
  173.   /* Enable the SPI clock */
  174.   SPIx_CLK_INIT(SPIx_CLK, ENABLE);
  175.   
  176.   /* Enable GPIO clocks */
  177.   RCC_AHB1PeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
  178.   
  179.   /* Enable DMA clock */
  180.   RCC_AHB1PeriphClockCmd(SPIx_DMA_CLK, ENABLE);

  181.   /* SPI GPIO Configuration --------------------------------------------------*/
  182.   /* GPIO Deinitialisation */
  183.   GPIO_DeInit(SPIx_SCK_GPIO_PORT);
  184.   GPIO_DeInit(SPIx_MISO_GPIO_PORT);
  185.   GPIO_DeInit(SPIx_MOSI_GPIO_PORT);
  186.   
  187.   /* Connect SPI pins to AF5 */  
  188.   GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
  189.   GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);   
  190.   GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);

  191.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  192.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  193.   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  194.   GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;

  195.   /* SPI SCK pin configuration */
  196.   GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN;
  197.   GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
  198.   
  199.   /* SPI  MISO pin configuration */
  200.   GPIO_InitStructure.GPIO_Pin =  SPIx_MISO_PIN;
  201.   GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStructure);  

  202.   /* SPI  MOSI pin configuration */
  203.   GPIO_InitStructure.GPIO_Pin =  SPIx_MOSI_PIN;
  204.   GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStructure);

  205.   /* SPI configuration -------------------------------------------------------*/
  206.   SPI_I2S_DeInit(SPIx);
  207.   SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  208.   SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  209.   SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  210.   SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  211.   SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  212.   SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  213.   SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  214.   SPI_InitStructure.SPI_CRCPolynomial = 7;
  215.   
  216.   /* DMA configuration -------------------------------------------------------*/
  217.   /* Deinitialize DMA Streams */
  218.   DMA_DeInit(SPIx_TX_DMA_STREAM);
  219.   DMA_DeInit(SPIx_RX_DMA_STREAM);
  220.   
  221.   /* Configure DMA Initialization Structure */
  222.   DMA_InitStructure.DMA_BufferSize = BUFFERSIZE ;
  223.   DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable ;
  224.   DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ;
  225.   DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single ;
  226.   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  227.   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  228.   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  229.   DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) (&(SPIx->DR)) ;
  230.   DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  231.   DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  232.   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  233.   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
  234.   /* Configure TX DMA */
  235.   DMA_InitStructure.DMA_Channel = SPIx_TX_DMA_CHANNEL ;
  236.   DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral ;
  237.   DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aTxBuffer ;
  238.   DMA_Init(SPIx_TX_DMA_STREAM, &DMA_InitStructure);
  239.   /* Configure RX DMA */
  240.   DMA_InitStructure.DMA_Channel = SPIx_RX_DMA_CHANNEL ;
  241.   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory ;
  242.   DMA_InitStructure.DMA_Memory0BaseAddr =(uint32_t)aRxBuffer ;
  243.   DMA_Init(SPIx_RX_DMA_STREAM, &DMA_InitStructure);
  244.   
  245. }

  246. /**
  247.   * @brief  Configure a SysTick Base time to 10 ms.
  248.   * @param  None
  249.   * @retval None
  250.   */
  251. static void SysTickConfig(void)
  252. {
  253.   /* Setup SysTick Timer for 10ms interrupts  */
  254.   if (SysTick_Config(SystemCoreClock / 100))
  255.   {
  256.     /* Capture error */
  257.     while (1);
  258.   }

  259.   /* Configure the SysTick handler priority */
  260.   NVIC_SetPriority(SysTick_IRQn, 0x0);
  261. }


  262. /**
  263.   * @brief  Compares two buffers.
  264.   * @param  pBuffer1, pBuffer2: buffers to be compared.
  265.   * @param  BufferLength: buffer's length
  266.   * @retval PASSED: pBuffer1 identical to pBuffer2
  267.   *         FAILED: pBuffer1 differs from pBuffer2
  268.   */
  269. static TestStatus Buffercmp(uint8_t* pBuffer1, __IO uint8_t* pBuffer2, uint16_t BufferLength)
  270. {
  271.   while (BufferLength--)
  272.   {
  273.     if (*pBuffer1 != *pBuffer2)
  274.     {
  275.       return FAILED;
  276.     }
  277.     pBuffer1++;
  278.     pBuffer2++;
  279.   }

  280.   return PASSED;
  281. }

  282. #ifdef  USE_FULL_ASSERT
  283. /**
  284.   * @brief  Reports the name of the source file and the source line number
  285.   *         where the assert_param error has occurred.
  286.   * @param  file: pointer to the source file name
  287.   * @param  line: assert_param error line source number
  288.   * @retval None
  289.   */
  290. void assert_failed(uint8_t* file, uint32_t line)
  291. {
  292.   /* User can add his own implementation to report the file name and line number,
  293.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  294.   /* Infinite loop */
  295.   while (1)
  296.   {}
  297. }
  298. #endif

  299. /**
  300.   * @}
  301.   */

  302. /**
  303.   * @}
  304.   */

  305. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码


回复

使用道具 举报

10

主题

38

回帖

68

积分

初级会员

积分
68
 楼主| 发表于 2018-1-12 10:31:59 | 显示全部楼层
eric2013 发表于 2018-1-11 00:56
这个有个官方做的F4板子SPI DMA双板通信例子,你对比下看看。F2的DMA配置库跟F4差不多。
main.h
main.C

感谢!!!
回复

使用道具 举报

10

主题

38

回帖

68

积分

初级会员

积分
68
 楼主| 发表于 2018-1-13 15:17:44 | 显示全部楼层
eric2013 发表于 2018-1-11 00:56
这个有个官方做的F4板子SPI DMA双板通信例子,你对比下看看。F2的DMA配置库跟F4差不多。
main.h
main.C

现在能发和能收了但是发送大数据量的时候接收和发送就不匹配了。将MISO和MOSI短路进行测试,前面的数据正常后面的就异常了,代码如下,烦请帮忙看看,谢谢 !
  1. /*****************************************
  2. 函数名称:Cdfinger_Bspspi_init
  3. 函数作用:spi初始化函数
  4. 备注:
  5. ******************************************/
  6. void Cdfinger_Bspspi_init(void)
  7. {
  8.         GPIO_InitTypeDef GPIO_InitStructure;
  9.         SPI_InitTypeDef  SPI_InitStructure;
  10.         DMA_InitTypeDef  DMA_InitStructure;
  11.         NVIC_InitTypeDef NVIC_InitStructure;
  12.   
  13.         int ii =0;
  14.        
  15.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  16.         /*!< Enable SPI2 clocks */
  17.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
  18.         /*!< Enable GPIO clocks */
  19.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
  20.         /*!< Connect SPI pins to AF5 */
  21.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource13,GPIO_AF_SPI2);
  22.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource14,GPIO_AF_SPI2);
  23.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource15,GPIO_AF_SPI2);

  24.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  25.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  26.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  27.         GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
  28.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  29.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  30.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  31.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  32.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  33.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  34.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_NOPULL;
  35.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  36.         Cdfinger_BspCs_HIGH();
  37.         //Cdfinger_Bspdelay_ms(1);
  38.   //Cdfinger_BspCs_LOW();
  39.         SPI_Cmd(SPI2,DISABLE);
  40.         SPI_DeInit(SPI2);
  41.         SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  42.         SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  43.         SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  44.         SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  45.         SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  46.         SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//;
  47.         SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  48.         SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  49.         SPI_InitStructure.SPI_CRCPolynomial = 7;
  50.         SPI_Init(SPI2, &SPI_InitStructure);

  51.   memset(&cdfingerimgtxbuf[0],0x55,COMMUNICATIONLEN);
  52.         /* DMA1 Stream0 channel4 spi tx configuration **************************************/
  53.         DMA_DeInit(DMA1_Stream4);
  54.         DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  55.         DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR);
  56.         DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&cdfingerimgtxbuf[0];
  57.         DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  58.         DMA_InitStructure.DMA_BufferSize = COMMUNICATIONLEN;
  59.         DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//;
  60.         DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  61.         DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  62.         DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  63.         DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// ;
  64.         DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//DMA_Priority_Medium;//DMA_Priority_High;
  65.         DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//;//DMA_FIFOMode_Enable;
  66.         DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  67.         DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  68.         DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  69.         DMA_Init(DMA1_Stream4, &DMA_InitStructure);
  70.   
  71.         /* DMA1 Stream0 channel3 spi rx configuration **************************************/
  72.         DMA_DeInit(DMA1_Stream3);
  73.         DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  74.         DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR);
  75.         DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&cdfingerimgrxbuf[0];//(uint32_t)&cdfingerimgrxbuf[0];
  76.         DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  77.         DMA_InitStructure.DMA_BufferSize =COMMUNICATIONLEN;
  78.         DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  79.         DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  80.         DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  81.         DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  82.         DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA_Mode_Circular;
  83.         DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//DMA_Priority_Medium;
  84.         DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//DMA_FIFOMode_Disable;
  85.         DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  86.         DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  87.         DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  88.         DMA_Init(DMA1_Stream3, &DMA_InitStructure);
  89.         //Cdfinger_BspCs_HIGH();
  90.        
  91.         //发送中断
  92.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  93.         NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;     
  94.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  95.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;
  96.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  97.         NVIC_Init(&NVIC_InitStructure);
  98.        
  99.         //接收中断
  100.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  101.         NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream3_IRQn;     
  102.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  103.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x04;
  104.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  105.         NVIC_Init(&NVIC_InitStructure);
  106.        
  107. //        SPI_TIModeCmd(SPI2,ENABLE);
  108. //        SPI_Cmd(SPI2, ENABLE);
  109.         DMA_ITConfig(DMA1_Stream4,DMA_IT_TC,ENABLE);
  110.         DMA_ITConfig(DMA1_Stream3,DMA_IT_TC,ENABLE);
  111.    
  112. //        SPI_I2S_ITConfig(SPI2,SPI_I2S_IT_TXE|SPI_I2S_IT_RXNE,ENABLE);
  113.         SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx|SPI_I2S_DMAReq_Rx, ENABLE);
  114.   DMA_Cmd(DMA1_Stream3, ENABLE);
  115.         DMA_Cmd(DMA1_Stream4, ENABLE);
  116.         SPI_Cmd(SPI2, ENABLE);
  117.         for(ii=0;ii<COMMUNICATIONLEN;ii++)
  118.         {
  119.           if(ii%8==0)
  120.                 {
  121.                                 printf("\r\n");
  122.                 }
  123.                 printf("  0x%x",cdfingerimgrxbuf[ii]);
  124.         }
  125.         printf("111\r\n");
  126. }
  127. void DMA1_Stream4_IRQHandler(void)
  128. {
  129.   if(DMA_GetITStatus(DMA1_Stream4,DMA_IT_TCIF4) != RESET)
  130.   {
  131.          // printf("DMA1_Stream4_IRQHandler = %d \r\n",DMA_GetCurrDataCounter(DMA1_Stream3));
  132.     DMA_Cmd(DMA1_Stream4, DISABLE);
  133.                 DMA_ClearITPendingBit(DMA1_Stream4,DMA_IT_TCIF4);
  134.           DMA_ClearFlag(DMA1_Stream4,DMA_IT_TCIF4);
  135.   }
  136. }

  137. void DMA1_Stream3_IRQHandler(void)
  138. {
  139.   if(DMA_GetITStatus(DMA1_Stream3,DMA_IT_TCIF3) != RESET)
  140.   {
  141.         //  printf("DMA1_Stream3_IRQHandler = %d \r\n",DMA_GetCurrDataCounter(DMA1_Stream4));
  142.     DMA_Cmd(DMA1_Stream3, DISABLE);
  143.                 DMA_ClearITPendingBit(DMA1_Stream3,DMA_IT_TCIF3);
  144.           DMA_ClearFlag(DMA1_Stream3,DMA_IT_TCIF3);
  145.   }
  146. }
复制代码
回复

使用道具 举报

10

主题

38

回帖

68

积分

初级会员

积分
68
 楼主| 发表于 2018-1-16 13:21:26 | 显示全部楼层
我重新回复一下也方便其他人,由于SPI操作慢于SPI的操作,所以导致接收的数据错误,测试 28k 数据通信正常的代码如下:
  1. /*****************************************
  2. 函数名称:Cdfinger_Bspspi_init
  3. 函数作用:spi初始化函数
  4. 备注:
  5. ******************************************/
  6. void Cdfinger_Bspspi_init(void)
  7. {
  8.         GPIO_InitTypeDef GPIO_InitStructure;
  9.         SPI_InitTypeDef  SPI_InitStructure;
  10.         DMA_InitTypeDef  DMA_InitStructure;
  11.         NVIC_InitTypeDef NVIC_InitStructure;
  12.          
  13.     memset(&cdfingerimgtxbuf[0],0x55,COMMUNICATIONLEN);
  14.         memset(&cdfingerimgrxbuf[0],0xa2,COMMUNICATIONLEN);
  15.        
  16.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  17.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);

  18.         /*!< Enable SPI2 clocks */
  19.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
  20.         /*!< Enable GPIO clocks */
  21.         /*!< Connect SPI pins to AF5 */
  22.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource13,GPIO_AF_SPI2);
  23.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource14,GPIO_AF_SPI2);
  24.         GPIO_PinAFConfig(GPIOB, GPIO_PinSource15,GPIO_AF_SPI2);

  25.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  26.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  27.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  28.         GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_DOWN;
  29.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  30.         GPIO_Init(GPIOB, &GPIO_InitStructure);

  31.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  32.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  33.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  34.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  35.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_NOPULL;
  36.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  37.         Cdfinger_BspCs_HIGH();
  38.         SPI_Cmd(SPI2,DISABLE);
  39.         SPI_DeInit(SPI2);
  40.         SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  41.         SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  42.         SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  43.         SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  44.         SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  45.         SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//;
  46.         SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
  47.         SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  48.         SPI_InitStructure.SPI_CRCPolynomial = 7;
  49.         SPI_Init(SPI2, &SPI_InitStructure);

  50.         /* DMA1 Stream0 channel4 spi tx configuration **************************************/
  51.         DMA_DeInit(DMA1_Stream4);
  52.         DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  53.         DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR);
  54.         DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&cdfingerimgtxbuf;
  55.         DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  56.         DMA_InitStructure.DMA_BufferSize = COMMUNICATIONLEN;
  57.         DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//;
  58.         DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  59.         DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  60.         DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  61.         DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;// ;
  62.         DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMA_Priority_High;
  63.         DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  64.         DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  65.         DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  66.         DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  67.         DMA_Init(DMA1_Stream4, &DMA_InitStructure);
  68.   
  69.         /* DMA1 Stream0 channel3 spi rx configuration **************************************/
  70.         DMA_DeInit(DMA1_Stream3);
  71.         DMA_InitStructure.DMA_Channel = DMA_Channel_0;
  72.         DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR);
  73.         DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&cdfingerimgrxbuf;
  74.         DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
  75.         DMA_InitStructure.DMA_BufferSize =COMMUNICATIONLEN;
  76.         DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  77.         DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  78.         DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  79.         DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  80.         DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;//DMA_Mode_Circular;
  81.         DMA_InitStructure.DMA_Priority = DMA_Priority_High;//DMA_Priority_Medium;
  82.         DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//DMA_FIFOMode_Disable;
  83.         DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
  84.         DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  85.         DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  86.         DMA_Init(DMA1_Stream3, &DMA_InitStructure);
  87.        
  88.         //MDA spi 发送中断
  89.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  90.         NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;     
  91.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  92.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x03;
  93.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  94.         NVIC_Init(&NVIC_InitStructure);
  95.        
  96.         //DMA SPI 接收中断
  97.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
  98.         NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream3_IRQn;     
  99.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  100.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x04;
  101.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  102.         NVIC_Init(&NVIC_InitStructure);
  103.        
  104.         SPI_TIModeCmd(SPI2,ENABLE);
  105.         SPI_Cmd(SPI2, ENABLE);

  106.         DMA_ITConfig(DMA1_Stream4,DMA_IT_TC,ENABLE);
  107.         DMA_ITConfig(DMA1_Stream3,DMA_IT_TC,ENABLE);
  108.     SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx|SPI_I2S_DMAReq_Rx, ENABLE);
  109. }

  110. /*****************************************
  111. 函数名称:Cdfinger_BspSpi_sentdate
  112. 函数作用:硬件初始化接口
  113. 备注:
  114. ******************************************/
  115. int Cdfinger_BspSpi_sentdate(void)
  116. {
  117.         int ii =0;
  118.         Cdfinger_BspCs_LOW();
  119.         Cdfinger_Bspdelay_ms(2);

  120.         DMA_Cmd(DMA1_Stream4, ENABLE);
  121.         DMA_Cmd(DMA1_Stream3, ENABLE);
  122.         while(DMA_GetCurrDataCounter(DMA1_Stream4) !=0);
  123.     while(DMA_GetCurrDataCounter(DMA1_Stream3) !=0);
  124.         Cdfinger_BspCs_HIGH();
  125.     Cdfinger_Bspdelay_ms(2);
  126.         return 0;
  127. }

  128. void DMA1_Stream4_IRQHandler(void)
  129. {
  130.   if(DMA_GetITStatus(DMA1_Stream4,DMA_IT_TCIF4) != RESET)
  131.   {
  132.                 DMA_ClearITPendingBit(DMA1_Stream4,DMA_IT_TCIF4);
  133.           DMA_ClearFlag(DMA1_Stream4,DMA_IT_TCIF4);
  134.   }
  135. }

  136. void DMA1_Stream3_IRQHandler(void)
  137. {
  138.   if(DMA_GetITStatus(DMA1_Stream3,DMA_IT_TCIF3) != RESET)
  139.   {
  140.                 DMA_ClearITPendingBit(DMA1_Stream3,DMA_IT_TCIF3);
  141.           DMA_ClearFlag(DMA1_Stream3,DMA_IT_TCIF3);
  142.   }
  143. }
复制代码
回复

使用道具 举报

24

主题

60

回帖

132

积分

初级会员

积分
132
发表于 2024-1-12 14:41:27 | 显示全部楼层
有没有stm32f205的标准库?
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106856
QQ
发表于 2024-1-13 08:48:10 | 显示全部楼层
Y1ng 发表于 2024-1-12 14:41
有没有stm32f205的标准库?

F2的标准库软件包里面应该有的。
回复

使用道具 举报

24

主题

60

回帖

132

积分

初级会员

积分
132
发表于 2024-1-14 19:28:51 | 显示全部楼层
eric2013 发表于 2024-1-13 08:48
F2的标准库软件包里面应该有的。

下载下来是HAL库,还不带例程
回复

使用道具 举报

1万

主题

6万

回帖

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
106856
QQ
发表于 2024-1-15 01:44:06 | 显示全部楼层
回复

使用道具 举报

0

主题

5

回帖

5

积分

新手上路

积分
5
发表于 2024-1-23 17:33:53 | 显示全部楼层
Y1ng 发表于 2024-1-14 19:28
下载下来是HAL库,还不带例程

F4的HAL库我倒是用过,SPI飞线跑的10.5MHz没问题
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-5-7 16:50 , Processed in 0.209949 second(s), 25 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表