drv_gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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-9-4 Philo 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_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_get,
  43. };
  44. static rt_uint32_t g_u32PinIrqMask = 0x0;
  45. /* Functions define ------------------------------------------------------------*/
  46. static rt_err_t nu_port_check(rt_int32_t pin)
  47. {
  48. if (NU_GET_PORT(pin) >= NU_PORT_CNT)
  49. return -(RT_ERROR);
  50. else if ((NU_GET_PORT(pin) == NU_PH) && (NU_GET_PINS(pin) > MAX_PORTH_PIN_MAX))
  51. return -(RT_ERROR);
  52. return RT_EOK;
  53. }
  54. static rt_int32_t nu_find_irqindex(rt_uint32_t pin_index)
  55. {
  56. rt_int32_t irqindex;
  57. rt_int32_t u32PinIrqStatus = g_u32PinIrqMask;
  58. // Find index of pin is attached in pool.
  59. while ((irqindex = nu_ctz(u32PinIrqStatus)) < IRQ_MAX_NUM) // Count Trailing Zeros ==> Find First One
  60. {
  61. if (pin_irq_hdr_tab[irqindex].pin == pin_index)
  62. return irqindex;
  63. u32PinIrqStatus &= ~(1 << irqindex);
  64. }
  65. return -(RT_ERROR);
  66. }
  67. static void pin_irq_hdr(rt_uint32_t irq_status, rt_uint32_t port_index)
  68. {
  69. rt_int32_t irqindex, i;
  70. rt_int32_t pinindex = port_index * GPIO_PIN_MAX ;
  71. while ((i = nu_ctz(irq_status)) < GPIO_PIN_MAX)// Count Trailing Zeros ==> Find First One
  72. {
  73. int pin_mask = (1 << i);
  74. irqindex = nu_find_irqindex(pinindex + i);
  75. if (irqindex != -(RT_ERROR))
  76. {
  77. if (pin_irq_hdr_tab[irqindex].hdr)
  78. {
  79. pin_irq_hdr_tab[irqindex].hdr(pin_irq_hdr_tab[irqindex].args);
  80. }
  81. }
  82. // Clear the served bit.
  83. irq_status &= ~pin_mask;
  84. }
  85. }
  86. static void nu_gpio_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
  87. {
  88. GPIO_T *PORT;
  89. if (nu_port_check(pin))
  90. return;
  91. PORT = (GPIO_T *)(PA_BASE + (NU_GET_PORT(pin) * PORT_OFFSET));
  92. if (mode == PIN_MODE_OUTPUT)
  93. {
  94. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_OUTPUT);
  95. }
  96. else if (mode == PIN_MODE_INPUT)
  97. {
  98. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  99. }
  100. else if (mode == PIN_MODE_OUTPUT_OD)
  101. {
  102. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_OPEN_DRAIN);
  103. }
  104. #if 0
  105. else if (mode == PIN_MODE_INPUT_PULLUP)
  106. {
  107. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  108. }
  109. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  110. {
  111. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  112. }
  113. #else
  114. else
  115. {
  116. rt_kprintf("M031 not support this GPIO mode\n");
  117. }
  118. #endif
  119. }
  120. static void nu_gpio_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
  121. {
  122. if (nu_port_check(pin))
  123. return;
  124. GPIO_PIN_DATA(NU_GET_PORT(pin), NU_GET_PINS(pin)) = value;
  125. }
  126. static int nu_gpio_read(struct rt_device *device, rt_base_t pin)
  127. {
  128. if (nu_port_check(pin))
  129. return PIN_LOW;
  130. return GPIO_PIN_DATA(NU_GET_PORT(pin), NU_GET_PINS(pin));
  131. }
  132. 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)
  133. {
  134. rt_base_t level;
  135. rt_int32_t irqindex;
  136. if (nu_port_check(pin))
  137. return -(RT_ERROR);
  138. level = rt_hw_interrupt_disable();
  139. // Find index of pin is attached in pool.
  140. if ((irqindex = nu_find_irqindex(pin)) >= 0)
  141. goto exit_nu_gpio_attach_irq;
  142. // Find available index of pin in pool.
  143. if ((irqindex = nu_cto(g_u32PinIrqMask)) < IRQ_MAX_NUM) // Count Trailing Ones ==> Find First Zero
  144. goto exit_nu_gpio_attach_irq;
  145. rt_hw_interrupt_enable(level);
  146. return -(RT_EBUSY);
  147. exit_nu_gpio_attach_irq:
  148. pin_irq_hdr_tab[irqindex].pin = pin;
  149. pin_irq_hdr_tab[irqindex].hdr = hdr;
  150. pin_irq_hdr_tab[irqindex].mode = mode;
  151. pin_irq_hdr_tab[irqindex].args = args;
  152. g_u32PinIrqMask |= (1 << irqindex);
  153. rt_hw_interrupt_enable(level);
  154. return RT_EOK;
  155. }
  156. static rt_err_t nu_gpio_detach_irq(struct rt_device *device, rt_int32_t pin)
  157. {
  158. rt_base_t level;
  159. rt_int32_t irqindex;
  160. rt_int32_t u32PinIrqStatus;
  161. if (nu_port_check(pin))
  162. return -(RT_ERROR);
  163. level = rt_hw_interrupt_disable();
  164. u32PinIrqStatus = g_u32PinIrqMask;
  165. // Find index of pin is attached in pool.
  166. while ((irqindex = nu_ctz(u32PinIrqStatus)) < IRQ_MAX_NUM)// Count Trailing Zeros ==> Find First One
  167. {
  168. if (pin_irq_hdr_tab[irqindex].pin == pin)
  169. {
  170. pin_irq_hdr_tab[irqindex].pin = PIN_IRQ_PIN_NONE;
  171. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  172. pin_irq_hdr_tab[irqindex].mode = PIN_IRQ_MODE_RISING;
  173. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  174. g_u32PinIrqMask &= ~(1 << irqindex);
  175. break;
  176. }
  177. u32PinIrqStatus &= ~(1 << irqindex);
  178. }
  179. rt_hw_interrupt_enable(level);
  180. return RT_EOK;
  181. }
  182. static rt_err_t nu_gpio_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  183. {
  184. GPIO_T *PORT;
  185. rt_base_t level;
  186. uint32_t u32IntAttribs;
  187. rt_int32_t irqindex;
  188. rt_err_t ret = RT_EOK;
  189. if (nu_port_check(pin))
  190. return -(RT_ERROR);
  191. level = rt_hw_interrupt_disable();
  192. irqindex = nu_find_irqindex(pin);
  193. if (irqindex == -(RT_ERROR))
  194. {
  195. ret = -RT_ERROR;
  196. goto exit_nu_gpio_irq_enable;
  197. }
  198. PORT = (GPIO_T *)(PA_BASE + (NU_GET_PORT(pin) * PORT_OFFSET));
  199. if (enabled == PIN_IRQ_ENABLE)
  200. {
  201. if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_RISING)
  202. u32IntAttribs = GPIO_INT_RISING;
  203. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_FALLING)
  204. u32IntAttribs = GPIO_INT_FALLING;
  205. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_RISING_FALLING)
  206. u32IntAttribs = GPIO_INT_BOTH_EDGE;
  207. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_HIGH_LEVEL)
  208. u32IntAttribs = GPIO_INT_HIGH;
  209. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_LOW_LEVEL)
  210. u32IntAttribs = GPIO_INT_LOW;
  211. else
  212. goto exit_nu_gpio_irq_enable;
  213. GPIO_EnableInt(PORT, NU_GET_PINS(pin), u32IntAttribs);
  214. if ((NU_GET_PORT(pin) == NU_PA) || (NU_GET_PORT(pin) == NU_PB) || (NU_GET_PORT(pin) == NU_PG) || (NU_GET_PORT(pin) == NU_PH))
  215. {
  216. NVIC_EnableIRQ(GPIO_PAPBPGPH_IRQn);
  217. }
  218. else
  219. {
  220. NVIC_EnableIRQ(GPIO_PCPDPEPF_IRQn);
  221. }
  222. }
  223. else
  224. {
  225. GPIO_DisableInt(PORT, NU_GET_PINS(pin));
  226. }
  227. exit_nu_gpio_irq_enable:
  228. rt_hw_interrupt_enable(level);
  229. return -(ret);
  230. }
  231. static rt_base_t nu_gpio_get(const char *name)
  232. {
  233. /* Get pin number by name,such as PA.0, PF12 */
  234. if ((name[2] == '\0')||((name[2] == '.')&&(name[3] == '\0')))
  235. return -(RT_EINVAL);
  236. long number;
  237. if ((name[2] == '.'))
  238. number = atol(&name[3]);
  239. else
  240. number = atol(&name[2]);
  241. if (number > 15)
  242. return -(RT_EINVAL);
  243. if (name[1] >= 'A' && name[1] <= 'H')
  244. return ((name[1] - 'A') * 0x10) + number;
  245. if (name[1] >= 'a' && name[1] <= 'h')
  246. return ((name[1] - 'a') * 0x10) + number;
  247. return -(RT_EINVAL);
  248. }
  249. int rt_hw_gpio_init(void)
  250. {
  251. rt_int32_t irqindex;
  252. for (irqindex = 0; irqindex < IRQ_MAX_NUM ; irqindex++)
  253. {
  254. pin_irq_hdr_tab[irqindex].pin = PIN_IRQ_PIN_NONE;
  255. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  256. pin_irq_hdr_tab[irqindex].mode = PIN_IRQ_MODE_RISING;
  257. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  258. }
  259. return rt_device_pin_register("gpio", &nu_gpio_ops, RT_NULL);
  260. }
  261. INIT_BOARD_EXPORT(rt_hw_gpio_init);
  262. void GPABGH_IRQHandler(void)
  263. {
  264. rt_uint32_t int_status;
  265. rt_interrupt_enter();
  266. int_status = PA->INTSRC;
  267. if (int_status)
  268. {
  269. pin_irq_hdr(int_status, NU_PA);
  270. PA->INTSRC = int_status;
  271. }
  272. int_status = PB->INTSRC;
  273. if (int_status)
  274. {
  275. pin_irq_hdr(int_status, NU_PB);
  276. PB->INTSRC = int_status;
  277. }
  278. int_status = PG->INTSRC;
  279. if (int_status)
  280. {
  281. pin_irq_hdr(int_status, NU_PG);
  282. PG->INTSRC = int_status;
  283. }
  284. int_status = PH->INTSRC;
  285. if (int_status)
  286. {
  287. pin_irq_hdr(int_status, NU_PH);
  288. PH->INTSRC = int_status;
  289. }
  290. rt_interrupt_leave();
  291. }
  292. void GPCDEF_IRQHandler(void)
  293. {
  294. rt_uint32_t int_status;
  295. rt_interrupt_enter();
  296. int_status = PC->INTSRC;
  297. if (int_status)
  298. {
  299. pin_irq_hdr(int_status, NU_PC);
  300. PC->INTSRC = int_status;
  301. }
  302. int_status = PD->INTSRC;
  303. if (int_status)
  304. {
  305. pin_irq_hdr(int_status, NU_PD);
  306. PD->INTSRC = int_status;
  307. }
  308. int_status = PE->INTSRC;
  309. if (int_status)
  310. {
  311. pin_irq_hdr(int_status, NU_PE);
  312. PE->INTSRC = int_status;
  313. }
  314. int_status = PF->INTSRC;
  315. if (int_status)
  316. {
  317. pin_irq_hdr(int_status, NU_PF);
  318. PF->INTSRC = int_status;
  319. }
  320. rt_interrupt_leave();
  321. }
  322. #endif //#if (defined(BSP_USING_GPIO) && defined(RT_USING_PIN))