我一直在为这个平台上的现场可编程门阵列中实现的硬件外设开发软件驱动程序。我一直在使用 C++ 编程语言和 arm-none-eabi-gcc 编译器。
其中一个外设具有以下寄存器映射
Register Offset in bytes in Bits description
respect to the base address
========================================================================================
REGISTER_0 0x00 0-15
16-31
----------------------------------------------------------------------------------------
REGISTER_1 0x04 0-15
----------------------------------------------------------------------------------------
REGISTER_2 0x08 0-15
----------------------------------------------------------------------------------------
REGISTER_3 0x0C 0-15
16-31
----------------------------------------------------------------------------------------
CONTROL_REG 0x10 0: enable
1: reset
2: mask_00
3: mask_01
4: mask_02
5: mask_03
6: mask_04
7: mask_05
8: mask_06
9: mask_07
----------------------------------------------------------------------------------------
REGISTER_5 0x14 0-15
16-31
----------------------------------------------------------------------------------------
REGISTER_6 0x18 0-15
16
----------------------------------------------------------------------------------------
REGISTER_7 0x1C 0-15
16
----------------------------------------------------------------------------------------
REGISTER_8 0x20 0-31
----------------------------------------------------------------------------------------
对于寄存器 CONTROL_REG,我可以方便地同时使用各个掩码位以及所有掩码位。所以我决定在位域中使用联合
int main(int argc, char** argv) {
struct ControlReg
{
uint32_t enable_bit: 1;
uint32_t reset_bit: 1;
union
{
struct
{
uint32_t bit_0: 1;
uint32_t bit_1: 1;
uint32_t bit_2: 1;
uint32_t bit_3: 1;
uint32_t bit_4: 1;
uint32_t bit_5: 1;
uint32_t bit_6: 1;
uint32_t bit_7: 1;
}bits;
uint8_t byte;
}mask;
};
ControlReg control_reg;
control_reg.mask.byte = 0xAA;
cout << "Mask bit 0: " << control_reg.mask.bits.bit_0 << endl;
cout << "Mask bit 1: " << control_reg.mask.bits.bit_1 << endl;
cout << "Mask bit 2: " << control_reg.mask.bits.bit_2 << endl;
cout << "Mask bit 3: " << control_reg.mask.bits.bit_3 << endl;
cout << "Mask bit 4: " << control_reg.mask.bits.bit_4 << endl;
cout << "Mask bit 5: " << control_reg.mask.bits.bit_5 << endl;
cout << "Mask bit 6: " << control_reg.mask.bits.bit_6 << endl;
cout << "Mask bit 7: " << control_reg.mask.bits.bit_7 << endl;
}
我已经测试过了,它可以工作。不过,我怀疑这种结构是否正确。谁能告诉我我选择的方法是否是我问题的正确解决方案?提前致谢。