cancel
Showing results for 
Search instead for 
Did you mean: 

How to save an array to a particular flash position at compile/link time?

smoothmanifolds
Associate II

I'm using an STM32F103C8 and I have an array of unit8_t that I want to save to a particular position in flash memory, where such position is specified at compile/link time.

 

Eg. suppose the application resides in flash memory from position 0x08000000 to position 0x08004000, and then I want this array to not live with the rest of the application, but to start after the application ends in memory, at flash location 0x08004000.

 

And then at runtime I want to be able to modify the array by using flash write operations to that particular flash memory location and read/access the array by using flash read operations to the same flash memory location.

 

How can that be done?

2 REPLIES 2
TDK
Guru

> Eg. suppose the application resides in flash memory from position 0x08000000 to position 0x08004000, and then I want this array to not live with the rest of the application, but to start after the application ends in memory, at flash location 0x08004000.

There's not an easy way to do this with STM32CubeIDE/GCC, but you do have some options:

  • Define and access the array using a pointer. You'll need to ensure the space isn't used by other stuff, so you will need to remove this section of FLASH from the linker file. However, you won't be able to set the initial value of this array.
  • Create a section in the linker file specifically for this array and use __attribute__ to assign it as such, with necessary modifications so that it gets assigned at compile time.

 

> And then at runtime I want to be able to modify the array by using flash write operations to that particular flash memory location and read/access the array by using flash read operations to the same flash memory location.

FLASH is not RAM and cannot be easily modified as such. If you want to modify it, you will have to erase the entire page it resides in first, then write the new value. There are no special requirements for reading the array, just writing to it.

If you feel a post has answered your question, please click "Accept as Solution".

Thank you.

 

The part I don't know too much about is:

"Create a section in the linker file specifically for this array and use __attribute__ to assign it as such, with necessary modifications so that it gets assigned at compile time."

 

Can you elaborate on how to do that and what the __attribute__ looks like?