当前位置:网站首页>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
边栏推荐
- Common auxiliary classes
- 【测绘程序设计】坐标方位角推算神器(C#版)
- Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
- Operating skills of spot gold_ Wave estimation curve
- What if you encounter symbols you don't know in mathematical formulas
- Alibaba cloud IOT transfer to PostgreSQL database scheme
- 【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
- 减治思想——二分查找详细总结
- Set经典小题目
- Difference between LabVIEW small end sequence and large end sequence
猜你喜欢
Installation and configuration of clion under win10
Add the compiled and installed Mysql to the path environment variable
Set classic topics
ROS series (IV): ROS communication mechanism series (1): topic communication
Now is the best time to empower industrial visual inspection with AI
Shopping mall for transportation tools based on PHP
Digital image processing third edition Gonzalez notes Chapter 2
Machine translation baseline
Openvino only supports Intel CPUs of generation 6 and above
网络原理 | TCP/IP中的连接管理机制 重要协议与核心机制
随机推荐
matlab读取多张fig图然后合并为一张图(子图的形式)
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
知乎有问题,谁来解答?
How Zotero quotes in word jump to references / hyperlink
Basic knowledge of convolutional neural network
Photoshop installation under win10
Xshell、Xftp连接新创建的Unbutu系统虚拟机全流程
The whole process of connecting the newly created unbutu system virtual machine with xshell and xftp
Qt程序集成EasyPlayer-RTSP流媒体播放器出现画面闪烁是什么原因?
Matlab reads multiple fig graphs and then combines them into one graph (in the form of sub graph)
Paddlepaddle model to onnx
Why is it necessary to divide the variance by 255^2 when adding Gaussian noise using the imnoise function of MATLAB
Variables, constants, operators
Mysql出现2013 Lost connection to MySQL server during query
Cmake qmake simple knowledge
Shopping mall for transportation tools based on PHP
Man's life
【测绘程序设计】坐标反算神器V1.0(附C/C#/VB源程序)
[latex] differences in the way scores are written
AI CC 2019 installation tutorial under win10 (super detailed - small white version)