指针到特定的固定的地址 [英] Pointer to a specific fixed address

查看:184
本文介绍了指针到特定的固定的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你如何分配一个特定的内存地址的指针?

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

这篇关于指针到特定的固定的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆