HeapCrash / Challenges / RTC wake-up from STOP 2

RTC wake-up from STOP 2

An STM32L4 node sleeps in STOP 2 and should wake every 60 s from the RTC wake-up timer. It never wakes by itself. A bench test that polled WUTF with the CPU running showed the period stuck at an old value whatever was requested. A colleague's patch got it waking, but then the CPU never went back to sleep: the wake-up interrupt kept firing.

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.

void rtc_wakeup_every(uint16_t seconds)
{
    RTC->CR &= ~RTC_CR_WUTE;
    RTC->WUTR = seconds - 1u;
    RTC->CR |= RTC_CR_WUCKSEL_2 | RTC_CR_WUTIE | RTC_CR_WUTE;   /* ck_spre, 1 Hz */
    NVIC_EnableIRQ(RTC_WKUP_IRQn);
}

void RTC_WKUP_IRQHandler(void)
{
    RTC->ISR &= ~RTC_ISR_WUTF;
    sample_flag = true;
}

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