2020-04-01 09:36 AM
Hello,
I want to save data in flash memory of a STM32F0 using HAL library :
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, 0x20000000, data);
HAL_FLASH_Lock();
I don't know which address is allowed to store user data in flash.
Here is the stm32f042k6 memory model (from datasheet) :
Is there a specific area for user data in flash ?
Thank you for reading,
Best regards,
Valentin
Solved! Go to Solution.
2020-04-01 10:43 AM
There is no special area, the only restriction is that the application vector table must be placed at 0x08000000, i.e. the lowest address in the flash, so you can't use page 0 for user data. The most straightforward way is to reserve the last pages of the flash for user data.
The flash is documented in chapter 3 of the reference manual, find the Flash memory organization table for your MCU there. Note the page size, pages are the units for erase operation, i.e. a whole page must be erased at one whenever you would like to overwrite data.
Find the linker script of your project (*.ld when using the gnu toolchain), and decrease the flash length parameter there. I have a STM32F030F4, the flash memory definition in my STM32F030F4PX_FLASH.ld reads
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 16K
the above mentioned table says I have 1K pages, so I could e.g. replace it with
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 13K
to reserve 3 pages. If you have an MCU with 2K pages, stick to even numbers.
Now the remaining area beyond the LENGTH value will never be touched by the linker, and your application is free to use it.
2020-04-01 10:43 AM
There is no special area, the only restriction is that the application vector table must be placed at 0x08000000, i.e. the lowest address in the flash, so you can't use page 0 for user data. The most straightforward way is to reserve the last pages of the flash for user data.
The flash is documented in chapter 3 of the reference manual, find the Flash memory organization table for your MCU there. Note the page size, pages are the units for erase operation, i.e. a whole page must be erased at one whenever you would like to overwrite data.
Find the linker script of your project (*.ld when using the gnu toolchain), and decrease the flash length parameter there. I have a STM32F030F4, the flash memory definition in my STM32F030F4PX_FLASH.ld reads
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 16K
the above mentioned table says I have 1K pages, so I could e.g. replace it with
FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 13K
to reserve 3 pages. If you have an MCU with 2K pages, stick to even numbers.
Now the remaining area beyond the LENGTH value will never be touched by the linker, and your application is free to use it.
2020-04-01 11:04 AM
Not 1000, but 10000 erase-write cycles.
2020-04-01 11:04 AM
The flash memory which you can program (erase and rewrite) is the white block from 0x08000000 up to the grey block above it.
-- pa
2020-04-01 11:09 AM
Oops, looked at the wrong datasheet.
2020-04-01 01:51 PM
Hello berendi, Piranha and Pavel A.
First, thank you for your accurate answers. I add some information if someone is asking the same question as me.
Best regards,
Valentin