cancel
Showing results for 
Search instead for 
Did you mean: 

Save a variable to flash memory, and restore variable later after power loss?

Matt Roybal
Associate II
Posted on April 11, 2018 at 23:02

Hello, I was wondering if its possible to save a variable to flash memory, so that after power down or reset, I could then restore that variable?

The specific chip I am using is the STM32F303CCT6 and the STM32F301C6, and I am usinc STM32CubeMX to generate the project. Any help or advice on this would be greatly appreciated, thanks!

8 REPLIES 8
Andrew Neil
Evangelist III
Posted on April 11, 2018 at 23:59

Look for the App Notes on EEPROM emulation.

Szymon PANECKI
Senior III
Posted on April 12, 2018 at 00:22

Hello Matt,

Defenitely you can write in Flash your variable and after power down or reset you can read it. For writing you can use dedicated functionHAL_FLASH_Program(). For reading there is no function needed, as you just need to refer to memory adress. Below I put simple example of writing to Flash and reading from Flash.

HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, Address, DATA_32);
data32 = *(__IO uint32_t *)Address;
�?�?

In order to evaluate an example application you can refer to STM32F3 Cube package. You will find it under this link:

...\STM32Cube_FW_F3_V1.9.0\Projects\STM32F303RE-Nucleo\Examples\FLASH\FLASH_EraseProgram

Regards

Szymon

Posted on April 12, 2018 at 19:41

 ,

 ,

Hi Szymon, thanks for the info!

I started looking into this more, and it sounds like you need to unlock it first ('HAL_FLASH_Unlock(),'), followed by erasing, then program. I cannot find the erase function in the HAL library, do you know why its missing? Also, do you know what address I should be using?

I tried your link, but the link seemed to be incomplete and didn't work.

EDIT:

So I found the erase stuff under ,stm32f3xx_hal_flash_ex, but I get error messages with it:

void SaveBaselineMeasurements()

 ,

{

 ,

uint32_t pageAddress = 0x08008000,

 ,

uint16_t buffer = 0, //Fill buffer with data to write

HAL_FLASH_Unlock(),

 ,

FLASH_PageErase(pageAddress),

 ,

HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, pageAddress, buffer),

 ,

HAL_FLASH_Lock(),

 ,

}

The problem is '

FLASH_PageErase(pageAddress),

' is underlined in red with a message saying 'warning: , ♯ 223-D: function 'FLASH_PageErase' declared implicitly', meaning it doesn't see that function definition?
Pavel A.
Evangelist III
Posted on April 13, 2018 at 02:04

Note that you have also the 'backup registers' of RTC. It survives reset but not power down, unless you have a backup battery.

--- pavel

Posted on April 13, 2018 at 13:36

Actually, the 'normal' RAM survives a reset - you just have to configure your 'C' runtime not to initialise it ...

Posted on April 13, 2018 at 14:22

Yup you are right of course. Why ST did not provide eeprom in STM32, like in the STM8 family.

 -- pa

USuha.1
Associate II

I am also in same situation.

i have code side of 64k in stm32f401ccu6, so all the 4 16k sectors are used by the main application. i need to add the algorithm of eeprom emulation in my code to store parameters in to the flash memory, these parameter may update during the life of device hundreds of time.

will it be possible to have two 16k blank sector in my case. so that eeprom emulation can be incorporated.

Ok, couple of options here. Make a small 16KB loader that lives at 0x08000000 and your main app at 0x08010000

Or modify the Linker Script to make a hole of 3 of the 16KB Flash Sectors. Make a 16KB Memory section at 0x08000000 and direct vectors into that, and change the main FLASH Memory section to start at 0x08010000 and shrink the length by 64KB.

Then 0x08004000, 0x08008000 and 0x0800C000 are available for EEPROM Emulation or configuration parameters. 

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