drv_gpio.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-12-02 Meco Man the first version
  9. */
  10. #include "drv_gpio.h"
  11. #include <board.h>
  12. #include <rtthread.h>
  13. #define GET_GPIOx(pin) ((GPIO_TypeDef *)(rt_uint32_t)(pin & 0xFFFFFFFFULL))
  14. #define GET_GPIOPin(pin) ((uint16_t)(pin >> 32U))
  15. void rt_pin_mode(rt_uint64_t pin, rt_uint8_t mode)
  16. {
  17. GPIO_InitTypeDef GPIO_InitStruct = {0};
  18. GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
  19. uint16_t GPIO_Pin = GET_GPIOPin(pin);
  20. RT_ASSERT(mode == PIN_MODE_OUTPUT || mode == PIN_MODE_INPUT ||
  21. mode == PIN_MODE_INPUT_PULLUP || mode == PIN_MODE_INPUT_PULLDOWN ||
  22. mode == PIN_MODE_OUTPUT_OD);
  23. switch((rt_ubase_t)GPIOx)
  24. {
  25. case (rt_ubase_t)GPIOA:
  26. __HAL_RCC_GPIOA_CLK_ENABLE();
  27. break;
  28. case (rt_ubase_t)GPIOB:
  29. __HAL_RCC_GPIOB_CLK_ENABLE();
  30. break;
  31. case (rt_ubase_t)GPIOC:
  32. __HAL_RCC_GPIOC_CLK_ENABLE();
  33. break;
  34. #ifdef GPIOD
  35. case (rt_ubase_t)GPIOD:
  36. __HAL_RCC_GPIOD_CLK_ENABLE();
  37. break;
  38. #endif /* GPIOD */
  39. #ifdef GPIOE
  40. case (rt_ubase_t)GPIOE:
  41. __HAL_RCC_GPIOE_CLK_ENABLE();
  42. break;
  43. #endif /* GPIOE */
  44. #ifdef GPIOF
  45. case (rt_ubase_t)GPIOF:
  46. __HAL_RCC_GPIOF_CLK_ENABLE();
  47. break;
  48. #endif /* GPIOF */
  49. #ifdef GPIOG
  50. case (rt_ubase_t)GPIOG:
  51. __HAL_RCC_GPIOG_CLK_ENABLE();
  52. break;
  53. #endif /* GPIOG */
  54. #ifdef GPIOH
  55. case (rt_ubase_t)GPIOH:
  56. __HAL_RCC_GPIOH_CLK_ENABLE();
  57. break;
  58. #endif /* GPIOH */
  59. #ifdef GPIOI
  60. case (rt_ubase_t)GPIOI:
  61. __HAL_RCC_GPIOI_CLK_ENABLE();
  62. break;
  63. #endif /* GPIOI */
  64. }
  65. GPIO_InitStruct.Pin = GPIO_Pin;
  66. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  67. if (mode == PIN_MODE_OUTPUT)
  68. {
  69. /* output setting */
  70. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  71. GPIO_InitStruct.Pull = GPIO_NOPULL;
  72. }
  73. else if (mode == PIN_MODE_INPUT)
  74. {
  75. /* input setting: not pull. */
  76. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  77. GPIO_InitStruct.Pull = GPIO_NOPULL;
  78. }
  79. else if (mode == PIN_MODE_INPUT_PULLUP)
  80. {
  81. /* input setting: pull up. */
  82. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  83. GPIO_InitStruct.Pull = GPIO_PULLUP;
  84. }
  85. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  86. {
  87. /* input setting: pull down. */
  88. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  89. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  90. }
  91. else if (mode == PIN_MODE_OUTPUT_OD)
  92. {
  93. /* output setting: od. */
  94. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  95. GPIO_InitStruct.Pull = GPIO_NOPULL;
  96. }
  97. HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);
  98. }
  99. void rt_pin_write(rt_uint64_t pin, rt_uint8_t value)
  100. {
  101. GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
  102. uint16_t GPIO_Pin = GET_GPIOPin(pin);
  103. RT_ASSERT(value == PIN_LOW || value == PIN_HIGH);
  104. HAL_GPIO_WritePin(GPIOx, GPIO_Pin, (value == PIN_LOW) ? GPIO_PIN_RESET : GPIO_PIN_SET);
  105. }
  106. rt_int8_t rt_pin_read(rt_uint64_t pin)
  107. {
  108. GPIO_TypeDef *GPIOx = GET_GPIOx(pin);
  109. uint16_t GPIO_Pin = GET_GPIOPin(pin);
  110. return HAL_GPIO_ReadPin(GPIOx, GPIO_Pin) == GPIO_PIN_RESET ? PIN_LOW : PIN_HIGH;
  111. }