STM32F103 I2C bus stuck in HAL_BUSY after a failed transfer — bus recovery
## The symptom
I am bringing up an STM32F103C8 blue pill talking to an SHT31 over I2C1 at 100kHz, using STM32CubeIDE 1.14.0 with HAL_F1 1.8.5. Everything was fine until I unplugged the sensor mid-read during a cable test. After that, every HAL_I2C_Master_Transmit returned HAL_BUSY immediately, even after I powered the sensor back on:
if (HAL_I2C_Master_Transmit(&hi2c1, 0x44 << 1, buf, 1, 100) != HAL_OK) {
Error_Handler(); // status is always 0x02
}
HAL_BUSY is 0x02. It never timed out, never recovered, and ignored my 100ms timeout entirely.
## Reading the register that explains it
The flag to look at is the busy bit in SR2:
if (hi2c1.Instance->SR2 & I2C_SR2_BUSY) { / stuck / }
It stayed set because the peripheral thought a transfer was still in flight. Meanwhile SDA was physically held low by the sensor's output stage when it lost power. Nine clocks on SCL would have let it release, one per bit of the shifted-out byte, but the peripheral refuses to clock SCL while it believes the bus is busy. Classic deadlock between the physical layer and the state machine, and it is not specific to the F1; I have seen the same thing on an F4.
## The recovery routine
The workaround is to temporarily take the two pins over as open-drain GPIO, clock SCL by hand, then hand them back to the I2C peripheral:
static void I2C1_BusRecovery(void) {
GPIO_InitTypeDef g = {0};
g.Pin = GPIO_PIN_6 | GPIO_PIN_7; // PB6 SCL, PB7 SDA
g.Mode = GPIO_MODE_OUTPUT_OD;
g.Pull = GPIO_NOPULL;
g.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &g); for (int i = 0; i < 9; i++) {
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
HAL_Delay(1);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET);
HAL_Delay(1);
}
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET);
HAL_I2C_Init(&hi2c1); // reconfigure as alternate function
}
Only call it when SR2 busy has been stuck for more than a few milliseconds, otherwise you will stomp on a legitimate slow transfer. Note that SCL goes low then high; SDA is left alone until the end, because driving it while the slave holds it is exactly what you are trying to avoid.
## What went wrong first
I tried HAL_I2C_DeInit followed by HAL_I2C_Init, which I assumed would reset the state machine. It did nothing useful, because the problem is not just the peripheral's flags; SDA is genuinely low. Another guy on the bench suggested toggling the sensor's reset pin, which would have worked if we had routed it. We had not, so that was a dead end too. Worth checking whether SCL is stuck as well; a slave can hold that line down too, though it is rarer. On my scope the SCL trace looked clean, which is how I narrowed it to SDA specifically.
## Making it stick
I now call the recovery once at boot before any I2C traffic, and again from my transmit wrapper after three HAL_BUSY results in a row. It has survived a week of me yanking cables during tests. If you are seeing HAL_BUSY that never clears, check SR2 before you start rewriting your whole driver. Nine clock pulses is usually the answer.