Fault Injection & Debugging
Debugging UART baud mismatches and framing errors
When serial output turns into random bytes or stops after one framing error, check divider error math first and verify ORE recovery in CI.
UART framing errors (FE) and overrun errors (ORE) kill telemetry links silently. On STM32, an uncleared ORE flag can stall RX interrupts indefinitely unless the ISR explicitly clears ICR.ORECF (or reads SR then DR on STM32F1/F4).
Combine exact divider verification from the UART Baud Calculator with headless twin tests that assert clean 8N1 framing and exercise RX overrun recovery.
Common UART register mistakes
| MCU family | Mistake | Symptom & fix |
|---|---|---|
AVR @ 16 MHz | 115200 bps with U2X=0 | -3.55% error corrupts bytes -> enable U2X=1 (+2.12%) |
STM32F4/F7 | Using PCLK2 clock for APB1 USART2/3 | Actual baud is 50% of target -> read PCLK1 |
STM32F1/F4 | Not clearing ORE in RX ISR | RX interrupts lock up -> read SR then DR on error |
STM32G0/H7/L4 | Writing DR instead of ICR to clear FE/ORE | Interrupt storms -> write USART_ICR_ORECF | FECF |
Local CLI workflow
ct run --json --board nucleo-f446re --record trace/ build/uart_app.elfSuite & fault configuration
version: 1
board: nucleo-f446re
firmware: build/uart_app.elf
tests:
- name: uart-command-echo-and-framing
timeout_ms: 2000
uart_tx:
- at_ms: 50
text: "PING 1234\n"
expect:
console_contains: "PONG 1234"
exit_code: 0CI pipeline integration
name: UART Protocol CI
on: [push, pull_request]
jobs:
uart:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: cmake --build build
- uses: circuittwin/action@v1
with:
suite: tests/uart-framing.suite.yamlTesting UART framing errors, ORE overruns and DMA idle-line
An uncleared USART ORE (overrun error) flag silently kills RX interrupts forever on STM32. Catch it in CI.
On STM32 USART peripherals, a single burst of bytes while interrupts are masked sets the ORE flag. If the ISR only reads DR on RXNE and forgets to clear ORE, RX interrupts stop firing permanently.
CircuitTwin injects baud mismatch, burst traffic and framing faults so CI proves your UART ISR and DMA circular idle-line parser recover cleanly.
UART failure modes verified
| Error flag | Trigger condition | Recovery verified |
|---|---|---|
ORE (Overrun) | Byte arrives before previous DR read | ISR clears ORE via ICR/SR+DR sequence and resumes RX |
FE (Framing) | Baud mismatch > 3% or stop-bit glitch | Parser discards corrupted frame and re-syncs header |
IDLE + DMA | Variable-length packet pause | Circular DMA read index wraps cleanly without dropping bytes |
ct test run --json --junit results/uart-ore.xml tests/uart-faults.suite.yamlversion: 1
board: nucleo-f446re
firmware: build/uart_dma.elf
tests:
- name: uart-framing-error-resync
timeout_ms: 2000
faults:
- at_ms: 150
bus: USART2
kind: baud_mismatch
error_pct: 6.0
duration_ms: 50
expect:
console_contains: "UART_RESYNC_OK"
exit_code: 0Troubleshooting checklist
| Symptom | Fix |
|---|---|
| Configured baud differs >2% from peer | Run your exact peripheral clock through the UART Baud Calculator and enable 8x oversampling if needed. |
| RX stops after burst of bytes | Inspect USART_SR/ISR for ORE (overrun) and ensure your error handler clears it without disabling RXNEIE. |
| Missing last byte before sleep/reset | Wait for TC (Transmission Complete), not just TXE (Transmit Data Register Empty), before entering STOP mode. |
| Half-duplex RS-485 clips first reply bit | De-assert DE pin only on TC interrupt rather than TXE. |
| USART RX stops permanently after first noise burst | Handle ORE/FE/NE flags in HAL_UART_ErrorCallback() and restart HAL_UART_Receive_IT/DMA(). |
| Baud rate drifts on HSI oscillator | Check exact USART_BRR error percentage in the UART Baud Rate Calculator. |
Frequently asked questions
| Question | Answer |
|---|---|
| What maximum baud-rate mismatch can 8N1 UART tolerate? | Combined transmitter plus receiver clock error must stay below roughly 4% (2% per side) so the stop bit is sampled inside the valid bit window. |