EmbeddedRelated.com
Forums

Non-volatile counter implementation

Started by andrew queisser August 17, 2006
On Thu, 17 Aug 2006 20:20:42 GMT, "andrew queisser"
<andrewdotqueisser@hp.com> wrote:

>Hi all, > >I'm thinking of ways to implement a nearly non-volatile counter using flash >and RAM. Idea is to count events in a RAM counter and write the count to >flash every n times. At first I thought I'd just use successive locations in >flash until I need to reformat the entire sector but I'm thinking it may be >better to flip single bits from 1 to 0 with each bit indicating n events. >That way I don't waste an entire DWORD for each update.
If count only [m/n] , can you live with the fault between successive runs ? E.g. a zero-bit means 8 runs, so 0xfe => 1..8 runs 0xfc => 8..16 etc. But the main problem stays, you have to erase the flash at a time in the future. But why not realy storing the 32bit value (btw. DWORD on an ARM CPU would mean 64bit :-) Assume an (flash) array of 128 elements, all initialized to 0xff, if an element is 0 => take next if an element is > 0 && < 0xffffffff => last count stored if an element is 0xffffffff => free (Note: count of 0xffffffff cannot be stored, special handling is needed.) If you no free element, erase all and start with first. After storing a new count, clear all bits of the last. Sound fairly easy to implement. -- 42Bastian Do not email to bastian42@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !
On 17-Aug-2006, "andrew queisser" <andrewdotqueisser@hp.com> wrote:

> I'm thinking of ways to implement a nearly non-volatile counter using > flash > and RAM. Idea is to count events in a RAM counter and write the count to > flash every n times. At first I thought I'd just use successive locations > in > flash until I need to reformat the entire sector but I'm thinking it may > be > better to flip single bits from 1 to 0 with each bit indicating n events. > That way I don't waste an entire DWORD for each update. > > Does anybody know of a ready-made solution for this kind of thing? > > Thanks, > Andrew
I use a "power going down" interrupt, and save off counters (and other things) as the power is going down. -Hershel
"42Bastian Schick" <bastian42@yahoo.com> wrote in message 
news:44e5aaf9.367450565@news.individual.de...
> > If count only [m/n] , can you live with the fault between successive > runs ? > > E.g. a zero-bit means 8 runs, so > 0xfe => 1..8 runs > 0xfc => 8..16 etc. > > But the main problem stays, you have to erase the flash at a time in > the future. > > But why not realy storing the 32bit value (btw. DWORD on an ARM CPU > would mean 64bit :-) > > Assume an (flash) array of 128 elements, all initialized to 0xff, > if an element is 0 => take next > if an element is > 0 && < 0xffffffff => last count stored > if an element is 0xffffffff => free > (Note: count of 0xffffffff cannot be stored, special handling > is needed.) > > If you no free element, erase all and start with first. > > After storing a new count, clear all bits of the last. > > Sound fairly easy to implement.
That's exactly what I had in mind. I would flip a bit every 10 cycles. If the power goes down between cycles I lose 10 events. My flash is good for about 1 million erase cycles so I really don't need to worry about erase cycles as much as the time hit to erase a sector. The events happen about every second so I could really afford to flip a bit after every single cycle but then I might have to worry about having to erase the sector while I'm doing a time-critical operation instead of waiting for the next power cycle which would happen when the battery gets disconnected for recharge. Andrew
"Hershel Roberson" <hrjunk01@moment.net> wrote in message 
news:ec4fbt01hk9@enews1.newsguy.com...
> > On 17-Aug-2006, "andrew queisser" <andrewdotqueisser@hp.com> wrote: > >> I'm thinking of ways to implement a nearly non-volatile counter using >> flash >> and RAM. Idea is to count events in a RAM counter and write the count to >> flash every n times. At first I thought I'd just use successive locations >> in >> flash until I need to reformat the entire sector but I'm thinking it may >> be >> better to flip single bits from 1 to 0 with each bit indicating n events. >> That way I don't waste an entire DWORD for each update. >> >> Does anybody know of a ready-made solution for this kind of thing? >> >> Thanks, >> Andrew > > I use a "power going down" interrupt, and save off counters (and other > things) > as the power is going down. > > -Hershel
Hmm, I don't have one of those but maybe we should add something like that. Thanks, Andrew
On Fri, 18 Aug 2006 14:20:00 GMT, "andrew queisser"
<andrewdotqueisser@hp.com> wrote:

>but then I might have to worry about having to erase the sector while I'm >doing a time-critical operation instead of waiting for the next power cycle >which would happen when the battery gets disconnected for recharge.
I think to be (power)fail-safe you'll need 3 sectors, two management sectors and one data sector. Only one data sector, as you are only flipping bits, you might store the actual count in a management sector while erasing. Two management sectors, since you sometimes have to erase one. -- 42Bastian Do not email to bastian42@yahoo.com, it's a spam-only account :-) Use <same-name>@monlynx.de instead !