luat_gpio_air101.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "luat_msgbus.h"
  4. #include "luat_timer.h"
  5. #include "luat_gpio.h"
  6. #include "wm_include.h"
  7. #include "luat_irq.h"
  8. // typedef struct wm_gpio_conf
  9. // {
  10. // luat_gpio_irq_cb cb;
  11. // void* args;
  12. // }wm_gpio_conf_t;
  13. // static wm_gpio_conf_t confs[WM_IO_PB_31 + 1];
  14. static void luat_gpio_irq_callback(void *ptr)
  15. {
  16. int pin = (int)ptr;
  17. if (pin < 0 || pin > WM_IO_PB_31)
  18. return;
  19. int ret = tls_get_gpio_irq_status(pin);
  20. if(ret)
  21. {
  22. tls_clr_gpio_irq_status(pin);
  23. luat_gpio_irq_default(pin, (void*)tls_gpio_read(pin));
  24. }
  25. }
  26. int luat_gpio_setup(luat_gpio_t *gpio){
  27. int dir = 0;
  28. int attr = 0;
  29. int irq = 0;
  30. int ret;
  31. if (gpio->pin < 0 || gpio->pin > WM_IO_PB_31) return 0;
  32. switch (gpio->mode){
  33. case Luat_GPIO_OUTPUT:
  34. dir = WM_GPIO_DIR_OUTPUT;
  35. attr = WM_GPIO_ATTR_FLOATING;
  36. break;
  37. case Luat_GPIO_INPUT:
  38. case Luat_GPIO_IRQ:
  39. {
  40. dir = WM_GPIO_DIR_INPUT;
  41. switch (gpio->pull)
  42. {
  43. case Luat_GPIO_PULLUP:
  44. attr = WM_GPIO_ATTR_PULLHIGH;
  45. break;
  46. case Luat_GPIO_PULLDOWN:
  47. attr = WM_GPIO_ATTR_PULLLOW;
  48. break;
  49. case Luat_GPIO_DEFAULT:
  50. default:
  51. attr = WM_GPIO_ATTR_FLOATING;
  52. break;
  53. }
  54. }
  55. break;
  56. default:
  57. dir = WM_GPIO_DIR_INPUT;
  58. attr = WM_GPIO_ATTR_FLOATING;
  59. break;
  60. }
  61. tls_gpio_cfg(gpio->pin, dir, attr);
  62. if (gpio->mode == Luat_GPIO_IRQ)
  63. {
  64. if (gpio->irq == Luat_GPIO_RISING)
  65. {
  66. irq = WM_GPIO_IRQ_TRIG_RISING_EDGE;
  67. }
  68. else if (gpio->irq == Luat_GPIO_FALLING)
  69. {
  70. irq = WM_GPIO_IRQ_TRIG_FALLING_EDGE;
  71. }
  72. else
  73. {
  74. irq = WM_GPIO_IRQ_TRIG_DOUBLE_EDGE;
  75. }
  76. tls_clr_gpio_irq_status(gpio->pin);
  77. tls_gpio_isr_register(gpio->pin, luat_gpio_irq_callback, (void *)gpio->pin);
  78. tls_gpio_irq_enable(gpio->pin, irq);
  79. return 0;
  80. }
  81. else{
  82. tls_gpio_irq_disable(gpio->pin);
  83. }
  84. return 0;
  85. }
  86. int luat_gpio_set(int pin, int level)
  87. {
  88. if (pin < 0 || pin > WM_IO_PB_31) return 0;
  89. tls_gpio_write(pin, level);
  90. return 0;
  91. }
  92. //hyj
  93. void luat_gpio_pulse(int pin, uint8_t *level, uint16_t len,uint16_t delay_ns)
  94. {
  95. if (pin < 0 || pin > WM_IO_PB_31) return 0;
  96. tls_gpio_pulse(pin,level,len,delay_ns);
  97. return 0;
  98. }
  99. int luat_gpio_get(int pin)
  100. {
  101. if (pin < 0 || pin > WM_IO_PB_31) return 0;
  102. int re = tls_gpio_read(pin);
  103. return re;
  104. }
  105. void luat_gpio_close(int pin)
  106. {
  107. if (pin < 0 || pin > WM_IO_PB_31) return;
  108. tls_gpio_cfg(pin, WM_GPIO_DIR_INPUT, WM_GPIO_ATTR_FLOATING);
  109. tls_gpio_irq_disable(pin);
  110. // confs[pin].cb = NULL;
  111. }
  112. // int luat_gpio_set_irq_cb(int pin, luat_gpio_irq_cb cb, void* args) {
  113. // if (pin < 0 || pin > WM_IO_PB_31) return -1;
  114. // if (cb) {
  115. // confs[pin].cb = cb;
  116. // confs[pin].args = args;
  117. // }
  118. // return 0;
  119. // }