Keil- ARM compiler Write and read from absolute address

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Keil- ARM compiler Write and read from absolute address

跳至解决方案
1,759 次查看
aswinprabhu
Contributor III

Hi,

Can someone specify ways to a) write to and b) read from, an absolute address for ARM compiler? Microcontroller is LPC54628.

Issue:

A global variable 'var' prints a certain value to serial terminal when called from main function but when this value is printed from inside an Interrupt Service Routine, it shows up as 0. If I change the initializing value for this variable to something else, say 100, then it will print 100. Can't figure out why. Hence thinking of accessing the variable by specifying the address location.

Any help would be greatly appreciated. Thanks.

0 项奖励
1 解答
1,755 次查看
ErichStyger
Senior Contributor V

Something like this, assuming you want a 32bit read/write:

#define MY_ADDRESS (0x12345678) /* address of object *(

/* read */
int32_t i = *((int32_t*)MY_ADDRESS);

/* write */
*((int32_t*)MY_ADDRESS) = i;

There are of course many other ways you could do this in C/C++, e.g. do all the casting in the macro.

I hope this helps,

Erich

在原帖中查看解决方案

3 回复数
1,734 次查看
converse
Senior Contributor V

[duplicate, removed]

0 项奖励
1,733 次查看
converse
Senior Contributor V

It is difficult to know the problem without seeing your code, but I guess it because you have not defined your variable as volatile.

for more information, see https://www.embedded.com/introduction-to-the-volatile-keyword/

 

0 项奖励
1,756 次查看
ErichStyger
Senior Contributor V

Something like this, assuming you want a 32bit read/write:

#define MY_ADDRESS (0x12345678) /* address of object *(

/* read */
int32_t i = *((int32_t*)MY_ADDRESS);

/* write */
*((int32_t*)MY_ADDRESS) = i;

There are of course many other ways you could do this in C/C++, e.g. do all the casting in the macro.

I hope this helps,

Erich