gpio.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016-08-29 Aubr.Cool the first version
  9. */
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #include <board.h>
  13. #ifdef RT_USING_PIN
  14. #include "stm32l0xx.h"
  15. #define __RCC_GPIO_CLK_ENABLE(BIT) do { \
  16. __IO uint32_t tmpreg; \
  17. SET_BIT(RCC->IOPENR, BIT);\
  18. /* Delay after an RCC peripheral clock enabling */ \
  19. tmpreg = READ_BIT(RCC->IOPENR, RCC_IOPENR_GPIOCEN);\
  20. UNUSED(tmpreg); \
  21. } while(0)
  22. #define __RCC_GPIO_CLK_DISABLE(BIT) CLEAR_BIT(RCC->IOPENR, BIT)
  23. #define __STM32_PIN(index, gpio, gpio_index) \
  24. {index, RCC_IOPENR_GPIO##gpio##EN, GPIO##gpio, GPIO_PIN_##gpio_index}
  25. #define __STM32_PIN_DEFAULT {-1, 0, 0}
  26. /* STM32 GPIO driver */
  27. struct pin_index
  28. {
  29. int index;
  30. unsigned int clk;
  31. GPIO_TypeDef *gpio;
  32. uint32_t pin;
  33. };
  34. static const struct pin_index pins[] =
  35. {
  36. #ifdef UFQFPN32
  37. __STM32_PIN_DEFAULT,
  38. __STM32_PIN(1, C, 14),
  39. __STM32_PIN(2, C, 15),
  40. __STM32_PIN_DEFAULT,
  41. __STM32_PIN_DEFAULT,
  42. __STM32_PIN_DEFAULT,
  43. __STM32_PIN(6, A, 0),
  44. __STM32_PIN(7, A, 1),
  45. __STM32_PIN(8, A, 2),
  46. __STM32_PIN(9, A, 3),
  47. __STM32_PIN(10, A, 4),
  48. __STM32_PIN(11, A, 5),
  49. __STM32_PIN(12, A, 6),
  50. __STM32_PIN(13, A, 7),
  51. __STM32_PIN(14, B, 0),
  52. __STM32_PIN(15, B, 1),
  53. __STM32_PIN_DEFAULT,
  54. __STM32_PIN_DEFAULT,
  55. __STM32_PIN(18, A, 8),
  56. __STM32_PIN(19, A, 9),
  57. __STM32_PIN(20, A, 10),
  58. __STM32_PIN(21, A, 11),
  59. __STM32_PIN(22, A, 12),
  60. __STM32_PIN(23, A, 13),
  61. __STM32_PIN_DEFAULT,
  62. __STM32_PIN(25, A, 14),
  63. __STM32_PIN(26, B, 4),
  64. __STM32_PIN(27, B, 5),
  65. __STM32_PIN(28, B, 6),
  66. __STM32_PIN(29, B, 7),
  67. __STM32_PIN_DEFAULT,
  68. __STM32_PIN_DEFAULT,
  69. __STM32_PIN_DEFAULT,
  70. #endif
  71. };
  72. #define ITEM_NUM(items) sizeof(items)/sizeof(items[0])
  73. const struct pin_index *get_pin(uint8_t pin)
  74. {
  75. const struct pin_index *index;
  76. if (pin < ITEM_NUM(pins))
  77. {
  78. index = &pins[pin];
  79. if (index->index == -1)
  80. index = RT_NULL;
  81. }
  82. else
  83. {
  84. index = RT_NULL;
  85. }
  86. return index;
  87. };
  88. inline void stm32_pin_write_early(rt_base_t pin, rt_base_t value)
  89. {
  90. const struct pin_index *index;
  91. index = get_pin(pin);
  92. if (index == RT_NULL)
  93. {
  94. return;
  95. }
  96. HAL_GPIO_WritePin(index->gpio, index->pin, value);
  97. }
  98. void stm32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  99. {
  100. stm32_pin_write_early(pin, value);
  101. }
  102. inline int stm32_pin_read_early(rt_base_t pin)
  103. {
  104. int value;
  105. const struct pin_index *index;
  106. value = PIN_LOW;
  107. index = get_pin(pin);
  108. if (index == RT_NULL)
  109. {
  110. return value;
  111. }
  112. value = HAL_GPIO_ReadPin(index->gpio, index->pin);
  113. return value;
  114. }
  115. int stm32_pin_read(rt_device_t dev, rt_base_t pin)
  116. {
  117. return stm32_pin_read_early(pin);
  118. }
  119. void stm32_pin_mode_early(rt_base_t pin, rt_base_t mode)
  120. {
  121. const struct pin_index *index;
  122. GPIO_InitTypeDef GPIO_InitStructure;
  123. index = get_pin(pin);
  124. if (index == RT_NULL)
  125. {
  126. return;
  127. }
  128. /* Configure GPIO_InitStructure */
  129. GPIO_InitStructure.Pin = index->pin;
  130. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  131. GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  132. __RCC_GPIO_CLK_ENABLE(index->clk);
  133. if (mode == GPIO_MODE_OUTPUT_PP)
  134. {
  135. /* output setting */
  136. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
  137. GPIO_InitStructure.Pull = GPIO_NOPULL;
  138. } else if(mode == GPIO_MODE_OUTPUT_OD)
  139. {
  140. GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;
  141. GPIO_InitStructure.Pull = GPIO_NOPULL;
  142. }
  143. else if (mode == GPIO_MODE_INPUT)
  144. {
  145. /* input setting: not pull. */
  146. GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
  147. GPIO_InitStructure.Pull = GPIO_PULLUP;
  148. } else if(((mode & 0xFF) == GPIO_MODE_AF_PP) ||
  149. ((mode & 0xFF) == GPIO_MODE_AF_OD)
  150. ) {
  151. GPIO_InitStructure.Mode = (mode & ~0xFF00);
  152. GPIO_InitStructure.Pull = GPIO_NOPULL;
  153. GPIO_InitStructure.Alternate = (mode & 0xFF00) >> 8;
  154. } else if(mode == GPIO_MODE_ANALOG){
  155. GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
  156. } else {
  157. /* input setting:default. */
  158. GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
  159. GPIO_InitStructure.Pull = GPIO_NOPULL;
  160. }
  161. HAL_GPIO_Init(index->gpio, &GPIO_InitStructure);
  162. }
  163. void stm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  164. {
  165. stm32_pin_mode_early(pin, mode);
  166. }
  167. const static struct rt_pin_ops _stm32_pin_ops =
  168. {
  169. stm32_pin_mode,
  170. stm32_pin_write,
  171. stm32_pin_read,
  172. };
  173. int stm32_hw_pin_init(void)
  174. {
  175. int result;
  176. result = rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);
  177. return result;
  178. }
  179. INIT_BOARD_EXPORT(stm32_hw_pin_init);
  180. #endif