cancel
Showing results for 
Search instead for 
Did you mean: 

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

Tony1
Associate II

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.

10 REPLIES 10

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

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

S.Ma
Principal

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
Associate II

Yes, it's a SRAM address.

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

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 Venmo
Up vote any posts that you find helpful, it shows what's working..

oh, I see, this is the simpler way,

Thanks

Rahul.Santhosh
Associate II

@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.

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
Rahul.Santhosh
Associate II

@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