drv_pin.c 5.9 KB

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