HeapCrash / Challenges / Sleep until there's work

Sleep until there's work

A battery sensor node sleeps in the main loop until a UART command arrives. Current draw is higher than the datasheet sum, and in the field a command sometimes waits a full 60 s (the RTC wake-up period) before it's handled.

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.

static volatile bool work_pending;

void USART1_IRQHandler(void)
{
    rx_push(USART1->RDR);
    work_pending = true;
}

void main_loop(void)
{
    for (;;) {
        if (!work_pending) {
            __WFI();
        }
        work_pending = false;
        process_rx();
    }
}

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