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
ct test run --json --junit results/spi.xml tests/spi-faults.suite.yamlSuite & fault configuration
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
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.yamlSPI 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 |
ct test run --json --junit results/spi-faults.xml tests/spi-glitch.suite.yamlversion: 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: 0Troubleshooting 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. |