用于寻址外围设备的#define 与枚举 [英] #define vs. enums for addressing peripherals

查看:34
本文介绍了用于寻址外围设备的#define 与枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在基于 ARM9 的微控制器中对外围寄存器进行编程.

I have to program peripheral registers in an ARM9-based microcontroller.

例如,对于 USART,我将相关的内存地址存储在 enum 中:

For instance, for the USART, I store the relevant memory addresses in an enum:

enum USART
{
    US_BASE = (int) 0xFFFC4000,
    US_BRGR = US_BASE + 0x16,
    //...
};

然后,我在函数中使用指针来初始化寄存器:

Then, I use pointers in a function to initialize the registers:

void init_usart (void)
{
    vuint* pBRGR = (vuint*) US_BRGR;
    *pBRGR = 0x030C;
    //...
}

但是老师说我最好用#define,比如:

But my teacher says I'd better use #defines, such as:

#define US_BASE (0xFFFC4000)
#define US_BRGR (US_BASE + 0x16)
#define pBRGR   ((vuint*) US_BRGR)

void init_usart (void)
{
    *pBRGR = 0x030C;
}

他说,就像这样,你没有在堆栈中分配指针的开销.

Like so, he says, you don't have the overhead of allocating pointers in the stack.

就个人而言,我不太喜欢#define,也不喜欢其他预处理器指令.所以问题是,在这种特殊情况下,是否真的值得使用 #define 代替 enum 和堆栈分配的指针?

Personally, I don't like #defines much, nor other preprocessor directives. So the question is, in this particular case, are #defines really worth using instead of enums and stack-allocated pointers?

相关问题:想在ARM9中配置特定外设寄存器基于芯片

推荐答案

我一直喜欢的方法是首先定义一个反映外设寄存器布局的结构

The approach I've always preferred is to first define a struct reflecting the peripherals register layout

typedef volatile unsigned int reg32; // or other appropriate 32-bit integer type
typedef struct USART
{
    reg32 pad1;
    reg32 pad2;
    reg32 pad3;
    reg32 pad4;
    reg32 brgr;
    // any other registers
} USART;

USART *p_usart0 = (USART * const) 0xFFFC4000;

然后在代码中我可以使用

Then in code I can just use

p_usart0->brgr = 0x030C;

当您有多个相同类型外围设备的实例时,这种方法会更简洁:

This approach is much cleaner when you have multiple instances of the same sort of peripheral:

USART *p_usart1 = (USART * const) 0xFFFC5000;
USART *p_usart2 = (USART * const) 0xFFFC6000;

用户 sbass 提供了一个链接到 ,详细介绍了这种技术,并指出了它相对于其他方法的优势.

User sbass provided a link to an excellent column by Dan Saks that gives much more detail on this technique, and points out its advantages over other approaches.

如果您有幸使用 C++,那么您可以为外设上的所有常见操作添加方法,并很好地封装设备特性.

If you're lucky enough to be using C++, then you can add methods for all the common operations on the peripheral and nicely encapsulate the devices peculiarities.

这篇关于用于寻址外围设备的#define 与枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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