[其他ST产品] stm32循环buffer数据处理实例

[复制链接]
 楼主| 怎么总是重复啊 发表于 2021-12-31 23:39 | 显示全部楼层 |阅读模式
接着上次的《stm32实用循环buffer》,给大家提供一个参考实例,根据实际情况改改就能用。
  1. /******************************************************************************
  2. * [url=home.php?mod=space&uid=288409]@file[/url]         : usart2.c
  3. * [url=home.php?mod=space&uid=187600]@author[/url]   : qianxin.chen@qq.com
  4. * [url=home.php?mod=space&uid=895143]@version[/url]  : V0.0.1
  5. * [url=home.php?mod=space&uid=212281]@date[/url]         : 20-February-2020
  6. * @Brief        : This file provides all the usart2 functions.
  7. ******************************************************************************
  8. * @Attention:
  9. * Non
  10. *
  11. ******************************************************************************/
  12. /* Includes -----------------------------------------------------------------*/
  13. #include "includes.h"
  14. #include "usart2.h"
  15. #include "fifo.h"        //fifo头文件
  16. #include "crc.h"        //用于校验
  17. #include "usart1.h"        //用于printf打印

  18. /* Define -------------------------------------------------------------------*/
  19. #define USART2_FIFO_LEN 64
  20. #define FRAME_HEAD1 0xAA
  21. #define FRAME_TAIL1 0x55
  22. #define FRAME_TAIL2 0x55       
  23. #define FRAME_LEN 5
  24. #define complement(a) (FRAME_LEN-a)

  25. /* Variables ----------------------------------------------------------------*/
  26. struct fifo fifo2;
  27. static unsigned char copy_len = FRAME_LEN;
  28. #ifdef FIX_BUFFER //用于区分固定分配或动态分配buffer
  29. unsigned char fifo_buf[USART2_FIFO_LEN];
  30. #endif

  31. /* Functions ----------------------------------------------------------------*/
  32. /* must init this function */
  33. void init_usart2_fifo(void)
  34. {       
  35.         signed int ret;
  36.        
  37.         ret = fifo_alloc(&fifo2, USART2_FIFO_LEN);
  38. #ifdef FIX_BUFFER
  39.         ret = fifo_init(&fifo2, fifo_buf, USART2_FIFO_LEN);
  40. #endif
  41.         if(ret<0){
  42.                 printf("fifo2 alloc fail!\n");
  43.         }
  44. }

  45. void usart2_Init(u32 bound)
  46. {
  47.         GPIO_InitTypeDef GPIO_InitStructure;
  48.         USART_InitTypeDef USART_InitStructure;
  49.         NVIC_InitTypeDef NVIC_InitStructure;
  50.        
  51.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);        
  52.        
  53.         USART_DeInit(USART2);        
  54.        
  55.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  56.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  57.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       
  58.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  59.    
  60.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  61.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  62.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  63.         NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  64.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
  65.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  66.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  67.         NVIC_Init(&NVIC_InitStructure);

  68.         USART_InitStructure.USART_BaudRate = bound;
  69.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  70.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  71.         USART_InitStructure.USART_Parity = USART_Parity_No;
  72.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  73.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;       

  74.         USART_Init(USART2, &USART_InitStructure);
  75.         USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

  76.         USART_Cmd(USART2, ENABLE);
  77. }

  78. void usart2_send_data(u8 *sdata, u8 len)
  79. {
  80.         u8 i;
  81.        
  82.         GPIO_ResetBits(GPIOA, GPIO_Pin_4);
  83.         USART_ClearFlag(USART2,USART_FLAG_TC);
  84.         for(i=0;i<len;i++)
  85.                 {
  86.                 USART_SendData(USART2, sdata[i]);
  87.                 while(USART_GetFlagStatus(USART2, USART_FLAG_TC)!=SET);
  88.         }
  89.         GPIO_SetBits(GPIOA, GPIO_Pin_4);
  90. }

  91. void USART2_IRQHandler(void)
  92. {
  93.         unsigned int ret;
  94.         unsigned char buf[1];
  95.        
  96.         OSIntEnter();        //如果没有用ucos屏蔽此行
  97.         if(USART_GetITStatus(USART2, USART_IT_RXNE)!= RESET){       
  98.                 USART_ClearITPendingBit(USART2,USART_IT_RXNE);
  99.                 buf[0] = USART_ReceiveData(USART2);
  100.                 ret = fifo_in(&fifo2, buf, 1);
  101.                 if(ret<1){
  102.                    printf("fifo2 in err!\n");
  103.                 }         
  104.          }
  105.          OSIntExit();        //如果没有用ucos屏蔽此行
  106. }

  107. /* 在buffer中查找、校验符合条件的帧数据 */
  108. void usart2_data_handler(void)
  109. {
  110.         unsigned int len;
  111.         unsigned char buf[FRAME_LEN];
  112.         /* 执行到这里的时候,只要有数据就循环处理,直到处理完数据 */
  113.         while((fifo_used(&fifo2))>((copy_len))){
  114.                 len = fifo_out(&fifo2, buf+complement(copy_len), (copy_len));        //fifo中提取需要的长度字节
  115.                 if(len <1){
  116.                         printf("usart2 fifo out err!\n");
  117.                 }
  118.                 /* 校验帧头、帧尾、和crc校验 */
  119.                 //if(buf[0] == FRAME_HEAD1 && buf[FRAME_LEN-1]==FRAME_TAIL1 && buf[FRAME_LEN-1]==FRAME_TAIL2
  120.                 //&& (buf[sizeof(buf)-2] == crc8_maxim(buf+CRC_OFFSET, (sizeof(buf)-SIZE_SUB+1)))){
  121.                 /* 只校验帧头、帧尾 */
  122.                 if(buf[0] == FRAME_HEAD1 && buf[FRAME_LEN-1]==FRAME_TAIL1 && buf[FRAME_LEN-1]==FRAME_TAIL2){
  123.                         copy_len=FRAME_LEN;
  124.                         {
  125.                                 /* 帧数据处理 */
  126.                         }
  127.                         continue;
  128.                 }
  129.                 else{
  130.                         /* 不符合帧数据条件,循环查找帧头,并把找到的帧头移动到buf的前面,下次提取的数据附加在后边 */                       
  131.                         for(copy_len=1;copy_len<FRAME_LEN;copy_len++){
  132.                                 if(buf[copy_len] == FRAME_HEAD1){
  133.                                         memcpy(buf,buf+copy_len,(complement(copy_len)));
  134.                                   break;
  135.                                 }
  136.                         }
  137.                 }
  138.         }
  139. }




 楼主| 怎么总是重复啊 发表于 2021-12-31 23:40 | 显示全部楼层
头文件:

  1. /*******************************************************************************
  2. * @File         : usart.h
  3. * @Author   : qianxin.chen@qq.com
  4. * @Version  : V0.0.0
  5. * @Date         : 20-February-2016
  6. * @Brief        : This file provides all the write internal usart2 functions.
  7. ********************************************************************************
  8. * @Attention:
  9. * Non
  10. *
  11. *******************************************************************************/

  12. /* Define to prevent recursive inclusion -------------------------------------*/
  13. #ifndef _USART2_H
  14. #define _USART2_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif

  18. /* Includes ------------------------------------------------------------------*/
  19. #include "includes.h"

  20. /* function prototypes -------------------------------------------------------*/
  21. void init_usart2_fifo(void);         
  22. void usart2_Init(u32 bound);
  23. void usart2_send_data(u8 *sdata,u8 len);
  24. void USART2_IRQHandler(void);
  25. void usart2_data_handler(void);

  26. #ifdef __cplusplus
  27. }
  28. #endif
  29. #endif
您需要登录后才可以回帖 登录 | 注册

本版积分规则

29

主题

262

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

29

主题

262

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部