Skip to main content
Associate
May 8, 2024
Question

Writing to flash on power loss using PVD on STM32F103C8T6

  • May 8, 2024
  • 3 replies
  • 3043 views

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.

3 replies

Pavel A.
Super User
May 8, 2024

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.

 

Thesek1Author
Associate
May 10, 2024

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?

AScha.3
Super User
May 8, 2024

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""."
Thesek1Author
Associate
May 10, 2024

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?

Senior
May 9, 2024

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.


 

Tesla DeLorean
Guru
May 9, 2024

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 VenmoUp vote any posts that you find helpful, it shows what's working..
Thesek1Author
Associate
May 10, 2024

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