我正在研究嵌入式设备的微控制器。这些控制器提供多个使用寄存器进行配置和控制的外设。外设通常具有与其关联的特定寄存器组。多个相同类型的外设(例如,Timer0、Timer1...)具有相同的寄存器组,但它们位于不同的基地址。
通常制造商提供头文件来定义每个外设的寄存器和相关地址。
例如,可以提供定时器外设的头文件:
typedef struct {
uint32_t IR;
uint32_t TCR;
uint32_t TC;
uint32_t MCR;
uint32_t MR[4];
} CTIMER_Type;
#define CTIMER0_BASE (0x40008000u)
#define CTIMER0 ((CTIMER_Type *)CTIMER0_BASE)
#define CTIMER1_BASE (0x40009000u)
#define CTIMER1 ((CTIMER_Type *)CTIMER1_BASE)
#define CTIMER_BASE_PTRS { CTIMER0, CTIMER1 }
Run Code Online (Sandbox Code Playgroud)
这些头文件特定于特定设备。在此示例中,提供了有关定时器外设的信息:有两个定时器,它们的寄存器组分别位于CTIMER0 a
nd CTIMER1。
使用 C,我可以使用以下命令将基址寄存器地址列表拉CTIMER_BASE_PTRS入我的实现中:
static CTIMER_Type * ctimers[] = CTIMER_BASE_PTRS;
static int timers_count = sizeof(ctimers)/sizeof(*ctimers);
Run Code Online (Sandbox Code Playgroud)
现在使用 C++,我想编写一个驱动程序类并包含可用计时器的列表CTIMER_BASE_PTRS。我还想计算可用计时器的数量。
我想出了这段代码,您可以自己尝试一下:
#include <stdio.h>
#include <type_traits>
#include <stdint.h>
/* usually provided by chip manufacturer …Run Code Online (Sandbox Code Playgroud)