cancel
Showing results for 
Search instead for 
Did you mean: 

How to save a variable in non-volatile memory?

Marco S
Associate II
Posted on September 04, 2017 at 09:49

Hello,

I'm using a STM32F091VBTx microcontroller and for the application I'm implementing I need a variable to be constantly updated and it must be stored in a non-volatile memory in order to keep its value even if the microcontroller resets. I've found AN2594 about EEPROM emulation, but the functions described there are not available for the microcontroller I'm using. Should I use the functions provided in the stm32f0xx_hal_flash.c file? If yes, I would like to know if there is a demo software for this or if there is any other method to update values in a non-volatile memory.

Thanks.

#stm32f091vb #variable #non-volatile #memory #eeprom-emulation

Note: this post was migrated and contained many threaded conversations, some content may be missing.
45 REPLIES 45
Jeroen3
Senior
Posted on September 04, 2017 at 10:25

Often changing variables are not recommended to be stored in FLASH.

Writing on-chip FLASH has a significant performance penalty*, and it will wear out relatively quickly.

I limit my use cases for using on-chip flash to configurations and bootloaders only. Both are use cases when the chip is not performing it's critical task.

If you have a variable that you need to keep, I recommend using the backup registers or external EEPROM or FRAM.

Depending on the frequency you want to update the variable with you can decide if the durability of EEPROM is sufficient, else you can use FRAM which will last forever. (10 trillion writes)

*up to 40 millisecond halt for erase, endurance of 'only' 10 kcycles.

Posted on September 04, 2017 at 11:26

Thanks for your answer Jeroen3, I will keep your recommendation in mind for future developments of the project I'm working on. Unfortunately, by the moment I'm limited to use the on-chip Flash, so I'm trying to find a way to save there the variable which value I need to keep even if the MCU is powered off.

Danish1
Lead II
Posted on September 04, 2017 at 14:22

You can have a variable keep its value if the microcontroller resets. All you need do is convince the start-up code put in by the compiler not to put a default value in there.

With Rowley Crossworks I put load = 'No' into the section placement file. So I'd declare my variable e.g.

int preservedVariable __attribute__((section('.persistent')));

and have this line in the section-placement file:

<ProgramSection alignment='4' load='No' name='.persistent'/>

I'm not sufficiently-familiar with other development environments to say how you'd do it with them.

Of course the variable is volatile and at power-on-reset or other severe crash the value is junk. You can tell if you're at a power-on-reset by looking at RCC->CSR

Hope this helps,

Danish

Posted on September 04, 2017 at 12:38

Agreeing with Jeroen3, I see three options.

  •  backup registers, + a battery
  •  proper external non-volatile memory (FRAM, EEPROM)
  •  magic

Albeit bean-counting project managers and sales people love option 3, it never worked up to now ...

Posted on September 04, 2017 at 12:56

Okay, you will want to use some kind of wear leveling if you intend to write often.

You've read about this in AN2594. If AN4061 is also incompatible, you'll have to code it yourself.

Posted on September 04, 2017 at 13:59

Having been involved in similar project, I'd like to dwell a bit longer on it ...

First, collect all your requirements.

How often do you need to write (worst case scenario), with which estimated lifetime of the device ?

Since flashing blocks code execution, what are the timing requirements ?

Other constraints, like limited power reserves (save after mains-off, power supplied from a cap charge) ?

What maximal device/chip temperature you allow ?

The guaranteed number of erase/write cycles (e.g. 10k) is specified for the worst case, i.e. maximal temperature.

At room temperature, you could bravely expect upt to 10^7 cycles.

I had been in a company that adopted a more conservative approach after learning the hard way ...

Posted on September 04, 2017 at 16:35

Hello AvaTar, thanks for your reply. I will need to update the value of a variable every five days (in the worst case scenario) in a lifetime of five years. What do you mean with timing requirements? I'm using a 8 MHz clock if that's what you mean. The device will be connected to a battery, the objective is not add an external memory. Nevertheless, there exists the risk that due to variations in the power supply the MCU resets. In that case I need the value which is updated every five days to perform an internal calculation of the device. Normally the device should work at 35°C at most. What I would like to know is how can I test to write the variable in the backup registers or in the flash. By the moment it doesn't matter if the flash wears out, this way I can test if this solution fits or not for the application I'm working on.

Posted on September 04, 2017 at 16:46

Hello Danish, thanks for your answer. About this line:

<ProgramSection alignment='4' load='No' name='.persistent'/>

Is this to be added in the block SECTIONS of the *.ld file?

Brian TIDAL
ST Employee
Posted on September 04, 2017 at 19:37

Hi Marco,

you may find some interesting things in

http://www.st.com/content/ccc/resource/technical/document/application_note/2e/d4/65/6b/87/dd/40/25/DM00049914.pdf/files/DM00049914.pdf/jcr:content/translations/en.DM00049914.pdf

EEPROM emulation in STM32F0xx microcontrollers.

Regards

Bruno

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.