Search code examples
cembeddedflash-memory

Best logic to erase fewer bytes than sector size(minimum erasable size) in flash


I am using Spansion's flash memory of 16MB. The sector size is 256KB. I am using the flash to read/write/delete 30 byte blocks(structures). I have found in the data sheet of the IC that minimum erasable size is 256KB. One way of deleting a particular block is to

  1. Read the sector containing the block to delete into a temporary array.
  2. Erase that sector.
  3. Delete the required block in temporary array
  4. Write back the temporary array into Flash.

I want to ask is there any better alternative logic to this.


Solution

  • There is no way to erase less than the minimum erasable sector size in flash.

    However, there is a typical way to handle invalidating small structures on a large flash sector. Simply add a header to indicate the state of the data in that structure location.

    Simple example:

    • 0xffff Structure is erased and available for use.
    • 0xa5a5 Structure contains data that is valid.
    • 0x0000 Structure contains data that is not valid.

    The header will be 0xffff after erasing. When writing new data to a structure, set the header to 0xa5a5. When that data is no longer needed, set the header to 0x0000.

    The data won't actually be erased, but it can be detected as invalid. This allows you to wait until the sector is full and then clean up the invalid records and perhaps compact the valid ones.