cancel
Showing results for 
Search instead for 
Did you mean: 

Writing to flash on power loss using PVD on STM32F103C8T6

Thesek1
Associate II

Hello.

I am trying to save variables to flash when there is power loss that i detect using PVD. I got PVD to work correctly and trigger interrupt at 2.9V, but now i cannot find the right examples to see how  to save the variables quickly to flash. I have about  5ms from 2.9V-2V to  to save them, how many and how big(uint32_t?) do you think i can save. Those variables are my timer counters and some user defined settings. When the power is back, can the program start to run forward where it left off?

Thank you.

9 REPLIES 9
Pavel A.
Evangelist III

The right question is how much time the MCU needs to save the data, and how much time the power supply will last until the voltage drops below the minimum. Let's start from the former.

If there are no ready examples, improvise, based on the common sense. For one, you know that erasing takes long time, so it is excluded. The flash area for the emergency save must be already erased and ready. Etc,etc.

Then write a test and measure the required time. Then check that the power supply indeed provides this time. If not - add a super-cap or something else.

 When the power is back, can the program start to run forward where it left off?

If power is totally off for prolonged time, all SRAM will be lost. If the voltage only drops below normal, for short time, you can try to enter a low power state and hope that RAM data survives when the normal voltage is back. In any case the MCU will reset. Can you consider a backup battery for this project?

Good luck.

 

AScha.3
Chief III

Hi,

just from ds :

AScha3_0-1715199209480.png

So in 5ms can write 71 x 16b .

Including some "safety margin" i guess : 30 x int16 should be possible without problems. 

+

>When the power is back, can the program start to run forward where it left off?

No. You would have to store the cpu registers + the stack + your "to save variables" -> too much.

If you feel a post has answered your question, please click "Accept as Solution".
eduardo_reis
Senior

I am trying to do the same thing and am glad I found your fresh post.

How did you get to this information? Did you get it from the dataset, reference manual, etc?


@Thesek1 wrote:

I have about  5ms from 2.9V-2V to  to save them.


 

Write flash into a collapsing supply is probably the quickest ways to brick a device.

The charge pump is trying get 7.0 - 12.0 VPP for programming.

If at all possible use the NVRAM / BKP memory to stash things you need to retain.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hello. I calculated that. Between my power source and 3.3Vin, i have a diode, so that no current can flow back into the circuit when power off. At 3.3V i have my known capacitance available, i also know my current draw of the board that i am powering. After that, you can use simply equation for capacitor discharge voltage curve, there are many calculators on the internet for that. 

Hello. Than you for your answer. So the battery backup will be a better solution, because i need my program to start back where it left of when the power is back on. Using battery backup, i can do that more correctly right?

Hello, thank you for the answer. So using the battery backup on VBAT pin, i will be able to restore the program where it left of more easily?

I also want to restore the program where it left off. Is the battery backup on VBAT better option?

Sure - but only 20 bytes:

AScha3_0-1715321250578.png

Why you choose the almost oldest and cheapest cpu for this ??

If you feel a post has answered your question, please click "Accept as Solution".