HeapCrash / Challenges / Tick timeouts and delays

Tick timeouts and delays

Small timing helpers from a thermostat codebase that's meant to run for years without a reboot. The core runs at 160 MHz.

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 uint32_t g_ticks;   /* 1 ms SysTick */

void SysTick_Handler(void)
{
    g_ticks++;
}

bool timeout_expired(uint32_t start, uint32_t timeout_ms)
{
    return g_ticks > start + timeout_ms;
}

/* Busy-wait, roughly 4 cycles per loop */
void delay_us(uint16_t us)
{
    uint16_t loops = us * (SystemCoreClock / 1000000u) / 4u;
    while (loops--) {
        __NOP();
    }
}

/* Called from the main loop AND the button ISR */
void led_toggle(void)
{
    GPIOA->ODR ^= (1u << 5);
}

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