gpio-esp32.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * esp32 / esp-idf gpio
  3. *
  4. * Copyright (C) 2019 - 2020 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #include <libwebsockets.h>
  25. static void
  26. lws_gpio_esp32_mode(_lws_plat_gpio_t gpio, int flags)
  27. {
  28. int mode, pup = GPIO_FLOATING;
  29. switch (flags & (LWSGGPIO_FL_READ | LWSGGPIO_FL_WRITE)) {
  30. default:
  31. lwsl_err("%s: neither read nor write\n", __func__);
  32. return;
  33. case LWSGGPIO_FL_READ:
  34. mode = GPIO_MODE_INPUT;
  35. break;
  36. case LWSGGPIO_FL_WRITE:
  37. mode = GPIO_MODE_OUTPUT;
  38. break;
  39. case LWSGGPIO_FL_READ | LWSGGPIO_FL_WRITE:
  40. mode = GPIO_MODE_INPUT_OUTPUT;
  41. break;
  42. }
  43. switch (flags & (LWSGGPIO_FL_PULLUP | LWSGGPIO_FL_PULLDOWN)) {
  44. default:
  45. break;
  46. case LWSGGPIO_FL_PULLUP:
  47. pup = GPIO_PULLUP_ONLY;
  48. break;
  49. case LWSGGPIO_FL_PULLDOWN:
  50. pup = GPIO_PULLDOWN_ONLY;
  51. break;
  52. case LWSGGPIO_FL_PULLUP | LWSGGPIO_FL_PULLDOWN:
  53. pup = GPIO_PULLUP_PULLDOWN;
  54. break;
  55. }
  56. gpio_reset_pin(gpio);
  57. gpio_set_direction(gpio, mode);
  58. gpio_set_pull_mode(gpio, pup);
  59. gpio_set_level(gpio, flags & LWSGGPIO_FL_START_LOW ? 0 : 1);
  60. }
  61. static int
  62. lws_gpio_esp32_read(_lws_plat_gpio_t gpio)
  63. {
  64. return gpio_get_level(gpio);
  65. }
  66. static void
  67. lws_gpio_esp32_set(_lws_plat_gpio_t gpio, int val)
  68. {
  69. gpio_set_level(gpio, val);
  70. }
  71. static int
  72. lws_gpio_esp32_irq_mode(_lws_plat_gpio_t gpio, lws_gpio_irq_t irq_type,
  73. lws_gpio_irq_cb_t cb, void *arg)
  74. {
  75. if (gpio_set_intr_type(gpio, irq_type))
  76. return 1;
  77. if (cb)
  78. return gpio_isr_handler_add(gpio, cb, arg);
  79. return gpio_isr_handler_remove(gpio);
  80. }
  81. const lws_gpio_ops_t lws_gpio_plat = {
  82. .mode = lws_gpio_esp32_mode,
  83. .read = lws_gpio_esp32_read,
  84. .set = lws_gpio_esp32_set,
  85. .irq_mode = lws_gpio_esp32_irq_mode,
  86. };