Skip to content

Commit 9768e98

Browse files
Felipe Nevescfriedt
authored andcommitted
drivers: gpio_esp32: added support for esp32c3
in the gpio drivers and pinmux for esp32 chip series Signed-off-by: Felipe Neves <[email protected]>
1 parent 2977339 commit 9768e98

File tree

6 files changed

+73
-6
lines changed

6 files changed

+73
-6
lines changed

boards/riscv/esp32c3_devkitm/esp32c3_devkitm.dts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
zephyr,console = &uart0;
1818
zephyr,shell-uart = &uart0;
1919
};
20+
21+
aliases {
22+
sw0 = &user_button1;
23+
};
24+
25+
gpio_keys {
26+
compatible = "gpio-keys";
27+
user_button1: button_1 {
28+
label = "User SW1";
29+
gpios = <&gpio0 2 (GPIO_PULL_UP | GPIO_ACTIVE_LOW)>;
30+
};
31+
};
2032
};
2133

2234
&uart0 {
@@ -27,3 +39,6 @@
2739
&trng0 {
2840
status = "okay";
2941
};
42+
&gpio0 {
43+
status = "okay";
44+
};

boards/riscv/esp32c3_devkitm/esp32c3_devkitm_defconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,7 @@ CONFIG_SERIAL=y
1010
CONFIG_UART_CONSOLE=y
1111
CONFIG_UART_ROM_ESP32C3=y
1212
CONFIG_XIP=n
13+
CONFIG_PINMUX=y
14+
CONFIG_PINMUX_ESP32=y
15+
CONFIG_GPIO=y
16+
CONFIG_GPIO_ESP32=y

drivers/gpio/Kconfig.esp32

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
menuconfig GPIO_ESP32
77
bool "ESP32 GPIO"
8-
depends on SOC_ESP32 || SOC_ESP32S2
8+
depends on SOC_ESP32 || SOC_ESP32S2 || SOC_ESP32C3
99
help
1010
Enables the ESP32 GPIO driver
1111

@@ -19,7 +19,8 @@ config GPIO_ESP32_0
1919

2020
config GPIO_ESP32_1
2121
bool "ESP32 GPIO (pins 32-39)"
22-
default y
22+
default y if SOC_ESP32 || SOC_ESP32S2
23+
default n if SOC_ESP32C3
2324
help
2425
Include support for GPIO pins 32-39 on the ESP32.
2526

drivers/gpio/gpio_esp32.c

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#define DT_DRV_COMPAT espressif_esp32_gpio
99

1010
/* Include esp-idf headers first to avoid redefining BIT() macro */
11+
#ifndef CONFIG_SOC_ESP32C3
1112
#include <soc/dport_reg.h>
13+
#endif
1214
#include <soc/gpio_reg.h>
1315
#include <soc/io_mux_reg.h>
1416
#include <soc/soc.h>
@@ -18,7 +20,11 @@
1820
#include <errno.h>
1921
#include <device.h>
2022
#include <drivers/gpio.h>
23+
#ifdef CONFIG_SOC_ESP32C3
24+
#include <drivers/interrupt_controller/intc_esp32c3.h>
25+
#else
2126
#include <drivers/interrupt_controller/intc_esp32.h>
27+
#endif
2228
#include <kernel.h>
2329
#include <sys/util.h>
2430
#include <drivers/pinmux.h>
@@ -30,6 +36,20 @@ LOG_MODULE_REGISTER(gpio_esp32, CONFIG_LOG_DEFAULT_LEVEL);
3036

3137
#define DEV_CFG(_dev) ((struct gpio_esp32_config *const)(_dev)->config)
3238

39+
#ifdef CONFIG_SOC_ESP32C3
40+
/* gpio structs in esp32c3 series are diferent from xtensa ones */
41+
#define out out.data
42+
#define in in.data
43+
#define out_w1ts out_w1ts.val
44+
#define out_w1tc out_w1tc.val
45+
/* arch_curr_cpu() is not available for riscv based chips */
46+
#define CPU_ID() 0
47+
#define ISR_HANDLER isr_handler_t
48+
#else
49+
#define CPU_ID() arch_curr_cpu()->id
50+
#define ISR_HANDLER intr_handler_t
51+
#endif
52+
3353
struct gpio_esp32_config {
3454
/* gpio_driver_config needs to be first */
3555
struct gpio_driver_config drv_cfg;
@@ -144,8 +164,10 @@ static int gpio_esp32_port_get_raw(const struct device *port, uint32_t *value)
144164

145165
if (cfg->gpio_port0) {
146166
*value = cfg->gpio_dev->in;
167+
#if defined(CONFIG_GPIO_ESP32_1)
147168
} else {
148169
*value = cfg->gpio_dev->in1.data;
170+
#endif
149171
}
150172

151173
return 0;
@@ -160,8 +182,10 @@ static int gpio_esp32_port_set_masked_raw(const struct device *port,
160182

161183
if (cfg->gpio_port0) {
162184
cfg->gpio_dev->out = (cfg->gpio_dev->out & ~mask) | (mask & value);
185+
#if defined(CONFIG_GPIO_ESP32_1)
163186
} else {
164187
cfg->gpio_dev->out1.data = (cfg->gpio_dev->out1.data & ~mask) | (mask & value);
188+
#endif
165189
}
166190

167191
irq_unlock(key);
@@ -176,8 +200,10 @@ static int gpio_esp32_port_set_bits_raw(const struct device *port,
176200

177201
if (cfg->gpio_port0) {
178202
cfg->gpio_dev->out_w1ts = pins;
203+
#if defined(CONFIG_GPIO_ESP32_1)
179204
} else {
180205
cfg->gpio_dev->out1_w1ts.data = pins;
206+
#endif
181207
}
182208

183209
return 0;
@@ -190,8 +216,10 @@ static int gpio_esp32_port_clear_bits_raw(const struct device *port,
190216

191217
if (cfg->gpio_port0) {
192218
cfg->gpio_dev->out_w1tc = pins;
219+
#if defined(CONFIG_GPIO_ESP32_1)
193220
} else {
194221
cfg->gpio_dev->out1_w1tc.data = pins;
222+
#endif
195223
}
196224

197225
return 0;
@@ -205,8 +233,10 @@ static int gpio_esp32_port_toggle_bits(const struct device *port,
205233

206234
if (cfg->gpio_port0) {
207235
cfg->gpio_dev->out ^= pins;
236+
#if defined(CONFIG_GPIO_ESP32_1)
208237
} else {
209238
cfg->gpio_dev->out1.data ^= pins;
239+
#endif
210240
}
211241

212242
irq_unlock(key);
@@ -281,7 +311,7 @@ static uint32_t gpio_esp32_get_pending_int(const struct device *dev)
281311
{
282312
struct gpio_esp32_config *const cfg = DEV_CFG(dev);
283313
uint32_t irq_status;
284-
uint32_t const core_id = arch_curr_cpu()->id;
314+
uint32_t const core_id = CPU_ID();
285315

286316
#if defined(CONFIG_GPIO_ESP32_1)
287317
gpio_ll_get_intr_status_high(cfg->gpio_base, core_id, &irq_status);
@@ -296,7 +326,7 @@ static void IRAM_ATTR gpio_esp32_fire_callbacks(const struct device *dev)
296326
struct gpio_esp32_config *const cfg = DEV_CFG(dev);
297327
struct gpio_esp32_data *data = dev->data;
298328
uint32_t irq_status;
299-
uint32_t const core_id = arch_curr_cpu()->id;
329+
uint32_t const core_id = CPU_ID();
300330

301331
#if defined(CONFIG_GPIO_ESP32_1)
302332
gpio_ll_get_intr_status_high(cfg->gpio_base, core_id, &irq_status);
@@ -327,7 +357,7 @@ static int gpio_esp32_init(const struct device *dev)
327357
}
328358

329359
if (!isr_connected) {
330-
esp_intr_alloc(DT_IRQN(DT_NODELABEL(gpio0)), 0, gpio_esp32_isr, (void *)dev, NULL);
360+
esp_intr_alloc(DT_IRQN(DT_NODELABEL(gpio0)), 0, (ISR_HANDLER)gpio_esp32_isr, (void *)dev, NULL);
331361
isr_connected = true;
332362
}
333363

drivers/pinmux/Kconfig.esp32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
config PINMUX_ESP32
77
bool "ESP32 Pin multiplexer driver"
8-
depends on SOC_ESP32 || SOC_ESP32S2
8+
depends on SOC_ESP32 || SOC_ESP32S2 || SOC_ESP32C3
99
help
1010
Enable driver for ESP32 Pin multiplexer.

dts/riscv/espressif/esp32c3.dtsi

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66
#include <mem.h>
7+
#include <dt-bindings/gpio/gpio.h>
78
#include <dt-bindings/interrupt-controller/esp-esp32c3-intmux.h>
89

910
/ {
@@ -35,6 +36,11 @@
3536
reg = <0x3fc7c000 0x50000>;
3637
};
3738

39+
pinmux: pinmux@60009000 {
40+
compatible = "espressif,esp32-pinmux";
41+
reg = <0x60009000 0x94>;
42+
};
43+
3844
intc: interrupt-controller@600c2000 {
3945
#interrupt-cells = <1>;
4046
compatible = "espressif,esp32-intc";
@@ -53,6 +59,17 @@
5359
status = "okay";
5460
};
5561

62+
gpio0: gpio@60004000 {
63+
compatible = "espressif,esp32-gpio";
64+
gpio-controller;
65+
#gpio-cells = <2>;
66+
reg = <0x60004000 0x800>;
67+
interrupts = <GPIO_INTR_SOURCE>;
68+
interrupt-parent = <&intc>;
69+
label = "GPIO_0";
70+
ngpios = <32>; /* 0..31 */
71+
};
72+
5673
uart0: uart@60000000 {
5774
compatible = "espressif,esp32c3-uart";
5875
reg = <0x60000000 0x400>;

0 commit comments

Comments
 (0)