来自野火,写的挺好,清晰明了。
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) //不必纠结怎么算的
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//#define GPIOA_BASE (APB2PERIPH_BASE + 0x0800)
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C GPIOA->ODR寄存器地址
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define IIC_SCL PCout(12) //SCL
#define IIC_SDA PCout(11) //SDA
#define READ_SDA PCin(11) //输入SDA
具体的偏移量可以在STM32内核手册查到。