Content originally posted in LPCWare by kevink on Tue Aug 17 12:12:09 MST 2010
I had the same requirements (except I was writing 4 bytes, but this is no issue).
Here are my definitions:
[LIST]
[*]Word - 32bits / 4 Bytes of memory
[*]Sector - 4096 Bytes of memory (1024 words)
[*]Block - 256 Bytes of memory (64 words)
[/LIST]
Here are my algorithms for reading/writing. The writing algorithm ensures you only need to erase once every 16 writes. I have done timing on the writing/reading of a sector of flash on the LPC1114, at 40MHz, and the times are around 100ms for erasing, and 1 ms for reading (it increases linearly depending upon how far into the sector your data is).
Writing new Data:
[LIST=1]
[*]Read the flash sector using the blank check function of IAP functions. This will return to you the first non blank word (I think)
[*]Using this returned value, you know in which block (there are 16 blocks in a sector) your data is in.
[*]If the block is the first block, then erase entire sector because you know that all blocks after the block with data in it should have old data in them. write your new data to the last block.
[*]Else, write your new data to the block previous to the one with old data in it.
[/LIST]
Reading Data:
[LIST=1]
[*]Do a blank check of the sector, this will return to you the first word that is not blank.
[*]Read the first 10 bytes (or however long your data is) to get your newest data.
[*]If the sector is all blank, you haven't written any data to it yet.
[/LIST]
The minimum write size is a block of 64 words (as specified in the datasheets of LPC11xxs) and I have read in multiple places that even if you write FF's to the rest of the block, the memory controller will still apply voltage to the memory, so there is no point in trying to write a single block more than once before erasing it.