1
0

drv_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-08-26 AisinoChip first version
  9. * 2021-10-15 AisinoChip add special pin setting
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <rtdevice.h>
  14. #include <rtconfig.h>
  15. #include "board.h"
  16. #ifdef RT_USING_PIN
  17. #include <rtdevice.h>
  18. #define __ACM32_PIN(index, gpio, gpio_index) \
  19. { \
  20. index, GPIO##gpio, GPIO_PIN_##gpio_index \
  21. }
  22. #define __ACM32_PIN_RESERVE \
  23. { \
  24. -1, 0, 0 \
  25. }
  26. /* ACM32 GPIO driver */
  27. struct pin_index
  28. {
  29. int index;
  30. enum_GPIOx_t gpio;
  31. uint32_t pin;
  32. };
  33. struct pin_irq_map
  34. {
  35. rt_uint16_t line;
  36. EXTI_HandleTypeDef handle;
  37. };
  38. static const struct pin_index pins[] =
  39. {
  40. #if defined(BSP_USING_GPIO1)
  41. __ACM32_PIN(0, A, 0),
  42. __ACM32_PIN(1, A, 1),
  43. __ACM32_PIN(2, A, 2),
  44. __ACM32_PIN(3, A, 3),
  45. __ACM32_PIN(4, A, 4),
  46. __ACM32_PIN(5, A, 5),
  47. __ACM32_PIN(6, A, 6),
  48. __ACM32_PIN(7, A, 7),
  49. __ACM32_PIN(8, A, 8),
  50. __ACM32_PIN(9, A, 9),
  51. __ACM32_PIN(10, A, 10),
  52. __ACM32_PIN(11, A, 11),
  53. __ACM32_PIN(12, A, 12),
  54. __ACM32_PIN(13, A, 13),
  55. __ACM32_PIN(14, A, 14),
  56. __ACM32_PIN(15, A, 15),
  57. __ACM32_PIN(16, B, 0),
  58. __ACM32_PIN(17, B, 1),
  59. __ACM32_PIN(18, B, 2),
  60. __ACM32_PIN(19, B, 3),
  61. __ACM32_PIN(20, B, 4),
  62. __ACM32_PIN(21, B, 5),
  63. __ACM32_PIN(22, B, 6),
  64. __ACM32_PIN(23, B, 7),
  65. __ACM32_PIN(24, B, 8),
  66. __ACM32_PIN(25, B, 9),
  67. __ACM32_PIN(26, B, 10),
  68. __ACM32_PIN(27, B, 11),
  69. __ACM32_PIN(28, B, 12),
  70. __ACM32_PIN(29, B, 13),
  71. __ACM32_PIN(30, B, 14),
  72. __ACM32_PIN(31, B, 15),
  73. #if defined(BSP_USING_GPIO2)
  74. __ACM32_PIN(32, C, 0),
  75. __ACM32_PIN(33, C, 1),
  76. __ACM32_PIN(34, C, 2),
  77. __ACM32_PIN(35, C, 3),
  78. __ACM32_PIN(36, C, 4),
  79. __ACM32_PIN(37, C, 5),
  80. __ACM32_PIN(38, C, 6),
  81. __ACM32_PIN(39, C, 7),
  82. __ACM32_PIN(40, C, 8),
  83. __ACM32_PIN(41, C, 9),
  84. __ACM32_PIN(42, C, 10),
  85. __ACM32_PIN(43, C, 11),
  86. __ACM32_PIN(44, C, 12),
  87. __ACM32_PIN(45, C, 13),
  88. __ACM32_PIN(46, C, 14),
  89. __ACM32_PIN(47, C, 15),
  90. __ACM32_PIN(48, D, 0),
  91. __ACM32_PIN(49, D, 1),
  92. __ACM32_PIN(50, D, 2),
  93. __ACM32_PIN(51, D, 3),
  94. __ACM32_PIN(52, D, 4),
  95. __ACM32_PIN(53, D, 5),
  96. __ACM32_PIN(54, D, 6),
  97. __ACM32_PIN(55, D, 7),
  98. __ACM32_PIN(56, D, 8),
  99. __ACM32_PIN(57, D, 9),
  100. __ACM32_PIN(58, D, 10),
  101. __ACM32_PIN(59, D, 11),
  102. __ACM32_PIN(60, D, 12),
  103. __ACM32_PIN(61, D, 13),
  104. __ACM32_PIN(62, D, 14),
  105. __ACM32_PIN(63, D, 15),
  106. #endif /* defined(BSP_USING_GPIO2) */
  107. #endif /* defined(BSP_USING_GPIO1) */
  108. };
  109. static struct pin_irq_map pin_irq_map[] =
  110. {
  111. {EXTI_LINE_0, {0}},
  112. {EXTI_LINE_1, {0}},
  113. {EXTI_LINE_2, {0}},
  114. {EXTI_LINE_3, {0}},
  115. {EXTI_LINE_4, {0}},
  116. {EXTI_LINE_5, {0}},
  117. {EXTI_LINE_6, {0}},
  118. {EXTI_LINE_7, {0}},
  119. {EXTI_LINE_8, {0}},
  120. {EXTI_LINE_9, {0}},
  121. {EXTI_LINE_10, {0}},
  122. {EXTI_LINE_11, {0}},
  123. {EXTI_LINE_12, {0}},
  124. {EXTI_LINE_13, {0}},
  125. {EXTI_LINE_14, {0}},
  126. {EXTI_LINE_15, {0}},
  127. };
  128. static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
  129. {
  130. {-1, 0, RT_NULL, RT_NULL},
  131. {-1, 0, RT_NULL, RT_NULL},
  132. {-1, 0, RT_NULL, RT_NULL},
  133. {-1, 0, RT_NULL, RT_NULL},
  134. {-1, 0, RT_NULL, RT_NULL},
  135. {-1, 0, RT_NULL, RT_NULL},
  136. {-1, 0, RT_NULL, RT_NULL},
  137. {-1, 0, RT_NULL, RT_NULL},
  138. {-1, 0, RT_NULL, RT_NULL},
  139. {-1, 0, RT_NULL, RT_NULL},
  140. {-1, 0, RT_NULL, RT_NULL},
  141. {-1, 0, RT_NULL, RT_NULL},
  142. {-1, 0, RT_NULL, RT_NULL},
  143. {-1, 0, RT_NULL, RT_NULL},
  144. {-1, 0, RT_NULL, RT_NULL},
  145. {-1, 0, RT_NULL, RT_NULL},
  146. };
  147. static uint32_t pin_irq_enable_mask = 0;
  148. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  149. static const struct pin_index *get_pin(uint8_t pin)
  150. {
  151. const struct pin_index *index;
  152. if (pin < ITEM_NUM(pins))
  153. {
  154. index = &pins[pin];
  155. if (index->index == -1)
  156. index = RT_NULL;
  157. }
  158. else
  159. {
  160. index = RT_NULL;
  161. }
  162. return index;
  163. };
  164. static void acm32_pin_write(rt_device_t dev, rt_base_t pin, rt_uint8_t value)
  165. {
  166. const struct pin_index *index;
  167. index = get_pin(pin);
  168. if (index == RT_NULL)
  169. {
  170. return;
  171. }
  172. HAL_GPIO_WritePin(index->gpio, index->pin, (enum_PinState_t)value);
  173. }
  174. static rt_ssize_t acm32_pin_read(rt_device_t dev, rt_base_t pin)
  175. {
  176. int value;
  177. const struct pin_index *index;
  178. value = PIN_LOW;
  179. index = get_pin(pin);
  180. if (index == RT_NULL)
  181. {
  182. return value;
  183. }
  184. value = HAL_GPIO_ReadPin(index->gpio, index->pin);
  185. return value;
  186. }
  187. static void acm32_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
  188. {
  189. const struct pin_index *index;
  190. GPIO_InitTypeDef GPIO_InitStruct;
  191. index = get_pin(pin);
  192. if (index == RT_NULL)
  193. {
  194. return;
  195. }
  196. /* Configure GPIO_InitStructure */
  197. GPIO_InitStruct.Pin = index->pin;
  198. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  199. GPIO_InitStruct.Pull = GPIO_NOPULL;
  200. GPIO_InitStruct.Alternate = GPIO_FUNCTION_0;
  201. if (mode == PIN_MODE_OUTPUT)
  202. {
  203. /* output setting */
  204. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  205. GPIO_InitStruct.Pull = GPIO_NOPULL;
  206. }
  207. else if (mode == PIN_MODE_INPUT)
  208. {
  209. /* input setting: not pull. */
  210. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  211. GPIO_InitStruct.Pull = GPIO_NOPULL;
  212. }
  213. else if (mode == PIN_MODE_INPUT_PULLUP)
  214. {
  215. /* input setting: pull up. */
  216. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  217. GPIO_InitStruct.Pull = GPIO_PULLUP;
  218. }
  219. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  220. {
  221. /* input setting: pull down. */
  222. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  223. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  224. }
  225. else if (mode == PIN_MODE_OUTPUT_OD)
  226. {
  227. /* output setting: od. */
  228. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  229. GPIO_InitStruct.Pull = GPIO_NOPULL;
  230. }
  231. /* special PIN process */
  232. if(index->gpio == GPIOC && index->pin == GPIO_PIN_13)
  233. {
  234. __HAL_RTC_PC13_DIGIT();
  235. __HAL_RTC_PC13_SEL(0); /* GPIO function */
  236. __HAL_RTC_PC13_PULL_DOWN_DISABLE();
  237. __HAL_RTC_PC13_PULL_UP_DISABLE();
  238. if(GPIO_InitStruct.Pull == GPIO_PULLUP)
  239. {
  240. __HAL_RTC_PC13_PULL_UP_ENABLE();
  241. }
  242. else if(GPIO_InitStruct.Pull == GPIO_PULLDOWN)
  243. {
  244. __HAL_RTC_PC13_PULL_DOWN_ENABLE();
  245. }
  246. }
  247. if(index->gpio == GPIOC && index->pin == GPIO_PIN_14)
  248. {
  249. __HAL_RTC_PC14_DIGIT();
  250. __HAL_RTC_PC14_SEL(0); /* GPIO function */
  251. __HAL_RTC_PC14_PULL_DOWN_DISABLE();
  252. __HAL_RTC_PC14_PULL_UP_DISABLE();
  253. if(GPIO_InitStruct.Pull == GPIO_PULLUP)
  254. {
  255. __HAL_RTC_PC14_PULL_UP_ENABLE();
  256. }
  257. else if(GPIO_InitStruct.Pull == GPIO_PULLDOWN)
  258. {
  259. __HAL_RTC_PC14_PULL_DOWN_ENABLE();
  260. }
  261. }
  262. if(index->gpio == GPIOC && index->pin == GPIO_PIN_15)
  263. {
  264. __HAL_RTC_PC15_DIGIT();
  265. __HAL_RTC_PC15_SEL(0); /* GPIO function */
  266. __HAL_RTC_PC15_PULL_DOWN_DISABLE();
  267. __HAL_RTC_PC15_PULL_UP_DISABLE();
  268. if(GPIO_InitStruct.Pull == GPIO_PULLUP)
  269. {
  270. __HAL_RTC_PC15_PULL_UP_ENABLE();
  271. }
  272. else if(GPIO_InitStruct.Pull == GPIO_PULLDOWN)
  273. {
  274. __HAL_RTC_PC15_PULL_DOWN_ENABLE();
  275. }
  276. }
  277. HAL_GPIO_Init(index->gpio, &GPIO_InitStruct);
  278. }
  279. #define PIN2INDEX(pin) ((pin) % 16)
  280. static rt_err_t acm32_pin_attach_irq(struct rt_device *device, rt_base_t pin,
  281. rt_uint8_t mode, void (*hdr)(void *args), void *args)
  282. {
  283. const struct pin_index *index;
  284. rt_base_t level;
  285. rt_int32_t irqindex = -1;
  286. index = get_pin(pin);
  287. if (index == RT_NULL)
  288. {
  289. return -RT_ENOSYS;
  290. }
  291. irqindex = PIN2INDEX(pin);
  292. level = rt_hw_interrupt_disable();
  293. if (pin_irq_hdr_tab[irqindex].pin == pin &&
  294. pin_irq_hdr_tab[irqindex].hdr == hdr &&
  295. pin_irq_hdr_tab[irqindex].mode == mode &&
  296. pin_irq_hdr_tab[irqindex].args == args)
  297. {
  298. rt_hw_interrupt_enable(level);
  299. return RT_EOK;
  300. }
  301. if (pin_irq_hdr_tab[irqindex].pin != -1)
  302. {
  303. rt_hw_interrupt_enable(level);
  304. return -RT_EBUSY;
  305. }
  306. pin_irq_hdr_tab[irqindex].pin = pin;
  307. pin_irq_hdr_tab[irqindex].hdr = hdr;
  308. pin_irq_hdr_tab[irqindex].mode = mode;
  309. pin_irq_hdr_tab[irqindex].args = args;
  310. rt_hw_interrupt_enable(level);
  311. return RT_EOK;
  312. }
  313. static rt_err_t acm32_pin_dettach_irq(struct rt_device *device, rt_base_t pin)
  314. {
  315. const struct pin_index *index;
  316. rt_base_t level;
  317. rt_int32_t irqindex = -1;
  318. index = get_pin(pin);
  319. if (index == RT_NULL)
  320. {
  321. return -RT_ENOSYS;
  322. }
  323. irqindex = PIN2INDEX(pin);
  324. level = rt_hw_interrupt_disable();
  325. if (pin_irq_hdr_tab[irqindex].pin == -1)
  326. {
  327. rt_hw_interrupt_enable(level);
  328. return RT_EOK;
  329. }
  330. pin_irq_hdr_tab[irqindex].pin = -1;
  331. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  332. pin_irq_hdr_tab[irqindex].mode = 0;
  333. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  334. rt_hw_interrupt_enable(level);
  335. return RT_EOK;
  336. }
  337. static rt_err_t acm32_pin_irq_enable(struct rt_device *device, rt_base_t pin,
  338. rt_uint8_t enabled)
  339. {
  340. const struct pin_index *index;
  341. struct pin_irq_map *irqmap;
  342. rt_base_t level;
  343. rt_int32_t irqindex = -1;
  344. GPIO_InitTypeDef GPIO_InitStruct;
  345. index = get_pin(pin);
  346. if (index == RT_NULL)
  347. {
  348. return -RT_ENOSYS;
  349. }
  350. irqindex = PIN2INDEX(pin);
  351. irqmap = &pin_irq_map[irqindex];
  352. if (enabled == PIN_IRQ_ENABLE)
  353. {
  354. level = rt_hw_interrupt_disable();
  355. if (pin_irq_hdr_tab[irqindex].pin == -1)
  356. {
  357. rt_hw_interrupt_enable(level);
  358. return -RT_ENOSYS;
  359. }
  360. System_Module_Enable(EN_EXTI);
  361. /* Configure GPIO_InitStructure */
  362. GPIO_InitStruct.Pin = index->pin;
  363. GPIO_InitStruct.Alternate = GPIO_FUNCTION_0;
  364. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  365. irqmap->handle.u32_Line = irqmap->line;
  366. irqmap->handle.u32_Mode = EXTI_MODE_INTERRUPT;
  367. switch (pin_irq_hdr_tab[irqindex].mode)
  368. {
  369. case PIN_IRQ_MODE_RISING:
  370. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  371. irqmap->handle.u32_Trigger = EXTI_TRIGGER_RISING;
  372. break;
  373. case PIN_IRQ_MODE_FALLING:
  374. GPIO_InitStruct.Pull = GPIO_PULLUP;
  375. irqmap->handle.u32_Trigger = EXTI_TRIGGER_FALLING;
  376. break;
  377. case PIN_IRQ_MODE_RISING_FALLING:
  378. GPIO_InitStruct.Pull = GPIO_NOPULL;
  379. irqmap->handle.u32_Trigger = EXTI_TRIGGER_RISING_FALLING;
  380. break;
  381. }
  382. HAL_GPIO_Init(index->gpio, &GPIO_InitStruct);
  383. irqmap->handle.u32_GPIOSel = pin / 16;
  384. HAL_EXTI_SetConfigLine(&irqmap->handle);
  385. pin_irq_enable_mask |= 1 << irqindex;
  386. rt_hw_interrupt_enable(level);
  387. }
  388. else if (enabled == PIN_IRQ_DISABLE)
  389. {
  390. if ((pin_irq_enable_mask & (1 << irqindex)) == 0)
  391. {
  392. return -RT_ENOSYS;
  393. }
  394. level = rt_hw_interrupt_disable();
  395. EXTI->IENR &= ~irqmap->line;
  396. EXTI->EENR &= ~irqmap->line;
  397. rt_hw_interrupt_enable(level);
  398. }
  399. else
  400. {
  401. return -RT_ENOSYS;
  402. }
  403. return RT_EOK;
  404. }
  405. const static struct rt_pin_ops _acm32_pin_ops =
  406. {
  407. acm32_pin_mode,
  408. acm32_pin_write,
  409. acm32_pin_read,
  410. acm32_pin_attach_irq,
  411. acm32_pin_dettach_irq,
  412. acm32_pin_irq_enable,
  413. };
  414. rt_inline void pin_irq_hdr(int irqno)
  415. {
  416. if (pin_irq_hdr_tab[irqno].hdr)
  417. {
  418. pin_irq_hdr_tab[irqno].hdr(pin_irq_hdr_tab[irqno].args);
  419. }
  420. }
  421. int rt_hw_pin_init(void)
  422. {
  423. return rt_device_pin_register("pin", &_acm32_pin_ops, RT_NULL);
  424. }
  425. INIT_BOARD_EXPORT(rt_hw_pin_init);
  426. void EXTI_IRQHandler(void)
  427. {
  428. /* enter interrupt */
  429. rt_interrupt_enter();
  430. for (int i = 0; i < 16; i++)
  431. {
  432. if (EXTI->PDR & pin_irq_map[i].line)
  433. {
  434. EXTI->PDR = pin_irq_map[i].line;
  435. pin_irq_hdr(i);
  436. break;
  437. }
  438. }
  439. /* leave interrupt */
  440. rt_interrupt_leave();
  441. }
  442. #endif /* RT_USING_PIN */