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 kindWhat it simulatesWhat firmware must do
nack (count: 1–3)Transient EMI or sensor busy during conversionClear AF/NACKF, issue STOP, retry within timeout
nack (permanent)Unsoldered or dead IC on PCBLog error, switch to degraded mode, never deadlock
register_override (WHO_AM_I)Wrong BOM substitution or silent bit-shiftReject probe before enabling control loops
frozen_readingStuck internal sensor ADC / FIFODetect stale value across N samples and flag fault

Local CLI workflow

Shell — run I2C fault matrix
ct test matrix run --json --junit results/i2c-faults.xml tests/i2c-faults.yaml

Suite & fault configuration

tests/i2c-faults.yaml
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

.github/workflows/i2c-faults.yml
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.xml

Testing 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

StepGPIO / Peripheral actionWhat CI verifies
1. Detect BUSYRead SDA input before enabling I2CDriver enters recovery path instead of timing out
2. Toggle 9 SCLSwitch SCL to open-drain output, pulse 9 clocksTarget shifts out remaining bits and releases SDA
3. Generate STOPDrive SDA low then high while SCL is highPeripheral initializes with BUSY=0 and reads chip ID
Shell - run stuck-SDA recovery test
ct test run --json --junit results/i2c-stuck-sda.xml tests/i2c-stuck-sda.suite.yaml
tests/i2c-stuck-sda.suite.yaml
version: 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: 0

Troubleshooting checklist

SymptomFix
STM32 HAL_I2C_Master_Receive returns HAL_BUSY foreverClear 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 kHzCompute exact Rp(min)/Rp(max) for your bus capacitance in the I2C Pull-Up Calculator.
7-bit vs 8-bit address mismatchSTM32 CubeHAL expects (addr7 << 1); Zephyr and Linux i2c APIs take unshifted 7-bit addresses.
Address collision between identical sensorsPut conflicting sensors behind a TCA9548A I2C mux and test channel-switching faults.
STM32 I2C stays stuck in BUSY after GPIO recoveryToggle 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 sensorsInsert at least a 5 us delay per half-cycle while bit-banging SCL.

Frequently asked questions

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

Related boards & component twins

More in Fault Injection & Debugging