drv_gpio.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #define LOG_TAG "drv.gpio"
  22. #define DBG_ENABLE
  23. #define DBG_SECTION_NAME LOG_TAG
  24. #define DBG_LEVEL DBG_INFO
  25. #define DBG_COLOR
  26. #include <rtdbg.h>
  27. /* Private define ---------------------------------------------------------------*/
  28. #define PORT_OFFSET 0x40
  29. #define IRQ_MAX_NUM 16 //Max support 32
  30. #define GPIO_PIN_MAX 16
  31. /* Private functions ------------------------------------------------------------*/
  32. static void nu_gpio_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode);
  33. static void nu_gpio_write(struct rt_device *device, rt_base_t pin, rt_base_t value);
  34. static int nu_gpio_read(struct rt_device *device, rt_base_t pin);
  35. 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);
  36. static rt_err_t nu_gpio_detach_irq(struct rt_device *device, rt_int32_t pin);
  37. static rt_err_t nu_gpio_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled);
  38. static rt_base_t nu_gpio_pin_get(const char *name);
  39. /* Private variables ------------------------------------------------------------*/
  40. static struct rt_pin_irq_hdr pin_irq_hdr_tab[IRQ_MAX_NUM];
  41. static struct rt_pin_ops nu_gpio_ops =
  42. {
  43. nu_gpio_mode,
  44. nu_gpio_write,
  45. nu_gpio_read,
  46. nu_gpio_attach_irq,
  47. nu_gpio_detach_irq,
  48. nu_gpio_irq_enable,
  49. nu_gpio_pin_get,
  50. };
  51. static rt_uint32_t g_u32PinIrqMask = 0x0;
  52. static uint32_t au32PinMaskTbl[] = {GPIOA_MASK, GPIOB_MASK, GPIOC_MASK, GPIOD_MASK, GPIOE_MASK, GPIOF_MASK, GPIOG_MASK, GPIOH_MASK, GPIOI_MASK, GPIOJ_MASK};
  53. /* Functions define ------------------------------------------------------------*/
  54. static rt_err_t nu_port_check(rt_int32_t pin)
  55. {
  56. if (NU_GET_PORT(pin) >= NU_PORT_CNT)
  57. {
  58. LOG_E("Over port group. %04x", pin);
  59. return -(RT_ERROR);
  60. }
  61. if (!(au32PinMaskTbl[NU_GET_PORT(pin)] & NU_GET_PIN_MASK(NU_GET_PINS(pin))))
  62. {
  63. LOG_E("Over port-pin group. %04x", pin);
  64. return -(RT_ERROR);
  65. }
  66. return RT_EOK;
  67. }
  68. static rt_int32_t nu_find_irqindex(rt_uint32_t pin_index)
  69. {
  70. rt_int32_t irqindex;
  71. rt_int32_t u32PinIrqStatus = g_u32PinIrqMask;
  72. // Find index of pin is attached in pool.
  73. while ((irqindex = nu_ctz(u32PinIrqStatus)) < IRQ_MAX_NUM) // Count Trailing Zeros ==> Find First One
  74. {
  75. if (pin_irq_hdr_tab[irqindex].pin == pin_index)
  76. return irqindex;
  77. u32PinIrqStatus &= ~(1 << irqindex);
  78. }
  79. return -(RT_ERROR);
  80. }
  81. static void pin_irq_hdr(rt_uint32_t irq_status, rt_uint32_t port_index)
  82. {
  83. rt_int32_t irqindex, i;
  84. rt_int32_t pinindex = port_index * GPIO_PIN_MAX ;
  85. while ((i = nu_ctz(irq_status)) < GPIO_PIN_MAX)// Count Trailing Zeros ==> Find First One
  86. {
  87. int pin_mask = (1 << i);
  88. irqindex = nu_find_irqindex(pinindex + i);
  89. if (irqindex != -(RT_ERROR))
  90. {
  91. if (pin_irq_hdr_tab[irqindex].hdr)
  92. {
  93. pin_irq_hdr_tab[irqindex].hdr(pin_irq_hdr_tab[irqindex].args);
  94. }
  95. }
  96. // Clear the served bit.
  97. irq_status &= ~pin_mask;
  98. }
  99. }
  100. static rt_base_t nu_gpio_pin_get(const char *name)
  101. {
  102. /* Get pin number by name,such as PA.0, PF12 */
  103. if ((name[2] == '\0') || ((name[2] == '.') && (name[3] == '\0')))
  104. return -(RT_EINVAL);
  105. long number;
  106. if ((name[2] == '.'))
  107. number = atol(&name[3]);
  108. else
  109. number = atol(&name[2]);
  110. if (number > 15)
  111. return -(RT_EINVAL);
  112. if (name[1] >= 'A' && name[1] <= 'J')
  113. return ((name[1] - 'A') * 0x10) + number;
  114. if (name[1] >= 'a' && name[1] <= 'i')
  115. return ((name[1] - 'a') * 0x10) + number;
  116. return -(RT_EINVAL);
  117. }
  118. static void nu_gpio_mode(struct rt_device *device, rt_base_t pin, rt_base_t mode)
  119. {
  120. GPIO_PORT PORT;
  121. if (nu_port_check(pin))
  122. return;
  123. PORT = (GPIO_PORT)(GPIOA + (NU_GET_PORT(pin) * PORT_OFFSET));
  124. switch (mode)
  125. {
  126. case PIN_MODE_INPUT_PULLUP:
  127. GPIO_OpenBit(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), DIR_INPUT, PULL_UP);
  128. break;
  129. case PIN_MODE_INPUT_PULLDOWN:
  130. GPIO_OpenBit(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), DIR_INPUT, PULL_DOWN);
  131. break;
  132. case PIN_MODE_OUTPUT:
  133. GPIO_OpenBit(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), DIR_OUTPUT, NO_PULL_UP);
  134. break;
  135. case PIN_MODE_INPUT:
  136. GPIO_OpenBit(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), DIR_INPUT, NO_PULL_UP);
  137. break;
  138. case PIN_MODE_OUTPUT_OD:
  139. default:
  140. LOG_E("Open-drian is not supportted.");
  141. break;
  142. }
  143. }
  144. static void nu_gpio_write(struct rt_device *device, rt_base_t pin, rt_base_t value)
  145. {
  146. GPIO_PORT PORT;
  147. if (nu_port_check(pin))
  148. return;
  149. PORT = (GPIO_PORT)(GPIOA + (NU_GET_PORT(pin) * PORT_OFFSET));
  150. if (value)
  151. GPIO_SetBit(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)));
  152. else
  153. GPIO_ClrBit(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)));
  154. }
  155. static int nu_gpio_read(struct rt_device *device, rt_base_t pin)
  156. {
  157. GPIO_PORT PORT;
  158. if (nu_port_check(pin))
  159. return PIN_LOW;
  160. PORT = (GPIO_PORT)(GPIOA + (NU_GET_PORT(pin) * PORT_OFFSET));
  161. return GPIO_ReadBit(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)));
  162. }
  163. 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)
  164. {
  165. rt_base_t level;
  166. rt_int32_t irqindex;
  167. if (nu_port_check(pin))
  168. return -(RT_ERROR);
  169. level = rt_hw_interrupt_disable();
  170. /* Find index of pin is attached in pool. */
  171. if ((irqindex = nu_find_irqindex(pin)) >= 0)
  172. goto exit_nu_gpio_attach_irq;
  173. /* Find available index of pin in pool. */
  174. if ((irqindex = nu_cto(g_u32PinIrqMask)) < IRQ_MAX_NUM) // Count Trailing Ones ==> Find First Zero
  175. goto exit_nu_gpio_attach_irq;
  176. rt_hw_interrupt_enable(level);
  177. return -(RT_EBUSY);
  178. exit_nu_gpio_attach_irq:
  179. pin_irq_hdr_tab[irqindex].pin = pin;
  180. pin_irq_hdr_tab[irqindex].hdr = hdr;
  181. pin_irq_hdr_tab[irqindex].mode = mode;
  182. pin_irq_hdr_tab[irqindex].args = args;
  183. g_u32PinIrqMask |= (1 << irqindex);
  184. rt_hw_interrupt_enable(level);
  185. return RT_EOK;
  186. }
  187. static rt_err_t nu_gpio_detach_irq(struct rt_device *device, rt_int32_t pin)
  188. {
  189. rt_base_t level;
  190. rt_int32_t irqindex;
  191. rt_int32_t u32PinIrqStatus;
  192. if (nu_port_check(pin))
  193. return -(RT_ERROR);
  194. level = rt_hw_interrupt_disable();
  195. u32PinIrqStatus = g_u32PinIrqMask;
  196. // Find index of pin is attached in pool.
  197. while ((irqindex = nu_ctz(u32PinIrqStatus)) < IRQ_MAX_NUM)// Count Trailing Zeros ==> Find First One
  198. {
  199. if (pin_irq_hdr_tab[irqindex].pin == pin)
  200. {
  201. pin_irq_hdr_tab[irqindex].pin = PIN_IRQ_PIN_NONE;
  202. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  203. pin_irq_hdr_tab[irqindex].mode = PIN_IRQ_MODE_RISING;
  204. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  205. g_u32PinIrqMask &= ~(1 << irqindex);
  206. break;
  207. }
  208. u32PinIrqStatus &= ~(1 << irqindex);
  209. }
  210. rt_hw_interrupt_enable(level);
  211. return RT_EOK;
  212. }
  213. static void nu_gpio_isr(int vector, void *param)
  214. {
  215. int i;
  216. rt_uint32_t u32IntStatus_Port;
  217. u32IntStatus_Port = inpw(REG_GPIO_ISR) | ~((1 << MAX_PORT) - 1);
  218. while ((i = nu_ctz(u32IntStatus_Port)) < MAX_PORT)// Count Trailing Zeros ==> Find First One
  219. {
  220. int port_mask = (1 << i);
  221. rt_uint32_t u32IntStatus_Pins = inpw(REG_GPIOA_ISR + PORT_OFFSET * i);
  222. /* Invoke pins status and port number */
  223. pin_irq_hdr(u32IntStatus_Pins, i);
  224. /* Clear Interrupt flag. */
  225. outpw(REG_GPIOA_ISR + PORT_OFFSET * i, u32IntStatus_Pins);
  226. /* Clear the served bit. */
  227. u32IntStatus_Port &= ~port_mask;
  228. }
  229. /* Clear interrupt */
  230. outpw(REG_AIC_SCCRH, IRQ_GPIO - 1);
  231. }
  232. static rt_err_t nu_gpio_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint32_t enabled)
  233. {
  234. GPIO_PORT PORT;
  235. GPIO_TRIGGER_TYPE triggerType;
  236. rt_base_t level;
  237. rt_int32_t irqindex;
  238. rt_err_t ret = RT_EOK;
  239. if (nu_port_check(pin))
  240. return -(RT_ERROR);
  241. level = rt_hw_interrupt_disable();
  242. irqindex = nu_find_irqindex(pin);
  243. if (irqindex == -(RT_ERROR))
  244. {
  245. ret = RT_ERROR;
  246. goto exit_nu_gpio_irq_enable;
  247. }
  248. PORT = (GPIO_PORT)(GPIOA + (NU_GET_PORT(pin) * PORT_OFFSET));
  249. if (enabled == PIN_IRQ_ENABLE)
  250. {
  251. switch (pin_irq_hdr_tab[irqindex].mode)
  252. {
  253. case PIN_IRQ_MODE_RISING:
  254. triggerType = RISING;
  255. break;
  256. case PIN_IRQ_MODE_FALLING:
  257. triggerType = FALLING;
  258. break;
  259. case PIN_IRQ_MODE_RISING_FALLING:
  260. triggerType = BOTH_EDGE;
  261. break;
  262. case PIN_IRQ_MODE_HIGH_LEVEL:
  263. triggerType = HIGH;
  264. break;
  265. case PIN_IRQ_MODE_LOW_LEVEL:
  266. triggerType = LOW;
  267. break;
  268. default:
  269. goto exit_nu_gpio_irq_enable;
  270. }
  271. GPIO_EnableTriggerType(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)), triggerType);
  272. }
  273. else
  274. {
  275. GPIO_DisableTriggerType(PORT, NU_GET_PIN_MASK(NU_GET_PINS(pin)));
  276. }
  277. exit_nu_gpio_irq_enable:
  278. rt_hw_interrupt_enable(level);
  279. return -(ret);
  280. }
  281. int rt_hw_gpio_init(void)
  282. {
  283. char szTmp[16];
  284. rt_int32_t irqindex;
  285. for (irqindex = 0; irqindex < IRQ_MAX_NUM ; irqindex++)
  286. {
  287. pin_irq_hdr_tab[irqindex].pin = PIN_IRQ_PIN_NONE;
  288. pin_irq_hdr_tab[irqindex].hdr = RT_NULL;
  289. pin_irq_hdr_tab[irqindex].mode = PIN_IRQ_MODE_RISING;
  290. pin_irq_hdr_tab[irqindex].args = RT_NULL;
  291. }
  292. nu_sys_ipclk_enable(GPIOCKEN);
  293. snprintf(szTmp, sizeof(szTmp), "gpio");
  294. rt_hw_interrupt_install(IRQ_GPIO, nu_gpio_isr, RT_NULL, szTmp);
  295. rt_hw_interrupt_set_type(IRQ_GPIO, HIGH_LEVEL_SENSITIVE);
  296. rt_hw_interrupt_umask(IRQ_GPIO);
  297. return rt_device_pin_register("gpio", &nu_gpio_ops, RT_NULL);
  298. }
  299. INIT_BOARD_EXPORT(rt_hw_gpio_init);
  300. #endif //#if (defined(BSP_USING_GPIO) && defined(RT_USING_PIN))