开发者

How to specify variable location in physical memory?

开发者 https://www.devze.com 2023-02-21 19:10 出处:网络
gcc allows to control a section where a variable is placed to using the section attribute: struct duart a __attribute__ ((section (\"DUART_A\"))) = { 0 };

gcc allows to control a section where a variable is placed to using the section attribute:

struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };

Is there a way to specify an exact location of this section in physical memory in my C-code? I assume no virtual memory present, or, alternatively, virtual address is directly translated into ph开发者_C百科ysical address.

I know i can handle it through a script I pass to the linker, but it would be better if I could to specify it directly in my (autogenerated) C.


You can achieve the effect of having the variable at some physical address by using a macro, for example:

#define a (*(volatile struct duart *)0xdeadbeef)

This doesn't use the linker; it doesn't declare any variables so you won't see a in the object file. But i guess you don't need this.

Note volatile keyword, which is always needed when using memory-mapped hardware. It will often work without volatile, but sometimes it won't, and debugging such failures is hard - so don't forget that volatile keyword!

Usage:

// I assume the duart structure has fields write_buf and read_buf of type uint8_t
a.write_buf = 0x55; // write data to DUART
a.write_buf = 0xaa; // write more data to DUART
uint8_t byte1 = a.read_buf; // read data from DUART
uint8_t byte2 = a.read_buf; // read more data from DUART


A simple way is to define a pointer to your structure and initialize it with the correct value, so you don't need special linker options. Of course, the trick works only if you don't need any speciel link-time feature.

Bye!

0

精彩评论

暂无评论...
验证码 换一张
取 消