cancel
Showing results for 
Search instead for 
Did you mean: 

Storing variables in flash [HAL]

Posted on September 18, 2015 at 14:01

Hello there,

Does the HAL library supports storing variables in the flash memory? In AVR, with a certain macro added to the variable, I could tell the compiler that it should be stored in flash instead of sram and it was not erased between power cycles.

I would apreciate all help here.

#flash #stm32
8 REPLIES 8
jpeacock
Associate II
Posted on September 18, 2015 at 14:14

If you are referring to string literals then storing in flash is a function of the compiler.  The const keyword directs the linker to place the data in flash.

If you want to store actual variables in flash it's far more complicated.  Every time you change the value of a single variable you would have to copy the entire block, erase it, and rewrite the block with the modified value.  And you would quickly wear out the flash doing this.

Some STM32 parts have a 4KB SRAM area that's in the backup power domain.  If you have a backup power supply on Vbat then you can assign the variable to that area of memory, but assigning variables to a fixed area of memory is compiler dependent.  Variables in this region are preserved across resets and power cycles as long as the backup supply is operational.

  Jack Peacock

e1369008
Associate II
Posted on September 18, 2015 at 14:15

If you declare your variable const (that is read only) then it will end up only in the flash and not use any RAM space. If you want read/write variables not to be copied to RAM then you should do it manually. Search for EEPROM emulation.

Posted on September 18, 2015 at 14:19

You'll have to manage this manually, as the erase/write cycles are very slow and can stall the processor.

Suggest you put your configuration/non-volatile items into a structure, cache it in RAM, and manage the update or journalling of the data into FLASH.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on September 18, 2015 at 15:02

This is all fine. I dont mind slow read/ write as well as wear leveling because I can implement wear leveling alghoritm if needed (but probably wont need, as the values wont be changed very often). I was trying to find something about EEPROM emulation but there are no libraries for HAL.

All I need is software library for easy storing and reading data from flash. I hope I wont have to implement these low level functions mysalfe. Is there anything like that available for HAL ans STM32?

I didnt mean const variables.
Posted on September 18, 2015 at 15:19

Using the FLASH isn't that complicated, emulating it as an EEPROM creates more issues than it actually solves.

STM32Cube_FW_F4_V1.3.0\Projects\STM324xG_EVAL\Examples\FLASH\FLASH_EraseProgram\Src\main.c

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on September 18, 2015 at 15:24

I dont mind writing a block of data to the flash. Thank you for directions.

e1369008
Associate II
Posted on September 18, 2015 at 15:27

What parts are you using? There are several EEPROM emulation examples in STM32Cube repository. Check out these directories

STM32Cube_FW_F0_V1.3.0\Projects\STM32F091RC-Nucleo\Applications\EEPROM directory

STM32Cube_FW_F1_V1.2.0\Projects\STM32F103RB-Nucleo\Applications\EEPROM\EEPROM_Emulation

STM32Cube_FW_L4_V1.0.0\Projects\STM32L476G_EVAL\Applications\EEPROM\EEPROM_Emulation

STM32Cube_FW_F7_V1.1.0\Projects\STM32746G-Discovery\Applications\EEPROM\EEPROM_Emulation

STM32Cube_FW_F7_V1.1.0\Projects\STM32756G_EVAL\Applications\EEPROM\EEPROM_Emulation

Posted on September 18, 2015 at 17:52

There are several EEPROM emulation examples in STM32Cube repository.

Ok, but if you spend a man-day implementing/understanding either the FLASH or the EEPROM approach, you're always going to win if you choose managing the FLASH natively, because sooner or later the reality of how it actually functions is going the penetrate the veil of pretending it's something that it's not.

The code will also be smaller, and there will be less pitfalls for the unwary to stumble into.

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