STM32F0 memory model - where can I save data in flash ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-01 9: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.
- Labels:
-
Flash
-
STM32F0 Series
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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.
- You can't overwrite already programmed areas in flash with arbitrary values, only unused ones. To overwrite it, you must erase the whole page first.
- Overwriting a halfword with 0x0000 works.
- Note that the flash is not guaranteed to work after
100010000 erase operations.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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.
- You can't overwrite already programmed areas in flash with arbitrary values, only unused ones. To overwrite it, you must erase the whole page first.
- Overwriting a halfword with 0x0000 works.
- Note that the flash is not guaranteed to work after
100010000 erase operations.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-01 11:04 AM
Not 1000, but 10000 erase-write cycles.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-01 11:09 AM
Oops, looked at the wrong datasheet.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-04-01 1: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.
- I found the flash memory definition STM32F042K6TX_FLASH.ld (generated by STM32CubeIDE), and I changed the LENGTH
- Then I use the last page of the flash (from address 0x8007C00 to 0x8007FFF)
- STM32F042K6 : total flash size : 32768 bytes, 32 pages (1024 bytes each)
Best regards,
Valentin
