supercat
Member level 3
I'm trying to rework an application which used a type of Serial NOR flash which is getting difficult to find (it had small erase blocks, buffers to copy from one page to another, etc.) and would like the new design to be as adaptable as possible to different chips. In case of unexpected power loss when writing data, the system must always be able to either complete the operation or recover as though the data was never written.
My present plan is to divide each 4K erase block into various smaller records. Each record will three state control bits: started, valid, and obsolete. Before any data is written to a record, it will be marked "started". Then the data for the record will be written. Once that is done, the record will be marked "valid". If the data becomes obsolete, it will be marked "obsolete" and will remain so until the whole block is erased.
On startup, all the records will be scanned; any record marked "started" but not "valid" will be marked "invalid".
Questions:
-1- Is it necessary to use separate bytes for the three flags, or is there some safe way to combine them into fewer bytes given that certain state transitions will always go through a certain sequence? My concern is that some flash chips might use ECC which can does not allow all 8! sequences of writing the bits in a byte; are ECC algorithms chosen so that zero may be written regardless of what's been written before?
-2- Is it safe to assume that a single-byte write operation will either leave a byte thoroughly programmed or unaffected? I would expect a multi-byte write which takes hundreds of microseconds to complete might leave data partially-written, but if I'm just programming a single byte and the supply isn't low enough to clobber the CPU before the write starts, I would expect the write to run to completion before the supply sags enough to kill the flash.
I'd like to make my code as portable as possible among different flash chips, but also have it be robust and run efficiently. I'm a little paranoid about a partially-written bit sometimes reading one way and sometimes another. Using more bits for each record would allow the system to survive metastable bits in all but the most nasty cases (I think guaranteed survival is mathematically impossible).
What's the best plan?
My present plan is to divide each 4K erase block into various smaller records. Each record will three state control bits: started, valid, and obsolete. Before any data is written to a record, it will be marked "started". Then the data for the record will be written. Once that is done, the record will be marked "valid". If the data becomes obsolete, it will be marked "obsolete" and will remain so until the whole block is erased.
On startup, all the records will be scanned; any record marked "started" but not "valid" will be marked "invalid".
Questions:
-1- Is it necessary to use separate bytes for the three flags, or is there some safe way to combine them into fewer bytes given that certain state transitions will always go through a certain sequence? My concern is that some flash chips might use ECC which can does not allow all 8! sequences of writing the bits in a byte; are ECC algorithms chosen so that zero may be written regardless of what's been written before?
-2- Is it safe to assume that a single-byte write operation will either leave a byte thoroughly programmed or unaffected? I would expect a multi-byte write which takes hundreds of microseconds to complete might leave data partially-written, but if I'm just programming a single byte and the supply isn't low enough to clobber the CPU before the write starts, I would expect the write to run to completion before the supply sags enough to kill the flash.
I'd like to make my code as portable as possible among different flash chips, but also have it be robust and run efficiently. I'm a little paranoid about a partially-written bit sometimes reading one way and sometimes another. Using more bits for each record would allow the system to survive metastable bits in all but the most nasty cases (I think guaranteed survival is mathematically impossible).
What's the best plan?