STM32F4 + ChibiOS UART Receive Using DMA – Best Practice?

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: barthess, RoccoMarco

lostintheether
Posts: 2
Joined: Mon May 12, 2025 9:02 am

STM32F4 + ChibiOS UART Receive Using DMA – Best Practice?

Postby lostintheether » Fri Jun 06, 2025 9:55 am

Hi all,

I’m currently working on an STM32F407 project using ChibiOS, and I’m trying to implement efficient UART reception using DMA. I’ve gone through some of the examples in the testhal directories, but I’m still not completely sure about the best way to handle circular DMA reception and buffer management in ChibiOS.

Here’s what I’m aiming for:

UART RX via DMA in circular mode

Non-blocking reception with callback or polling

Safe buffer handling with minimal CPU overhead

I’m using the HAL UART driver and wondering:

Is there a better way to trigger callbacks on partial buffer fills?

Any gotchas or tips for integrating this cleanly with a working thread or event-driven model?

Any guidance, working examples, or references would be greatly appreciated. Thanks in advance!

User avatar
Giovanni
Site Admin
Posts: 14662
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1137 times
Been thanked: 948 times

Re: STM32F4 + ChibiOS UART Receive Using DMA – Best Practice?

Postby Giovanni » Fri Jun 06, 2025 11:47 am

Hi,

The UART driver does not implement a circular mode, it is a frame-oriented driver. The serial "SD" driver implements circular buffers but it is interrupts-driven.

There is also a 3rd (and newer) driver model called SIO, it does not manage buffering internally but allows the use to "hook" DMAs on it and program DMA channels freely. This SIO driver however is only available on newer STM32, F4 is quite old and uses an older USART not supported by this driver.

One option would be to use something newer like a STM32G4 for your development.

Giovanni

faisal
Posts: 382
Joined: Wed Jul 19, 2017 12:44 am
Has thanked: 45 times
Been thanked: 62 times

Re: STM32F4 + ChibiOS UART Receive Using DMA – Best Practice?

Postby faisal » Fri Jun 06, 2025 4:09 pm

Use cobs encoding for frames. That will allow you to use DMA in receive mode without having to constantly poll/scan the DMA buffer if you have received a frame or not. Set receive DMA length to maximum frame size, and use the character match interrupt set to the 0 byte (COBS framing character) to detect end of frame.

User avatar
FXCoder
Posts: 398
Joined: Sun Jun 12, 2016 4:10 am
Location: Sydney, Australia
Has thanked: 186 times
Been thanked: 133 times

Re: STM32F4 + ChibiOS UART Receive Using DMA – Best Practice?

Postby FXCoder » Sun Jun 08, 2025 3:04 am

In The F4 series you also have DMA double buffer mode.
See Ref Manual 10.3.9 Double buffer mode.

So making use of this with the two DMA buffers allows processing (or copy out) of one DMA buffer while another packet is incoming on the other buffer. Not unlike what you would do with circular but perhaps more convenient and possibly lower overhead.

Keep in mind the need to have a robust means of establishing/re-establishing packet sync for the COBS system.
Some good reading on COBS here (see the paper on PPP for extra interest).


Return to “STM32 Support”

Who is online

Users browsing this forum: Bing [Bot] and 64 guests