1 /*
2 * esp32 / esp-idf gpio
3 *
4 * Copyright (C) 2019 - 2020 Andy Green <andy@warmcat.com>
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
25 #include <libwebsockets.h>
26
27 static void
lws_gpio_esp32_mode(_lws_plat_gpio_t gpio,int flags)28 lws_gpio_esp32_mode(_lws_plat_gpio_t gpio, int flags)
29 {
30 int mode, pup = GPIO_FLOATING;
31
32 switch (flags & (LWSGGPIO_FL_READ | LWSGGPIO_FL_WRITE)) {
33 default:
34 lwsl_err("%s: neither read nor write\n", __func__);
35 return;
36 case LWSGGPIO_FL_READ:
37 mode = GPIO_MODE_INPUT;
38 break;
39 case LWSGGPIO_FL_WRITE:
40 mode = GPIO_MODE_OUTPUT;
41 break;
42 case LWSGGPIO_FL_READ | LWSGGPIO_FL_WRITE:
43 mode = GPIO_MODE_INPUT_OUTPUT;
44 break;
45 }
46
47 switch (flags & (LWSGGPIO_FL_PULLUP | LWSGGPIO_FL_PULLDOWN)) {
48 default:
49 break;
50 case LWSGGPIO_FL_PULLUP:
51 pup = GPIO_PULLUP_ONLY;
52 break;
53 case LWSGGPIO_FL_PULLDOWN:
54 pup = GPIO_PULLDOWN_ONLY;
55 break;
56 case LWSGGPIO_FL_PULLUP | LWSGGPIO_FL_PULLDOWN:
57 pup = GPIO_PULLUP_PULLDOWN;
58 break;
59 }
60
61 gpio_reset_pin(gpio);
62 gpio_set_direction(gpio, mode);
63 gpio_set_pull_mode(gpio, pup);
64 gpio_set_level(gpio, flags & LWSGGPIO_FL_START_LOW ? 0 : 1);
65 }
66
67 static int
lws_gpio_esp32_read(_lws_plat_gpio_t gpio)68 lws_gpio_esp32_read(_lws_plat_gpio_t gpio)
69 {
70 return gpio_get_level(gpio);
71 }
72 static void
lws_gpio_esp32_set(_lws_plat_gpio_t gpio,int val)73 lws_gpio_esp32_set(_lws_plat_gpio_t gpio, int val)
74 {
75 gpio_set_level(gpio, val);
76 }
77
78 static int
lws_gpio_esp32_irq_mode(_lws_plat_gpio_t gpio,lws_gpio_irq_t irq_type,lws_gpio_irq_cb_t cb,void * arg)79 lws_gpio_esp32_irq_mode(_lws_plat_gpio_t gpio, lws_gpio_irq_t irq_type,
80 lws_gpio_irq_cb_t cb, void *arg)
81 {
82 if (gpio_set_intr_type(gpio, irq_type))
83 return 1;
84
85 if (cb)
86 return gpio_isr_handler_add(gpio, cb, arg);
87
88 return gpio_isr_handler_remove(gpio);
89 }
90
91 const lws_gpio_ops_t lws_gpio_plat = {
92 .mode = lws_gpio_esp32_mode,
93 .read = lws_gpio_esp32_read,
94 .set = lws_gpio_esp32_set,
95 .irq_mode = lws_gpio_esp32_irq_mode,
96 };
97