HeapCrash / Challenges / Stuck I2C bus after reset

Stuck I2C bus after reset

An I2C temperature sensor shares a bus with an EEPROM. read_temp_c runs in the sensor task; the settings task writes the EEPROM through the same hi2c1. If the MCU resets in the middle of a sensor read, every transfer fails after reboot until power is cycled, and the sensor task freezes. Now and then a settings write corrupts a temperature reading.

The code

Embedded C for Cortex-M. It compiles. There are 3 real bugs in it: the kind that pass a bench test and fail in the field.

float read_temp_c(void)
{
    uint8_t raw[2];
    while (HAL_I2C_Mem_Read(&hi2c1, TMP_ADDR << 1, TMP_REG,
                            I2C_MEMADD_SIZE_8BIT, raw, 2,
                            HAL_MAX_DELAY) != HAL_OK) {
        HAL_Delay(10);
    }
    int16_t t = (int16_t)((raw[0] << 8) | raw[1]) / 16;
    return t * 0.0625f;
}

void i2c_init(void)
{
    MX_I2C1_Init();
}

What an interviewer expects

The bugs, reference fixes and the follow-up question are in the practice app, after you've tried it.

Companies that test this

Matched on the skills each company's embedded roles emphasise.

Related challenges

Flag the lines, say what breaks, then fix it and build it with clang in your browser.

Find the bugs