I'm working on a project where I'm storing some persistent settings in my microcontroller's built-in EEPROM. Because the settings will be written every time the user changes something, which could be several times a day, I've decided to try and employ some wear levelling techniques so that the EEPROM doesn't wear out too quickly. The EEPROM in my microcontroller is rated for 10k write/erase cycles with 20 year retention, and 100k cycles with 1 year retention.
The primary technique I've decided to use is the common one of treating the entire EEPROM area as a cyclical array of entries, and each time the settings are written, it does so at the next entry to the last used one, wrapping around appropriately. In order to determine which entry is the latest, each entry has a monotonically incrementing sequence number, and the latest is whichever one has the greatest number (determined by scanning the entire EEPROM). I can store 16 entries, so that multiplies the most conservative rated cycle count to 160,000 writes. If the settings are written 20 times a day, that gives a lifespan of 8,000 days, or nearly 22 years, which should be suitable for my needs.
One other thing I am doing is that because the datasheet states that the granularity of the EEPROM is 4 bytes ("cycling is performed on 4 bytes even when a write/erase operation addresses a single byte"), I have made the sequence number a uint32_t, and the settings struct is padded to be a multiple of 4 bytes in size.
My first query is with the sequence number. Should I be concerned with roll-over? Now, a maximum possible value of 4,294,967,296 is capability for a lot of writes, and I'm confident that the EEPROM will have expired long before this sequence value ever gets near rolling over, but maybe there's something I'm not thinking of...
The other question I have is whether it is worth doing something like inverting (i.e. taking one's compliment of) the sequence number when writing/reading so that, for a long while at least, the majority of the bits in the sequence value are 1s rather than 0s. That is, if the sequence number is 2000 it gets written as 0xFFFFF82F rather than 0x000007D0. As I understand things, because the natural default value of an EEPROM is 0xFF (all 1s), when you write a value only the 0s perform a wear-inducing operation, and writing of 1s is essentially a 'no-op'. And so always writing the zero-value MSBs will wear things out quicker. Am I right? Should I do this? Or is it pointless, because the LSBs of the sequence numbers will still wear out first, and if they are corrupted, then it doesn't matter if the MSBs are still fine?
Also, is there any potential in using Gray code for the sequence number? IIRC, counting up with Gray code only ever changes one bit per increment.
Anyone got any other advice or insights?