lukewalker_p2501
lukewalker_p2501

Reputation: 1

How to pick address in STM32 flash, where I want to store a variable that will remain after power shutdown?

I want to store some variables in "flash" using function HAL_FLASH_Program(TypeProgram, Address, Data), so that they will retain after power shutdown. I am using STM32G071KB.

What should I set the Address to? Maybe start of SRAM (0x20000000)?

Reference manual: https://www.st.com/resource/en/reference_manual/rm0444-stm32g0x1-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

Upvotes: 0

Views: 2246

Answers (1)

Tagli
Tagli

Reputation: 2592

You need to save them into main flash memory. On most STM32 parts, it starts from 0x8000000. However, this is also where the vector table and .text section (your program) resides. So you need to find a unused area, probably at the end of the flash.

To be safe, you need to edit the linker script to limit the free space used for .text section so that the compiler won't place anything on the flash memory area you plan to use for your non-volatile data.

Flash memory consists of multiple pages. It may be a good idea to reserve the last few of them as your non-volatile storage.

Also keep it in mind that on STM32G0 series it's only possible to write 8-byte blocks and it's only possible to delete whole 2 KiB pages. So you can't simply write and delete single bytes.

Upvotes: 1

Related Questions