HeapCrash / Challenges / FreeRTOS sensor pipeline

FreeRTOS sensor pipeline

A temperature sensor pulls a data-ready line on PB3. This code reads it over I2C and logs readings from a FreeRTOS task on an STM32U5. The board locks up a few minutes after boot.

The code

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

QueueHandle_t     sensor_q;
SemaphoreHandle_t i2c_mutex;

/* Sensor data-ready line on PB3 */
void EXTI3_IRQHandler(void)
{
    EXTI->RPR1 = EXTI_RPR1_RPIF3;
    xSemaphoreTake(i2c_mutex, portMAX_DELAY);
    uint16_t raw = i2c_read_reg(SENSOR_ADDR, REG_TEMP);
    xSemaphoreGive(i2c_mutex);
    xQueueSend(sensor_q, &raw, 0);
}

void sensor_task(void *arg)
{
    char log_line[1024];
    uint16_t raw;

    for (;;) {
        if (xQueueReceive(sensor_q, &raw, portMAX_DELAY) == pdTRUE) {
            float temp_c = raw * 0.0625f;
            snprintf(log_line, sizeof(log_line), "T=%.2f C", temp_c);
            log_write(log_line);
        }
    }
}

void app_init(void)
{
    sensor_q  = xQueueCreate(8, sizeof(uint16_t));
    i2c_mutex = xSemaphoreCreateMutex();
    xTaskCreate(sensor_task, "sensor", 256, NULL, 2, NULL);
    NVIC_SetPriority(EXTI3_IRQn, 2);
    NVIC_EnableIRQ(EXTI3_IRQn);
}

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