Simvik

Fix: every 256th byte is wrong

Data written to SPI flash reads back wrong at every page start after the first. A medium embedded C problem (SPI flash and SD cards), run and graded in the…

medium · SPI flash and SD cards · find-the-bug · nor-flash · off-by-one

Bug report. Large writes to the SPI flash read back almost perfectly, but every byte at a page start after the first page is wrong. Short writes inside one page are fine.

page_program models the chip and is correct. Find the defect in flash_write and fix it by changing one line.

Starter code

void flash_write(uint32_t addr, const uint8_t *data, uint32_t len) {
  while (len > 0) {
    uint32_t room = PAGE - (addr % PAGE) + 1;
    uint32_t n = len < room ? len : room;
    page_program(addr, data, n);
    addr += n;
    data += n;
    len -= n;
  }
}

Example tests

600 bytes from the middle of a page

test_write(0x80, 600, 1);

Expected output:

ok

Learn it first

More SPI flash and SD cards problems