luat_gpio_air101.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. tls_clr_gpio_irq_status(pin);
  18. //luat_gpio_irq_cb cb = confs[pin].cb;
  19. //if (cb == NULL)
  20. luat_irq_gpio_cb(pin, NULL);
  21. //else
  22. // cb(pin, confs[pin].args);
  23. }
  24. int luat_gpio_setup(luat_gpio_t *gpio){
  25. int dir = 0;
  26. int attr = 0;
  27. int irq = 0;
  28. int ret;
  29. if (gpio->pin < 0 || gpio->pin > WM_IO_PB_31) return 0;
  30. switch (gpio->mode){
  31. case Luat_GPIO_OUTPUT:
  32. dir = WM_GPIO_DIR_OUTPUT;
  33. attr = WM_GPIO_ATTR_FLOATING;
  34. break;
  35. case Luat_GPIO_INPUT:
  36. case Luat_GPIO_IRQ:
  37. {
  38. dir = WM_GPIO_DIR_INPUT;
  39. switch (gpio->pull)
  40. {
  41. case Luat_GPIO_PULLUP:
  42. attr = WM_GPIO_ATTR_PULLHIGH;
  43. break;
  44. case Luat_GPIO_PULLDOWN:
  45. attr = WM_GPIO_ATTR_PULLLOW;
  46. break;
  47. case Luat_GPIO_DEFAULT:
  48. default:
  49. attr = WM_GPIO_ATTR_FLOATING;
  50. break;
  51. }
  52. }
  53. break;
  54. default:
  55. dir = WM_GPIO_DIR_INPUT;
  56. attr = WM_GPIO_ATTR_FLOATING;
  57. break;
  58. }
  59. tls_gpio_cfg(gpio->pin, dir, attr);
  60. if (gpio->mode == Luat_GPIO_IRQ)
  61. {
  62. if (gpio->irq == Luat_GPIO_RISING)
  63. {
  64. irq = WM_GPIO_IRQ_TRIG_RISING_EDGE;
  65. }
  66. else if (gpio->irq == Luat_GPIO_FALLING)
  67. {
  68. irq = WM_GPIO_IRQ_TRIG_FALLING_EDGE;
  69. }
  70. else
  71. {
  72. irq = WM_GPIO_IRQ_TRIG_DOUBLE_EDGE;
  73. }
  74. tls_clr_gpio_irq_status(gpio->pin);
  75. tls_gpio_isr_register(gpio->pin, luat_gpio_irq_callback, (void *)gpio->pin);
  76. // if (gpio->irq_cb) {
  77. // confs[gpio->pin].cb = gpio->irq_cb;
  78. // confs[gpio->pin].args = gpio->irq_args;
  79. // }
  80. tls_gpio_irq_enable(gpio->pin, irq);
  81. return 0;
  82. }
  83. else{
  84. tls_gpio_irq_disable(gpio->pin);
  85. }
  86. return 0;
  87. }
  88. int luat_gpio_set(int pin, int level)
  89. {
  90. if (pin < 0 || pin > WM_IO_PB_31) return 0;
  91. tls_gpio_write(pin, level);
  92. return 0;
  93. }
  94. int luat_gpio_get(int pin)
  95. {
  96. if (pin < 0 || pin > WM_IO_PB_31) return 0;
  97. int re = tls_gpio_read(pin);
  98. return re;
  99. }
  100. void luat_gpio_close(int pin)
  101. {
  102. if (pin < 0 || pin > WM_IO_PB_31) return;
  103. tls_gpio_cfg(pin, WM_GPIO_DIR_INPUT, WM_GPIO_ATTR_FLOATING);
  104. tls_gpio_irq_disable(pin);
  105. // confs[pin].cb = NULL;
  106. }
  107. // int luat_gpio_set_irq_cb(int pin, luat_gpio_irq_cb cb, void* args) {
  108. // if (pin < 0 || pin > WM_IO_PB_31) return -1;
  109. // if (cb) {
  110. // confs[pin].cb = cb;
  111. // confs[pin].args = args;
  112. // }
  113. // return 0;
  114. // }