stm32库函数GPIO_Init()解析

来源:互联网 发布:华为交换机mac绑定 编辑:程序博客网 时间:2024/05/21 17:33
GPIO_Init函数是IO引脚的初始化函数,进行个个引脚的初始化配置,主要接受两个参数,一个是配置引脚组(GPIO_TypeDef* GPIOx),一个是配置的参数( GPIO_InitTypeDef* GPIO_InitStruct),具体如下


点击(此处)折叠或打开

  1. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  2. /*其中第一个参数为那组引脚,每组拥有16个引脚,每组都具有不同的寄存器配置地址,第二个参数是一个数据结构,也就是将基本配置信息放在这个数据结构里面,再将这个结构传入函数进行配置*/
  3. //其中数据机构可以表示为如下
  4. typedef struct
  5. {
  6.   uint16_t GPIO_Pin; //引脚号
  7.   GPIOSpeed_TypeDef GPIO_Speed; //配置速度
  8.   GPIOMode_TypeDef GPIO_Mode; //工作模式
  9. }GPIO_InitTypeDef;

  10. //其中配置模式和工作模式为GPIOSpeed_TypeDef和GPIOMode_TypeDef的枚举变量
为了方面的解析这个函数我们需要把几个常量的定义罗列一下

点击(此处)折叠或打开

  1. //首先是引脚定义
  2. #define GPIO_Pin_0 ((uint16_t)0x0001) /* Pin 0 selected */
  3. #define GPIO_Pin_1 ((uint16_t)0x0002) /* Pin 1 selected */
  4. #define GPIO_Pin_2 ((uint16_t)0x0004) /* Pin 2 selected */
  5. #define GPIO_Pin_3 ((uint16_t)0x0008) /* Pin 3 selected */
  6. #define GPIO_Pin_4 ((uint16_t)0x0010) /* Pin 4 selected */
  7. #define GPIO_Pin_5 ((uint16_t)0x0020) /* Pin 5 selected */
  8. #define GPIO_Pin_6 ((uint16_t)0x0040) /* Pin 6 selected */
  9. #define GPIO_Pin_7 ((uint16_t)0x0080) /* Pin 7 selected */
  10. #define GPIO_Pin_8 ((uint16_t)0x0100) /* Pin 8 selected */
  11. #define GPIO_Pin_9 ((uint16_t)0x0200) /* Pin 9 selected */
  12. #define GPIO_Pin_10 ((uint16_t)0x0400) /* Pin 10 selected */
  13. #define GPIO_Pin_11 ((uint16_t)0x0800) /* Pin 11 selected */
  14. #define GPIO_Pin_12 ((uint16_t)0x1000) /* Pin 12 selected */
  15. #define GPIO_Pin_13 ((uint16_t)0x2000) /* Pin 13 selected */
  16. #define GPIO_Pin_14 ((uint16_t)0x4000) /* Pin 14 selected */
  17. #define GPIO_Pin_15 ((uint16_t)0x8000) /* Pin 15 selected */
  18. #define GPIO_Pin_All ((uint16_t)0xFFFF) /* All pins selected */
  19. //其次是模式定义
  20. typedef enum
  21. { GPIO_Mode_AIN = 0x0, //模拟输入
  22. GPIO_Mode_IN_FLOATING = 0x04, //浮空输入模式, 默认
  23. GPIO_Mode_IPD = 0x28, //上拉/下拉输入模式
  24. GPIO_Mode_IPU = 0x48, //保留
  25. GPIO_Mode_Out_OD = 0x14, //通用开漏输出
  26. GPIO_Mode_Out_PP = 0x10, //通用推挽输出
  27. GPIO_Mode_AF_OD = 0x1C, //复用(开漏)输出
  28. GPIO_Mode_AF_PP = 0x18 //复用(推挽)输出
  29. }GPIOMode_TypeDef;
  30. //最后是速度定义
  31. typedef enum
  32. {
  33. GPIO_Speed_10MHz = 1,
  34. GPIO_Speed_2MHz,
  35. GPIO_Speed_50MHz
  36. }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来表示
具体的函数分析如下

点击(此处)折叠或打开

  1. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
  2. {
  3.  /*初始化各个变量*/
  4.   uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
  5.   uint32_t tmpreg = 0x00, pinmask = 0x00;
  6.   //currentmode 用于存放临时的LCIR
  7.   //currentpin 用于存放配置的引脚位
  8.   //pinpos 用于存放当前操作的引脚号
  9.   //pos 存放当前操作的引脚位
  10.   //tmreg 当前的CIR
  11.   //pinmask


  12.   //判断参数
  13.   assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
  14.   assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
  15.   assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin)); 
  16.   
  17.   //取出配置信息里面的模式信息并且取它的低4位
  18.   currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
  19.   if ((((uint32_t)GPIO_OInitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00) //输出模式
  20.   { 

  21.     //判断参数
  22.     assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
  23.     
  24.     //将速度信息放入currentmode低二位
  25.     currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
  26.   }
  27.   if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00) //引脚有定义
  28.   {
  29.     //当前的CRL保存
  30.     tmpreg = GPIOx->CRL;

  31.     //循环低八位引脚
  32.     for (pinpos = 0x00; pinpos < 0x08; pinpos++)
  33.     {
  34.       //当前是那个引脚,那个位置1
  35.       pos = ((uint32_t)0x01) << pinpos;

  36.       //读取引脚信息里面的当前引脚
  37.       currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
  38.       if (currentpin == pos)     //如果当前引脚在配置信息里存在
  39.       {
  40.         pos = pinpos << 2; //pos=引脚号x2
  41.         pinmask = ((uint32_t)0x0F) << pos; //1111<<引脚号x2,根据 CRL 的结构很容易理解
  42.         tmpreg &= ~pinmask; //当前应该操作的CRL位清0
  43.         tmpreg |= (currentmode << pos); //设置当前操作的 CRL 位
  44.         if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD) // 端口置为高电平
  45.         {
  46.           GPIOx->BRR = (((uint32_t)0x01) << pinpos);
  47.         }
  48.         else
  49.         {
  50.           if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)    // 端口清0
  51.           {
  52.             GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
  53.           }
  54.         }
  55.       }
  56.     }
  57.     GPIOx->CRL = tmpreg;
  58.   }
最后就是把配置好的CRL传入CRL寄存器。设置完毕

原创粉丝点击