2018-04-11 02:02 PM
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!
2018-04-11 02:59 PM
Look for the App Notes on EEPROM emulation.
2018-04-11 03:22 PM
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
2018-04-12 12:41 PM
,
,
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 writeHAL_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?2018-04-12 05:04 PM
Note that you have also the 'backup registers' of RTC. It survives reset but not power down, unless you have a backup battery.
--- pavel
2018-04-13 06:36 AM
Actually, the 'normal' RAM survives a reset - you just have to configure your 'C' runtime not to initialise it ...
2018-04-13 07:22 AM
Yup you are right of course. Why ST did not provide eeprom in STM32, like in the STM8 family.
-- pa
2024-04-23 06:22 AM
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.
2024-04-23 12:48 PM
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.