Electronic – Frequent Write to Non-volatile Memory

eeprommicrocontrollernon-volatile-memory

I am designing a device that automatically adjusts it's physical position as temperature changes. If the device it shut off or power is disconnected the device needs to remember it's last temperature and position. I have the ability to store these values in EEPROM but the problem is the position and temperature could be changing very rapidly. If I were to write the temp and pos to EEPROM after every time they changed that would (1) slow down the firmware a bit, and (2) likely kill the EEPROM after a year or two. So as I see it my options are as follows…

1) use a capacitor/battery to keep the device powered for a short time after power is lost so that I can write the values to EEPROM at that time only. I don't like this because the board is kinda power hungry and this would require a big cap. And I don't have a ton of free space. And I don't want the added cost of a battery and battery holder / or a big cap.

2) use F-RAM instead of EEPROM so that I can write to it trillions of times without wearing it out. I don't like this option because FRAM is quite a bit more expensive than EEPROM and this is for a production product (not just one).

3) Only write the position and temperature every 5 minutes or so. That way I always have a fairly recent position/temp recorded but I am not writing every second so my program is not slowed down and the EEPROM won't die as fast. This seems like my best option.

Does anyone else have any suggestions that I'm not thinking of?

Best Answer

What you need is a technique called wear leveling. It doesn't write your data every time at the same location in the EEPROM, but uses some algorithm to use different locations. I've read about complex wear leveling algorithms, but I wouldn't know why the following simple method wouldn't work.

Add to your data a 24-bit counter, so that your data block is for instance 8 bytes long. Pages on a 24AA64 are 32 bytes long, so a 64kb EEPROM holds 256 pages. From the datasheet:

"When doing a write of less than 32 bytes the data in the rest of the page is refreshed along with the data bytes being written. This will force the entire page to endure a write cycle, for this reason endurance is specified per page."

so it doesn't make sense to use data blocks smaller than a 32 bytes page.

Look at the counter of the first page. If it's zero you used the maximum number of write cycles for that page, so you move on to the next page and check that counter. Repeat until you find a counter > zero. That's the page you're currently using. Microchip's EEPROMs have a 1 million cycles endurance, which you can increase to 256 million with the given example of maximum 32 bytes per block in a 64kb EEPROM. That should be enough to outlast your product: 40 years if you write once every 5 seconds(!).

You'll want to initialize your EEPROM on first use. How do you know when that is. Use the last page to write a unique signature upon initialization. Check at each power-up if the signature is there. If it isn't the device has to be initialized. You can preset the counter in each page with 0xF4240 (for 1 million) or clear everything to 0xFF and write the 0xF4240 when you first use the page.
Initializing an EEPROM is needed because sometimes a certain pattern is written to it in the production/test process.

edit
The wear leveling should solve your problems, but I still want to comment on the capacitor solution. You say the board is rather power-hungry, but maybe you can isolate the microcontroller/EEPROM's power from the rest of the board with a diode. So you'll probably need only a few mA when main power is gone. The 24AA64 writes a page in less than 5ms, then at 10mA and an allowable voltage drop of 100mV you'll need

\$ C = \dfrac{I \cdot t}{\Delta V} = \dfrac{10mA \cdot 5ms}{100mV} = 500\mu F \$

Easy with a small supercap.

further reading
datasheet 24AA64
EEPROM Endurance Tutorial