cancel
Showing results for 
Search instead for 
Did you mean: 

Placing string in a fixed memory address on flash

DGo.1
Associate II

I want to place a variable at a fixed address on my flash memory:

#define MYDEF "abcdefg"
const u8 MY_CHAR[]  __attribute__((section(".ARM.__at_0x0blabla"))) = MYDEF;

this works with KEIL IDE, but I switched to STM32CUBE IDE and it is not working. The mcu is an STM32F10

How do I have to change the attribute? Do I have also to change the linker script file ? This scares my...

Thanks

7 REPLIES 7
alister
Lead

STM32CubeIDE uses a different toolchain to KEIL.

Its compiler is described at https://gcc.gnu.org/.

Its linker is described at https://sourceware.org/binutils/docs/ld/index.html.

DGo.1
Associate II

So, I changed this to:

const u8 MY_CHAR[] __attribute__((section(".mySection"))) = MYDEF;

and add this to .ld file:

/* Memories definition */
MEMORY
{
  RAM    (xrw)    : ORIGIN = 0x20000000,   LENGTH = 64K
  FLASH    (rx)    : ORIGIN = 0x8000000,   LENGTH = 128K
}
 
/* Sections */
SECTIONS
{    
   /* other placements follow here... */  
   
   .mySegment 0x8002000 : {KEEP(*(.mySection))}
   
  /* The startup code into "FLASH" Rom type memory */
...and so on

But got compilation error if I put an address related to flash ( i.e. 0x8002000)

If I put 0x2000000 I got no error, what am I doing wrong ?

section .mySegment LMA [0000000008002000,000000000800200e] overlaps section .text LMA [00000000080001e8,000000000800c0cf]

TDK
Guru

The GCC linker isn't really set up to do this. You need to create a new section in memory and place the variable there, but it can't be in the middle of your flash.

It's easier to use a pointer and initialize manually.

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

Noobie question:

In this way also my .bin file will have this string at the address i want?

I want to put a string in a particular address i.e. : 0x8002000 ( so if I open the file with HxD it will show me this string at 0x8002000) is it possible to do with a pointer at compiling time ?

> I want to put a string in a particular address i.e. : 0x8002000 ( so if I open the file with HxD it will show me this string at 0x8002000) is it possible to do with a pointer at compiling time ?

No, it's not. You'd need to modify the linker to do that. But there's no easy way to place it at an absolute location within an existing memory section.

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

Does the executable code take more than 0x2000 bytes ? the linker error seems to indicate that the code goes beyond 0x08002000 and you try to force a constant to be at this address.

could you try to place the constant after the end of the code ? (way after 0x08002000).

> want to put a string in a particular address i.e. : 0x8002000

As apps grow larger and complex, where things are needs organising. That's what the linker command file is for.

Many details and options. Read the link I posted earlier about LD thoroughly.

This is a really dumbed-down example.

In the linker command file:

<snip>
 
MEMORY
{
  <snip>
 
  MY_FLASH_AREA (r)                  : ORIGIN = 0x8002000, LENGTH = 1K
}
 
SECTIONS
{
  <snip>
 
  MY_FLASH_AREA (NOLOAD) :
  {
    *(MY_FLASH_AREA)
  } >MY_FLASH_AREA
}

In your C source code:

char myString[1024] __attribute__((section("MY_FLASH_AREA")));

You didn't mentioned how the string is programmed.

Perhaps it's during manufacture and it's immutable. If that's the case, add a const qualification to that myString declaration.