cancel
Showing results for 
Search instead for 
Did you mean: 

How to receive using UART to a precised byte

LOnGr.1
Associate III

Hi !

I am receiving data through UART using DMA to a buffer of 10 bytes. I want to always receive to the uartBuffer[0].

Thank you!

2 REPLIES 2
Bob S
Principal

If you want to receive 6 bytes and then start over that the beginning of the buffer, then set the DMA to only receive 6 bytes, not 10.

BUT - unless you know there will be a pause after each group of 6 bytes, there is a (small) chance you might miss the first byte of the next group. You could set the DMA to circular mode, but then you still need to guarantee you read the bytes before the next group arrives. And what do you do if some noise or bad connection causes unexpected or missing/corrupted bytes? That will throw off your "read 6 bytes" scheme.

See this code for using DMA in a more robust manner: https://github.com/MaJerle/stm32-usart-uart-dma-rx-tx

Synchronization and resync probably best solved separately from buffering. It is hard to change sizing on the fly, and restarting/aborting can potential miss bytes if ill-timed.

If you can live with IRQs each byte can generate an interrupt and you can process via a state machine. This will generally be the most responsive as you can advance the state at each interrupt immediately after the last byte arrives.

If DMA is critical, you can make a larger circular buffer, and then sweep periodically to recover long/short packets, and not lose any data. The depth of the buffer can be determined by the frequency of the processing vs baud rate, or the length of time each packet takes to process / respond too.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..