cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0 memory model - where can I save data in flash ?

ValentinG
Associate II

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) :

0693W000000VAieQAG.png

Is there a specific area for user data in flash ?

Thank you for reading,

Best regards,

Valentin

1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal

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 1000 10000 erase operations.

View solution in original post

5 REPLIES 5
berendi
Principal

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 1000 10000 erase operations.

Not 1000, but 10000 erase-write cycles.

Pavel A.
Evangelist III

The flash memory which you can program (erase and rewrite) is the white block from 0x08000000 up to the grey block above it.

-- pa

Oops, looked at the wrong datasheet.

ValentinG
Associate II

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)

0693W000000VBL2QAO.png

Best regards,

Valentin