本文介绍了指向特定固定地址的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为指针分配特定的内存地址?
How do you assign a specific memory address to a pointer?
AVR m128 等微控制器中的特殊功能寄存器有固定地址,AVR GCC 在 io.h 头文件中定义了 SFR,但我想自己处理.
The Special Function Registers in a microcontroller such AVR m128 has fixed addresses, the AVR GCC defines the SFR in the io.h header file, but I want to handle it myself.
推荐答案
好的,没问题.你可以直接将它分配给一个变量:
Sure, no problem. You can just assign it directly to a variable:
volatile unsigned int *myPointer = (volatile unsigned int *)0x12345678;
我通常做的是声明一个内存映射 I/O 宏:
What I usually do is declare a memory-mapped I/O macro:
#define mmio32(x) (*(volatile unsigned long *)(x))
然后在头文件中定义我的寄存器:
And then define my registers in a header file:
#define SFR_BASE (0xCF800000)
#define SFR_1 (SFR_BASE + 0x0004)
#define SFR_2 (SFR_BASE + 0x0010)
然后使用它们:
unsigned long registerValue = mmio32(SFR_1); // read
mmio32(SFR2) = 0x85748312; // write
这篇关于指向特定固定地址的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!