External flash in the S25FL512SAGMFI011 device can be read or written through the PSoC® 6 SMIF interface. How can I correctly erase and write to make full use of its storage capacity?
Each flash sector of S25FL512SAGMFI011 must be erased before writing to it. However, because the range size of the sector erased and written each time are different, the previously written data may be erased unexpectedly. The user needs be aware of the following erase/write mechanism for S25FL512SAGMFI011:
This data array contains address bytes for external flash. For S25FL512SAGMFI011, it contains 3 bytes. extMemAddress is the A23:A16 bits of the address.
- sector erase
The sector erase command erases 256 KB of sector every time it executes, which is defined in the S25FL512SAGMFI011 spec. As each sector is 256 (2*16KB aligned), the address 0xFC0000 is masked for a 24-bit address. This means it ignores bits A17:A0 of the sector address when a sector erase command is asserted.
Since the current sector is erased before each ‘write’ time, A17:A16 of extMemAddress is ignored for each sector erase command. Each time the extMemAddress++ 64-kB data is skipped, the current sector does not change within four successive increments. It moves to the next sector only when the fourth increment completes; that is, it keeps erasing data written in the previous 3 writes. Only the fourth write is retained as the next erase moves to the next sector.
The user needs to perform the sector erase accordingly. In the code, you just need to erase the sector only before the first time out of four successive fourth writes.