gpio 设置流程:
- 设置gpio的时钟
- 设置gpio 的pin角
- 设置gpio pin的功能(速度 输入输出模式 reset)
- 设置gpio data
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_SetBits(GPIOG, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15); //GPIOG13,G14,G15
}
stm32f4xx_rcc.c
//功能为打开或关闭对应的外设输出时钟端口
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
{
assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph)); //参数纠正
assert_param(IS_FUNCTIONAL_STATE(NewState)); //参数验证
/*参考结构体RCC_TypeDef,APB2NR为外设时钟使能寄存器,偏移地址0x18 */
if (NewState != DISABLE)
{
RCC->APB2ENR |= RCC_APB2Periph; //打开对应的外设时钟输出口
}
else
{
RCC->APB2ENR &= ~RCC_APB2Periph;//关闭对应的外设时钟输出口
}
}
#define RCC ((RCC_TypeDef *) RCC_BASE)
#define RCC_BASE (AHB1PERIPH_BASE + 0x3800)
#define AHB1PERIPH_BASE (PERIPH_BASE + 0x00020000)
#define PERIPH_BASE ((uint32_t)0x40000000)
即: RCC bassaddress = 0x40023800
根据映射地址:
AHB1 enable
#define RCC_AHB1Periph_GPIOD ((uint32_t)0x00000008)
#define RCC_AHB1Periph_GPIOE ((uint32_t)0x00000010)
#define RCC_AHB1Periph_GPIOF ((uint32_t)0x00000020)
#define RCC_AHB1Periph_GPIOG ((uint32_t)0x00000040)
#define RCC_AHB1Periph_GPIOH ((uint32_t)0x00000080)
#define RCC_AHB1Periph_GPIOI ((uint32_t)0x00000100)
#define RCC_AHB1Periph_GPIOJ ((uint32_t)0x00000200)
由系统时钟可以看到gpio 是通过ahb给外设分配时钟: