stm32库函数之GPIO_Init()详细解析
GPIO_Init函数是IO引脚的初始化函数,进⾏个个引脚的初始化配置,主要接受两个参数,⼀个是配置引脚组(GPIO_TypeDef* GPIOx),⼀个是配置的参数
( GPIO_InitTypeDef* GPIO_InitStruct),具体如下
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
/*其中第⼀个参数为那组引脚,每组拥有16个引脚,每组都具有不同的寄存器配置地址,第⼆个参数是⼀个数据结构,也就是将基本配置信息放在这个数据结构⾥⾯,再将这个结构传⼊函数进⾏配置*/
//其中数据机构可以表⽰为如下
typedef struct
{
uint16_t GPIO_Pin; //引脚号
GPIOSpeed_TypeDef GPIO_Speed; //配置速度
GPIOMode_TypeDef GPIO_Mode; //⼯作模式
}GPIO_InitTypeDef;
//其中配置模式和⼯作模式为GPIOSpeed_TypeDef和GPIOMode_TypeDef的枚举变量
为了⽅⾯的解析这个函数我们需要把⼏个常量的定义罗列⼀下
//⾸先是引脚定义
#define GPIO_Pin_0 ((uint16_t)0x0001) /* Pin 0 selected */
#define GPIO_Pin_1 ((uint16_t)0x0002) /* Pin 1 selected */
#define GPIO_Pin_2 ((uint16_t)0x0004) /* Pin 2 selected */
#define GPIO_Pin_3 ((uint16_t)0x0008) /* Pin 3 selected */
#define GPIO_Pin_4 ((uint16_t)0x0010) /* Pin 4 selected */
#define GPIO_Pin_5 ((uint16_t)0x0020) /* Pin 5 selected */
#define GPIO_Pin_6 ((uint16_t)0x0040) /* Pin 6 selected */
#define GPIO_Pin_7 ((uint16_t)0x0080) /* Pin 7 selected */
#define GPIO_Pin_8 ((uint16_t)0x0100) /* Pin 8 selected */
#define GPIO_Pin_9 ((uint16_t)0x0200) /* Pin 9 selected */
#define GPIO_Pin_10 ((uint16_t)0x0400) /* Pin 10 selected */
#define GPIO_Pin_11 ((uint16_t)0x0800) /* Pin 11 selected */
enum函数#define GPIO_Pin_12 ((uint16_t)0x1000) /* Pin 12 selected */
#define GPIO_Pin_13 ((uint16_t)0x2000) /* Pin 13 selected */
#define GPIO_Pin_14 ((uint16_t)0x4000) /* Pin 14 selected */
#define GPIO_Pin_15 ((uint16_t)0x8000) /* Pin 15 selected */
#define GPIO_Pin_All ((uint16_t)0xFFFF) /* All pins selected */
/
/其次是模式定义
typedef enum
{ GPIO_Mode_AIN = 0x0,    //模拟输⼊
GPIO_Mode_IN_FLOATING = 0x04,    //浮空输⼊模式,默认
GPIO_Mode_IPD = 0x28,    //下拉输⼊模式
GPIO_Mode_IPU = 0x48,    //上拉输⼊模式
GPIO_Mode_Out_OD = 0x14,    //通⽤开漏输出
GPIO_Mode_Out_PP = 0x10,    //通⽤推挽输出
GPIO_Mode_AF_OD = 0x1C,    //复⽤(开漏)输出
GPIO_Mode_AF_PP = 0x18//复⽤(推挽)输出
}GPIOMode_TypeDef;
/
/最后是速度定义
typedef enum
{
GPIO_Speed_10MHz = 1,
GPIO_Speed_2MHz,
GPIO_Speed_50MHz
}GPIOSpeed_TypeDef;
其中引脚定义很容以看出引脚0则16位的第0位置1 ,引脚为2则第2位置1,⼀次类推,所以如果要定义多个引脚只需要使⽤或逻辑运算  " | "
其次模式定义也有他的规律,参考《stmf10xxx参考⼿册》可以得出知道存储的⾼低配置寄存器中每4位表达⼀个模式+速度,其中模式占⾼2位,速度占低2位,16个引脚就拥有4*16=64位来存储,所以这样定义就有它的道理,原因就是模式占⾼位例如10表⽰上拉/下拉输出模式,则在4位中占⾼2位,就应该
是1000,低2位先⽤00表⽰,这样的话上拉/下拉输出模式就可以表⽰为1000=0x8,或者⽤0x28的第四位表⽰也可以,其它也是⼀样,就是⾥⾯的第五位1表⽰输出模式,0表⽰输⼊模式。
速度就直接⽤1,2,3来表⽰
具体的函数分析如下
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
/*初始化各个变量*/
uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
uint32_t tmpreg = 0x00, pinmask = 0x00;
//currentmode ⽤于存放临时的GPIO_Mode、GPIO_Speed的数据
//currentpin ⽤于存放配置的引脚位
//pinpos ⽤于存放当前操作的引脚号
//pos 存放当前操作的引脚位
//tmreg 当前的CIR
//pinmask
//判断参数
assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
//读取并保存配置的GPIO_Mode的数据并且只取它的低4位(参考CRL、CRH寄存器就理解了),屏蔽掉⾼4位
currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
if ((((uint32_t)GPIO_OInitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00) //判断是否为输出模式(根据那8种模式的定义可知,输出模式的值分别为:0x14、0x10、0x1C、0x18)  {
//判断参数
assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
//将配置的GPIO_Speed信息放⼊currentmode低⼆位
currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
}
if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00) //判断引脚是否有定义,并且端⼝号为0~7
{
tmpreg = GPIOx->CRL;//读出CRL寄存器中的值,并保存
//循环低⼋位引脚
for (pinpos = 0x00; pinpos < 0x08; pinpos++)
{
//当前是那个引脚,那个位置1
pos = ((uint32_t)0x01) << pinpos;
currentpin = (GPIO_InitStruct->GPIO_Pin) & pos; //读取引脚信息⾥⾯的当前引脚,即传进来的GPIO_Pin_X(X为0~7)
if (currentpin == pos)    //如果当前引脚在配置信息⾥存在
{
pos = pinpos << 2; //pos = pinpos *4,即pos = 引脚号x4,因为每个引脚配置占4位
pinmask = ((uint32_t)0x0F) << pos; // 0000_1111<<;引脚号x4,根据 CRL 的结构很容易理解
tmpreg &= ~pinmask; //当前应该操作的CRL位清0
tmpreg |= (currentmode << pos); //设置当前操作的 CRL 位,(最终只需移动保存的GPIO_Mode、GPIO_Speed的数据)if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD) // 如果是下拉模式,相应的引脚号清零
{
GPIOx->BRR = (((uint32_t)0x01) << pinpos);  //清除对应的 ODRy为0(ODR 为端⼝输出数据寄存器)
}
else
{
if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)    //如果是上拉模式,相应的引脚号置1,即⾼电平
{
GPIOx->BSRR = (((uint32_t)0x01) << pinpos);  //设置对应的 ODRy为1
}
}
}
}
GPIOx->CRL = tmpreg; //最后就是把配置好的数据写⼊CRL寄存器,设置完毕
}
配置⾼⼋位的引脚道理也是⼀样的——————————————————————————————————————————————————————————————在此感谢原作者!