I am developing a high level library to read and write for winbond W25Q512JV and ATmega32U4 mcu.

From the flash datasheet:

The Page Program instruction allows from one byte to 256 bytes (a page) of data to be programmed at previously erased (FFh) memory locations.

Additionally the smaller amount you can erase are 4kB (a sector).


To overwrite data on the flash device the process it seems like I must:

  • Read sector involved
  • Erase sector
  • Write whole sector

However when reading the sector that can be up to 4k length which is quite huge to keep on the memory stack. What is the best approach for this cases?


1 Answer 1


It is certainly possible if that is your question, how to do it is more complex.

There is no single answer, and there can be no best case as you don't say how you will use the memory, as what is best case depends on how the memory will be used.

But you need a wear leveling algorithm of some sorts, or a file system of some sorts that can handle it, and can handle the physical storage with logical level.

A very crude approach is to just copy data temporarily to another sector, erase the original sector, and write back the data with the modified parts. You can use as small blocks to copy the data as necessary.

A slightly better way is to copy the old data sector to new data sector with the required modifications and have some kind of list to map logical sectors to physical sectors and update the list accordingly.

One approach might be to keep track of which pages are marked for erasing and erase a full sector when all or enough pages of a sector need erasing.

So it all depends how you need to read, modify and write data that is in the memory.

Many MCU or compiler manufacturers might already provide a flash file system for you to use so you don't have to re-invent the wheel.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.