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 classRoot causeFix
Lost bit update on GPIO/flagsNon-atomic flags |= BIT in main + ISRUse atomic bit-set (BSRR), LDREX/STREX or PRIMASK critical section
Torn multi-word struct readMain reads 64-bit timestamp while ISR updatesSequence counter (seqlock) or disable IRQ during copy
Missing volatile on ISR flagCompiler hoists loop load into registerMark shared flag volatile or use C11 _Atomic
Re-entrant SPI/I2C bus useISR starts transfer while main holds busDefer 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.yaml

Suite & 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.yaml

Troubleshooting checklist

SymptomFix
Bug disappears at -O0 and fails at -O2Audit every variable written in an ISR and read in main() for missing volatile / atomic barriers.
DMA half-transfer overwrites unread bufferVerify double-buffering ping-pong index handoff and check D-Cache invalidation on Cortex-M7.
Nested higher-priority ISR corrupts shared queueSet uniform priority group and guard critical sections with BASEPRI rather than ad-hoc flags.
Need to inspect exact instruction interleavingInspect the JSONL trace from --record DIR or reverse-step across the IRQ boundary in GDB.

Related boards & component twins

More in Fault Injection & Debugging