luat_gpio_air105.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. #include "luat_base.h"
  22. #include "luat_malloc.h"
  23. #include "luat_msgbus.h"
  24. #include "luat_timer.h"
  25. #include "luat_gpio.h"
  26. #include "core_gpio.h"
  27. #include "platform_define.h"
  28. #include "luat_irq.h"
  29. //typedef struct wm_gpio_conf
  30. //{
  31. // luat_gpio_irq_cb cb;
  32. // void* args;
  33. //}wm_gpio_conf_t;
  34. //
  35. //
  36. //static wm_gpio_conf_t confs[HAL_GPIO_MAX];
  37. //static void luat_gpio_irq_callback(void *ptr, void *pParam)
  38. //{
  39. // int pin = (int)ptr;
  40. // luat_gpio_irq_cb cb = confs[pin].cb;
  41. // if (cb == NULL)
  42. // luat_irq_gpio_cb(pin, confs[pin].args);
  43. // else
  44. // cb(pin, confs[pin].args);
  45. //}
  46. int luat_gpio_setup(luat_gpio_t *gpio){
  47. if (gpio->pin < HAL_GPIO_2 || gpio->pin > HAL_GPIO_MAX) return 0;
  48. GPIO_Iomux(gpio->pin, 1);
  49. switch (gpio->mode){
  50. case Luat_GPIO_OUTPUT:
  51. GPIO_Config(gpio->pin, 0, 0);
  52. break;
  53. case Luat_GPIO_INPUT:
  54. GPIO_Config(gpio->pin, 1, 0);
  55. break;
  56. case Luat_GPIO_IRQ:{
  57. switch (gpio->irq){
  58. case Luat_GPIO_RISING:
  59. GPIO_ExtiConfig(gpio->pin, 0,1,0);
  60. break;
  61. case Luat_GPIO_FALLING:
  62. GPIO_ExtiConfig(gpio->pin, 0,0,1);
  63. break;
  64. case Luat_GPIO_BOTH:
  65. GPIO_ExtiConfig(gpio->pin, 0,1,1);
  66. default:
  67. break;
  68. }
  69. if (gpio->irq_cb) {
  70. GPIO_ExtiSetCB(gpio->pin, gpio->irq_cb, gpio->irq_args);
  71. }
  72. else
  73. {
  74. GPIO_ExtiSetCB(gpio->pin, luat_irq_gpio_cb, gpio->irq_args);
  75. }
  76. }break;
  77. default:
  78. break;
  79. }
  80. switch (gpio->pull){
  81. case Luat_GPIO_PULLUP:
  82. GPIO_PullConfig(gpio->pin, 1, 1);
  83. break;
  84. case Luat_GPIO_PULLDOWN:
  85. GPIO_PullConfig(gpio->pin, 1, 0);
  86. break;
  87. case Luat_GPIO_DEFAULT:
  88. GPIO_PullConfig(gpio->pin, 0, 0);
  89. default:
  90. break;
  91. }
  92. return 0;
  93. }
  94. int luat_gpio_set(int pin, int level){
  95. if (pin < HAL_GPIO_2 || pin >= HAL_GPIO_MAX) return 0;
  96. GPIO_Output(pin, level);
  97. return 0;
  98. }
  99. int luat_gpio_get(int pin){
  100. if (pin < HAL_GPIO_2 || pin >= HAL_GPIO_MAX) return 0;
  101. int re = GPIO_Input(pin);
  102. return re;
  103. }
  104. void luat_gpio_close(int pin){
  105. if (pin < HAL_GPIO_2 || pin >= HAL_GPIO_MAX) return ;
  106. GPIO_ExtiSetCB(pin, NULL, 0);
  107. GPIO_ExtiConfig(pin, 0,0,0);
  108. return ;
  109. }
  110. void luat_gpio_init(void){
  111. // GPIO_GlobalInit(luat_gpio_irq_callback);
  112. }
  113. int luat_gpio_set_irq_cb(int pin, luat_gpio_irq_cb cb, void* args) {
  114. if (pin < HAL_GPIO_2 || pin >= HAL_GPIO_MAX) return -1;
  115. if (cb) {
  116. GPIO_ExtiSetCB(pin, cb, args);
  117. }
  118. return 0;
  119. }