Home / Learn Chip Programming / STM32 UART: Polling, Interrupt and DMA
STM32 UART: Polling, Interrupt and DMA
Three ways to run the serial port and when to use each.
Theory in plain words
What it is
The USART has TDR/RDR data registers, flags TXE/RXNE/TC/IDLE and a baud rate register BRR = f_pclk / baud.
Why we use it
Serial is the first debug channel and the base for GPS, modems and PC protocols. Dropped bytes come from using the wrong method.
How it works
Polling: simple, blocks. Interrupt: byte by byte into a ring buffer. DMA: block transfers, use the IDLE-line interrupt to detect end of a frame of unknown length.
Where it is used
- Debug logs over ST-LINK VCP
- GPS NMEA sentences
- Modbus and AT command modems
BRR and error calculator
With a 16 bit oversampling clock STM32 baud errors are tiny, unlike AVR. Check the APB bus clock of the specific USART (USART1 on APB2, USART2 on APB1).
| Method | CPU load | Risk | Use when |
|---|---|---|---|
| Polling | high, blocking | misses data if busy | quick test, boot messages |
| Interrupt + ring buffer | low to medium | ISR rate at high baud | command lines up to ~230 kbaud |
| DMA circular + IDLE | near zero | needs buffer management | high speed, frames, GPS, modems |
| HAL_UART_Receive_IT | medium | one buffer at a time | quick prototypes |
Quick quiz
1. IDLE line interrupt is useful for...
It fires when the RX line is quiet for one frame time.
2. BRR = ?
With 16x oversampling.