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

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

## Related boards & component twins

[Board: nrf52840-dk](https://circuittwin.ai/boards/nrf52840-dk) [Board: nucleo-f767zi](https://circuittwin.ai/boards/nucleo-f767zi) [Board: stm32f4-discovery](https://circuittwin.ai/boards/stm32f4-discovery) [Twin: BMI088](https://circuittwin.ai/twins/bmi088) [Twin: LSM6DSO](https://circuittwin.ai/twins/lsm6dso) [\--record trace flags](https://circuittwin.ai/docs/cli) [Test matrices](https://circuittwin.ai/docs/testing)

## 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) [Debugging UART baud mismatches and framing errors](https://circuittwin.ai/guides/uart-baud-mismatch-debugging) [Cortex-M HardFault triage in CI and reverse GDB](https://circuittwin.ai/guides/cortex-m-hardfault-ci-triage)

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