luat_gpio_air105.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #define LUAT_LOG_TAG "luat.gpio"
  30. #include "luat_log.h"
  31. static void luat_gpio_irq_callback(void *ptr, void *pParam)
  32. {
  33. int pin = (int)ptr;
  34. luat_gpio_irq_default(pin, (void*)luat_gpio_get(pin));
  35. }
  36. int luat_gpio_setup(luat_gpio_t *gpio){
  37. if (gpio->pin < HAL_GPIO_2 || gpio->pin > HAL_GPIO_MAX) return 0;
  38. GPIO_Iomux(gpio->pin, 1);
  39. switch (gpio->mode){
  40. case Luat_GPIO_OUTPUT:
  41. GPIO_Config(gpio->pin, 0, 0);
  42. break;
  43. case Luat_GPIO_INPUT:
  44. GPIO_Config(gpio->pin, 1, 0);
  45. break;
  46. case Luat_GPIO_IRQ:{
  47. switch (gpio->irq){
  48. case Luat_GPIO_RISING:
  49. GPIO_ExtiConfig(gpio->pin, 0,1,0);
  50. break;
  51. case Luat_GPIO_FALLING:
  52. GPIO_ExtiConfig(gpio->pin, 0,0,1);
  53. break;
  54. case Luat_GPIO_BOTH:
  55. GPIO_ExtiConfig(gpio->pin, 0,1,1);
  56. default:
  57. break;
  58. }
  59. if (gpio->irq_cb) {
  60. GPIO_ExtiSetCB(gpio->pin, gpio->irq_cb, gpio->irq_args);
  61. }
  62. else
  63. {
  64. GPIO_ExtiSetCB(gpio->pin, luat_gpio_irq_callback, gpio->irq_args);
  65. }
  66. }break;
  67. default:
  68. break;
  69. }
  70. switch (gpio->pull){
  71. case Luat_GPIO_PULLUP:
  72. GPIO_PullConfig(gpio->pin, 1, 1);
  73. break;
  74. case Luat_GPIO_PULLDOWN:
  75. LLOGE("air105 can't config pulldown");
  76. GPIO_PullConfig(gpio->pin, 1, 0);
  77. break;
  78. case Luat_GPIO_DEFAULT:
  79. GPIO_PullConfig(gpio->pin, 0, 0);
  80. default:
  81. break;
  82. }
  83. return 0;
  84. }
  85. int luat_gpio_set(int pin, int level){
  86. if (pin < HAL_GPIO_2 || pin >= HAL_GPIO_MAX) return 0;
  87. GPIO_Output(pin, level);
  88. return 0;
  89. }
  90. int luat_gpio_get(int pin){
  91. if (pin < HAL_GPIO_2 || pin >= HAL_GPIO_MAX) return 0;
  92. int re = GPIO_Input(pin);
  93. return re;
  94. }
  95. void luat_gpio_close(int pin){
  96. if (pin < HAL_GPIO_2 || pin >= HAL_GPIO_MAX) return ;
  97. GPIO_ExtiSetCB(pin, NULL, 0);
  98. GPIO_ExtiConfig(pin, 0,0,0);
  99. return ;
  100. }
  101. void luat_gpio_pulse(int pin, uint8_t *level, uint16_t len, uint16_t delay_ns) {
  102. GPIO_OutPulse(pin, level, len, delay_ns);
  103. }