当前位置:网站首页>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
边栏推荐
- 創下國產手機在海外市場銷量最高紀錄的小米,重新關注國內市場
- 将编译安装的mysql加入PATH环境变量
- As a code farmer, what kind of experience is it that a girlfriend can code better than herself?
- MySQL 2013 lost connection to MySQL server during query
- Identificateur, mot - clé, type de données
- Win10 boot VMware virtual machine boot seconds blue screen problem perfect solution
- 【BIM+GIS】ArcGIS Pro2.8如何打开Revit模型,BIM和GIS融合?
- 【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解
- CRF based medical entity recognition baseline
- Vscode delete uninstall residue
猜你喜欢

ROS series (III): introduction to ROS architecture

Alibaba cloud IOT transfer to PostgreSQL database scheme

Set classic topics

Who will answer the question?

Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)

VSCode配置之Matlab极简配置

LabVIEW 小端序和大端序区别

Detailed explanation on the use of annotation tool via (VGg image annotator) in mask RCNN

Retrieval question answering system baseline
![[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021](/img/c8/90d020d192fe791c4dec5f4161e597.png)
[AI vision · quick review of robot papers today, issue 28] wed, 1 Dec 2021
随机推荐
[mapping program design] coordinate azimuth calculation artifact (version C)
Hard core chip removal
Overview of knowledge map (II)
Xiaohongshu was exposed to layoffs of 20% as a whole, and the internal volume among large factories was also very serious
Set经典小题目
伦敦银最新价格走势图与买卖点
Basic usage of Google colab (I)
Let matlab2018b support the mex configuration of vs2019
[BIM introduction practice] wall hierarchy and FAQ in Revit
php导出Excel表格
[AI vision · quick review of robot papers today, issue 29] Mon, 14 Feb 2022
創下國產手機在海外市場銷量最高紀錄的小米,重新關注國內市場
Alibaba cloud IOT transfer to PostgreSQL database scheme
PolarMask is not in the models registry
[Li Hongyi 2022 machine learning spring] hw6_ Gan (don't understand...)
网络原理 | TCP/IP中的连接管理机制 重要协议与核心机制
Set classic topics
秒杀所有区间相关问题
Installation and configuration of clion under win10
Numpy's broadcasting mechanism (with examples)