GPIO_Init() 引脚初始化理解

转载自https://blog.csdn.net/wqx521/article/details/50925659
  1. /* GPIO_InitTypeDef结构体 */

  2. typedef enum

  3. {

  4. GPIO_Speed_10MHz = 1, //枚举常量,值为 1,代表输出速率最高为 10MHz

  5. GPIO_Speed_2MHz, //对不赋值的枚举变量,自动加 1,此常量值为 2

  6. GPIO_Speed_50MHz //常量值为 3

  7. } GPIOSpeed_TypeDef;

  8.  
  9. typedef enum

  10. {

  11. GPIO_Mode_AIN = 0x0, //模拟输入模式

  12. GPIO_Mode_IN_FLOATING = 0x04, //浮空输入模式

  13. GPIO_Mode_IPD = 0x28, //下拉输入模式

  14. GPIO_Mode_IPU = 0x48, //上拉输入模式

  15. GPIO_Mode_Out_OD = 0x14, //开漏输出模式

  16. GPIO_Mode_Out_PP = 0x10, //通用推挽输出模式

  17. GPIO_Mode_AF_OD = 0x1C, //复用功能开漏输出

  18. GPIO_Mode_AF_PP = 0x18 //复用功能推挽输出

  19. } GPIOMode_TypeDef;

  20.  
  21. typedef struct

  22. {

  23. uint16_t GPIO_Pin; /* 指定要配置的引脚 */

  24. GPIOSpeed_TypeDef GPIO_Speed; /* 指定GPIO引脚输出的最高频率 */

  25. GPIOMode_TypeDef GPIO_Mode; /* 指定GPIO引脚工作状态 */

  26. } GPIO_InitTypeDef;

 
  1. /* 初始化GPIO -- GPIO_Init() */

  2. void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)

  3. {

  4. uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;

  5. uint32_t tmpreg = 0x00, pinmask = 0x00;

  6. /* 断言,用于检查输入的参数是否正确 */

  7. assert_param(IS_GPIO_ALL_PERIPH(GPIOx));

  8. assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));

  9. assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));

  10. /*---------------------------- GPIO 的模式配置 -----------------------*/

  11. /*把输入参数 GPIO_Mode 的低四位暂存在 currentmode*/

  12. currentmode = ((uint32_t)GPIO_InitStruct -

  13. > GPIO_Mode) & ((uint32_t)0x0F);

  14. /*判断是否为输出模式,输出模式,可输入参数中输出模式的 bit4 位都是 1*/

  15. if ((((uint32_t)GPIO_InitStruct -

  16. > GPIO_Mode) & ((uint32_t)0x10)) != 0x00)

  17. {

  18. /* 检查输入参数 */

  19. assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));

  20. /* 输出模式,所以要配置 GPIO 的速率:00(输入模式) 01(10MHz) 10(2MHz) 11 */

  21. currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;

  22. }

  23. /*----------------------------配置 GPIO 的 CRL 寄存器 -----------------------

  24. -*/

  25. /* 判断要配置的是否为 pin0 ~~ pin7 */

  26. if (((uint32_t)GPIO_InitStruct -

  27. > GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)

  28. {

  29. /*备份原 CRL 寄存器的值*/

  30. tmpreg = GPIOx->CRL;

  31. /*循环,一个循环设置一个寄存器位*/

  32. for (pinpos = 0x00; pinpos < 0x08; pinpos++)

  33. {

  34. /*pos 的值为 1 左移 pinpos 位*/

  35. pos = ((uint32_t)0x01) << pinpos;

  36. /* 令 pos 与输入参数 GPIO_PIN 作位与运算,为下面的判断作准备 */

  37. currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;

  38. /*判断,若 currentpin=pos,说明 GPIO_PIN 参数中含的第 pos 个引脚需要配置*/

  39. if (currentpin == pos)

  40. {

  41. /*pos 的值左移两位(乘以 4),因为寄存器中 4 个寄存器位配置一个引脚*/

  42. pos = pinpos << 2;

  43. /*以下两个句子,把控制这个引脚的 4 个寄存器位清零,其它寄存器位不变*/

  44. pinmask = ((uint32_t)0x0F) << pos;

  45. tmpreg &= ~pinmask;

  46. /* 向寄存器写入将要配置的引脚的模式 */

  47. tmpreg |= (currentmode << pos);

  48. /* 复位 GPIO 引脚的输入输出默认值*/

  49. /*判断是否为下拉输入模式*/

  50. if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)

  51. {

  52. /*下拉输入模式,引脚默认置 0,对 BRR 寄存器写 1 可对引脚置 0*/

  53. GPIOx->BRR = (((uint32_t)0x01) << pinpos);

  54. }

  55. else

  56. {

  57. /*判断是否为上拉输入模式*/

  58. if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)

  59. {

  60. /*上拉输入模式,引脚默认值为 1,对 BSRR 寄存器写 1 可对引脚置 1*/

  61. GPIOx->BSRR = (((uint32_t)0x01) << pinpos);

  62. }

  63. }

  64. }

  65. }

  66. /*把前面处理后的暂存值写入到 CRL 寄存器之中*/

  67. GPIOx->CRL = tmpreg;

  68. }

  69. /*---------------------------- 以下部分是对 CRH 寄存器配置的 -----------------

  70. --------当要配置的引脚为 pin8 ~~ pin15 的时候,配置 CRH 寄存器, -----

  71. ------------- -----这过程和配置 CRL 寄存器类似------------------------------

  72. ------

  73. -------读者可自行分析,看看自己是否了解了上述过程--^_^-----------*/

  74. /* Configure the eight high port pins */

  75. if (GPIO_InitStruct->GPIO_Pin > 0x00FF)

  76. {

  77. tmpreg = GPIOx->CRH;

  78. for (pinpos = 0x00; pinpos < 0x08; pinpos++)

  79. {

  80. pos = (((uint32_t)0x01) << (pinpos + 0x08));

  81. /* Get the port pins position */

  82. currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);

  83. if (currentpin == pos)

  84. {

  85. pos = pinpos << 2;

  86. /* Clear the corresponding high control register bits */

  87. pinmask = ((uint32_t)0x0F) << pos;

  88. tmpreg &= ~pinmask;

  89. /* Write the mode configuration in the corresponding bits */

  90. tmpreg |= (currentmode << pos);

  91. /* Reset the corresponding ODR bit */

  92. if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)

  93. {

  94. GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));

  95. }

  96. /* Set the corresponding ODR bit */

  97. if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)

  98. {

  99. GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));

  100. }

  101. }

  102. }

  103. GPIOx->CRH = tmpreg;

  104. }

  105. }

猜你喜欢

转载自blog.csdn.net/qq_41883371/article/details/81156467