Skip to main content
Tony1
Associate
August 11, 2019
Question

How to place a variable at a given absolute address in memory with STM32CubeIDE

  • August 11, 2019
  • 4 replies
  • 8542 views

Hello everyone,

I just migrate from Keil. I can use __attribute__((at(0x6000000))) directive place a variable at a given absolute address ,but how to do this in STM32CubeIDE?

Thanks.

This topic has been closed for replies.

4 replies

Tesla DeLorean
Guru
August 11, 2019

It is generally considered non-portable. You can use the attribute directive and section naming in the linker script of GNU tools.

Consider if you can just use pointers. For larger groups of data, structures.

ie

char *foo = (char *)0x6000000;

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Tony1
Tony1Author
Associate
August 11, 2019

Thank you for your prompt reply.

I have a piece of code , such as

//-----------------------------------------------------------------------------------------------------------

__align(32) u8 mem2base[MEM2_MAX_SIZE] __attribute__((at(0X60000000)));

//-----------------------------------------------------------------------------------------------------------

There is a align directive ,so I don't think I can use the point.

I will use the attribute directive and section naming that you mentioned.

Thanks

Tesla DeLorean
Guru
August 13, 2019

You're providing an absolute address, where you explicitly control the placement.

The __align directive doesn't impact any other usage of the of the variable, as far as I'm aware

u8 *mem2base = (u8 *)0x60000000;

would have materially the same effect, and be accessible in the same way, ie printf("%02X\n", mem2base[123]);

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
August 11, 2019

Unless it's a physical special area, it would be better to avoid absolute address, especially when porting to different MCU.

Why such a need?

Tony1
Tony1Author
Associate
August 13, 2019

Yes, it's a SRAM address.

Thanks, I solved the issue by changing the link script file, add section directive.

Rahul.Santhosh
Associate II
September 22, 2022

@Community member​ 

I'm tryna use this method of pointers, but how can I use this pointer to point to a variable?

It would be good if you post it with an example.

Tesla DeLorean
Guru
September 22, 2022

It's a pointer method, it works the way pointers do, perhaps K&R chapter on such things?

typedef struct _CONFIG_STRUCT {
 uint32_t a;
 uint32_t b;
 uint32_t c[12];
 
 uint32_t checksum;
} CONFIG_STRUCT;
 
CONFIG_STRUCT *cfg = (CONFIG_STRUCT *)0x60001000;
 
 
cfg->checksum = 1234;
cfg->a = 1;
cfg->b = 2;
 
printf("%d\n", cfg->b);
 
int foo = cfg->a;
 
...

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Rahul.Santhosh
Associate II
September 23, 2022

@Community member​ 

Thanks for the example. But this does not work if I'm giving the address as the initial address of the RAM and If I declare any global variable, it gets over written on the same address

Tesla DeLorean
Guru
September 23, 2022

Your use case seems different to that of the OP, where the use of AT directives in code or linker script were not portable.

Not clear at all what you're trying to achieve.

Perhaps you should change the RAM base that the linker starts using in the Linker Script (.LD) to carve out whatever space it is you want to use, and have the startup.s code leave alone.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..