I’ve written a library to give organized user access to the serial flash. Basic wear leveling and CRC checking of restored data is supported. There is also a stream mode which I have planned but not implemented so far.
From the header file:
NVM is a non-volatile storage class to handle user interaction with storing
blocks or streams in the external sFLASH, of which 1.5MB is available for
the user.
There are two ways to interact with NVM: block writes and stream writes.
Blocks are written in their entirety all at once. A block is CRC checked
and this check is performed when restoring the block. When a block is
written, any previously stored content of the block is replaced.
Streams are written at up to one byte at a time. Streams have a fixed
maximum number of bytes that can be written to the stream record based
on the NVM map definition. Streams can only be appended or reset (to empty).
Once all available bytes of a stream have been written, the stream record
must be flushed to return it to empty. Streams are not checked with a CRC.
The NVM map defines how flash is laid out in terms of blocks and streams.
If the NVM map changes from one version of software to another, all data
stored in the NVM is reset.
Wear leveling is performed by writing copies of the block data following
the already written block until the page is full, then the page is erased.
Every block or stream write causes a new info block to be written, which is
64 bytes long. Given 4K pages, this block can be written 64 times before the
page is erased. Pages can be erased upto 100k times, allowing at most
6.4 million writes before wearing out the info page.
I would like to get some feedback on this software:
- Interest in merging this library?
- Interest level in supporting a stream mode (I don’t need this for my own projects but if there is strong interest I would be willing to implement it)
Total .text size is about ~800 bytes and uses 260 bytes of RAM with support for up to 15 NVM records. If you want to check it out, merge mattande/core-firmware@nvm and mattande/core-common-lib@nvm_lib. There is a sample application.cpp that exercises the NVM API’s.
I’ve also issued a pull request with more general cleanups to the sFLASH driver that fixes bugs in the existing driver if starting a write at an odd flash address and/or writing an odd number of bytes. There is also a modest improvement in code space utilization and slight speedup when writing > 4K bytes in a single write.