Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Re: EEPROM write and power failure

2008-04-28 by Graham Davies

--- In AVR-Chat@yahoogroups.com, "Tim Gilbert" <tim@...> wrote:

> ... store a variable in EEPROM ...
> ... power will fail just as the
> software starts to write the data ...

I assume you also want to deal with the situation in which power fails 
after the write has started but before it is complete.  Your full 
requirement, then, is that after a power failure, the variable will 
have a valid value, i.e. one that was deliberately written and not 
something half-written as the power went down.  Therefore, suggestions 
such as recognizing all-ones as invalid will not work.

The first technique to do this is the impending power loss signal.  In 
full, you need to generate a signal that will go active sufficiently 
ahead of power failure that you have time to complete the write before 
power is totally gone.  Before you start the write, you check this 
signal and if it is active you simply don't start, leaving the previous 
valid value in the variable.  This is easy to implement on AC powered 
equipment because you can sense the loss of AC very quickly with an 
optocoupler driven from its own full-wave rectifier feeding a simple 
digital input port.  The reservoir capacitor can be chosen to hold the 
regulated DC power rail at full voltage for quite a while after input 
power loss.  I personally don't like the suggestions that involve 
waiting until the DC line is already dropping.

The second technique is to "journal" the write operation.  This is 
similar to the suggestion of writing it twice, but involves extra data 
so that when power comes back up you can not only tell that a data 
write didn't complete but you can retrieve good data.  You need at 
least two copies of the data.  Then, you need single flag bits to mark 
the start of the write and the end of the write to each copy.  By 
looking at the flags, you can immediately tell if a write was started 
and not completed.  There was a very good article in Circuit Cellar 
about this a while back.

If you need further guidance, perhaps you could indicate which method 
you presently favor.

Graham.

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.