1> 为什么需要位带操作
以点亮LED实验中:读写GPIOF的ODR寄存器为例:
//ODR的bit9清0:
unsigned int *GPIOF_ODR = (unsigned int *)(0x40021400 + 0x14);
*GPIOF_ODR = (*GPIOF_ODR) & (~(1<<9));
分析代码:需要做4步
Step 1> 取ODR地址;
Step 2> 读出ODR值;
Step 3> 运算;
Step 4> 写回ODR;
因读写某位时不能影响其他位,需要4步,有没有简单方法 ?
2> Cortex-M4存储器映射
Figure描述:
位带区(bit band Region):
1> 对4Byte空间读写;
2> 对1bit空间读写;
位带别名区(bit band Alias):
实现位带区的1bit空间读写;
3> 如何实现对1bit空间读写
Figure描述:
位带别名区写1:
unsigned int *room = (unsigned int *)(0x4200 0000);
*room = 1;
就是在位带区0x4000 0000~0x4000 0004 的bit0位 写1;
就这样实现了位读写;
4> 求GPIOF的ODR的bit9的位带别名区地址?
已知:GPIOF的ODR地址为:0x40021400 + 0x14。
step 1> 求GPOIOF的别名区地址:
(0x40021414-0x40000000) * 32 + 0x42000000;
//位带区偏移1Byte,位带别名区偏移32Byte;
step 2> 求bit9地址:
((0x40021414-0x40000000) * 32 + 0x42000000)+ 4 * 9;
//位带区偏移一个bit,位带别名区偏移32bit,为4Byte;
总结计算公式:Peripherals块
位带别名区地址 =
0x42000000 + (位带区地址-0x40000000)* 32 + (位偏移 * 4)
5> 开关LED
// 核心代码
//unsigned int *GPIOF_ODR = (unsigned int *)(0x40021400 + 0x14);
unsigned int *GPIOF_ODR = (unsigned int *)((0x40021400 + 0x14 - 0x40000000)*32 + 0x42000000 + 4 * 9);
while (1) {
// *GPIOF_ODR = (*GPIOF_ODR) & (~(1<<9)); /* 普通读写 */
*GPIOF_ODR = 0x1; /* 位带读写 */
delay(1000000);
//*GPIOF_ODR = (*GPIOF_ODR) | (1<<9); /* 普通读写 */
*GPIOF_ODR = 0x0; /* 位带读写 */
delay(10000000);
}
------------------------------------------------------------------------------------------------------------
*GPIOF_ODR = 0x10?, 最怎样?
发现:要保证位带别名区32位中bit0位是目标值即可;
6> 标准库示例
以RCC相关寄存器为例:
// "stm32f4xx.h"
#define PERIPH_BASE ((uint32_t)0x40000000) // 外设位带区基地址
#define PERIPH_BB_BASE ((uint32_t)0x42000000) // 外设位带别名区基地址
#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000)
#define RCC_BASE (AHB1PERIPH_BASE + 0x3800)
----------------------------------------------------------------------------------------
// "stm32f4xx_rcc.c"
#define RCC_OFFSET (RCC_BASE - PERIPH_BASE)
#define CR_OFFSET (RCC_OFFSET + 0x00)
#define HSION_BitNumber 0x00
#define CR_HSION_BB (PERIPH_BB_BASE + (CR_OFFSET * 32) + (HSION_BitNumber * 4))
----------------------------------------------------------------------------------------
// "stm32f4xx_rcc.c"
void RCC_PLLCmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
*(__IO uint32_t *) CR_PLLON_BB = (uint32_t)NewState; // 使用位带别名区赋值
}