2017-09-04 12:49 AM
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.2017-09-04 01:25 AM
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.
2017-09-04 04:26 AM
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.
2017-09-04 05:22 AM
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
2017-09-04 05:38 AM
Agreeing with Jeroen3, I see three options.
Albeit bean-counting project managers and sales people love option 3, it never worked up to now ...
2017-09-04 05:56 AM
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.
2017-09-04 06:59 AM
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 ...
2017-09-04 09:35 AM
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.
2017-09-04 09:46 AM
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?
2017-09-04 10:37 AM
Hi Marco,
you may find some interesting things in
EEPROM emulation in STM32F0xx microcontrollers.Regards
Bruno