HeapCrash / Challenges / SPI NOR flash driver

SPI NOR flash driver

A minimal driver for a standard SPI NOR flash (256-byte pages, active-low chip select). Writes sometimes vanish, and the status read returns junk.

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.

uint8_t flash_read_status(void)
{
    gpio_write(FLASH_CS, 1);
    spi_transfer(0x05);
    uint8_t s = spi_transfer(0x00);
    gpio_write(FLASH_CS, 0);
    return s;
}

void flash_write_page(uint32_t addr, const uint8_t *d, size_t n)
{
    gpio_write(FLASH_CS, 0);
    spi_transfer(0x02);
    spi_transfer(addr >> 16);
    spi_transfer(addr >> 8);
    spi_transfer(addr);
    for (size_t i = 0; i < n; i++) {
        spi_transfer(d[i]);
    }
    gpio_write(FLASH_CS, 1);
}

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