gpio.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* 包含头文件-----------------------------------------------------------------*/
  2. #include "gpio.h"
  3. /* 私有宏定义-----------------------------------------------------------------*/
  4. /* GPIO使用到的时钟 */
  5. #define GPIO_USED_CLK (RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC)
  6. /* GPIO基础操作API */
  7. #define GPIO_TOGGLE(port, pin) ((port)->BSRR = (((port)->ODR & (pin)) << 16) | (~(port)->ODR & (pin)))
  8. #define GPIO_SET(port, pin) ((port)->BSRR = (uint32_t)(pin))
  9. #define GPIO_RESET(port, pin) ((port)->BSRR = (uint32_t)(pin) << 16)
  10. #define GPIO_READ(port, pin) (((port)->IDR & (pin)) != 0)
  11. /* 用户实现的GPIO端口和引脚宏定义 */
  12. /* LED_RUN 0点亮 1熄灭 */
  13. #define LED_RUN_GPIO_PORT (GPIOA)
  14. #define LED_RUN_GPIO_PIN (GPIO_Pin_11)
  15. /* TEST_IN */
  16. #define TEST_IN_GPIO_PORT (GPIOA)
  17. #define TEST_IN_GPIO_PIN (GPIO_Pin_2)
  18. /* BT_RST */
  19. #define BT_RST_GPIO_PORT (GPIOC)
  20. #define BT_RST_GPIO_PIN (GPIO_Pin_15)
  21. /* BT_PWR */
  22. #define BT_PWR_GPIO_PORT (GPIOB)
  23. #define BT_PWR_GPIO_PIN (GPIO_Pin_5)
  24. /* 私有类型定义---------------------------------------------------------------*/
  25. /* 私有变量-------------------------------------------------------------------*/
  26. /* 全局变量-------------------------------------------------------------------*/
  27. /* 私有函数原型---------------------------------------------------------------*/
  28. /**
  29. * @brief GPIO初始化
  30. * @param None
  31. * @retval None
  32. */
  33. void gpio_config(void)
  34. {
  35. GPIO_InitTypeDef GPIO_InitStructure;
  36. RCC_APB2PeriphClockCmd(GPIO_USED_CLK, ENABLE); /* 开启GPIO时钟 */
  37. GPIO_InitStructure.GPIO_Pin = LED_RUN_GPIO_PIN; /* 设置引脚号 */
  38. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; /* 设置引脚模式 */
  39. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* 设置引脚速度 */
  40. GPIO_Init(LED_RUN_GPIO_PORT, &GPIO_InitStructure); /* 初始化GPIO */
  41. GPIO_InitStructure.GPIO_Pin = BT_RST_GPIO_PIN;
  42. GPIO_Init(BT_RST_GPIO_PORT, &GPIO_InitStructure);
  43. GPIO_InitStructure.GPIO_Pin = BT_PWR_GPIO_PIN;
  44. GPIO_Init(BT_PWR_GPIO_PORT, &GPIO_InitStructure);
  45. GPIO_InitStructure.GPIO_Pin = TEST_IN_GPIO_PIN;
  46. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  47. GPIO_Init(TEST_IN_GPIO_PORT, &GPIO_InitStructure);
  48. /* gpio default state */
  49. gpio_set(LED_RUN);
  50. gpio_reset(BT_RST);
  51. gpio_set(BT_PWR);
  52. }
  53. /**
  54. * @brief IO置高
  55. * @param gpiox : gpio枚举名称 @ref gpiox_enum_t
  56. * @retval None
  57. */
  58. void gpio_set(uint8_t gpiox)
  59. {
  60. switch(gpiox)
  61. {
  62. case LED_RUN: GPIO_SET(LED_RUN_GPIO_PORT, LED_RUN_GPIO_PIN); break;
  63. case TEST_IN: GPIO_SET(TEST_IN_GPIO_PORT, TEST_IN_GPIO_PIN); break;
  64. case BT_RST: GPIO_SET(BT_RST_GPIO_PORT, BT_RST_GPIO_PIN); break;
  65. case BT_PWR: GPIO_SET(BT_PWR_GPIO_PORT, BT_PWR_GPIO_PIN); break;
  66. }
  67. }
  68. /**
  69. * @brief IO置低
  70. * @param gpiox : gpio枚举名称 @ref gpiox_enum_t
  71. * @retval None
  72. */
  73. void gpio_reset(uint8_t gpiox)
  74. {
  75. switch(gpiox)
  76. {
  77. case LED_RUN: GPIO_RESET(LED_RUN_GPIO_PORT, LED_RUN_GPIO_PIN); break;
  78. case TEST_IN: GPIO_RESET(TEST_IN_GPIO_PORT, TEST_IN_GPIO_PIN); break;
  79. case BT_RST: GPIO_RESET(BT_RST_GPIO_PORT, BT_RST_GPIO_PIN); break;
  80. case BT_PWR: GPIO_RESET(BT_PWR_GPIO_PORT, BT_PWR_GPIO_PIN); break;
  81. }
  82. }
  83. /**
  84. * @brief IO翻转
  85. * @param gpiox : gpio枚举名称 @ref gpiox_enum_t
  86. * @retval None
  87. */
  88. void gpio_toggle(uint8_t gpiox)
  89. {
  90. switch(gpiox)
  91. {
  92. case LED_RUN: GPIO_TOGGLE(LED_RUN_GPIO_PORT, LED_RUN_GPIO_PIN); break;
  93. case TEST_IN: GPIO_TOGGLE(TEST_IN_GPIO_PORT, TEST_IN_GPIO_PIN); break;
  94. case BT_RST: GPIO_TOGGLE(BT_RST_GPIO_PORT, BT_RST_GPIO_PIN); break;
  95. case BT_PWR: GPIO_TOGGLE(BT_PWR_GPIO_PORT, BT_PWR_GPIO_PIN); break;
  96. }
  97. }
  98. /**
  99. * @brief IO读取
  100. * @param gpiox : gpio枚举名称 @ref gpiox_enum_t
  101. * @retval 0:low level 1:high level
  102. */
  103. uint8_t gpio_read(uint8_t gpiox)
  104. {
  105. uint8_t temp = 0;
  106. switch(gpiox)
  107. {
  108. case LED_RUN: temp = GPIO_READ(LED_RUN_GPIO_PORT, LED_RUN_GPIO_PIN); break;
  109. case TEST_IN: temp = GPIO_READ(TEST_IN_GPIO_PORT, TEST_IN_GPIO_PIN); break;
  110. case BT_RST: temp = GPIO_READ(BT_RST_GPIO_PORT, BT_RST_GPIO_PIN); break;
  111. case BT_PWR: temp = GPIO_READ(BT_PWR_GPIO_PORT, BT_PWR_GPIO_PIN); break;
  112. }
  113. return temp;
  114. }