drv_pin.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (c) 2006-2024, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-03-24 YangXi the first version.
  9. */
  10. #include "drv_pin.h"
  11. #include "fsl_common.h"
  12. #include "fsl_gpio.h"
  13. #include "fsl_port.h"
  14. #include "fsl_inputmux.h"
  15. #ifdef RT_USING_PIN
  16. #define DBG_TAG "drv.pin"
  17. #define DBG_LVL DBG_INFO
  18. #include <rtdbg.h>
  19. #define GET_GPIO_PORT(x) ((x) / 32)
  20. #define GET_GPIO_PIN(x) ((x) % 32)
  21. static struct rt_pin_ops mcx_pin_ops;
  22. static GPIO_Type *GPIO_TYPE_TBL[] = GPIO_BASE_PTRS;
  23. static PORT_Type *PORT_TYPE_TBL[] = PORT_BASE_PTRS;
  24. static IRQn_Type IRQ_TYPE_TBL[] = GPIO_IRQS;
  25. #define PIN2GPIO(x) GPIO_TYPE_TBL[GET_GPIO_PORT(x)]
  26. #define PIN2PORT(x) PORT_TYPE_TBL[GET_GPIO_PORT(x)]
  27. #define PIN2IRQ(x) IRQ_TYPE_TBL[GET_GPIO_PORT(x)]
  28. struct rt_pin_irq_hdr pin_irq_hdr_tab[32*5] = {0};
  29. static void mcx_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
  30. {
  31. port_pin_config_t port_pin_config = {0};
  32. gpio_pin_config_t gpio_pin_config = {0};
  33. port_pin_config.mux = kPORT_MuxAlt0;
  34. switch (mode)
  35. {
  36. case PIN_MODE_OUTPUT:
  37. {
  38. gpio_pin_config.pinDirection = kGPIO_DigitalOutput;
  39. port_pin_config.pullSelect = kPORT_PullDisable;
  40. port_pin_config.inputBuffer = kPORT_InputBufferEnable;
  41. }
  42. break;
  43. case PIN_MODE_INPUT:
  44. {
  45. gpio_pin_config.pinDirection = kGPIO_DigitalInput;
  46. port_pin_config.pullSelect = kPORT_PullDisable;
  47. port_pin_config.inputBuffer = kPORT_InputBufferEnable;
  48. }
  49. break;
  50. case PIN_MODE_INPUT_PULLDOWN:
  51. {
  52. gpio_pin_config.pinDirection = kGPIO_DigitalInput;
  53. port_pin_config.pullSelect = kPORT_PullDown;
  54. port_pin_config.pullValueSelect = kPORT_LowPullResistor;
  55. port_pin_config.inputBuffer = kPORT_InputBufferEnable;
  56. }
  57. break;
  58. case PIN_MODE_INPUT_PULLUP:
  59. {
  60. gpio_pin_config.pinDirection = kGPIO_DigitalInput;
  61. port_pin_config.pullSelect = kPORT_PullUp;
  62. port_pin_config.pullValueSelect = kPORT_LowPullResistor;
  63. port_pin_config.inputBuffer = kPORT_InputBufferEnable;
  64. }
  65. break;
  66. case PIN_MODE_OUTPUT_OD:
  67. {
  68. port_pin_config.openDrainEnable = kPORT_OpenDrainEnable;
  69. gpio_pin_config.pinDirection = kGPIO_DigitalOutput;
  70. port_pin_config.inputBuffer = kPORT_InputBufferEnable;
  71. }
  72. break;
  73. }
  74. PORT_SetPinConfig(PIN2PORT(pin), GET_GPIO_PIN(pin), &port_pin_config);
  75. GPIO_PinInit(PIN2GPIO(pin), GET_GPIO_PIN(pin) , &gpio_pin_config);
  76. }
  77. static void mcx_pin_write(rt_device_t dev, rt_base_t pin, rt_uint8_t value)
  78. {
  79. GPIO_PinWrite(PIN2GPIO(pin), GET_GPIO_PIN(pin), value);
  80. }
  81. static rt_ssize_t mcx_pin_read(rt_device_t dev, rt_base_t pin)
  82. {
  83. return GPIO_PinRead(PIN2GPIO(pin), GET_GPIO_PIN(pin));
  84. }
  85. rt_inline void pin_irq_handler(uint8_t gpio_idx)
  86. {
  87. int i;
  88. rt_interrupt_enter();
  89. uint32_t INTFLAG = GPIO_GpioGetInterruptFlags(GPIO_TYPE_TBL[gpio_idx]);
  90. GPIO_GpioClearInterruptFlags(GPIO_TYPE_TBL[gpio_idx], INTFLAG);
  91. for(i=0; i<ARRAY_SIZE(pin_irq_hdr_tab); i++)
  92. {
  93. if((INTFLAG & (1<<GET_GPIO_PIN(pin_irq_hdr_tab[i].pin))) && pin_irq_hdr_tab[i].hdr && (GET_GPIO_PORT(pin_irq_hdr_tab[i].pin)) == gpio_idx)
  94. {
  95. pin_irq_hdr_tab[i].hdr(pin_irq_hdr_tab[i].args);
  96. }
  97. }
  98. rt_interrupt_leave();
  99. }
  100. void GPIO0_IRQHandler(void)
  101. {
  102. pin_irq_handler(0);
  103. }
  104. void GPIO1_IRQHandler(void)
  105. {
  106. pin_irq_handler(1);
  107. }
  108. void GPIO2_IRQHandler(void)
  109. {
  110. pin_irq_handler(2);
  111. }
  112. void GPIO3_IRQHandler(void)
  113. {
  114. pin_irq_handler(3);
  115. }
  116. void GPIO4_IRQHandler(void)
  117. {
  118. pin_irq_handler(4);
  119. }
  120. static rt_err_t mcx_pin_attach_irq(struct rt_device *device, rt_base_t pin, rt_uint8_t mode, void (*hdr)(void *args), void *args)
  121. {
  122. switch (mode)
  123. {
  124. case PIN_IRQ_MODE_RISING:
  125. GPIO_SetPinInterruptConfig(PIN2GPIO(pin), GET_GPIO_PIN(pin), kGPIO_InterruptRisingEdge);
  126. break;
  127. case PIN_IRQ_MODE_FALLING:
  128. GPIO_SetPinInterruptConfig(PIN2GPIO(pin), GET_GPIO_PIN(pin), kGPIO_InterruptFallingEdge);
  129. break;
  130. case PIN_IRQ_MODE_RISING_FALLING:
  131. GPIO_SetPinInterruptConfig(PIN2GPIO(pin), GET_GPIO_PIN(pin), kGPIO_InterruptEitherEdge);
  132. break;
  133. case PIN_IRQ_MODE_HIGH_LEVEL:
  134. GPIO_SetPinInterruptConfig(PIN2GPIO(pin), GET_GPIO_PIN(pin), kGPIO_InterruptLogicOne);
  135. break;
  136. case PIN_IRQ_MODE_LOW_LEVEL:
  137. GPIO_SetPinInterruptConfig(PIN2GPIO(pin), GET_GPIO_PIN(pin), kGPIO_InterruptLogicZero);
  138. break;
  139. }
  140. pin_irq_hdr_tab[pin].pin = pin;
  141. pin_irq_hdr_tab[pin].mode = mode;
  142. pin_irq_hdr_tab[pin].hdr = hdr;
  143. pin_irq_hdr_tab[pin].args = args;
  144. return RT_EOK;
  145. }
  146. static rt_err_t mcx_pin_detach_irq(struct rt_device *device, rt_base_t pin)
  147. {
  148. GPIO_SetPinInterruptConfig(PIN2GPIO(pin), GET_GPIO_PIN(pin), kGPIO_InterruptStatusFlagDisabled);
  149. return RT_EOK;
  150. }
  151. static rt_err_t mcx_pin_irq_enable(struct rt_device *device, rt_base_t pin, rt_uint8_t enabled)
  152. {
  153. if(enabled)
  154. {
  155. EnableIRQ(PIN2IRQ(pin));
  156. }
  157. else
  158. {
  159. DisableIRQ(PIN2IRQ(pin));
  160. }
  161. return RT_EOK;
  162. }
  163. int rt_hw_pin_init(void)
  164. {
  165. int ret = RT_EOK;
  166. mcx_pin_ops.pin_mode = mcx_pin_mode;
  167. mcx_pin_ops.pin_read = mcx_pin_read;
  168. mcx_pin_ops.pin_write = mcx_pin_write;
  169. mcx_pin_ops.pin_attach_irq = mcx_pin_attach_irq;
  170. mcx_pin_ops.pin_detach_irq = mcx_pin_detach_irq;
  171. mcx_pin_ops.pin_irq_enable = mcx_pin_irq_enable;
  172. mcx_pin_ops.pin_get = RT_NULL,
  173. ret = rt_device_pin_register("pin", &mcx_pin_ops, RT_NULL);
  174. return ret;
  175. }
  176. INIT_BOARD_EXPORT(rt_hw_pin_init);
  177. #endif /*RT_USING_PIN */
  178. // end file