Fault Injection & Debugging

# SPI bus fault testing and chip-select verification

SPI has no hardware ACK bit—when a slave drops off the bus, MISO silently reads 0x00 or 0xFF. Test your detection paths automatically in CI.

Because SPI has no acknowledgement phase, a disconnected sensor, bad CPOL/CPHA mode or dropped CS line simply returns 0xFF or shifted garbage. Firmware without chip-ID checks, CRC validation or range sanity checks happily feeds 0xFFFF into a PID loop.

CircuitTwin lets you run real SPI drivers against twins like Bosch BMI088, MAX31855, SX1276 and SX1280 while injecting bus disconnects, busy-pin holds and frozen register values.

## SPI failure modes to gate in CI

| Failure mode                     | What MISO returns                  | How to catch it in firmware                              |
| -------------------------------- | ---------------------------------- | -------------------------------------------------------- |
| Slave disconnected / unpowered   | All 0xFF or 0x00                   | Check WHO\_AM\_I / CHIP\_ID + reject all-0xFF frames     |
| Wrong CPHA or dummy byte         | Bit-shifted ID (e.g. 0x3C vs 0x1E) | Assert exact reset CHIP\_ID against twin                 |
| CS raised while TX FIFO draining | Truncated write / ignored command  | Poll SPI\_SR.BSY == 0 before raising CS                  |
| Radio BUSY pin ignored (SX1280)  | Dropped SetTx / config command     | Poll BUSY low before every SPI opcode (verified by AC-6) |

## Local CLI workflow

Shell — run SPI fault suite

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

## Suite & fault configuration

tests/spi-faults.suite.yaml

```
version: 1
board: nucleo-f767zi
firmware: build/spi_imu.elf
tests:
  - name: bmi088-bus-disconnect-recovery
    faults:
      - at_ms: 80
        part: bmi088
        kind: bus_disconnect
        duration_ms: 30
    expect:
      console_contains: "SPI_REINIT_OK"
      exit_code: 0
  - name: max31855-open-thermocouple
    faults:
      - at_ms: 50
        part: max31855
        kind: fault_bit
        bit: OC
    expect:
      console_contains: "TC_OPEN_FAULT"
```

## CI pipeline integration

.github/workflows/spi-faults.yml

```
name: SPI Fault CI
on: [push, pull_request]
jobs:
  spi-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: cmake --build build
      - uses: circuittwin/action@v1
        with:
          suite: tests/spi-faults.suite.yaml
```

## SPI chip-select glitch and bus-disconnect testing

Catch wrong SPI CPOL/CPHA modes, missing CS de-assertion and frozen MISO readings before flashing a PCB.

Shared SPI buses fail silently when one driver leaves CS low or forgets to restore CPOL/CPHA after talking to a Mode 3 IMU and a Mode 0 flash chip.

CircuitTwin models full-duplex SPI transactions, CS framing and per-device fault injection so CI fails immediately when an unexpected chip ID or 0xFF/0x00 bus read goes unchecked.

### SPI faults caught in CI

| Fault                    | Wire symptom               | Expected firmware check                                           |
| ------------------------ | -------------------------- | ----------------------------------------------------------------- |
| bus\_disconnect          | MISO reads 0xFF or 0x00    | Driver rejects invalid JEDEC/WHO\_AM\_I instead of trusting zeros |
| Mode mismatch            | Shifted bit values on MISO | Init fails fast with SPI\_MODE\_ERROR                             |
| CS held across transfers | Command framing misaligned | Twin logs framing fault and driver recovers cleanly               |

Shell - run SPI disconnect fault suite

```
ct test run --json --junit results/spi-faults.xml tests/spi-glitch.suite.yaml
```

tests/spi-glitch.suite.yaml

```
version: 1
board: nucleo-h743zi2
firmware: build/app.elf
tests:
  - name: spi-imu-disconnect-and-reinit
    timeout_ms: 2000
    faults:
      - at_ms: 80
        part: icm-42688-p
        kind: bus_disconnect
        duration_ms: 40
    expect:
      console_contains: "SPI_SENSOR_RECOVERED"
      exit_code: 0
```

## Troubleshooting checklist

| Symptom                                          | Fix                                                                                                |
| ------------------------------------------------ | -------------------------------------------------------------------------------------------------- |
| BMI088 accelerometer ID reads 0x00 on first byte | BMI088 accelerometer requires 1 dummy byte after the address byte in SPI mode; gyroscope does not. |
| Slave misses last write byte at high SCLK        | Wait for RXNE and BSY==0 before releasing CS; check prescaler in the SPI Clock Divider Calculator. |
| SX1280 ignores SetTx command intermittently      | Poll the BUSY GPIO until low before asserting NSS (checked by radio acceptance criterion AC-6).    |
| Shared SPI bus corrupts flash during IMU DMA     | Guard bus access with a mutex and verify CS polarity/idle states for every device on the bus.      |
| CS rises before last SPI byte finishes shifting  | Wait for SPI\_SR\_BSY == 0 (not just TXE == 1) before pulling NSS high.                            |
| SPI read lags by one byte                        | Flush the RX FIFO or read SPI\_DR after every dummy write to clear OVR.                            |

## Frequently asked questions

| Question                                                   | Answer                                                                                                                                                 |
| ---------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
| Why does pulling SPI CS high on TXE corrupt the last byte? | TXE fires when the transmit buffer is empty, while the shift register is still clocking the final byte onto MOSI. Always poll BSY=0 before raising CS. |

## Related boards & component twins

[Board: nucleo-f767zi](https://circuittwin.ai/boards/nucleo-f767zi) [Board: nucleo-h743zi2](https://circuittwin.ai/boards/nucleo-h743zi2) [Board: rpi-pico](https://circuittwin.ai/boards/rpi-pico) [Board: nrf52840-dk](https://circuittwin.ai/boards/nrf52840-dk) [Board: rpi-pico2](https://circuittwin.ai/boards/rpi-pico2) [Twin: BMI088](https://circuittwin.ai/twins/bmi088) [Twin: MAX31855](https://circuittwin.ai/twins/max31855) [Twin: SX1280](https://circuittwin.ai/twins/sx1280) [Twin: SX1276](https://circuittwin.ai/twins/sx1276) [Twin: W25Q128JV](https://circuittwin.ai/twins/w25q128jv) [Twin: ICM-42688-P](https://circuittwin.ai/twins/icm-42688-p) [Twin: MAX31865](https://circuittwin.ai/twins/max31865) [Twin: ST7789](https://circuittwin.ai/twins/st7789) [Sensor & radio oracle records](https://circuittwin.ai/docs/accuracy) [Fault injection reference](https://circuittwin.ai/docs/inputs) [SPI bus fault testing overview](https://circuittwin.ai/guides/spi-bus-fault-testing)

## More in Fault Injection & Debugging

[I2C NACK and stuck-bus fault injection in CI](https://circuittwin.ai/guides/i2c-nack-fault-injection) [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) [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)
