gpio.c 5.2 KB

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