Skip to main content
Associate II
October 31, 2025
Solved

how to write a specific portion of memory during compile?

  • October 31, 2025
  • 4 replies
  • 290 views

hello everyone ,i am using stm32f1 and cubeide , i want to put a 16bit security key on flash memory position page 63 during compile or build process ,is it possible ?

Best answer by ahmash

problem solved I put this part at the bottom of linker file before closing '}' of 

/* Sections */

SECTIONS
/////start user section////

.mydata 0x0800FC04 : //address

{

KEEP(*(.mydata))

} >FLASH
//end user section
} //SECTIONS closing brace

then add this following line in c file
const uint32_t myValue __attribute__((section(".mydata"))) = 33668899; //store key 33668899
i tried this method from claude thanks everyone

4 replies

Pavel A.
Super User
October 31, 2025

With a GNU toolchain (that comes with CubeIDE or any other): include a snippet like following in the SECTIONS part of the linker script:

ADDR = 0x08010000 ; /* the address */

SECTIONS
{
....................
....................

 .security_key ADDR:
 {
 key_start = .; /* Public symbol to access from C */
 SHORT(0x1234)
 }
...........
}

 

Karl Yamashita
Principal
October 31, 2025
If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
Tesla DeLorean
Guru
October 31, 2025

At a "compiler" level you could also cast a pointer.

uint16_t *key = (uint16_t *)0x080FF800;

Can use for structures too, adapt the address to reflect the specific STM32 part you are using. Shrink the FLASH size in the Linker Script to stop it being used.

Other part have OTP or EEPROM.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
ahmashAuthorBest answer
Associate II
November 1, 2025

problem solved I put this part at the bottom of linker file before closing '}' of 

/* Sections */

SECTIONS
/////start user section////

.mydata 0x0800FC04 : //address

{

KEEP(*(.mydata))

} >FLASH
//end user section
} //SECTIONS closing brace

then add this following line in c file
const uint32_t myValue __attribute__((section(".mydata"))) = 33668899; //store key 33668899
i tried this method from claude thanks everyone

MM..1
Chief III
November 1, 2025

Hi, marking own reply as solution isnt very fair and primary your question miss reality. Compile or build WRITE nothink into MCU. And more simple is write with STMProgrammer CLI, that have simple write command.