你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

如何做SPI主从收发实验基于STM32F103?

[复制链接]
攻城狮Melo 发布时间:2023-3-30 16:47
  1. #include <stm32f10x_conf.h>
  2. #include "sys.h"
  3. #include "delay.h"
  4. #include "usart.h"
  5. #include "timer.h"
  6. #include "pwm.h"
  7. #include "gpio.h"
  8. #include "fsmc.h"
  9. #include "iic.h"
  10. #include "FreeRTOS.h"
  11. #include "task.h"
  12. #include "semphr.h"
  13. #include "task_com.h"
  14. #include "task_motor.h"
  15. #include "task_led.h"
  16. #include "at_cmd.h"
  17. #include "string.h"
  18. #include "lib5500.h"
  19. #include "flash.h"
  20. #include "iwdg.h"
  21. #include "ws2812_sim.h"

  22. #define LOG_LEVEL_DISABLE      -1
  23. #define LOG_LEVEL_ERROR        0
  24. #define LOG_LEVEL_WARNING      1
  25. #define LOG_LEVEL_INFO         2
  26. #define LOG_LEVEL_DEBUG        3

  27. #define LOG_LEVEL   LOG_LEVEL_DEBUG

  28. #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_DEBUG)
  29. #define LOGD(fmt, ...)  printf("[D %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)   //%16s,%3d
  30. #else
  31. #define LOGD(fmt, ...)
  32. #endif

  33. #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_INFO)
  34. #define LOGI(fmt, ...)  printf("[I %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  35. #else
  36. #define LOGI(fmt, ...)
  37. #endif

  38. #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_WARNING)
  39. #define LOGW(fmt, ...)  printf( "[W %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  40. #else
  41. #define LOGW(fmt, ...)
  42. #endif

  43. #if defined(LOG_LEVEL) && (LOG_LEVEL >= LOG_LEVEL_ERROR)
  44. #define LOGE(fmt, ...)  printf( "[E %s,%d] "fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__)
  45. #else
  46. #define LOGE(fmt, ...)
  47. #endif

  48. #define MARK    LOGD("\n");

  49. SemaphoreHandle_t SensorReadLock;
  50. SemaphoreHandle_t UdpSendLock;
  51. SemaphoreHandle_t slave_ready_lock;


  52. void TaskMaster( void *pvParameters );
  53. void TaskSlave( void *pvParameters );
  54. static void SPI1_init(void);
  55. static void SPI2_init(void);

  56. void Initialization()
  57. {
  58.     MCU_INIT(RCC_PLLMul_9);        //?????: 12MHz * 6 = 72MHz
  59.     Delay_Init(72);
  60.     Uart_Init(USART1,115200,72);    //???????,PCLK1??????PCLK??RCC_HCLK_Div1???
  61.     Uart_Init(USART2,9600,36);    //?????????,PCLK2??????PCLK??RCC_HCLK_Div1???
  62. //     Uart_Init(USART3,115200,36);    //???????????
  63.     printf("\r\n\r\n****************Build:%s %s****************\r\n",__DATE__,__TIME__);
  64. //     UartSendStr(USART2,"System start\r\n");
  65. }

  66. int main(void)
  67. {
  68.     Initialization();
  69.    
  70. //    QStepMotorCommand = xQueueCreate( 1,sizeof(int) );
  71. //    QHostCommand = xQueueCreate( 16,sizeof(int) );
  72.    
  73.     SensorReadLock = xSemaphoreCreateMutex();
  74.     xSemaphoreGive(SensorReadLock);
  75.    
  76.     UdpSendLock = xSemaphoreCreateMutex();
  77.     xSemaphoreGive(UdpSendLock);

  78.     slave_ready_lock = xSemaphoreCreateMutex();
  79.     xSemaphoreTake(slave_ready_lock, 0 * portTICK_PERIOD_MS);

  80.     /* Start the tasks defined within this file/specific to this demo. */
  81. //    xTaskCreate( TaskComm,    "task2", 2 * 256, NULL, 2, NULL );
  82.     xTaskCreate( TaskMaster,    "task1", 4 * 256, NULL, 3, NULL );
  83.     xTaskCreate( TaskSlave,    "task2", 4 * 256, NULL, 1, NULL );
  84.    
  85.     /* Start the scheduler. */
  86.     vTaskStartScheduler();
  87.    
  88.     while(1);
  89.    
  90. }


  91. #define USART1_RX_DMA_BUFF_SIZE        128
  92. #define SPI2_RX_DMA_BUFF_SIZE        32
  93. #define SPI2_TX_DMA_BUFF_SIZE        32


  94. static void read_uart_input_buff(void);
  95. static void USART1_RX_DMA_Init(void);
  96. static void SPI2_RX_DMA_Init(void);
  97. static void SPI2_TX_DMA_Init(void);
  98. static void read_spi2_input_buff(void);
  99. static void SPI2_frame_handler(uint8_t data);

  100. static u32 s_frame_buff[1024];
  101. static u8 USART1_RX_DMA_Buff[USART1_RX_DMA_BUFF_SIZE];    //????????????��????????????????????��????????????
  102. static u8 SPI2_RX_DMA_Buff[SPI2_RX_DMA_BUFF_SIZE];
  103. static u8 SPI2_TX_DMA_Buff[SPI2_TX_DMA_BUFF_SIZE] = {0xA0, 0x0 , 0x1 , 0x2 , 0x3 , 0x4 , 0x5 , 0x6 , 0x7 , 0x8 , 0x9 , 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x30};

  104. void TaskMaster( void *pvParameters )
  105. {
  106.     LOGI("TaskMaster start...\r\n");

  107.     // USART1_RX_DMA_Init();
  108.     // ws2812_sim_init();
  109.     SPI1_init();
  110.    
  111.     GPIO_InitTypeDef GPIO_InitStructure;                    //定义一个GPIO结构体变量
  112.     SPI_InitTypeDef SPI_InitStructure;

  113.     //初始化片选引脚
  114.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);   

  115.     //chip select/reset pin init
  116.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
  117.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  118.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  119.     GPIO_Init(GPIOC, &GPIO_InitStructure);


  120.     xSemaphoreTake(slave_ready_lock, portMAX_DELAY);

  121.     uint8_t count = 0;

  122.     while(1)
  123.     {
  124.         // LOGI("test %d %d\r\n", count, DMA_GetCurrDataCounter(DMA1_Channel4));

  125.         // read_uart_input_buff();

  126.         static int state = 0;
  127.         if (state == 0) {
  128.             SPIx_ReadWriteByte(0xA5);   // preamble
  129.             SPIx_ReadWriteByte(0x5A);
  130.             SPIx_ReadWriteByte(0x55);
  131.             SPIx_ReadWriteByte(0xAA);
  132.             SPIx_ReadWriteByte(count);   // addr
  133.             SPIx_ReadWriteByte(~count);   // cmd

  134.             // count++;

  135.             state = 1;
  136.         } else if (state == 1) {
  137.             static int timeout = 0;
  138.             uint8_t data = SPIx_ReadWriteByte(0xFF);   // dummy byte
  139.             // LOGI("%.2X\r\n", data);
  140.             if (data == 0xA0) {
  141.                 for (int i = 0; i < 32; i++) {
  142.                     uint8_t data = SPIx_ReadWriteByte(0xFF);
  143.                     // printf("%.2X ", data);
  144.                 }
  145.                 // printf("\r\n");
  146.                 timeout = 0;
  147.                 state = 0;
  148.                 PCout(13) = ~PCout(13);
  149.                 // vTaskDelay(100);
  150.             } else {
  151.                 if (timeout++ == 10) {
  152.                     timeout = 0;
  153.                     printf("timeout\r\n");
  154.                     vTaskDelay(1000);
  155.                     state = 0;
  156.                 }
  157.             }
  158.         } else if (state == 2) {
  159.         }

  160.         vTaskDelay(1);
  161.     }
  162. }

  163. void TaskSlave( void *pvParameters )
  164. {
  165.     LOGI("TaskSlave start...\r\n");

  166.     SPI2_init();
  167.     SPI2_RX_DMA_Init();
  168.     SPI2_TX_DMA_Init();

  169.     xSemaphoreGive(slave_ready_lock);
  170.    
  171.     uint8_t count = 0;

  172.     while(1)
  173.     {
  174.         read_spi2_input_buff();
  175.         
  176.         vTaskDelay(1);
  177.     }
  178. }

  179. static void USART1_RX_DMA_Init(void)
  180. {
  181.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  182.    
  183.     DMA_DeInit(DMA1_Channel5);
  184.    
  185.     DMA_InitTypeDef DMA_InitStruct;
  186.     DMA_StructInit(&DMA_InitStruct);
  187.     DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&USART1->DR);
  188.     DMA_InitStruct.DMA_MemoryBaseAddr = (u32)USART1_RX_DMA_Buff;
  189.     DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
  190.     DMA_InitStruct.DMA_BufferSize = sizeof(USART1_RX_DMA_Buff);
  191.     DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  192.     DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
  193.     DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  194.     DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  195.     DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
  196.     DMA_InitStruct.DMA_Priority = DMA_Priority_High;
  197.     DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
  198.    
  199.     DMA_Init(DMA1_Channel5,&DMA_InitStruct);
  200.    
  201.     DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,DISABLE);
  202.     DMA_ITConfig(DMA1_Channel5,DMA_IT_HT,DISABLE);
  203.     DMA_ITConfig(DMA1_Channel5,DMA_IT_TE,DISABLE);
  204.    
  205.     NVIC_InitTypeDef NVIC_InitStruct;
  206.     NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel5_IRQn;
  207.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  208.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  209.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  210.     NVIC_Init(&NVIC_InitStruct);

  211.     USART1->CR3 |= (1<<6);    //???DMA????
  212.     DMA_Cmd(DMA1_Channel5,ENABLE);    //????DMA????

  213. }

  214. // void DMA1_Channel5_IRQHandler()
  215. // {
  216. //     DMA_ClearFlag(DMA1_FLAG_TC5);
  217. //     int i;
  218. //     for(i=0;i<sizeof(USART1_RX_DMA_Buff);i++)
  219. //         printf("%c",USART1_RX_DMA_Buff[i]);
  220. //     printf("\r\n");
  221. // }

  222. static void read_uart_input_buff(void)
  223. {
  224.         static int LastDataCount = 0;
  225.         
  226.         //????????DMA��???.????DMA_GetCurrDataCounter?????????????????????.
  227.         int CurrDataCount = USART1_RX_DMA_BUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5);
  228.         
  229.         if(LastDataCount != CurrDataCount)
  230.         {
  231.             int i;
  232.             if(LastDataCount < CurrDataCount)
  233.             {
  234. //                 printf("1:%d %d,",LastDataCount,CurrDataCount);
  235.                 for(i = LastDataCount;i<CurrDataCount;i++) {
  236. //                    MasterFrameInput(USART2_RX_DMA_Buff[i]);
  237.                      printf("%c",USART1_RX_DMA_Buff[i]);
  238.                 }
  239. //                 printf("\r\n");
  240.             }
  241.             else
  242.             {
  243. //                 printf("2:%d %d,",LastDataCount,CurrDataCount);
  244.                 for(i = LastDataCount;i<USART1_RX_DMA_BUFF_SIZE;i++) {
  245. //                    MasterFrameInput(USART2_RX_DMA_Buff[i]);
  246.                      printf("%c",USART1_RX_DMA_Buff[i]);
  247.                 }
  248.                 for(i = 0;i<CurrDataCount;i++) {
  249. //                    MasterFrameInput(USART2_RX_DMA_Buff[i]);
  250.                      printf("%c",USART1_RX_DMA_Buff[i]);
  251.                 }
  252. //                 printf("\r\n");
  253.             }
  254.         }

  255.         LastDataCount = CurrDataCount;
  256. }

  257. static void SPI1_init(void)
  258. {
  259.     GPIO_InitTypeDef GPIO_InitStructure;                    //定义一个GPIO结构体变量
  260.     SPI_InitTypeDef SPI_InitStructure;

  261.     //初始化片选引脚
  262.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);   

  263.     //chip select/reset pin init
  264.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_11;
  265.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  266.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  267.       GPIO_Init(GPIOA, &GPIO_InitStructure);
  268.     // SPI_CS = 1;
  269.     // SPI_RST = 1;

  270.     //初始化SPI引脚
  271.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  272.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  273.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  274.       GPIO_Init(GPIOA, &GPIO_InitStructure);

  275.     //初始化SPI1
  276.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
  277.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//复位SPI1
  278.     RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//停止复位SPI1
  279.    
  280.       SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  281.       SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  282.       SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  283.       SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  284.       SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  285.       SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
  286.       SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  287.       SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  288.       SPI_InitStructure.SPI_CRCPolynomial = 7;
  289.     SPI_Init(SPI1,&SPI_InitStructure);

  290.     SPI_SSOutputCmd(SPI1, ENABLE);
  291.     // SPI_Cmd(SPI1, DISABLE);    //Enable SPI1  

  292.     SPI_Cmd(SPI1, ENABLE);    //Enable SPI1  
  293.     // SPIx_ReadWriteByte(0xff);    //启动传输
  294.     // SPI_Cmd(SPI1, DISABLE);    //Enable SPI1  
  295. }

  296. static void SPI2_init(void)
  297. {
  298.     GPIO_InitTypeDef GPIO_InitStructure;                    //定义一个GPIO结构体变量
  299.     SPI_InitTypeDef SPI_InitStructure;

  300.     //初始化片选引脚
  301.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);   

  302.     //chip select/reset pin init
  303.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  304.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  305.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  306.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  307.     // SPI_CS = 1;
  308.     // SPI_RST = 1;

  309.     //初始化SPI引脚
  310.     GPIO_InitStructure.GPIO_Pin =   GPIO_Pin_12 |   // note that NSS pin of the SPI slave is use to select the
  311.                                                     // device, if it is pulled up, SPI slave will not shift
  312.                                                     // any byte to its shift register
  313.                                     GPIO_Pin_13 |
  314.                                     GPIO_Pin_14 |
  315.                                     GPIO_Pin_15;
  316.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  317.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  318.     GPIO_Init(GPIOB, &GPIO_InitStructure);

  319.     //初始化SPI2
  320.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
  321.     RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,ENABLE);//复位SPI2
  322.     RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2,DISABLE);//停止复位SPI2

  323.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  324.     SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
  325.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  326.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  327.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  328.     SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
  329.     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  330.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  331.     SPI_InitStructure.SPI_CRCPolynomial = 7;
  332.     SPI_Init(SPI2,&SPI_InitStructure);

  333.     SPI_SSOutputCmd(SPI2, DISABLE);
  334.     // SPI_Cmd(SPI2, DISABLE);    //Enable SPI2  

  335.     SPI_Cmd(SPI2, ENABLE);    //Enable SPI2  
  336.     // SPIx_ReadWriteByte(0xff);    //启动传输
  337.     // SPI_Cmd(SPI2, DISABLE);    //Enable SPI2
  338.    
  339.     // SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, ENABLE);
  340.     SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_ERR, ENABLE);

  341.     NVIC_InitTypeDef NVIC_InitStruct;
  342.     NVIC_InitStruct.NVIC_IRQChannel = SPI2_IRQn;
  343.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  344.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  345.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  346.     NVIC_Init(&NVIC_InitStruct);
  347.    
  348. }

  349. void SPI2_IRQHandler()
  350. {
  351.     if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_RXNE) == SET) {
  352.         // SPI_I2S_ClearITPendingBit(SPI2, SPI_I2S_IT_RXNE);
  353.         LOGI("IRQ:%.2X\r\n", SPI2->DR);
  354.     } else if (SPI_I2S_GetITStatus(SPI2, SPI_I2S_IT_ERR) == SET) {
  355.         LOGE("SPI2 Error\r\n");
  356.         while (1);
  357.     } else {
  358.         LOGE("Unknown SPI2 interrupt\r\n");
  359.         while (1);
  360.     }
  361. }

  362. static void SPI2_RX_DMA_Init(void)
  363. {
  364.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  365.    
  366.     DMA_DeInit(DMA1_Channel4);
  367.    
  368.     DMA_InitTypeDef DMA_InitStruct;
  369.     DMA_StructInit(&DMA_InitStruct);
  370.     DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);
  371.     DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_RX_DMA_Buff;
  372.     DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralSRC;
  373.     DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_RX_DMA_Buff);
  374.     DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  375.     DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
  376.     DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  377.     DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  378.     DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
  379.     DMA_InitStruct.DMA_Priority = DMA_Priority_High;
  380.     DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
  381.    
  382.     DMA_Init(DMA1_Channel4,&DMA_InitStruct);
  383.    
  384.     DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,DISABLE);
  385.     DMA_ITConfig(DMA1_Channel4,DMA_IT_HT,DISABLE);
  386.     DMA_ITConfig(DMA1_Channel4,DMA_IT_TE,DISABLE);
  387.    
  388.     NVIC_InitTypeDef NVIC_InitStruct;
  389.     NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel4_IRQn;
  390.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  391.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  392.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  393.     NVIC_Init(&NVIC_InitStruct);

  394.     SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);

  395.     DMA_Cmd(DMA1_Channel4,ENABLE);    //????DMA????


  396. }

  397. void DMA1_Channel4_IRQHandler()
  398. {
  399.     DMA_ClearFlag(DMA1_FLAG_TC4);
  400.     int i;
  401.     for(i=0;i<sizeof(SPI2_RX_DMA_Buff);i++)
  402.         printf("%c",SPI2_RX_DMA_Buff[i]);
  403.     printf("\r\n");
  404. }

  405. static void read_spi2_input_buff(void)
  406. {
  407.         static int LastDataCount = 0;
  408.         
  409.         int CurrDataCount = SPI2_RX_DMA_BUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel4);
  410.         
  411.         if(LastDataCount != CurrDataCount)
  412.         {
  413.             int i;
  414.             if(LastDataCount < CurrDataCount)
  415.             {
  416. //              printf("1:%d %d,",LastDataCount,CurrDataCount);
  417.                 for(i = LastDataCount;i<CurrDataCount;i++) {
  418.                      // printf("%.2X ",SPI2_RX_DMA_Buff[i]);
  419.                     SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
  420.                 }
  421. //              printf("\r\n");
  422.             }
  423.             else
  424.             {
  425. //              printf("2:%d %d,",LastDataCount,CurrDataCount);
  426.                 for(i = LastDataCount;i<SPI2_RX_DMA_BUFF_SIZE;i++) {
  427.                     // printf("%.2X ",SPI2_RX_DMA_Buff[i]);
  428.                     SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
  429.                 }
  430.                 for(i = 0;i<CurrDataCount;i++) {
  431.                     // printf("%.2X ",SPI2_RX_DMA_Buff[i]);
  432.                     SPI2_frame_handler(SPI2_RX_DMA_Buff[i]);
  433.                 }
  434. //              printf("\r\n");
  435.             }
  436.         }

  437.         LastDataCount = CurrDataCount;
  438. }

  439. static void SPI2_frame_handler(uint8_t data)
  440. {
  441.     // printf("%.2X\r\n", data);

  442.     static int state = 0;

  443.     // 0xA5 5A 55 AA
  444.     if (state == 0) {
  445.         if (data == 0xA5) {
  446.             state = 1;
  447.         }
  448.     } else if (state == 1) {
  449.         if (data == 0x5A) {
  450.             state = 2;
  451.         } else if (data == 0xA5) {

  452.         } else {
  453.             LOGE("Frame err\r\n");
  454.             // while (1);
  455.             state = 0;
  456.         }
  457.     } else if (state == 2) {
  458.         if (data == 0x55) {
  459.             state = 3;
  460.         } else if (data == 0xA5) {
  461.             state = 1;
  462.         } else {
  463.             LOGE("Frame err\r\n");
  464.             // while (1);
  465.             state = 0;
  466.         }
  467.     } else if (state == 3) {
  468.         if (data == 0xAA) {
  469.             state = 4;
  470.         } else if (data == 0xA5) {
  471.             state = 1;
  472.         } else {
  473.             LOGE("Frame err\r\n");
  474.             // while (1);
  475.             state = 0;
  476.         }
  477.     } else if (state == 4) {
  478.         // printf("ADDR:%.2X\r\n", data);
  479.         state = 5;
  480.     } else if (state == 5) {
  481.         // printf("CMD:%.2X\r\n", data);
  482.         // SPI2->DR = 0x33;

  483.         DMA_Cmd(DMA1_Channel5, DISABLE);     // disable DMA before starting a new DMA transaction

  484.         DMA_InitTypeDef DMA_InitStruct;
  485.         DMA_StructInit(&DMA_InitStruct);
  486.         DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);
  487.         DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_TX_DMA_Buff;
  488.         DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
  489.         DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_TX_DMA_Buff);
  490.         DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  491.         DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
  492.         DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  493.         DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  494.         DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
  495.         DMA_InitStruct.DMA_Priority = DMA_Priority_High;
  496.         DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
  497.         
  498.         DMA_Init(DMA1_Channel5,&DMA_InitStruct);

  499.         DMA_Cmd(DMA1_Channel5, ENABLE);
  500.         
  501.         state = 0;
  502.     } else if (state == 6) {

  503.     } else if (state == 7) {

  504.     } else if (state == 8) {

  505.     } else if (state == 9) {

  506.     } else if (state == 10) {

  507.     } else {

  508.     }
  509. }

  510. static void SPI2_TX_DMA_Init(void)
  511. {
  512.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
  513.    
  514.     DMA_DeInit(DMA1_Channel5);
  515.    
  516.     DMA_InitTypeDef DMA_InitStruct;
  517.     DMA_StructInit(&DMA_InitStruct);
  518.     DMA_InitStruct.DMA_PeripheralBaseAddr = (u32)(&SPI2->DR);
  519.     DMA_InitStruct.DMA_MemoryBaseAddr = (u32)SPI2_TX_DMA_Buff;
  520.     DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;
  521.     DMA_InitStruct.DMA_BufferSize = sizeof(SPI2_TX_DMA_Buff);
  522.     DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  523.     DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
  524.     DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  525.     DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  526.     DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;
  527.     DMA_InitStruct.DMA_Priority = DMA_Priority_High;
  528.     DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;
  529.    
  530.     DMA_Init(DMA1_Channel5,&DMA_InitStruct);
  531.    
  532.     DMA_ITConfig(DMA1_Channel5,DMA_IT_TC,DISABLE);
  533.     DMA_ITConfig(DMA1_Channel5,DMA_IT_HT,DISABLE);
  534.     DMA_ITConfig(DMA1_Channel5,DMA_IT_TE,DISABLE);
  535.    
  536.     NVIC_InitTypeDef NVIC_InitStruct;
  537.     NVIC_InitStruct.NVIC_IRQChannel = DMA1_Channel5_IRQn;
  538.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  539.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  540.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  541.     NVIC_Init(&NVIC_InitStruct);

  542.     SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);

  543.     DMA_Cmd(DMA1_Channel5,DISABLE);    //????DMA????

  544.     // note: the last byte moved from RAM to SPI2->DR is seen as the end of DMA transmission
  545.     // but the last byte still not been transferred out from the shift register which means
  546.     // the SPI master might not received the last byte when the SPI slave finishing the DMA
  547.     // transfer.
  548. }

  549. void DMA1_Channel5_IRQHandler()
  550. {
  551.     DMA_ClearFlag(DMA1_FLAG_TC5);
  552.     // LOGI("Sent done\r\n");
  553.     DMA_Cmd(DMA1_Channel5, DISABLE);
  554.     // SPI2->DR = 0x00;     // set the shift register to 0x00 other wise SPI master will get the previous byte
  555.     //                      // which might be 0xA0 and causes error.
  556.                             // update: don't do it like this, due to the last byte still in the shift register
  557.                             // and waiting to be shifted out from MISO. We can add a byte of 0x00 int the last
  558.                             // byte of the DMA buffer if the last byte is 0xA0.
  559. }
复制代码
转载自:哐哐哐 Quan
如有侵权请联系删除

收藏 评论0 发布时间:2023-3-30 16:47

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版