2024-08-19 02:20 PM
Hi,
I would like to change at runtime some user-defined variables stored in Flash (so they remain on the system after power off). I have defined them as const and some are just 16 bits vars and others are more complex large structures. What is the best way to do that?
I came across the Flash Sector erase function but don't want to risk using a whole block erase (i.e. reading block, changing specific byte, word or struct then write back the entire sector with new values). Are there other options?
Also which include files do I need? Still getting familiar with the documentation, I came across three possible header files to include but not sure which to include in my code of these:
#include <stm32l4xx_hal_flash.h>
#include <stm32l4xx_hal_flash_ex.h>
#include <stm32l4xx_hal_flash_ramfunc.h>
could someone please clarify that too.
Thank you 😊
2024-08-19 02:28 PM
There's no way to modify a flash sector without erasing it first. It is a hardware limitation. The most common solution is to use a sector of flash which is reserved for user settings variables and can be erased as needed.
The top level HAL include should be "stm32l4xx_hal.h". That's it. Other files are included from within there as needed. Modify your stm32l4xx_hal_conf.h file to include other modules if needed.
2024-08-19 02:32 PM
You cannot change anything random in a flash memory.
After erase (block of memory typically) you can write it once - that's it. To write new data, you have to erase the area and then you can write it again.
So to store some values, you can use the battery backed up ram ( if you have a battery on your system) in RTC module, attach an eeprom or use a part of the CPU flash as a eeprom ( there are some examples for this in GitHub or STM examples ).
2024-08-19 03:08 PM
Journal the structure content over the available erased flash space.
By "journal" I mean write multiple instances of the structure, with say a sequence count and checksum or CRC, so you can walk it later and pull out the most recent valid instance.
Use two pages/sectors, so you always have a valid copy, erase the first page at the point you're ready to write the second instance into the second page.