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