Save data on power down

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Philosophix
Posts: 24
Joined: Wed Jan 02, 2019 11:45 am

Save data on power down

Post by Philosophix » Wed Jan 02, 2019 11:52 am

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.

nekomatic
Posts: 10
Joined: Thu May 08, 2014 9:31 pm

Re: Save data on power down

Post by nekomatic » Wed Jan 02, 2019 2:18 pm

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.

Philosophix
Posts: 24
Joined: Wed Jan 02, 2019 11:45 am

Re: Save data on power down

Post by Philosophix » Wed Jan 02, 2019 2:47 pm

For various reasons I'd rather save the data only on power down. Can VBAT somehow be used for this?

chuckbook
Posts: 85
Joined: Fri Oct 30, 2015 11:55 pm

Re: Save data on power down

Post by chuckbook » Wed Jan 02, 2019 4:01 pm

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.

Philosophix
Posts: 24
Joined: Wed Jan 02, 2019 11:45 am

Re: Save data on power down

Post by Philosophix » Wed Jan 02, 2019 4:36 pm

That sounds good! Would a CR-2032 battery suffice? How long would that last?

chuckbook
Posts: 85
Joined: Fri Oct 30, 2015 11:55 pm

Re: Save data on power down

Post by chuckbook » Thu Jan 03, 2019 9:16 am

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.

Philosophix
Posts: 24
Joined: Wed Jan 02, 2019 11:45 am

Re: Save data on power down

Post by Philosophix » Thu Jan 03, 2019 12:27 pm

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.

chuckbook
Posts: 85
Joined: Fri Oct 30, 2015 11:55 pm

Re: Save data on power down

Post by chuckbook » Thu Jan 03, 2019 10:06 pm

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.

Philosophix
Posts: 24
Joined: Wed Jan 02, 2019 11:45 am

Re: Save data on power down

Post by Philosophix » Fri Jan 04, 2019 5:24 am

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?

chuckbook
Posts: 85
Joined: Fri Oct 30, 2015 11:55 pm

Re: Save data on power down

Post by chuckbook » Fri Jan 04, 2019 9:41 am

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.

Post Reply