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!
STM32F4 + ChibiOS UART Receive Using DMA – Best Practice?
Moderators: barthess, RoccoMarco
-
- Posts: 2
- Joined: Mon May 12, 2025 9:02 am
- Giovanni
- Site Admin
- Posts: 14660
- Joined: Wed May 27, 2009 8:48 am
- Location: Salerno, Italy
- Has thanked: 1135 times
- Been thanked: 947 times
Re: STM32F4 + ChibiOS UART Receive Using DMA – Best Practice?
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
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
Re: STM32F4 + ChibiOS UART Receive Using DMA – Best Practice?
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.
- 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?
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).
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).
Who is online
Users browsing this forum: No registered users and 55 guests