stm32循环buffer数据处理实例
接着上次的《stm32实用循环buffer》,给大家提供一个参考实例,根据实际情况改改就能用。/******************************************************************************
* @file : usart2.c
* @author : qianxin.chen@qq.com
* @version: V0.0.1
* @date : 20-February-2020
* @Brief : This file provides all the usart2 functions.
******************************************************************************
* @Attention:
* Non
*
******************************************************************************/
/* Includes -----------------------------------------------------------------*/
#include "includes.h"
#include "usart2.h"
#include "fifo.h" //fifo头文件
#include "crc.h" //用于校验
#include "usart1.h" //用于printf打印
/* Define -------------------------------------------------------------------*/
#define USART2_FIFO_LEN 64
#define FRAME_HEAD1 0xAA
#define FRAME_TAIL1 0x55
#define FRAME_TAIL2 0x55
#define FRAME_LEN 5
#define complement(a) (FRAME_LEN-a)
/* Variables ----------------------------------------------------------------*/
struct fifo fifo2;
static unsigned char copy_len = FRAME_LEN;
#ifdef FIX_BUFFER //用于区分固定分配或动态分配buffer
unsigned char fifo_buf;
#endif
/* Functions ----------------------------------------------------------------*/
/* must init this function */
void init_usart2_fifo(void)
{
signed int ret;
ret = fifo_alloc(&fifo2, USART2_FIFO_LEN);
#ifdef FIX_BUFFER
ret = fifo_init(&fifo2, fifo_buf, USART2_FIFO_LEN);
#endif
if(ret<0){
printf("fifo2 alloc fail!\n");
}
}
void usart2_Init(u32 bound)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_DeInit(USART2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void usart2_send_data(u8 *sdata, u8 len)
{
u8 i;
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
USART_ClearFlag(USART2,USART_FLAG_TC);
for(i=0;i<len;i++)
{
USART_SendData(USART2, sdata);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)!=SET);
}
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
void USART2_IRQHandler(void)
{
unsigned int ret;
unsigned char buf;
OSIntEnter(); //如果没有用ucos屏蔽此行
if(USART_GetITStatus(USART2, USART_IT_RXNE)!= RESET){
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
buf = USART_ReceiveData(USART2);
ret = fifo_in(&fifo2, buf, 1);
if(ret<1){
printf("fifo2 in err!\n");
}
}
OSIntExit(); //如果没有用ucos屏蔽此行
}
/* 在buffer中查找、校验符合条件的帧数据 */
void usart2_data_handler(void)
{
unsigned int len;
unsigned char buf;
/* 执行到这里的时候,只要有数据就循环处理,直到处理完数据 */
while((fifo_used(&fifo2))>((copy_len))){
len = fifo_out(&fifo2, buf+complement(copy_len), (copy_len)); //fifo中提取需要的长度字节
if(len <1){
printf("usart2 fifo out err!\n");
}
/* 校验帧头、帧尾、和crc校验 */
//if(buf == FRAME_HEAD1 && buf==FRAME_TAIL1 && buf==FRAME_TAIL2
//&& (buf == crc8_maxim(buf+CRC_OFFSET, (sizeof(buf)-SIZE_SUB+1)))){
/* 只校验帧头、帧尾 */
if(buf == FRAME_HEAD1 && buf==FRAME_TAIL1 && buf==FRAME_TAIL2){
copy_len=FRAME_LEN;
{
/* 帧数据处理 */
}
continue;
}
else{
/* 不符合帧数据条件,循环查找帧头,并把找到的帧头移动到buf的前面,下次提取的数据附加在后边 */
for(copy_len=1;copy_len<FRAME_LEN;copy_len++){
if(buf == FRAME_HEAD1){
memcpy(buf,buf+copy_len,(complement(copy_len)));
break;
}
}
}
}
}
头文件:
/*******************************************************************************
* @File : usart.h
* @Author : qianxin.chen@qq.com
* @Version: V0.0.0
* @Date : 20-February-2016
* @Brief : This file provides all the write internal usart2 functions.
********************************************************************************
* @Attention:
* Non
*
*******************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _USART2_H
#define _USART2_H
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "includes.h"
/* function prototypes -------------------------------------------------------*/
void init_usart2_fifo(void);
void usart2_Init(u32 bound);
void usart2_send_data(u8 *sdata,u8 len);
void USART2_IRQHandler(void);
void usart2_data_handler(void);
#ifdef __cplusplus
}
#endif
#endif
页:
[1]