Persistent Counter

10 Dec 2009

Is there any simple way to keep track of an integer between resets?

I'm trying to keep track of the current angle of a stepper motor so that I can reset the mbed and it'll still know where it is at. I could write the step count to disk every time I move the stepper motor but this seems horribly inefficient!

I remember seeing *somewhere* that the mbed has inbuilt counters somewhere? Could I use those for persistent storage of an integer or do i have the wrong end of the proverbial stick?

10 Dec 2009

Why not reset the position of the stepper when you reset the mbed, that way you don't need to keep track.

11 Dec 2009 . Edited: 11 Dec 2009

The RTC block has 20 bytes which you can use and which are not changed during reset. If you want to keep them during power-offs, you'll need to connect a battery (3V) to VB.

LPC_RTC->GPREG0 = cur_angle;

(you can use GPREG0 to  GPREG4, 32 bits each).

Another option is writing to the chip's internal flash, same that's used by the firmware you upload (IAP, in-application programming), but that's not very reliable as it will likely get overwritten every time you upload a new compile.

11 Dec 2009

 

Vlad Cazan wrote:

Why not reset the position of the stepper when you reset the mbed, that way you don't need to keep track.

The stepper motors control a mechanism that doesn't repeat so if (during the course of one program run) the stepper motor turns 100 times I would need to reverse-turn 100 times *before* the mbed reset (I dont think the mbed can do things before its reset - if it could I would probably just write the stepper motor position to disk and read it into memory when the program began again instead)

 

Igor Skochinsky wrote:

The RTC block has 20 bytes which you can use and which are not changed during reset. If you want to keep them during power-offs, you'll need to connect a battery (3V) to VB.

LPC_RTC->GPREG0 = cur_angle;

(you can use GPREG0 to  GPREG4, 32 bits each).

Another option is writing to the chip's internal flash, same that's used by the firmware you upload (IAP, in-application programming), but that's not very reliable as it will likely get overwritten every time you upload a new compile.

Thanks Igor, that's perfect!

11 Dec 2009

Hi JP,

There is an abstraction for an AT45 SPI flash part that allows you to treat it as RAM. So if you need to store more than just a single counter, and you want to do it without a battery, that might be useful.


Cheers,

Chris