Skip to main content
ERODR.1
Associate II
November 21, 2021
Solved

SMT32CubeIDE - How to store a const struct at a specific flash memory address?

I'm trying to store a const struct at a specific flash memory location using the STM32CubeIDE. I tried appending the __attribute__((at(address))) to the struct initialization, but the compiler ignores it: "warning: 'at' attribute directive ignored [-Wattributes]"

const runtimeParams params __attribute__((at(0x0801F800))) = {
 // ... initialization values
};

What would be the correct way to accomplish this?

This topic has been closed for replies.
TDK
TDKBest answer
November 21, 2021

There's not an easy way to do this in GCC. You either need to create a separate section in the linker to place the code, or use a pointer to store it somewhere directly while ensuring nothing else lives in that space.

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

8 replies

TDK
TDKBest answer
November 21, 2021

There's not an easy way to do this in GCC. You either need to create a separate section in the linker to place the code, or use a pointer to store it somewhere directly while ensuring nothing else lives in that space.

"If you feel a post has answered your question, please click ""Accept as Solution""."
ERODR.1
ERODR.1Author
Associate II
November 21, 2021

Thanks for your input. I created a new section in the linker as suggested.

ERODR.1
ERODR.1Author
Associate II
November 21, 2021

For anybody else wondering how to do it. I edited the STM32F072CBTX_FLASH.ld to add a new section as below:

 /* Runtime parameters section in FLASH */
 _params_start_address = 0x0801F800;
 
 .params _params_start_address :
 {
 . = ALIGN(4);
 KEEP (*(.params))
 } >FLASH

And then specified the section attribute in the const initialization as follows:

const runtimeParams __attribute__((section (".params"))) params = {
 // ... Initialization values
};

rohittheozzy
Associate II
October 8, 2024

This creates an overlap with the .rodata for stm32f030, how do you get around this?

 

 

MEMORY
{
 RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
 FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 64K
}

 

gbm
Super User
October 8, 2024
MEMORY
{
 RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 8K
 FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 64K
 PARAMFLASH (rx) : ORIGIN = 0x801F800, LENGTH = 2K
}

.params _params_start_address :
 {
 . = ALIGN(4);
 KEEP (*(.params))
 } >PARAMFLASH

This should be one step closer to what you want to achieve.

I assume however, that you want to keep the config data when flashing a new program. If this is the case, don't declare the structure as const, just make sure that the address is unused (outside of MEMORY ranges), then do this:

#define cfg_in_Flash (*(const struct cfgdata_ *)(CFG_ADDRESS))

 

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice