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

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

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

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

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

## Related boards & component twins

[Board: nucleo-f767zi](https://circuittwin.ai/boards/nucleo-f767zi) [Board: nucleo-f411re](https://circuittwin.ai/boards/nucleo-f411re) [Board: arduino-uno](https://circuittwin.ai/boards/arduino-uno) [Board: esp32c3-devkitm](https://circuittwin.ai/boards/esp32c3-devkitm) [Twin: TMP117](https://circuittwin.ai/twins/tmp117) [Twin: TCA9548A](https://circuittwin.ai/twins/tca9548a) [Twin: SHT31-DIS](https://circuittwin.ai/twins/sht31-dis) [Twin: MPU-6050](https://circuittwin.ai/twins/mpu-6050) [Twin: 24LC256](https://circuittwin.ai/twins/24lc256) [Twin: AT24C256C](https://circuittwin.ai/twins/at24c256c) [Twin: BME280](https://circuittwin.ai/twins/bme280) [Inputs and faults reference](https://circuittwin.ai/docs/inputs) [Fault matrix docs](https://circuittwin.ai/docs/testing) [I2C NACK fault injection guide](https://circuittwin.ai/guides/i2c-nack-fault-injection)

## More in Fault Injection & Debugging

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