2014-05-30 08:33 AM
I am working on STM32F103 with an external flash memory chip via SPI. I use a very simple data format for storing data onto the flash memory, each record is 16 bytes, all records are saved sequentially. The problem is how to keep track of the current record location. I need to keep the data as long as possible, regardless resets and power cycling. When it reaches the full capacity it can overwrite the oldest records.
I can't use backup registers because no backup battery available in my case. I can try to implement a file system but my data structure is very simple, so a file system will be my last resort. I can keep the current record address at a fixed location on the flash memory, and update it every time I write a new record. But the flash memory has a limit of 100,000 program/erase cycles. This seems to be a classical problem with flash memory, I wonder if there is a simple solution.2014-05-30 11:39 AM
One of the simpler solutions is to write through the whole memory in a loop, with sequence numbers in your records, when you wrap you erase the oldest data, and when you start you implement a bisection algorithm on the erase block size to find the block with the newest data in it, and start from there with your next sequence number write.
Unless you have a file system that performs some wear leveling, or is flash aware, you are likely to destroy singular blocks2014-05-30 02:48 PM
2014-05-30 03:33 PM
You should look closely at the pseudo-EEPROM implementation, and the 10K (min) cycle life. It might not do what you want/expect.
How large is your proposed SPI Flash device? If you use a 32-bit sequence number, what are the chances your flash can hold 4 billion records? You should be able to determine if the records you have wrapped a some point. ie if you find a 0xFFFFxxxx record any subsequent record less than 0x80000000 is clearly newer rather than older. Find the highest and lowest record in the flash, if the highest is 0xFFFFFFFF then find the highest below 0x80000000. All the records will be written in a sequential order, and you'd probably want to have at least one erased block. When you start writing a new block your erase the next block ahead discarding the oldest data stored there. You can also create a structure in the block indicating the cycle count for the block, this combined with a 32-bit sequence number in the record, are not going to wrap within the lifetime of the device.2014-05-30 03:36 PM
Also be aware that writing and erasing the flash using the pseudo-EEPROM can stall the processor in an undesirable manner.
2014-05-30 04:04 PM
The trick is recovering the next record pointer at reset. The sequence numbers help to detect the break between head and tail, since they won't be sequential. You might look at a binary search over the whole memory to locate the break. It's very fast (Log2 * N) compared to a sequential search. Or just look at the first record on every erase block until you see sequence number out of sequence or an erased block, then you located the block where the head/tail are.
Old timers might remember the DEC VMS Spiralog file system, worked on a similar principle where data blocks were written sequentially in a circular ''spiral''. Extremely fast write, so-so read from disk since it had to scatter-gather the blocks in the spiral. Jack Peacock