Page 1 of 2

Save data on power down

Posted: Wed Jan 02, 2019 11:52 am
by Philosophix
I'm making a odometer for a tractor which reads pulses from the axle. When the tractor is shutdown the pulsecount would have to be stored so the total distance reading is not lost. I don't want to write the pulsecount everytime it changes as the pulse rate is high. How could I store the pulsecount automatically when pyboard loses power.

Re: Save data on power down

Posted: Wed Jan 02, 2019 2:18 pm
by nekomatic
In order to save something when the power goes down, you need to know that the power is about to go down so that you can take the action you need to save it. How can you tell that the power is about to go down? Well, you could run your pyboard from a battery- or capacitor-backed supply so that it still has power for a period of time after the main power has been lost. You would use an analogue or digital input on the pyboard to detect when main power was no longer present and trigger the shutdown routine.

Alternatively, for an odometer application is it really necessary to save the state you had at the exact moment that power was lost? Or could you get away with just saving the current value every few seconds and/or x metres travelled? You would lose any changes between last write and the moment of power loss but you might be able to say those were insignificant within the expected accuracy of your measurement, for example.

Re: Save data on power down

Posted: Wed Jan 02, 2019 2:47 pm
by Philosophix
For various reasons I'd rather save the data only on power down. Can VBAT somehow be used for this?

Re: Save data on power down

Posted: Wed Jan 02, 2019 4:01 pm
by chuckbook
With a Pyboard either RTC backup registers or static backup RAM (4kByte) can be used for such tasks.
For a simple ODOmeter something like:

Code: Select all

import stm
stm.mem32[stm.RTC+stm.RTC_BKP9R] += 1
would do the job.
As long as VBAT is powered the content of RTC_BKP9R will be valid.

Re: Save data on power down

Posted: Wed Jan 02, 2019 4:36 pm
by Philosophix
That sounds good! Would a CR-2032 battery suffice? How long would that last?

Re: Save data on power down

Posted: Thu Jan 03, 2019 9:16 am
by chuckbook
A CR2032 provides ~220mAh capacity. Assuming VBAT consumption is ~10 µA this would give ~3 years of operation.
At 1/256 s RTC tick frequency typical Pyboard VBAT consumption is more like 1.5 µA. This will result in a shelf life of >10 years.
With RTC disabled VBAT is << 1 µA.

Re: Save data on power down

Posted: Thu Jan 03, 2019 12:27 pm
by Philosophix
Thanx chuckbook for your trouble! So the 3 V provided by CR2032 is enough (could not find info on VBAT acceptable voltage range)? I don't need RTC, so how do I turn it off as this prolongs battery life? I only need VBAT for saving counter value when power is down.

Re: Save data on power down

Posted: Thu Jan 03, 2019 10:06 pm
by chuckbook
VBAT should be between 1.7V and 3.3V. Disabling RTC tick needs special handling as it is pretty hard to disable RTC once it is running :-). However, using a Pyboard Lite would be perfect as it doesn't have a LSE crystal, so RTC tick always comes from LSI, which runs from Vdd.

Re: Save data on power down

Posted: Fri Jan 04, 2019 5:24 am
by Philosophix
Ok. RTC running is not such a big deal.

About VBAT: i found this https://store.micropython.org/pyb-features where it says it should be 3.6-16 V?? I was already planning on using a 9 V battery as I happened to have a housing for that at home.

I can't find any documentation on the stm-module you mentioned. How big a number can stm.mem32[stm.RTC+stm.RTC_BKP9R] hold?

Re: Save data on power down

Posted: Fri Jan 04, 2019 9:41 am
by chuckbook
A 9V battery on MCU VBAT will destroy the MCU. Don't do this!
On a Pyboard VBAT is not MCU VBAT, I guess MCU VBAT is named VBACK on the board.
A CR2032 on VBACK is sufficient.