当前位置:网站首页>Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
Single chip microcomputer serial port data processing (2) -- ucosiii + cyclic queue receiving data
2022-04-23 04:00:00 【Chenxr32】
The last article introduced the optimization method of serial port sending data , Using interrupt to send data can improve the real-time performance of the system . This time introduces the method of receiving data through serial port . The new data receiving method combines uCOS-III Circular queue , It has better real-time performance . meanwhile , Use STM32 The bus idle interrupt judges that the data packet has been received , Use the state machine to check the correctness of the packet .
To configure USART Enable bus idle interrupt when , When MCU When it is detected that there is a byte of time on the serial bus and no data is received, the interrupt is triggered . In the interrupt processing function, the software must clear the interrupt flag bit to avoid repeatedly entering the idle interrupt , The specific method is to read USART_SR, Then read USART_DR. The sample code is as follows :
void USARTInit(void)
{
/*
...
Omit some serial port configuration codes
*/
USART_ITConfig(USART1, USART_IT_RXNE,ENABLE);// Open the non empty interrupt of the data register
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE); // Turn on the bus idle interrupt
USART_Cmd(USART1,ENABLE);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_IDLE)==SET)
{
ch=USART1->SR;
ch=USART1->DR;// Clear the bus idle flag
}
}
When the program enters the interrupt because the serial port data register is not empty , Read the bytes received by the serial port and store them in the circular queue , Start recording packet bytes . When the program is interrupted due to idle serial bus , Release (OSTaskQPost()) Packet header address and packet size to data processing task ( State machine ) In the message queue of , Record the write pointer of the current circular queue (RXqueue.pwrite) As the first address of the next packet , Clear the packet byte count variable .
void USART_IRQHandler(void)
{
unsigned char ch;
OS_ERR err;
static unsigned char* p_write=RXqueue.pwrite;
static OS_MSG_SIZE rxcnt=0;
OSIntEnter();
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
ch=USART1->DR;
if(RXqueue.FullCheck()==0)
{
*RXqueue.pwrite=ch;
if(RXqueue.pwrite==&RXqueue.arr[QUEUE_SIZE-1])
RXqueue.pwrite=RXqueue.arr;
else
RXqueue.pwrite++;
rxcnt++;
}
}
if(USART_GetITStatus(USART1,USART_IT_IDLE)==SET)
{
ch=USART1->SR;
ch=USART1->DR;
OSTaskQPost(&FiniteStatesTaskTCB,p_write,rxcnt,OS_OPT_POST_FIFO,&err);
rxcnt=0;
p_write=RXqueue.pwrite;
}
if(USART_GetITStatus(USART1,USART_IT_TXE)==SET)
{
if(TXqueue.EmptyCheck()==0)
{
USART1->DR=*TXqueue.pread;
if(TXqueue.pread==&TXqueue.arr[QUEUE_SIZE-1])
TXqueue.pread=TXqueue.arr;
else
TXqueue.pread++;
}
else
USART_ITConfig(USART1,USART_IT_TXE,DISABLE);
}
OSIntExit();
}
Data processing tasks , The program blocks waiting for a message , After a message is published, the data processing task obtains the message and starts processing the data . Task blocking waiting for message ,CPU Can handle other tasks , When the news is released , Data processing tasks can respond immediately , It has better real-time performance .
void FiniteStatesTask(void *arg)
{
OS_ERR err;
unsigned char *delta;// The first address of the received packet
OS_MSG_SIZE size;// Received packet size
CPU_TS ts;
unsigned char flag=0;
while(DEF_ON)
{
delta=(unsigned char*)OSTaskQPend(0,OS_OPT_PEND_BLOCKING,&size,&ts,&err);// Task blocking waiting message
while(size--)
{
TXqueue.PutData(RXqueue.GetData());// Store the received data into the transmission buffer queue
flag=receiveFiniteStates(*delta);// The state machine processes data
if(delta==&RXqueue.arr[QUEUE_SIZE-1])
delta=RXqueue.arr;
else
delta++;
}
if(flag==1)
{
flag=0;
USART1_SendChar();// If the data processing is correct , Then send the received data back
}
}
}
After testing , Baud rate 115200, Packet sending interval 1ms In this case, there is no packet loss . When I set the baud rate to 921600 when , No matter how long the transmission interval , Data packet loss is very serious , After hardware simulation, it is found that sometimes a packet of data cannot be received completely , My preliminary judgment may be that the baud rate is too high TTL Unstable level .
If there is a better method to prevent packet loss in serial data reception , Welcome to leave a message .
Project code download :
https://download.csdn.net/download/qdchenxr/10958685
Reference material :
be based on uCOSII Of MCU Serial port transmission processing
版权声明
本文为[Chenxr32]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230356325399.html
边栏推荐
- VSCode配置之Matlab极简配置
- Let matlab2018b support the mex configuration of vs2019
- 减治思想——二分查找详细总结
- ROS series (I): rapid installation of ROS
- Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
- 【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
- STM32上μC/Shell移植与应用
- CRF based medical entity recognition baseline
- Basic usage of Google colab (I)
- ROS series (IV): ROS communication mechanism series (6): parameter server operation
猜你喜欢
(valid for personal testing) compilation guide of paddedetection on Jetson
【ICCV 2019】MAP-VAE:Multi-Angle Point Cloud-VAE: Unsupervised Feature Learning for 3D Point Clouds..
[AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
Now is the best time to empower industrial visual inspection with AI
ROS series (IV): ROS communication mechanism series (5): Service Communication Practice
Variables, constants, operators
Set classic topics
秒杀所有区间相关问题
Detailed explanation on the use of annotation tool via (VGg image annotator) in mask RCNN
ROS series (IV): ROS communication mechanism series (1): topic communication
随机推荐
Concepts of objects and classes
Paddlepaddle does not support arm64 architecture.
C language character constant
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
一个函数秒杀2Sum 3Sum 4Sum问题
Basic introduction to spot gold
A function second kill 2sum 3sum 4sum problem
基于PHP的代步工具购物商城
Shopping mall for transportation tools based on PHP
Let matlab2018b support the mex configuration of vs2019
Photoshop installation under win10
[AI vision · quick review of today's sound acoustic papers issue 1] Thu, 14 APR 2022
matlab读取多张fig图然后合并为一张图(子图的形式)
Summary of knowledge map (I)
[mapping program design] coordinate azimuth calculation artifact (version C)
使用大华设备开发行AI人流量统计出现时间不正确的原因分析
创下国产手机在海外市场销量最高纪录的小米,重新关注国内市场
Instructions for fastmock
【测绘程序设计】坐标方位角推算神器(C#版)
列表、元组、字典和集合的区别