HeapCrash / Challenges / Touch controller interrupt

Touch controller interrupt

A capacitive touch controller (FT5x06-style) pulls an interrupt line low when it has a new point. This part reports raw 12-bit coordinates (0–4095), so the driver reads them over I2C and scales to a 480×272 panel. Some taps land off-screen, quick swipes freeze the whole UI, and the UI sometimes sees a tap nobody made.

The code

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

static volatile int16_t touch_x, touch_y;

void EXTI9_5_IRQHandler(void)
{
    EXTI->PR1 = EXTI_PR1_PIF7;
    uint8_t buf[4];
    HAL_I2C_Mem_Read(&hi2c1, TP_ADDR << 1, TP_REG_XY,
                     I2C_MEMADD_SIZE_8BIT, buf, 4, 100);
    uint16_t rx = (uint16_t)((buf[0] << 8) | buf[1]);
    uint16_t ry = (uint16_t)((buf[2] << 8) | buf[3]);
    touch_x = (int16_t)(rx * 480 / 4096);
    touch_y = (int16_t)(ry * 272 / 4096);
}

bool touch_get(int16_t *x, int16_t *y)
{
    *x = touch_x;
    *y = touch_y;
    return 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