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.

In shortThree ways to run the serial port and when to use each.

Theory in plain words

DMA receive with IDLE lineDMA fills rx_bufline goes idleIDLE interruptlen = size - DMA counterparse the frame
DMA receive with IDLE line

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

Baud rate registerTry it Yourself

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).

MethodCPU loadRiskUse when
Pollinghigh, blockingmisses data if busyquick test, boot messages
Interrupt + ring bufferlow to mediumISR rate at high baudcommand lines up to ~230 kbaud
DMA circular + IDLEnear zeroneeds buffer managementhigh speed, frames, GPS, modems
HAL_UART_Receive_ITmediumone buffer at a timequick prototypes
Tip:The Nucleo ST-LINK connects USART2 (PA2/PA3) to a USB virtual COM port. Open it at 115200 8N1 in any terminal to see printf output.

Quick quiz

1. IDLE line interrupt is useful for...

2. BRR = ?