gpio.c 5.2 KB

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