drv_gpio.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /**************************************************************************//**
  2. *
  3. * @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2020-2-7 YCHuang12 First version
  10. *
  11. ******************************************************************************/
  12. #include <rtconfig.h>
  13. #if (defined(BSP_USING_GPIO) && defined(RT_USING_PIN))
  14. #include <rtdevice.h>
  15. #include <rthw.h>
  16. #include "NuMicro.h"
  17. #include <nu_bitutil.h>
  18. #include <drv_gpio.h>
  19. #include <stdlib.h>
  20. /* Private define ---------------------------------------------------------------*/
  21. #define PORT_OFFSET 0x40
  22. #define IRQ_MAX_NUM 16 //Max support 32
  23. #define MAX_PORTH_PIN_MAX 11
  24. /* Private functions ------------------------------------------------------------*/
  25. static void nu_gpio_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode);
  26. static void nu_gpio_write(struct rt_device *device, rt_base_t pin, rt_base_t value);
  27. static int nu_gpio_read(struct rt_device *device, rt_base_t pin);
  28. static rt_err_t nu_gpio_attach_irq(struct rt_device *device, rt_int32_t pin, rt_uint32_t mode, void (*hdr)(void *args), void *args);
  29. static rt_err_t nu_gpio_detach_irq(struct rt_device *device, rt_int32_t pin);
  30. static rt_err_t nu_gpio_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
  31. static rt_base_t nu_gpio_pin_get(const char *name);
  32. /* Private variables ------------------------------------------------------------*/
  33. static struct rt_pin_irq_hdr pin_irq_hdr_tab[IRQ_MAX_NUM];
  34. static struct rt_pin_ops nu_gpio_ops =
  35. {
  36. nu_gpio_mode,
  37. nu_gpio_write,
  38. nu_gpio_read,
  39. nu_gpio_attach_irq,
  40. nu_gpio_detach_irq,
  41. nu_gpio_irq_enable,
  42. nu_gpio_pin_get,
  43. };
  44. static IRQn_Type au32GPIRQ[NU_PORT_CNT] = {GPA_IRQn, GPB_IRQn, GPC_IRQn, GPD_IRQn, GPE_IRQn, GPF_IRQn, GPG_IRQn, GPH_IRQn};
  45. static rt_uint32_t g_u32PinIrqMask = 0x0;
  46. /* Functions define ------------------------------------------------------------*/
  47. static rt_err_t nu_port_check(rt_int32_t pin)
  48. {
  49. if (NU_GET_PORT(pin) >= NU_PORT_CNT)
  50. return -(RT_ERROR);
  51. else if ((NU_GET_PORT(pin) == NU_PH) && (NU_GET_PINS(pin) > MAX_PORTH_PIN_MAX))
  52. return -(RT_ERROR);
  53. return RT_EOK;
  54. }
  55. static rt_int32_t nu_find_irqindex(rt_uint32_t pin_index)
  56. {
  57. rt_int32_t irqindex;
  58. rt_int32_t u32PinIrqStatus = g_u32PinIrqMask;
  59. // Find index of pin is attached in pool.
  60. while ((irqindex = nu_ctz(u32PinIrqStatus)) < IRQ_MAX_NUM) // Count Trailing Zeros ==> Find First One
  61. {
  62. if (pin_irq_hdr_tab[irqindex].pin == pin_index)
  63. return irqindex;
  64. u32PinIrqStatus &= ~(1 << irqindex);
  65. }
  66. return -(RT_ERROR);
  67. }
  68. static void pin_irq_hdr(rt_uint32_t irq_status, rt_uint32_t port_index)
  69. {
  70. rt_int32_t irqindex, i;
  71. rt_int32_t pinindex = port_index * GPIO_PIN_MAX ;
  72. while ((i = nu_ctz(irq_status)) < GPIO_PIN_MAX)// Count Trailing Zeros ==> Find First One
  73. {
  74. int pin_mask = (1 << i);
  75. irqindex = nu_find_irqindex(pinindex + i);
  76. if (irqindex != -(RT_ERROR))
  77. {
  78. if (pin_irq_hdr_tab[irqindex].hdr)
  79. {
  80. pin_irq_hdr_tab[irqindex].hdr(pin_irq_hdr_tab[irqindex].args);
  81. }
  82. }
  83. // Clear the served bit.
  84. irq_status &= ~pin_mask;
  85. }
  86. }
  87. static rt_base_t nu_gpio_pin_get(const char *name)
  88. {
  89. /* Get pin number by name,such as PA.0, PF12 */
  90. if ((name[2] == '\0') || ((name[2] == '.') && (name[3] == '\0')))
  91. return -(RT_EINVAL);
  92. long number;
  93. if ((name[2] == '.'))
  94. number = atol(&name[3]);
  95. else
  96. number = atol(&name[2]);
  97. if (number > 15)
  98. return -(RT_EINVAL);
  99. if (name[1] >= 'A' && name[1] <= 'H')
  100. return ((name[1] - 'A') * 0x10) + number;
  101. if (name[1] >= 'a' && name[1] <= 'h')
  102. return ((name[1] - 'a') * 0x10) + number;
  103. return -(RT_EINVAL);
  104. }
  105. static void nu_gpio_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
  106. {
  107. GPIO_T *PORT;
  108. if (nu_port_check(pin))
  109. return;
  110. PORT = (GPIO_T *)(GPIOA_BASE + (NU_GET_PORT(pin) * PORT_OFFSET));
  111. if (mode == PIN_MODE_INPUT_PULLUP)
  112. {
  113. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  114. GPIO_SetPullCtl(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_PUSEL_PULL_UP);
  115. }
  116. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  117. {
  118. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  119. GPIO_SetPullCtl(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_PUSEL_PULL_DOWN);
  120. }
  121. else if (mode == PIN_MODE_OUTPUT)
  122. {
  123. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_OUTPUT);
  124. }
  125. else if (mode == PIN_MODE_INPUT)
  126. {
  127. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  128. GPIO_SetPullCtl(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_PUSEL_DISABLE);
  129. }
  130. else if (mode == PIN_MODE_OUTPUT_OD)
  131. {
  132. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_OPEN_DRAIN);
  133. GPIO_SetPullCtl(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_PUSEL_DISABLE);
  134. }
  135. }
  136. static void nu_gpio_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
  137. {
  138. if (nu_port_check(pin))
  139. return;
  140. GPIO_PIN_DATA(NU_GET_PORT(pin), NU_GET_PINS(pin)) = value;
  141. }
  142. static int nu_gpio_read(struct rt_device *device, rt_base_t pin)
  143. {
  144. if (nu_port_check(pin))
  145. return PIN_LOW;
  146. return GPIO_PIN_DATA(NU_GET_PORT(pin), NU_GET_PINS(pin));
  147. }
  148. static rt_err_t nu_gpio_attach_irq(struct rt_device *device, rt_int32_t pin, rt_uint32_t mode, void (*hdr)(void *args), void *args)
  149. {
  150. rt_base_t level;
  151. rt_int32_t irqindex;
  152. if (nu_port_check(pin))
  153. return -(RT_ERROR);
  154. level = rt_hw_interrupt_disable();
  155. // Find index of pin is attached in pool.
  156. if ((irqindex = nu_find_irqindex(pin)) >= 0)
  157. goto exit_nu_gpio_attach_irq;
  158. // Find available index of pin in pool.
  159. if ((irqindex = nu_cto(g_u32PinIrqMask)) < IRQ_MAX_NUM) // Count Trailing Ones ==> Find First Zero
  160. goto exit_nu_gpio_attach_irq;
  161. rt_hw_interrupt_enable(level);
  162. return -(RT_EBUSY);
  163. exit_nu_gpio_attach_irq:
  164. pin_irq_hdr_tab[irqindex].pin = pin;
  165. pin_irq_hdr_tab[irqindex].hdr = hdr;
  166. pin_irq_hdr_tab[irqindex].mode = mode;
  167. pin_irq_hdr_tab[irqindex].args = args;
  168. g_u32PinIrqMask |= (1 << irqindex);
  169. rt_hw_interrupt_enable(level);
  170. return RT_EOK;
  171. }
  172. static rt_err_t nu_gpio_detach_irq(struct rt_device *device, rt_int32_t pin)
  173. {
  174. rt_base_t level;
  175. rt_int32_t irqindex;
  176. rt_int32_t u32PinIrqStatus;
  177. if (nu_port_check(pin))
  178. return -(RT_ERROR);
  179. level = rt_hw_interrupt_disable();
  180. u32PinIrqStatus = g_u32PinIrqMask;
  181. // Find index of pin is attached in pool.
  182. while ((irqindex = nu_ctz(u32PinIrqStatus)) < IRQ_MAX_NUM)// Count Trailing Zeros ==> Find First One
  183. {
  184. if (pin_irq_hdr_tab[irqindex].pin == pin)
  185. {
  186. pin_irq_hdr_tab[irqindex].pin = PIN_IRQ_PIN_NONE;
  187. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  188. pin_irq_hdr_tab[irqindex].mode = PIN_IRQ_MODE_RISING;
  189. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  190. g_u32PinIrqMask &= ~(1 << irqindex);
  191. break;
  192. }
  193. u32PinIrqStatus &= ~(1 << irqindex);
  194. }
  195. rt_hw_interrupt_enable(level);
  196. return RT_EOK;
  197. }
  198. static rt_err_t nu_gpio_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  199. {
  200. GPIO_T *PORT;
  201. rt_base_t level;
  202. uint32_t u32IntAttribs;
  203. rt_int32_t irqindex;
  204. rt_err_t ret = RT_EOK;
  205. if (nu_port_check(pin))
  206. return -(RT_ERROR);
  207. level = rt_hw_interrupt_disable();
  208. irqindex = nu_find_irqindex(pin);
  209. if (irqindex == -(RT_ERROR))
  210. {
  211. ret = RT_ERROR;
  212. goto exit_nu_gpio_irq_enable;
  213. }
  214. PORT = (GPIO_T *)(GPIOA_BASE + (NU_GET_PORT(pin) * PORT_OFFSET));
  215. if (enabled == PIN_IRQ_ENABLE)
  216. {
  217. if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_RISING)
  218. u32IntAttribs = GPIO_INT_RISING;
  219. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_FALLING)
  220. u32IntAttribs = GPIO_INT_FALLING;
  221. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_RISING_FALLING)
  222. u32IntAttribs = GPIO_INT_BOTH_EDGE;
  223. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_HIGH_LEVEL)
  224. u32IntAttribs = GPIO_INT_HIGH;
  225. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_LOW_LEVEL)
  226. u32IntAttribs = GPIO_INT_LOW;
  227. else
  228. goto exit_nu_gpio_irq_enable;
  229. GPIO_EnableInt(PORT, NU_GET_PINS(pin), u32IntAttribs);
  230. NVIC_EnableIRQ(au32GPIRQ[NU_GET_PORT(pin)]);
  231. }
  232. else
  233. {
  234. GPIO_DisableInt(PORT, NU_GET_PINS(pin));
  235. }
  236. exit_nu_gpio_irq_enable:
  237. rt_hw_interrupt_enable(level);
  238. return -(ret);
  239. }
  240. int rt_hw_gpio_init(void)
  241. {
  242. rt_int32_t irqindex;
  243. for (irqindex = 0; irqindex < IRQ_MAX_NUM ; irqindex++)
  244. {
  245. pin_irq_hdr_tab[irqindex].pin = PIN_IRQ_PIN_NONE;
  246. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  247. pin_irq_hdr_tab[irqindex].mode = PIN_IRQ_MODE_RISING;
  248. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  249. }
  250. return rt_device_pin_register("gpio", &nu_gpio_ops, RT_NULL);
  251. }
  252. INIT_BOARD_EXPORT(rt_hw_gpio_init);
  253. void GPA_IRQHandler(void)
  254. {
  255. rt_uint32_t int_status;
  256. rt_interrupt_enter();
  257. int_status = PA->INTSRC;
  258. pin_irq_hdr(int_status, NU_PA);
  259. PA->INTSRC = int_status;
  260. rt_interrupt_leave();
  261. }
  262. void GPB_IRQHandler(void)
  263. {
  264. rt_uint32_t int_status;
  265. rt_interrupt_enter();
  266. int_status = PB->INTSRC;
  267. pin_irq_hdr(int_status, NU_PB);
  268. PB->INTSRC = int_status;
  269. rt_interrupt_leave();
  270. }
  271. void GPC_IRQHandler(void)
  272. {
  273. rt_uint32_t int_status;
  274. rt_interrupt_enter();
  275. int_status = PC->INTSRC;
  276. pin_irq_hdr(int_status, NU_PC);
  277. PC->INTSRC = int_status;
  278. rt_interrupt_leave();
  279. }
  280. void GPD_IRQHandler(void)
  281. {
  282. rt_uint32_t int_status;
  283. rt_interrupt_enter();
  284. int_status = PD->INTSRC;
  285. pin_irq_hdr(int_status, NU_PD);
  286. PD->INTSRC = int_status;
  287. rt_interrupt_leave();
  288. }
  289. void GPE_IRQHandler(void)
  290. {
  291. rt_uint32_t int_status;
  292. rt_interrupt_enter();
  293. int_status = PE->INTSRC;
  294. pin_irq_hdr(int_status, NU_PE);
  295. PE->INTSRC = int_status;
  296. rt_interrupt_leave();
  297. }
  298. void GPF_IRQHandler(void)
  299. {
  300. rt_uint32_t int_status;
  301. rt_interrupt_enter();
  302. int_status = PF->INTSRC;
  303. pin_irq_hdr(int_status, NU_PF);
  304. PF->INTSRC = int_status;
  305. rt_interrupt_leave();
  306. }
  307. void GPG_IRQHandler(void)
  308. {
  309. rt_uint32_t int_status;
  310. rt_interrupt_enter();
  311. int_status = PG->INTSRC;
  312. pin_irq_hdr(int_status, NU_PG);
  313. PG->INTSRC = int_status;
  314. rt_interrupt_leave();
  315. }
  316. void GPH_IRQHandler(void)
  317. {
  318. rt_uint32_t int_status;
  319. rt_interrupt_enter();
  320. int_status = PH->INTSRC;
  321. pin_irq_hdr(int_status, NU_PH);
  322. PH->INTSRC = int_status;
  323. rt_interrupt_leave();
  324. }
  325. #endif //#if (defined(BSP_USING_GPIO) && defined(RT_USING_PIN))