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

Shell — capture UART trace & verify framing

```
ct run --json --board nucleo-f446re --record trace/ build/uart_app.elf
```

## Suite & fault configuration

tests/uart-framing.suite.yaml

```
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: 0
```

## CI pipeline integration

.github/workflows/uart-ci.yml

```
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.yaml
```

## Testing 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 |

Shell - run UART overrun & framing suite

```
ct test run --json --junit results/uart-ore.xml tests/uart-faults.suite.yaml
```

tests/uart-faults.suite.yaml

```
version: 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: 0
```

## Troubleshooting 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. |

## Related boards & component twins

[Board: nucleo-f446re](https://circuittwin.ai/boards/nucleo-f446re) [Board: arduino-uno](https://circuittwin.ai/boards/arduino-uno) [Board: rpi-pico](https://circuittwin.ai/boards/rpi-pico) [Board: esp32-devkitc](https://circuittwin.ai/boards/esp32-devkitc) [Board: nucleo-f767zi](https://circuittwin.ai/boards/nucleo-f767zi) [Board: nrf52840-dk](https://circuittwin.ai/boards/nrf52840-dk) [Twin: DS3231](https://circuittwin.ai/twins/ds3231) [Twin: TMP117](https://circuittwin.ai/twins/tmp117) [Twin: NEO-M9N](https://circuittwin.ai/twins/neo-m9n) [Twin: MAX3485](https://circuittwin.ai/twins/max3485) [Twin: QUECTEL-BG96](https://circuittwin.ai/twins/quectel-bg96) [ct CLI JSON output](https://circuittwin.ai/docs/cli) [Inspecting UART registers in GDB](https://circuittwin.ai/docs/ide) [UART baud mismatch guide](https://circuittwin.ai/guides/uart-baud-mismatch-debugging)

## More in Fault Injection & Debugging

[I2C NACK and stuck-bus fault injection in CI](https://circuittwin.ai/guides/i2c-nack-fault-injection) [SPI bus fault testing and chip-select verification](https://circuittwin.ai/guides/spi-bus-fault-testing) [Cortex-M HardFault triage in CI and reverse GDB](https://circuittwin.ai/guides/cortex-m-hardfault-ci-triage) [Detecting ISR race conditions deterministically](https://circuittwin.ai/guides/isr-race-condition-detection)

> ## Run this workflow on your own ELF
>
> Start free on CircuitTwin to run local board twins, fault matrices and CI checks with zero per-run credits.
>
> [Start now](https://app.circuittwin.ai/try) [Read the quickstart](https://circuittwin.ai/docs/quickstart) [Connect a coding agent](https://circuittwin.ai/docs/agents)
