gpio.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. * 2015-03-24 Bright the first version
  13. */
  14. #include <rthw.h>
  15. #include <rtdevice.h>
  16. #include <board.h>
  17. #ifdef RT_USING_PIN
  18. #define STM32F10X_PIN_NUMBERS 100
  19. #define __STM32_PIN(index, rcc, gpio, gpio_index) { 0, RCC_##rcc##Periph_GPIO##gpio, GPIO##gpio, GPIO_Pin_##gpio_index}
  20. #define __STM32_PIN_DEFAULT {-1, 0, 0, 0}
  21. /* STM32 GPIO driver */
  22. struct pin_index
  23. {
  24. int index;
  25. uint32_t rcc;
  26. GPIO_TypeDef *gpio;
  27. uint32_t pin;
  28. };
  29. static const struct pin_index pins[] =
  30. {
  31. #if (STM32F10X_PIN_NUMBERS == 100)
  32. __STM32_PIN_DEFAULT,
  33. __STM32_PIN(1, APB2, E, 2),
  34. __STM32_PIN(2, APB2, E, 3),
  35. __STM32_PIN(3, APB2, E, 4),
  36. __STM32_PIN(4, APB2, E, 5),
  37. __STM32_PIN(5, APB2, E, 6),
  38. __STM32_PIN_DEFAULT,
  39. __STM32_PIN(7, APB2, C, 13),
  40. __STM32_PIN(8, APB2, C, 14),
  41. __STM32_PIN(9, APB2, C, 15),
  42. __STM32_PIN_DEFAULT,
  43. __STM32_PIN_DEFAULT,
  44. __STM32_PIN_DEFAULT,
  45. __STM32_PIN_DEFAULT,
  46. __STM32_PIN_DEFAULT,
  47. __STM32_PIN(15, APB2, C, 0),
  48. __STM32_PIN(16, APB2, C, 1),
  49. __STM32_PIN(17, APB2, C, 2),
  50. __STM32_PIN(18, APB2, C, 3),
  51. __STM32_PIN_DEFAULT,
  52. __STM32_PIN_DEFAULT,
  53. __STM32_PIN_DEFAULT,
  54. __STM32_PIN_DEFAULT,
  55. __STM32_PIN(23, APB2, A, 0),
  56. __STM32_PIN(24, APB2, A, 1),
  57. __STM32_PIN(25, APB2, A, 2),
  58. __STM32_PIN(26, APB2, A, 3),
  59. __STM32_PIN_DEFAULT,
  60. __STM32_PIN_DEFAULT,
  61. __STM32_PIN(29, APB2, A, 4),
  62. __STM32_PIN(30, APB2, A, 5),
  63. __STM32_PIN(31, APB2, A, 6),
  64. __STM32_PIN(32, APB2, A, 7),
  65. __STM32_PIN(33, APB2, C, 4),
  66. __STM32_PIN(34, APB2, C, 5),
  67. __STM32_PIN(35, APB2, B, 0),
  68. __STM32_PIN(36, APB2, B, 1),
  69. __STM32_PIN(37, APB2, B, 2),
  70. __STM32_PIN(38, APB2, E, 7),
  71. __STM32_PIN(39, APB2, E, 8),
  72. __STM32_PIN(40, APB2, E, 9),
  73. __STM32_PIN(41, APB2, E, 10),
  74. __STM32_PIN(42, APB2, E, 11),
  75. __STM32_PIN(43, APB2, E, 12),
  76. __STM32_PIN(44, APB2, E, 13),
  77. __STM32_PIN(45, APB2, E, 14),
  78. __STM32_PIN(46, APB2, E, 15),
  79. __STM32_PIN(47, APB2, B, 10),
  80. __STM32_PIN(48, APB2, B, 11),
  81. __STM32_PIN_DEFAULT,
  82. __STM32_PIN_DEFAULT,
  83. __STM32_PIN(51, APB2, B, 12),
  84. __STM32_PIN(52, APB2, B, 13),
  85. __STM32_PIN(53, APB2, B, 14),
  86. __STM32_PIN(54, APB2, B, 15),
  87. __STM32_PIN(55, APB2, D, 8),
  88. __STM32_PIN(56, APB2, D, 9),
  89. __STM32_PIN(57, APB2, D, 10),
  90. __STM32_PIN(58, APB2, D, 11),
  91. __STM32_PIN(59, APB2, D, 12),
  92. __STM32_PIN(60, APB2, D, 13),
  93. __STM32_PIN(61, APB2, D, 14),
  94. __STM32_PIN(62, APB2, D, 15),
  95. __STM32_PIN(63, APB2, C, 6),
  96. __STM32_PIN(64, APB2, C, 7),
  97. __STM32_PIN(65, APB2, C, 8),
  98. __STM32_PIN(66, APB2, C, 9),
  99. __STM32_PIN(67, APB2, A, 8),
  100. __STM32_PIN(68, APB2, A, 9),
  101. __STM32_PIN(69, APB2, A, 10),
  102. __STM32_PIN(70, APB2, A, 11),
  103. __STM32_PIN(71, APB2, A, 12),
  104. __STM32_PIN(72, APB2, A, 13),
  105. __STM32_PIN_DEFAULT,
  106. __STM32_PIN_DEFAULT,
  107. __STM32_PIN_DEFAULT,
  108. __STM32_PIN(76, APB2, A, 14),
  109. __STM32_PIN(77, APB2, A, 15),
  110. __STM32_PIN(78, APB2, C, 10),
  111. __STM32_PIN(79, APB2, C, 11),
  112. __STM32_PIN(80, APB2, C, 12),
  113. __STM32_PIN(81, APB2, D, 0),
  114. __STM32_PIN(82, APB2, D, 1),
  115. __STM32_PIN(83, APB2, D, 2),
  116. __STM32_PIN(84, APB2, D, 3),
  117. __STM32_PIN(85, APB2, D, 4),
  118. __STM32_PIN(86, APB2, D, 5),
  119. __STM32_PIN(87, APB2, D, 6),
  120. __STM32_PIN(88, APB2, D, 7),
  121. __STM32_PIN(89, APB2, B, 3),
  122. __STM32_PIN(90, APB2, B, 4),
  123. __STM32_PIN(91, APB2, B, 5),
  124. __STM32_PIN(92, APB2, B, 6),
  125. __STM32_PIN(93, APB2, B, 7),
  126. __STM32_PIN_DEFAULT,
  127. __STM32_PIN(95, APB2, B, 8),
  128. __STM32_PIN(96, APB2, B, 9),
  129. __STM32_PIN(97, APB2, E, 0),
  130. __STM32_PIN(98, APB2, E, 1),
  131. __STM32_PIN_DEFAULT,
  132. __STM32_PIN_DEFAULT,
  133. #endif
  134. };
  135. #define ITEM_NUM(items) sizeof(items)/sizeof(items[0])
  136. const struct pin_index *get_pin(uint8_t pin)
  137. {
  138. const struct pin_index *index;
  139. if (pin < ITEM_NUM(pins))
  140. {
  141. index = &pins[pin];
  142. if (index->index == -1)
  143. index = RT_NULL;
  144. }
  145. else
  146. {
  147. index = RT_NULL;
  148. }
  149. return index;
  150. };
  151. void stm32_pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  152. {
  153. const struct pin_index *index;
  154. index = get_pin(pin);
  155. if (index == RT_NULL)
  156. {
  157. return;
  158. }
  159. if (value == PIN_LOW)
  160. {
  161. GPIO_ResetBits(index->gpio, index->pin);
  162. }
  163. else
  164. {
  165. GPIO_SetBits(index->gpio, index->pin);
  166. }
  167. }
  168. int stm32_pin_read(rt_device_t dev, rt_base_t pin)
  169. {
  170. int value;
  171. const struct pin_index *index;
  172. value = PIN_LOW;
  173. index = get_pin(pin);
  174. if (index == RT_NULL)
  175. {
  176. return value;
  177. }
  178. if (GPIO_ReadInputDataBit(index->gpio, index->pin) == Bit_RESET)
  179. {
  180. value = PIN_LOW;
  181. }
  182. else
  183. {
  184. value = PIN_HIGH;
  185. }
  186. return value;
  187. }
  188. void stm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  189. {
  190. const struct pin_index *index;
  191. GPIO_InitTypeDef GPIO_InitStructure;
  192. index = get_pin(pin);
  193. if (index == RT_NULL)
  194. {
  195. return;
  196. }
  197. /* GPIO Periph clock enable */
  198. RCC_APB2PeriphClockCmd(index->rcc, ENABLE);
  199. /* Configure GPIO_InitStructure */
  200. GPIO_InitStructure.GPIO_Pin = index->pin;
  201. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  202. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  203. if (mode == PIN_MODE_OUTPUT)
  204. {
  205. /* output setting */
  206. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  207. }
  208. else if (mode == PIN_MODE_INPUT)
  209. {
  210. /* input setting: not pull. */
  211. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  212. }
  213. else if (mode == PIN_MODE_INPUT_PULLUP)
  214. {
  215. /* input setting: pull up. */
  216. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  217. }
  218. else
  219. {
  220. /* input setting:default. */
  221. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  222. }
  223. GPIO_Init(index->gpio, &GPIO_InitStructure);
  224. }
  225. const static struct rt_pin_ops _stm32_pin_ops =
  226. {
  227. stm32_pin_mode,
  228. stm32_pin_write,
  229. stm32_pin_read,
  230. };
  231. int stm32_hw_pin_init(void)
  232. {
  233. int result;
  234. result = rt_device_pin_register("pin", &_stm32_pin_ops, RT_NULL);
  235. return result;
  236. }
  237. INIT_BOARD_EXPORT(stm32_hw_pin_init);
  238. #endif