drv_gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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-12-12 Wayne 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. #include <drv_sys.h>
  21. /* Private define ---------------------------------------------------------------*/
  22. #define PORT_OFFSET 0x40
  23. #define IRQ_MAX_NUM 16 //Max support 32
  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] = {IRQ_GPA, IRQ_GPB, IRQ_GPC, IRQ_GPD, IRQ_GPE, IRQ_GPF, IRQ_GPG};
  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. return RT_EOK;
  52. }
  53. static rt_int32_t nu_find_irqindex(rt_uint32_t pin_index)
  54. {
  55. rt_int32_t irqindex;
  56. rt_int32_t u32PinIrqStatus = g_u32PinIrqMask;
  57. // Find index of pin is attached in pool.
  58. while ((irqindex = nu_ctz(u32PinIrqStatus)) < IRQ_MAX_NUM) // Count Trailing Zeros ==> Find First One
  59. {
  60. if (pin_irq_hdr_tab[irqindex].pin == pin_index)
  61. return irqindex;
  62. u32PinIrqStatus &= ~(1 << irqindex);
  63. }
  64. return -(RT_ERROR);
  65. }
  66. static void pin_irq_hdr(rt_uint32_t irq_status, rt_uint32_t port_index)
  67. {
  68. rt_int32_t irqindex, i;
  69. rt_int32_t pinindex = port_index * GPIO_PIN_MAX ;
  70. while ((i = nu_ctz(irq_status)) < GPIO_PIN_MAX)// Count Trailing Zeros ==> Find First One
  71. {
  72. int pin_mask = (1 << i);
  73. irqindex = nu_find_irqindex(pinindex + i);
  74. if (irqindex != -(RT_ERROR))
  75. {
  76. if (pin_irq_hdr_tab[irqindex].hdr)
  77. {
  78. pin_irq_hdr_tab[irqindex].hdr(pin_irq_hdr_tab[irqindex].args);
  79. }
  80. }
  81. // Clear the served bit.
  82. irq_status &= ~pin_mask;
  83. }
  84. }
  85. static rt_base_t nu_gpio_pin_get(const char *name)
  86. {
  87. /* Get pin number by name,such as PA.0, PF12 */
  88. if ((name[2] == '\0') || ((name[2] == '.') && (name[3] == '\0')))
  89. return -(RT_EINVAL);
  90. long number;
  91. if ((name[2] == '.'))
  92. number = atol(&name[3]);
  93. else
  94. number = atol(&name[2]);
  95. if (number > 15)
  96. return -(RT_EINVAL);
  97. if (name[1] >= 'A' && name[1] <= 'H')
  98. return ((name[1] - 'A') * 0x10) + number;
  99. if (name[1] >= 'a' && name[1] <= 'h')
  100. return ((name[1] - 'a') * 0x10) + number;
  101. return -(RT_EINVAL);
  102. }
  103. static void nu_gpio_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
  104. {
  105. GPIO_T *PORT;
  106. if (nu_port_check(pin))
  107. return;
  108. PORT = (GPIO_T *)(PA_BA + (NU_GET_PORT(pin) * PORT_OFFSET));
  109. if (mode == PIN_MODE_INPUT_PULLUP)
  110. {
  111. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  112. GPIO_SetPullCtl(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_PUSEL_PULL_UP);
  113. }
  114. else if (mode == PIN_MODE_INPUT_PULLDOWN)
  115. {
  116. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  117. GPIO_SetPullCtl(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_PUSEL_PULL_DOWN);
  118. }
  119. else if (mode == PIN_MODE_OUTPUT)
  120. {
  121. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_OUTPUT);
  122. }
  123. else if (mode == PIN_MODE_INPUT)
  124. {
  125. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_INPUT);
  126. GPIO_SetPullCtl(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_PUSEL_DISABLE);
  127. }
  128. else if (mode == PIN_MODE_OUTPUT_OD)
  129. {
  130. GPIO_SetMode(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_MODE_OPEN_DRAIN);
  131. GPIO_SetPullCtl(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), GPIO_PUSEL_DISABLE);
  132. }
  133. }
  134. static void nu_gpio_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
  135. {
  136. if (nu_port_check(pin))
  137. return;
  138. GPIO_PIN_DATA(NU_GET_PORT(pin), NU_GET_PINS(pin)) = value;
  139. }
  140. static int nu_gpio_read(struct rt_device *device, rt_base_t pin)
  141. {
  142. if (nu_port_check(pin))
  143. return PIN_LOW;
  144. return GPIO_PIN_DATA(NU_GET_PORT(pin), NU_GET_PINS(pin));
  145. }
  146. 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)
  147. {
  148. rt_base_t level;
  149. rt_int32_t irqindex;
  150. if (nu_port_check(pin))
  151. return -(RT_ERROR);
  152. level = rt_hw_interrupt_disable();
  153. // Find index of pin is attached in pool.
  154. if ((irqindex = nu_find_irqindex(pin)) >= 0)
  155. goto exit_nu_gpio_attach_irq;
  156. // Find available index of pin in pool.
  157. if ((irqindex = nu_cto(g_u32PinIrqMask)) < IRQ_MAX_NUM) // Count Trailing Ones ==> Find First Zero
  158. goto exit_nu_gpio_attach_irq;
  159. rt_hw_interrupt_enable(level);
  160. return -(RT_EBUSY);
  161. exit_nu_gpio_attach_irq:
  162. pin_irq_hdr_tab[irqindex].pin = pin;
  163. pin_irq_hdr_tab[irqindex].hdr = hdr;
  164. pin_irq_hdr_tab[irqindex].mode = mode;
  165. pin_irq_hdr_tab[irqindex].args = args;
  166. g_u32PinIrqMask |= (1 << irqindex);
  167. rt_hw_interrupt_enable(level);
  168. return RT_EOK;
  169. }
  170. static rt_err_t nu_gpio_detach_irq(struct rt_device *device, rt_int32_t pin)
  171. {
  172. rt_base_t level;
  173. rt_int32_t irqindex;
  174. rt_int32_t u32PinIrqStatus;
  175. if (nu_port_check(pin))
  176. return -(RT_ERROR);
  177. level = rt_hw_interrupt_disable();
  178. u32PinIrqStatus = g_u32PinIrqMask;
  179. // Find index of pin is attached in pool.
  180. while ((irqindex = nu_ctz(u32PinIrqStatus)) < IRQ_MAX_NUM)// Count Trailing Zeros ==> Find First One
  181. {
  182. if (pin_irq_hdr_tab[irqindex].pin == pin)
  183. {
  184. pin_irq_hdr_tab[irqindex].pin = PIN_IRQ_PIN_NONE;
  185. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  186. pin_irq_hdr_tab[irqindex].mode = PIN_IRQ_MODE_RISING;
  187. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  188. g_u32PinIrqMask &= ~(1 << irqindex);
  189. break;
  190. }
  191. u32PinIrqStatus &= ~(1 << irqindex);
  192. }
  193. rt_hw_interrupt_enable(level);
  194. return RT_EOK;
  195. }
  196. static void nu_gpio_isr(int vector, void *param)
  197. {
  198. rt_uint32_t int_status;
  199. GPIO_T *PORT = (GPIO_T *)param;
  200. rt_uint32_t port_idx = ((rt_uint32_t)PORT - PA_BA) / PORT_OFFSET ;
  201. int_status = PORT->INTSRC;
  202. pin_irq_hdr(int_status, port_idx);
  203. PORT->INTSRC = int_status;
  204. }
  205. static rt_err_t nu_gpio_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  206. {
  207. GPIO_T *PORT;
  208. rt_base_t level;
  209. uint32_t u32IntAttribs;
  210. rt_int32_t irqindex;
  211. rt_err_t ret = RT_EOK;
  212. IRQn_Type irqn;
  213. if (nu_port_check(pin))
  214. return -(RT_ERROR);
  215. level = rt_hw_interrupt_disable();
  216. irqindex = nu_find_irqindex(pin);
  217. if (irqindex == -(RT_ERROR))
  218. {
  219. ret = RT_ERROR;
  220. goto exit_nu_gpio_irq_enable;
  221. }
  222. PORT = (GPIO_T *)(PA_BA + (NU_GET_PORT(pin) * PORT_OFFSET));
  223. irqn = au32GPIRQ[NU_GET_PORT(pin)];
  224. if (enabled == PIN_IRQ_ENABLE)
  225. {
  226. if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_RISING)
  227. u32IntAttribs = GPIO_INT_RISING;
  228. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_FALLING)
  229. u32IntAttribs = GPIO_INT_FALLING;
  230. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_RISING_FALLING)
  231. u32IntAttribs = GPIO_INT_BOTH_EDGE;
  232. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_HIGH_LEVEL)
  233. u32IntAttribs = GPIO_INT_HIGH;
  234. else if (pin_irq_hdr_tab[irqindex].mode == PIN_IRQ_MODE_LOW_LEVEL)
  235. u32IntAttribs = GPIO_INT_LOW;
  236. else
  237. goto exit_nu_gpio_irq_enable;
  238. GPIO_EnableInt(PORT, NU_GET_PINS(pin), u32IntAttribs);
  239. rt_hw_interrupt_umask(irqn);
  240. }
  241. else
  242. {
  243. GPIO_DisableInt(PORT, NU_GET_PINS(pin));
  244. rt_hw_interrupt_mask(irqn);
  245. }
  246. exit_nu_gpio_irq_enable:
  247. rt_hw_interrupt_enable(level);
  248. return -(ret);
  249. }
  250. int rt_hw_gpio_init(void)
  251. {
  252. char szTmp[16];
  253. int i;
  254. rt_int32_t irqindex;
  255. for (irqindex = 0; irqindex < IRQ_MAX_NUM ; irqindex++)
  256. {
  257. pin_irq_hdr_tab[irqindex].pin = PIN_IRQ_PIN_NONE;
  258. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  259. pin_irq_hdr_tab[irqindex].mode = PIN_IRQ_MODE_RISING;
  260. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  261. }
  262. for (i = 0; i < NU_PORT_CNT ; i++)
  263. {
  264. IRQn_Type irqn = au32GPIRQ[i];
  265. snprintf(szTmp, sizeof(szTmp), "gpio-%d", i);
  266. rt_hw_interrupt_install(irqn, nu_gpio_isr, (void *)(PA_BA + (i * PORT_OFFSET)), szTmp);
  267. rt_hw_interrupt_set_type(irqn, HIGH_LEVEL_SENSITIVE);
  268. }
  269. nu_sys_ipclk_enable(GPIOCKEN);
  270. return rt_device_pin_register("gpio", &nu_gpio_ops, RT_NULL);
  271. }
  272. INIT_BOARD_EXPORT(rt_hw_gpio_init);
  273. #endif //#if (defined(BSP_USING_GPIO) && defined(RT_USING_PIN))