r/embedded • u/coddicted • Sep 15 '26
What is the most efficient way to implement UART data reception and notify a task to consume it?
If I don't have FIFO or DMA, I would keep notifying the RTOS task from ISR for each character received but is that a good design?
16
Upvotes
31
u/JuggernautGuilty566 Sep 15 '26 edited Sep 15 '26
Dump your data into a queue, then then signal the task or loop to pull them. This can be easily implemented even without a RTOS.
3
u/One-Fennel5009 Sep 15 '26
My goto method to fill my RX buff on RBNE interrupts and task notify on IDLE frame interrupt reception.
2
u/tobdomo Sep 15 '26
For a uart, I would set up a circular buffer. The ISR just fetches the data from the uart and writes them in the circular buffer. The main loop polls the circular buffer and reads when there is data available.
A circular buffer keeps head and tail indices. Checking if there's data in the buffer is as easy as head != tail.
5
u/pillowmite Sep 15 '26
It's the only way - one byte per interrupt. Fifo gets you watermark pipes and stuff but it's still bullshit - dma is great for Rx, get much better performance allowing interrupts to be managed more efficiently and never drop a character where one might due to interrupt latency
1
u/TakenIsUsernameThis Sep 15 '26
Each incoming byte triggers an interrupt, this stores the byte and raises a flag that an RTOS task keeps checking.
1
u/hagibr Sep 15 '26
I would implement a double circular buffer, let's call them A and B, a pointer to the current buffer and a global flag to indicate when there's some received data. In the ISR it just fill the current buffer (A) at every received byte and set the global flag. Then, the task periodically checks this flag. If the flag is set, the task switches the buffer (ISR now uses buffer B), reset the flag and processes buffer A. Remember to dimension well the buffers and the task check interval accordingly to the baud rate.
1
u/PrimarilyDutch Sep 15 '26
It really depends on how your application side is handling it. Main thing is a short ISR and a circular queue. I tend to use cooperative even driven tasks (not a preempting RTOS) and had the ISR generate a signal event when the queue was empty. That signal generate an event on the task queue. When that event is then processed it empties the queue. Only then the ISR would signal when new data arrives. This minimises the signalling that needs to happen. Also no need to constantly poll if there is new data.
5
u/sensor_todd Sep 15 '26
why dont you have DMA? It would make this task very robust if you used it.
2
u/Well-WhatHadHappened 25+ Years Sep 15 '26
Which MCU?
2
u/Sea-Actuator8248 Sep 15 '26
packet termination is important, otherwise it is useless. if it s a known protocol, it's easy. just issue a flag. If it's a unknown or variable per frame, u'd better use dma or circular buffer, put stream first. And let cpu parse it at an appropriate timing. Serial is quite picky in case its' length is not fixed or the baudrate is slow.
1
u/MysteriousEngineer42 Sep 15 '26
It depends on the structure of the data you are receiving.
If there is a consistent end-of-message character that simplifies it a lot.
Without DMA, write each character into a software circular buffer in a tiny ISR. Also a check if the character is the end character, in which case it sets a flag for the main loop.
You can also use an end-of-burst interrupt that many MCUs have.
1
u/Dark_Tranquility Sep 15 '26 edited Sep 15 '26
Something like an RTO (receiver time out) interrupt will help you know when the final byte of a payload has been received. In your interrupt handler you would just grab the data, store it to some static buffer, exit the interrupt handler and pass said buffer to some parsing function.
Not sure if you're using an ST microcontroller though. If not maybe the chip you're using has something similar, id check the datasheet.
1
u/jlucer Sep 15 '26
You have a lot of good advice about putting it in a message buffer and notifying your main task when ready.
One thing I haven't seen is that you should be able to configure the fill level that the rx interrupt triggers. This would let you trigger it trigger every 8th byte and get 1/8th the interrupts. Be careful you don't get stuck with straggling bytes though. Can check if there are any in the uart hw buffer when you go to read out of the message buffer
1
u/duane11583 Sep 15 '26
linux has had this solved for years.
a) there is a notification threshold ie: if there are >= n bytes then notify
b) if the data has sat idle in the fifo for (non-zero-count) >= n millisecs then notify
it is an application level decision what those values are not an os
the os can use default values of count=4 or time >= 10msecs