2015-09-18 05:01 AM
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 #stm322015-09-18 05:14 AM
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 Peacock2015-09-18 05:15 AM
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.
2015-09-18 05:19 AM
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.2015-09-18 06:02 AM
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.2015-09-18 06:19 AM
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.c2015-09-18 06:24 AM
I dont mind writing a block of data to the flash. Thank you for directions.
2015-09-18 06:27 AM
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_Emulation2015-09-18 08:52 AM
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.