drv_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 Abbcc first version
  9. */
  10. #include <board.h>
  11. #include "drv_gpio.h"
  12. #ifdef RT_USING_PIN
  13. #define PIN_NUM(port, no) (((((port) & 0xFu) << 4) | ((no) & 0xFu)))
  14. #define PIN_PORT(pin) ((uint8_t)(((pin) >> 4) & 0xFu))
  15. #define PIN_NO(pin) ((uint8_t)((pin) & 0xFu))
  16. #define PIN_APMPORT(pin) ((GPIO_T *)(GPIOA_BASE + (0x400u * PIN_PORT(pin))))
  17. #define PIN_APMPIN(pin) ((uint16_t)(1u << PIN_NO(pin)))
  18. #if defined(GPIOZ)
  19. #define __APM32_PORT_MAX 12u
  20. #elif defined(GPIOK)
  21. #define __APM32_PORT_MAX 11u
  22. #elif defined(GPIOJ)
  23. #define __APM32_PORT_MAX 10u
  24. #elif defined(GPIOI)
  25. #define __APM32_PORT_MAX 9u
  26. #elif defined(GPIOH)
  27. #define __APM32_PORT_MAX 8u
  28. #elif defined(GPIOG)
  29. #define __APM32_PORT_MAX 7u
  30. #elif defined(GPIOF)
  31. #define __APM32_PORT_MAX 6u
  32. #elif defined(GPIOE)
  33. #define __APM32_PORT_MAX 5u
  34. #elif defined(GPIOD)
  35. #define __APM32_PORT_MAX 4u
  36. #elif defined(GPIOC)
  37. #define __APM32_PORT_MAX 3u
  38. #elif defined(GPIOB)
  39. #define __APM32_PORT_MAX 2u
  40. #elif defined(GPIOA)
  41. #define __APM32_PORT_MAX 1u
  42. #else
  43. #define __APM32_PORT_MAX 0u
  44. #error Unsupported APM32 GPIO peripheral.
  45. #endif
  46. #define PIN_APMPORT_MAX __APM32_PORT_MAX
  47. static const struct pin_irq_map pin_irq_map[] =
  48. {
  49. {GPIO_PIN_0, EINT0_IRQn},
  50. {GPIO_PIN_1, EINT1_IRQn},
  51. {GPIO_PIN_2, EINT2_IRQn},
  52. {GPIO_PIN_3, EINT3_IRQn},
  53. {GPIO_PIN_4, EINT4_IRQn},
  54. {GPIO_PIN_5, EINT9_5_IRQn},
  55. {GPIO_PIN_6, EINT9_5_IRQn},
  56. {GPIO_PIN_7, EINT9_5_IRQn},
  57. {GPIO_PIN_8, EINT9_5_IRQn},
  58. {GPIO_PIN_9, EINT9_5_IRQn},
  59. {GPIO_PIN_10, EINT15_10_IRQn},
  60. {GPIO_PIN_11, EINT15_10_IRQn},
  61. {GPIO_PIN_12, EINT15_10_IRQn},
  62. {GPIO_PIN_13, EINT15_10_IRQn},
  63. {GPIO_PIN_14, EINT15_10_IRQn},
  64. {GPIO_PIN_15, EINT15_10_IRQn},
  65. };
  66. static struct rt_pin_irq_hdr pin_irq_hdr_tab[] =
  67. {
  68. {-1, 0, RT_NULL, RT_NULL},
  69. {-1, 0, RT_NULL, RT_NULL},
  70. {-1, 0, RT_NULL, RT_NULL},
  71. {-1, 0, RT_NULL, RT_NULL},
  72. {-1, 0, RT_NULL, RT_NULL},
  73. {-1, 0, RT_NULL, RT_NULL},
  74. {-1, 0, RT_NULL, RT_NULL},
  75. {-1, 0, RT_NULL, RT_NULL},
  76. {-1, 0, RT_NULL, RT_NULL},
  77. {-1, 0, RT_NULL, RT_NULL},
  78. {-1, 0, RT_NULL, RT_NULL},
  79. {-1, 0, RT_NULL, RT_NULL},
  80. {-1, 0, RT_NULL, RT_NULL},
  81. {-1, 0, RT_NULL, RT_NULL},
  82. {-1, 0, RT_NULL, RT_NULL},
  83. {-1, 0, RT_NULL, RT_NULL},
  84. };
  85. static uint32_t pin_irq_enable_mask = 0;
  86. #define ITEM_NUM(items) sizeof(items) / sizeof(items[0])
  87. static rt_base_t _pin_get(const char *name)
  88. {
  89. rt_base_t pin = 0;
  90. int hw_port_num, hw_pin_num = 0;
  91. int i, name_len;
  92. name_len = rt_strlen(name);
  93. if ((name_len < 4) || (name_len >= 6))
  94. {
  95. return -RT_EINVAL;
  96. }
  97. if ((name[0] != 'P') || (name[2] != '.'))
  98. {
  99. return -RT_EINVAL;
  100. }
  101. if ((name[1] >= 'A') && (name[1] <= 'Z'))
  102. {
  103. hw_port_num = (int)(name[1] - 'A');
  104. }
  105. else
  106. {
  107. return -RT_EINVAL;
  108. }
  109. for (i = 3; i < name_len; i++)
  110. {
  111. hw_pin_num *= 10;
  112. hw_pin_num += name[i] - '0';
  113. }
  114. pin = PIN_NUM(hw_port_num, hw_pin_num);
  115. return pin;
  116. }
  117. static void _pin_write(rt_device_t dev, rt_base_t pin, rt_base_t value)
  118. {
  119. GPIO_T *gpio_port;
  120. uint16_t gpio_pin;
  121. if (PIN_PORT(pin) < PIN_APMPORT_MAX)
  122. {
  123. gpio_port = PIN_APMPORT(pin);
  124. gpio_pin = PIN_APMPIN(pin);
  125. GPIO_WriteBitValue(gpio_port, gpio_pin, (uint8_t)value);
  126. }
  127. }
  128. static int _pin_read(rt_device_t dev, rt_base_t pin)
  129. {
  130. GPIO_T *gpio_port;
  131. uint16_t gpio_pin;
  132. int value = PIN_LOW;
  133. if (PIN_PORT(pin) < PIN_APMPORT_MAX)
  134. {
  135. gpio_port = PIN_APMPORT(pin);
  136. gpio_pin = PIN_APMPIN(pin);
  137. value = GPIO_ReadInputBit(gpio_port, gpio_pin);
  138. }
  139. return value;
  140. }
  141. static void _pin_mode(rt_device_t dev, rt_base_t pin, rt_base_t mode)
  142. {
  143. GPIO_Config_T gpioConfig;
  144. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  145. {
  146. return;
  147. }
  148. /* Configure gpioConfigure */
  149. gpioConfig.pin = PIN_APMPIN(pin);
  150. gpioConfig.mode = GPIO_MODE_OUT_PP;
  151. gpioConfig.speed = GPIO_SPEED_50MHz;
  152. if (mode == PIN_MODE_OUTPUT)
  153. {
  154. /* output setting */
  155. gpioConfig.mode = GPIO_MODE_OUT_PP;
  156. }
  157. else if (mode == PIN_MODE_INPUT)
  158. {
  159. /* input setting: not pull. */
  160. gpioConfig.mode = GPIO_MODE_IN_PU;
  161. }
  162. else if (mode == PIN_MODE_INPUT_PULLUP)
  163. {
  164. /* input setting: pull up. */
  165. gpioConfig.mode = GPIO_MODE_IN_PU;
  166. }
  167. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  168. {
  169. /* input setting: pull down. */
  170. gpioConfig.mode = GPIO_MODE_IN_PD;
  171. }
  172. else if (mode == PIN_MODE_OUTPUT_OD)
  173. {
  174. /* output setting: od. */
  175. gpioConfig.mode = GPIO_MODE_OUT_OD;
  176. }
  177. GPIO_Config(PIN_APMPORT(pin), &gpioConfig);
  178. }
  179. rt_inline rt_int32_t bit2bitno(rt_uint32_t bit)
  180. {
  181. int i;
  182. for (i = 0; i < 32; i++)
  183. {
  184. if ((0x01 << i) == bit)
  185. {
  186. return i;
  187. }
  188. }
  189. return -1;
  190. }
  191. rt_inline const struct pin_irq_map *get_pin_irq_map(uint32_t pinbit)
  192. {
  193. rt_int32_t mapindex = bit2bitno(pinbit);
  194. if (mapindex < 0 || mapindex >= ITEM_NUM(pin_irq_map))
  195. {
  196. return RT_NULL;
  197. }
  198. return &pin_irq_map[mapindex];
  199. };
  200. static rt_err_t _pin_attach_irq(struct rt_device *device, rt_int32_t pin,
  201. rt_uint32_t mode, void (*hdr)(void *args), void *args)
  202. {
  203. rt_base_t level;
  204. rt_int32_t irqindex = -1;
  205. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  206. {
  207. return -RT_ENOSYS;
  208. }
  209. irqindex = bit2bitno(PIN_APMPIN(pin));
  210. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  211. {
  212. return RT_ENOSYS;
  213. }
  214. level = rt_hw_interrupt_disable();
  215. if (pin_irq_hdr_tab[irqindex].pin == pin &&
  216. pin_irq_hdr_tab[irqindex].hdr == hdr &&
  217. pin_irq_hdr_tab[irqindex].mode == mode &&
  218. pin_irq_hdr_tab[irqindex].args == args)
  219. {
  220. rt_hw_interrupt_enable(level);
  221. return RT_EOK;
  222. }
  223. if (pin_irq_hdr_tab[irqindex].pin != -1)
  224. {
  225. rt_hw_interrupt_enable(level);
  226. return RT_EBUSY;
  227. }
  228. pin_irq_hdr_tab[irqindex].pin = pin;
  229. pin_irq_hdr_tab[irqindex].hdr = hdr;
  230. pin_irq_hdr_tab[irqindex].mode = mode;
  231. pin_irq_hdr_tab[irqindex].args = args;
  232. rt_hw_interrupt_enable(level);
  233. return RT_EOK;
  234. }
  235. static rt_err_t _pin_dettach_irq(struct rt_device *device, rt_int32_t pin)
  236. {
  237. rt_base_t level;
  238. rt_int32_t irqindex = -1;
  239. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  240. {
  241. return -RT_ENOSYS;
  242. }
  243. irqindex = bit2bitno(PIN_APMPIN(pin));
  244. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  245. {
  246. return RT_ENOSYS;
  247. }
  248. level = rt_hw_interrupt_disable();
  249. if (pin_irq_hdr_tab[irqindex].pin == -1)
  250. {
  251. rt_hw_interrupt_enable(level);
  252. return RT_EOK;
  253. }
  254. pin_irq_hdr_tab[irqindex].pin = -1;
  255. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  256. pin_irq_hdr_tab[irqindex].mode = 0;
  257. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  258. rt_hw_interrupt_enable(level);
  259. return RT_EOK;
  260. }
  261. static rt_err_t _pin_irq_enable(struct rt_device *device, rt_base_t pin,
  262. rt_uint32_t enabled)
  263. {
  264. const struct pin_irq_map *irqmap;
  265. rt_base_t level;
  266. rt_int32_t irqindex = -1;
  267. GPIO_Config_T gpioConfig;
  268. if (PIN_PORT(pin) >= PIN_APMPORT_MAX)
  269. {
  270. return -RT_ENOSYS;
  271. }
  272. if (enabled == PIN_IRQ_ENABLE)
  273. {
  274. irqindex = bit2bitno(PIN_APMPIN(pin));
  275. if (irqindex < 0 || irqindex >= ITEM_NUM(pin_irq_map))
  276. {
  277. return RT_ENOSYS;
  278. }
  279. level = rt_hw_interrupt_disable();
  280. if (pin_irq_hdr_tab[irqindex].pin == -1)
  281. {
  282. rt_hw_interrupt_enable(level);
  283. return RT_ENOSYS;
  284. }
  285. irqmap = &pin_irq_map[irqindex];
  286. /* Configure gpioConfigure */
  287. gpioConfig.pin = PIN_APMPIN(pin);
  288. gpioConfig.speed = GPIO_SPEED_50MHz;
  289. switch (pin_irq_hdr_tab[irqindex].mode)
  290. {
  291. case PIN_IRQ_MODE_RISING:
  292. gpioConfig.mode = GPIO_MODE_IN_PD;
  293. break;
  294. case PIN_IRQ_MODE_FALLING:
  295. gpioConfig.mode = GPIO_MODE_IN_PU;
  296. break;
  297. case PIN_IRQ_MODE_RISING_FALLING:
  298. gpioConfig.mode = GPIO_MODE_IN_FLOATING;
  299. break;
  300. }
  301. GPIO_Config(PIN_APMPORT(pin), &gpioConfig);
  302. NVIC_EnableIRQRequest(irqmap->irqno, 5, 0);
  303. pin_irq_enable_mask |= irqmap->pinbit;
  304. rt_hw_interrupt_enable(level);
  305. }
  306. else if (enabled == PIN_IRQ_DISABLE)
  307. {
  308. irqmap = get_pin_irq_map(PIN_APMPIN(pin));
  309. if (irqmap == RT_NULL)
  310. {
  311. return RT_ENOSYS;
  312. }
  313. level = rt_hw_interrupt_disable();
  314. pin_irq_enable_mask &= ~irqmap->pinbit;
  315. if ((irqmap->pinbit >= GPIO_PIN_5) && (irqmap->pinbit <= GPIO_PIN_9))
  316. {
  317. if (!(pin_irq_enable_mask & (GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7 | GPIO_PIN_8 | GPIO_PIN_9)))
  318. {
  319. NVIC_DisableIRQRequest(irqmap->irqno);
  320. }
  321. }
  322. else if ((irqmap->pinbit >= GPIO_PIN_10) && (irqmap->pinbit <= GPIO_PIN_15))
  323. {
  324. if (!(pin_irq_enable_mask & (GPIO_PIN_10 | GPIO_PIN_11 | GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15)))
  325. {
  326. NVIC_DisableIRQRequest(irqmap->irqno);
  327. }
  328. }
  329. else
  330. {
  331. NVIC_DisableIRQRequest(irqmap->irqno);
  332. }
  333. rt_hw_interrupt_enable(level);
  334. }
  335. else
  336. {
  337. return -RT_ENOSYS;
  338. }
  339. return RT_EOK;
  340. }
  341. const static struct rt_pin_ops _apm32_pin_ops =
  342. {
  343. _pin_mode,
  344. _pin_write,
  345. _pin_read,
  346. _pin_attach_irq,
  347. _pin_dettach_irq,
  348. _pin_irq_enable,
  349. _pin_get,
  350. };
  351. rt_inline void pin_irq_hdr(int irqno)
  352. {
  353. if (pin_irq_hdr_tab[irqno].hdr)
  354. {
  355. pin_irq_hdr_tab[irqno].hdr(pin_irq_hdr_tab[irqno].args);
  356. }
  357. }
  358. void GPIO_EXTI_IRQHandler(uint16_t GPIO_Pin)
  359. {
  360. pin_irq_hdr(bit2bitno(GPIO_Pin));
  361. }
  362. void EINT0_IRQHandler(void)
  363. {
  364. rt_interrupt_enter();
  365. if (EINT_ReadIntFlag(EINT_LINE_0))
  366. {
  367. EINT_ClearIntFlag(EINT_LINE_0);
  368. GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  369. }
  370. rt_interrupt_leave();
  371. }
  372. void EINT1_IRQHandler(void)
  373. {
  374. rt_interrupt_enter();
  375. if (EINT_ReadIntFlag(EINT_LINE_1))
  376. {
  377. EINT_ClearIntFlag(EINT_LINE_1);
  378. GPIO_EXTI_IRQHandler(GPIO_PIN_1);
  379. }
  380. rt_interrupt_leave();
  381. }
  382. void EINT2_IRQHandler(void)
  383. {
  384. rt_interrupt_enter();
  385. if (EINT_ReadIntFlag(EINT_LINE_2))
  386. {
  387. EINT_ClearIntFlag(EINT_LINE_2);
  388. GPIO_EXTI_IRQHandler(GPIO_PIN_2);
  389. }
  390. rt_interrupt_leave();
  391. }
  392. void EINT3_IRQHandler(void)
  393. {
  394. rt_interrupt_enter();
  395. if (EINT_ReadIntFlag(EINT_LINE_3))
  396. {
  397. EINT_ClearIntFlag(EINT_LINE_3);
  398. GPIO_EXTI_IRQHandler(GPIO_PIN_3);
  399. }
  400. rt_interrupt_leave();
  401. }
  402. void EINT4_IRQHandler(void)
  403. {
  404. rt_interrupt_enter();
  405. if (EINT_ReadIntFlag(EINT_LINE_4))
  406. {
  407. EINT_ClearIntFlag(EINT_LINE_4);
  408. GPIO_EXTI_IRQHandler(GPIO_PIN_4);
  409. }
  410. rt_interrupt_leave();
  411. }
  412. void EINT9_5_IRQHandler(void)
  413. {
  414. rt_interrupt_enter();
  415. if (EINT_ReadIntFlag(EINT_LINE_5))
  416. {
  417. EINT_ClearIntFlag(EINT_LINE_5);
  418. GPIO_EXTI_IRQHandler(GPIO_PIN_5);
  419. }
  420. if (EINT_ReadIntFlag(EINT_LINE_6))
  421. {
  422. EINT_ClearIntFlag(EINT_LINE_6);
  423. GPIO_EXTI_IRQHandler(GPIO_PIN_6);
  424. }
  425. if (EINT_ReadIntFlag(EINT_LINE_7))
  426. {
  427. EINT_ClearIntFlag(EINT_LINE_7);
  428. GPIO_EXTI_IRQHandler(GPIO_PIN_7);
  429. }
  430. if (EINT_ReadIntFlag(EINT_LINE_8))
  431. {
  432. EINT_ClearIntFlag(EINT_LINE_8);
  433. GPIO_EXTI_IRQHandler(GPIO_PIN_8);
  434. }
  435. if (EINT_ReadIntFlag(EINT_LINE_9))
  436. {
  437. EINT_ClearIntFlag(EINT_LINE_9);
  438. GPIO_EXTI_IRQHandler(GPIO_PIN_9);
  439. }
  440. rt_interrupt_leave();
  441. }
  442. void EINT15_10_IRQHandler(void)
  443. {
  444. rt_interrupt_enter();
  445. if (EINT_ReadIntFlag(EINT_LINE_10))
  446. {
  447. EINT_ClearIntFlag(EINT_LINE_10);
  448. GPIO_EXTI_IRQHandler(GPIO_PIN_10);
  449. }
  450. if (EINT_ReadIntFlag(EINT_LINE_11))
  451. {
  452. EINT_ClearIntFlag(EINT_LINE_11);
  453. GPIO_EXTI_IRQHandler(GPIO_PIN_11);
  454. }
  455. if (EINT_ReadIntFlag(EINT_LINE_12))
  456. {
  457. EINT_ClearIntFlag(EINT_LINE_12);
  458. GPIO_EXTI_IRQHandler(GPIO_PIN_12);
  459. }
  460. if (EINT_ReadIntFlag(EINT_LINE_13))
  461. {
  462. EINT_ClearIntFlag(EINT_LINE_13);
  463. GPIO_EXTI_IRQHandler(GPIO_PIN_13);
  464. }
  465. if (EINT_ReadIntFlag(EINT_LINE_14))
  466. {
  467. EINT_ClearIntFlag(EINT_LINE_14);
  468. GPIO_EXTI_IRQHandler(GPIO_PIN_14);
  469. }
  470. if (EINT_ReadIntFlag(EINT_LINE_15))
  471. {
  472. EINT_ClearIntFlag(EINT_LINE_15);
  473. GPIO_EXTI_IRQHandler(GPIO_PIN_15);
  474. }
  475. rt_interrupt_leave();
  476. }
  477. int rt_hw_pin_init(void)
  478. {
  479. #ifdef GPIOA
  480. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOA);
  481. #endif
  482. #ifdef GPIOB
  483. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOB);
  484. #endif
  485. #ifdef GPIOC
  486. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
  487. #endif
  488. #ifdef GPIOD
  489. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOD);
  490. #endif
  491. #ifdef GPIOE
  492. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOE);
  493. #endif
  494. #ifdef GPIOF
  495. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOF);
  496. #endif
  497. #ifdef GPIOG
  498. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOG);
  499. #endif
  500. RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_AFIO);
  501. return rt_device_pin_register("pin", &_apm32_pin_ops, RT_NULL);
  502. }
  503. #endif /* RT_USING_PIN */