Skip to main content
arnold_w
Senior II
August 30, 2023
Solved

How do I place an INITIALIZED struct on a specific location in RAM?

I'm working with the STM32F7 microcontroller and I compile using STM32CubeIDE. I want to place myStruct on a specific location in RAM and I want it be initialized automatically:

MEMORY {
 ...
 MY_STRUCT_MEMORY(rwx): ORIGIN = 0x20007BA0 LENGTH = 8
 ...
 FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 2097152
}

SECTIONS {
 ...
 .myStructSection :
 {
 . = ALIGN(4);
 KEEP(*(.myStructSection))
 *(.myStructSection .myStructSection.*) 
 . = ALIGN(4);
 } >MY_STRUCT_MEMORY
 ...
}
typedef struct { int myInt1; int myInt2; } MyStruct_s;
__attribute__ ((section(".myStructSection"),used)) MyStruct_s myStruct = {12345, 67890};

 Does anybody know what I'm missing?

This topic has been closed for replies.
TDK
TDKBest answer
August 30, 2023

You're missing that the startup file is only programmed to initialize the default data section, between _sdata and _edata.

You're also missing that it should be ">MY_STRUCT_MEMORY AT> FLASH" since the data needs to be stored in flash before it's sent to RAM.

If you do that and modify your startup script to initialize the new section it'll work. Will need to define some new symbols. But the linker won't write a new startup file for you.

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

1 reply

TDK
TDKBest answer
August 30, 2023

You're missing that the startup file is only programmed to initialize the default data section, between _sdata and _edata.

You're also missing that it should be ">MY_STRUCT_MEMORY AT> FLASH" since the data needs to be stored in flash before it's sent to RAM.

If you do that and modify your startup script to initialize the new section it'll work. Will need to define some new symbols. But the linker won't write a new startup file for you.

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