Fault Injection & Debugging
I2C NACK and stuck-bus fault injection in CI
Most I2C drivers work on a quiet desk and lock up in the field on the first NACK. Automate NACK and stuck-SDA fault injection on every commit.
A single missed ACK from a brownout-reset sensor or an ESD spike on SDA will hang any driver that polls status flags without bounded timeouts. Worse, if the MCU resets mid-read while a slave holds SDA low, hardware I2C peripherals refuse to issue START until firmware bit-bangs 9 SCL clocks plus STOP.
CircuitTwin injects deterministic I2C NACKs, register overrides (wrong WHO_AM_I) and bus disconnects at exact millisecond timestamps so your retry and bus-recovery code runs in CI.
I2C fault patterns to test in every CI suite
| Fault kind | What it simulates | What firmware must do |
|---|---|---|
nack (count: 1–3) | Transient EMI or sensor busy during conversion | Clear AF/NACKF, issue STOP, retry within timeout |
nack (permanent) | Unsoldered or dead IC on PCB | Log error, switch to degraded mode, never deadlock |
register_override (WHO_AM_I) | Wrong BOM substitution or silent bit-shift | Reject probe before enabling control loops |
frozen_reading | Stuck internal sensor ADC / FIFO | Detect stale value across N samples and flag fault |
Local CLI workflow
ct test matrix run --json --junit results/i2c-faults.xml tests/i2c-faults.yamlSuite & fault configuration
version: 1
board: nucleo-f767zi
firmware: build/app.elf
tests:
- name: transient-nack-recovers
faults:
- at_ms: 40
bus: I2C1
kind: nack
address: 0x48
count: 2
expect:
console_contains: "TMP117_OK"
exit_code: 0
- name: wrong-device-id-rejected
faults:
- at_ms: 0
part: tmp117
kind: register_override
register: 0x0F
value: 0x0000
expect:
console_contains: "TMP117_ID_MISMATCH"CI pipeline integration
name: I2C Fault Matrix
on: [push, pull_request]
jobs:
i2c-faults:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: cmake --build build
- uses: circuittwin/action@v1
with:
suite: tests/i2c-faults.yaml
junit-out: results/i2c-faults.xmlTesting I2C stuck-SDA 9-clock recovery in CI
Simulate an MCU reset mid-I2C-read while a sensor holds SDA low, then verify your 9-clock recovery unsticks the bus.
If a watchdog resets your MCU while an I2C EEPROM or sensor is shifting out a 0 bit, the target keeps holding SDA low forever. Standard HAL_I2C_Init() sees BUSY=1 and refuses to start.
CircuitTwin lets CI hold SDA low at boot or mid-transaction so you can prove your GPIO bit-bang 9-clock plus STOP sequence clears BUSY before enabling the hardware I2C peripheral.
I2C stuck-bus recovery sequence
| Step | GPIO / Peripheral action | What CI verifies |
|---|---|---|
1. Detect BUSY | Read SDA input before enabling I2C | Driver enters recovery path instead of timing out |
2. Toggle 9 SCL | Switch SCL to open-drain output, pulse 9 clocks | Target shifts out remaining bits and releases SDA |
3. Generate STOP | Drive SDA low then high while SCL is high | Peripheral initializes with BUSY=0 and reads chip ID |
ct test run --json --junit results/i2c-stuck-sda.xml tests/i2c-stuck-sda.suite.yamlversion: 1
board: nucleo-f411re
firmware: build/app.elf
tests:
- name: sda-stuck-low-9-clock-recovery
timeout_ms: 2000
faults:
- at_ms: 0
bus: I2C1
kind: stuck_sda_low
release_after_clocks: 9
expect:
console_contains: "I2C_BUS_RECOVERED"
exit_code: 0Troubleshooting checklist
| Symptom | Fix |
|---|---|
| STM32 HAL_I2C_Master_Receive returns HAL_BUSY forever | Clear NACKF/STOPF, toggle SCL 9 times in GPIO open-drain mode, then reset I2C peripheral via RCC_APB1RSTR. |
| Pull-up resistor too large for 400 kHz | Compute exact Rp(min)/Rp(max) for your bus capacitance in the I2C Pull-Up Calculator. |
| 7-bit vs 8-bit address mismatch | STM32 CubeHAL expects (addr7 << 1); Zephyr and Linux i2c APIs take unshifted 7-bit addresses. |
| Address collision between identical sensors | Put conflicting sensors behind a TCA9548A I2C mux and test channel-switching faults. |
| STM32 I2C stays stuck in BUSY after GPIO recovery | Toggle I2C_CR1.SWRST (F1/F4) or clear/re-enable PE (F7/G0/L4/H7) after reconfiguring AF open-drain pins. |
| Recovery clocks run too fast for 100 kHz sensors | Insert at least a 5 us delay per half-cycle while bit-banging SCL. |
Frequently asked questions
| Question | Answer |
|---|---|
| Why does I2C recovery require 9 SCL pulses? | An I2C byte plus ACK is 9 clock cycles. Pulsing SCL up to 9 times guarantees any interrupted target finishes shifting its current byte and releases SDA to idle high. |