EEPROM for ESP8266

by kacang bawang

This article describes a small EEPROM emulation library that I wrote for ESP8266. It implements a very simple EEPROM emulation scheme that allows storing 16bit values. Each such 16bit value is found by its 16bit index.

For those no familiar with what EEPROM emulation is all about:

Flash can be written to 4 bytes at a time. But before doing so, it must be erased. Alas, erasing can only be done 4096 bytes at a time. You could erase all the 4096 bytes, then write whatever you need, and repeat this later, as needed. But there are two problems with that. One – erasing is slow. We want to finish a write as fast as we can, so that there’s less chance of being interrupted by something like a power outage. Two – you can only erase a given sector of flash about 10,000 times before it wears out. For something like a logging system, that is not a very large number.

To fix these two problems we devise the following scheme. We will write index/value pairs (4 bytes) into the larger flash page (4096 bytes) one after another. So, index=1 would not be an offset but rather a mark, that could appear more than once. The value corresponding to the last such entry would be the valid one. This way, we can write to index 1 for 4096 times before having to erase. Once the page is full, we find the latest values for every index, transfer them to the next page, and erase the old one. Now we are free to continue writing until the new page is also full. Needless to say, the minimum number of flash pages required is 2, but can be more. The more pages, the longer the flash will last. We trade space for longevity.

Let’s take a look at an example.

What will the contents of the current eeprom flash page look like after the cycle completes? That's right, like this:

The first 4 bytes are reserved for status bytes, but otherwise we have written to the same 4 locations a bunch of times. Now the page is full. If there is another write, we will migrate to the second/next page first, to make room. So, let's continue the example:

What will the new flash page look like?

As you can see the most recent values have been moved over, and index 3 has been updated further. The process thus continues.

If we always write to the same location, and do so 5 times a minute, we have ~12 years before our 2 flash pages wear out (at 10000 writes each). If that is not enough for you, increase the number of pages dedicated to EEPROM and increase the endurance further.

The library can be found here:
EEPROM for ESP8266