cancel
Showing results for 
Search instead for 
Did you mean: 

Buffering in STM32F4 uC for UART

JCorl.1
Senior

I recently got my STM32F407VG uC working with UART4. I can receive a byte and send a byte. Though I have been reading on how to acquire more than one byte at a time. It seems as though people like to use DMA. I am perfectly okay with polling for my experiments and projects.

I have been looking through the UART section of the reference manual. I see a lot of mention of multibuffering with DMA but I cannot seem to find how big the UART buffer is. I am trying to do something similar to what serial LCDs do where they have a command prefix and then an actual command with optional parameters.

In my mind, there would be a buffer of fixed size that would hold these byte command requests from another device transmitting to the uC. I could read each byte command in this buffer one by one and take the correct action for each one. For example, the Arduino I used had a 64 byte ring buffer and grabbing one byte at a time was possible.

Note: I am not a professional firmware programmer or engineer. Just trying to learn something here.

Any hints are appreciated! Thanks for your time!

1 ACCEPTED SOLUTION

Accepted Solutions
6 REPLIES 6

The DMA buffers will be as large as you make them.

The USART on the F4 has a 1-byte (9-bit) holding register, and needs to be serviced to avoid over/under run conditions.

Some of the newer families have a small FIFO, perhaps 8 or 16 deep separately for Rx and Tx paths.

For Rx I'd likely use the DMA to create a small FIFO/RING Buffer, and that would sweep/feed a larger FIFO buffer.

For Tx, large blocks of established length can typically be DMA'd directly, perhaps using scatter-gather / chaining methods.

More generally, and where the scope of the source buffer is dubious/transient, I'd likely use a larger FIFO buffer, and service that via DMA IRQ, dispatching available data blocks via DMA, and perhaps POLL/IRQ for byte only.

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

Since it's a software buffer, you decide how large it is. Technically, the buffer is limited to 65535 words or less due to DMA restrictions. Using UART with DMA in circular mode using a circular buffer is the best option, IMO.

If you feel a post has answered your question, please click "Accept as Solution".
JCorl.1
Senior

Thank you both for your answers. Looks like there is quite a bit to configure! When I get somewhere, I will let everyone know here and post code for future readers. I really only need to have a DMA for the RX line. I am okay with a single byte for TX with no DMA.

S.Ma
Principal

You might choose the buffer size depending on the write speed=baudrate, the largest message expected, the time it takes to read and process the message. If the baudrate is many times lower than core speed, byte interrupt method maybe enough.

Piranha
Chief II
JCorl.1
Senior

Thank you everyone! I decided to move onto using the HAL now as it seems it might be easier to use for DMA and many examples show DMA with it. Though I am stuck on it so please consider this thread closed. I have another thread in using HAL and UART here: https://community.st.com/s/question/0D53W00001BRSpgSAH/usart1-for-stm32f411ce-using-hal

Thanks again everyone for their time in responding!