在使用 NXP 的 MC56F84789 时遇到驱动库中使用 ## 的写法
刚开始看很懵的,完全不懂是什么操作,查了一波资料后有了大致的理解,## 就相当于将前后连接起来
我们来看库中的部分代码的应用
此处是寄存器某一位地址的宏
/*** QSPI0_SPDSR - SPI Data Size and Control Register; 0x0000E0B1 ***/
union
{
word Word;
} QSPI0_SPDSR_STR;
#define QSPI0_SPDSR_DS0_MASK 0x01U
#define QSPI0_SPDSR_DS1_MASK 0x02U
#define QSPI0_SPDSR_DS2_MASK 0x04U
#define QSPI0_SPDSR_DS3_MASK 0x08U
#define QSPI0_SPDSR_SPR3_MASK 0x10U
#define QSPI0_SPDSR_SSB_OVER_MASK 0x20U
#define QSPI0_SPDSR_SSB_STRB_MASK 0x40U
#define QSPI0_SPDSR_SSB_DDR_MASK 0x80U
#define QSPI0_SPDSR_SSB_AUTO_MASK 0x0100U
#define QSPI0_SPDSR_SSB_ODM_MASK 0x0200U
#define QSPI0_SPDSR_SSB_DATA_MASK 0x0400U
#define QSPI0_SPDSR_SSB_IN_MASK 0x0800U
#define QSPI0_SPDSR_BD2X_MASK 0x1000U
#define QSPI0_SPDSR_RDMAEN_MASK 0x2000U
#define QSPI0_SPDSR_TDMAEN_MASK 0x4000U
#define QSPI0_SPDSR_WOM_MASK 0x8000U
#define QSPI0_SPDSR_DS_MASK 0x0FU
#define QSPI0_SPDSR_DS_BITNUM 0x00U
#define QSPI0_SPDSR (*((volatile word *)0x0000E0B1))
/*** QSPI0_SPDRR - SPI Data Receive Register; 0x0000E0B2 ***/
union
{
word Word;
} QSPI0_SPDRR_STR;
#define QSPI0_SPDRR_R0_MASK 0x01U
#define QSPI0_SPDRR_R1_MASK 0x02U
#define QSPI0_SPDRR_R2_MASK 0x04U
#define QSPI0_SPDRR_R3_MASK 0x08U
#define QSPI0_SPDRR_R4_MASK 0x10U
#define QSPI0_SPDRR_R5_MASK 0x20U
#define QSPI0_SPDRR_R6_MASK 0x40U
#define QSPI0_SPDRR_R7_MASK 0x80U
#define QSPI0_SPDRR_R8_MASK 0x0100U
#define QSPI0_SPDRR_R9_MASK 0x0200U
#define QSPI0_SPDRR_R10_MASK 0x0400U
#define QSPI0_SPDRR_R11_MASK 0x0800U
#define QSPI0_SPDRR_R12_MASK 0x1000U
#define QSPI0_SPDRR_R13_MASK 0x2000U
#define QSPI0_SPDRR_R14_MASK 0x4000U
#define QSPI0_SPDRR_R15_MASK 0x8000U
#define QSPI0_SPDRR (*((volatile word *)0x0000E0B2))
此处是设置寄存器的宏
#define setRegBit(reg, bit) (reg |= (word)(reg##_##bit##_##MASK))
#define clrRegBit(reg, bit) (reg &= (word)(~(word)reg##_##bit##_##MASK))
#define getRegBit(reg, bit) (reg & reg##_##bit##_##MASK)
#define setReg(reg, val) (reg = (word)(val))
#define getReg(reg) (reg)
我们由上可以看出 我们截取了两个寄存器的宏,分别为寄存器 QSPI0_SPDSR、QSPI0_SPDRR
我们在配置寄存器的时候,把对应的寄存器名和相应控制位的名字填写到 setRegBit() 中的 reg 和 bit 中即可
假设对 寄存器 QSPI0_SPDRR 寄存器的 R0 位操作,我们直接写 setRegBit(QSPI0_SPDRR,R0); 即可
意义就相当于 QSPI0_SPDRR = QSPI0_SPDRR | QSPI0_SPDRR_R0_MASK
还有关于 # 的使用,请查看该文章:https://blog.csdn.net/baidu_33850454/article/details/79363033