Fault Injection & Debugging
Detecting ISR race conditions deterministically
Interrupt races hide for weeks on a bench because IRQ arrival phase rarely aligns twice. Sweep arrival phase deterministically and record shared-memory accesses.
A non-atomic read-modify-write on a shared flag between main() and a timer/DMA ISR passes 999 times and corrupts state on the 1000th. Hardware probes perturb timing when you add printf or breakpoints.
CircuitTwin records execution and ISR events to JSONL (--record DIR) without altering firmware timing, and lets you sweep fault/stimulus timestamps across a matrix to expose critical-section gaps.
Common ISR race patterns
| Race class | Root cause | Fix |
|---|---|---|
Lost bit update on GPIO/flags | Non-atomic flags |= BIT in main + ISR | Use atomic bit-set (BSRR), LDREX/STREX or PRIMASK critical section |
Torn multi-word struct read | Main reads 64-bit timestamp while ISR updates | Sequence counter (seqlock) or disable IRQ during copy |
Missing volatile on ISR flag | Compiler hoists loop load into register | Mark shared flag volatile or use C11 _Atomic |
Re-entrant SPI/I2C bus use | ISR starts transfer while main holds bus | Defer bus I/O to task/queue; never share blocking driver in ISR |
Local CLI workflow
Shell — record ISR trace & verify determinism
ct run --json --board nrf52840-dk --record trace/ build/app.elf
ct test run --json --recheck tests/isr-sweep.suite.yamlSuite & fault configuration
tests/isr-sweep.suite.yaml
version: 1
board: nrf52840-dk
firmware: build/app.elf
matrix:
- name: irq-phase-10ms
stimuli: [{ at_ms: 10, pin: P0.11, edge: rising }]
- name: irq-phase-10_3ms
stimuli: [{ at_ms: 10.3, pin: P0.11, edge: rising }]
- name: irq-phase-10_7ms
stimuli: [{ at_ms: 10.7, pin: P0.11, edge: rising }]CI pipeline integration
.github/workflows/isr-race-ci.yml
name: ISR Phase Sweep CI
on: [push, pull_request]
jobs:
isr-sweep:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: west build -b nrf52840dk/nrf52840 app/
- uses: circuittwin/action@v1
with:
suite: tests/isr-sweep.suite.yamlTroubleshooting checklist
| Symptom | Fix |
|---|---|
| Bug disappears at -O0 and fails at -O2 | Audit every variable written in an ISR and read in main() for missing volatile / atomic barriers. |
| DMA half-transfer overwrites unread buffer | Verify double-buffering ping-pong index handoff and check D-Cache invalidation on Cortex-M7. |
| Nested higher-priority ISR corrupts shared queue | Set uniform priority group and guard critical sections with BASEPRI rather than ad-hoc flags. |
| Need to inspect exact instruction interleaving | Inspect the JSONL trace from --record DIR or reverse-step across the IRQ boundary in GDB. |