drv_gpio.c 10 KB

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