Skip to content

Commit 232e2c5

Browse files
LucasTamborkartben
authored andcommitted
drivers: uart: espressif: Add LP UART driver
Add LP UART driver for LP Core Signed-off-by: Lucas Tamborrino <lucas.tamborrino@espressif.com>
1 parent 0b79b47 commit 232e2c5

File tree

12 files changed

+285
-1
lines changed

12 files changed

+285
-1
lines changed

boards/espressif/esp32c6_devkitc/esp32c6_devkitc_hpcore.dts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/dts-v1/;
88

99
#include <espressif/esp32c6/esp32c6_wroom_n8.dtsi>
10-
#include "esp32c6_devkitc-pinctrl.dtsi"
10+
#include "esp32c6_devkitc_hpcore-pinctrl.dtsi"
1111
#include <zephyr/dt-bindings/input/input-event-codes.h>
1212
#include <espressif/partitions_0x0_default.dtsi>
1313

boards/espressif/esp32c6_devkitc/esp32c6_devkitc_lpcore.dts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,12 @@
1515
chosen {
1616
zephyr,sram = &sramlp;
1717
zephyr,code-partition = &slot0_lpcore_partition;
18+
zephyr,console = &lp_uart;
19+
zephyr,shell-uart = &lp_uart;
1820
};
1921
};
22+
23+
&lp_uart {
24+
status = "okay";
25+
current-speed = <115200>;
26+
};

boards/espressif/esp32c6_devkitc/esp32c6_devkitc_lpcore.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ toolchain:
66
- zephyr
77
supported:
88
- cpu
9+
- uart
10+
- serial
911
testing:
1012
only_tags:
1113
- introduction

boards/espressif/esp32c6_devkitc/esp32c6_devkitc_lpcore_defconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ CONFIG_THREAD_CUSTOM_DATA=n
99
CONFIG_BOOT_BANNER=n
1010

1111
# Console
12+
CONFIG_SERIAL=y
13+
CONFIG_CONSOLE=y
14+
CONFIG_UART_CONSOLE=y
1215
CONFIG_PRINTK=n
16+
CONFIG_CBPRINTF_NANO=y
1317

1418
# Build
1519
CONFIG_SIZE_OPTIMIZATIONS=y

drivers/serial/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ zephyr_library_sources_ifdef(CONFIG_USB_CDC_ACM ${ZEPHYR_BASE}/misc/empty_file.c
1515

1616
# zephyr-keep-sorted-start
1717
zephyr_library_sources_ifdef(CONFIG_LEUART_GECKO leuart_gecko.c)
18+
zephyr_library_sources_ifdef(CONFIG_LPUART_ESP32 lpuart_esp32.c)
1819
zephyr_library_sources_ifdef(CONFIG_SERIAL_ESP32_USB serial_esp32_usb.c)
1920
zephyr_library_sources_ifdef(CONFIG_UART_ALTERA uart_altera.c)
2021
zephyr_library_sources_ifdef(CONFIG_UART_ALTERA_JTAG uart_altera_jtag.c)

drivers/serial/Kconfig.esp32

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ config UART_ESP32_RX_FIFO_THRESH
4343
range 1 127
4444
help
4545
Configure the RX FIFO threshold for ESP32 UART driver.
46+
47+
config LPUART_ESP32
48+
bool "ESP32 LP UART driver"
49+
default y
50+
depends on DT_HAS_ESPRESSIF_ESP32_LPUART_ENABLED
51+
select SERIAL_HAS_DRIVER
52+
help
53+
Enable the ESP32 LP UART.

drivers/serial/lpuart_esp32.c

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*
2+
* Copyright (c) 2025 Espressif Systems (Shanghai) Co., Ltd.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define DT_DRV_COMPAT espressif_esp32_lpuart
8+
9+
#include <zephyr/drivers/uart.h>
10+
#include <zephyr/dt-bindings/clock/esp32c6_clock.h>
11+
12+
#include <hal/uart_hal.h>
13+
#if defined(CONFIG_SOC_ESP32C6_HPCORE)
14+
#include <hal/uart_ll.h>
15+
#include <hal/clk_tree_ll.h>
16+
#include <hal/clk_tree_hal.h>
17+
#include <hal/rtc_io_hal.h>
18+
#include <soc/uart_pins.h>
19+
#include <soc/rtc_io_periph.h>
20+
#include <esp_private/esp_clk_tree_common.h>
21+
#include <ulp_lp_core_uart.h>
22+
#endif
23+
24+
#define ESP_LP_UART_TX_IDLE_NUM_DEFAULT (0U)
25+
26+
struct lp_uart_esp32_data {
27+
uart_hal_context_t hal;
28+
};
29+
30+
struct lp_uart_esp32_config {
31+
uint8_t tx_io_num;
32+
uint8_t rx_io_num;
33+
uint8_t rts_io_num;
34+
uint8_t cts_io_num;
35+
int baud_rate;
36+
uint8_t data_bits;
37+
uint8_t parity;
38+
uint8_t stop_bits;
39+
uint8_t flow_ctrl;
40+
uint8_t rx_flow_ctrl_thresh;
41+
uint8_t lp_uart_source_clk;
42+
};
43+
44+
static int lp_uart_esp32_poll_in(const struct device *dev, unsigned char *p_char)
45+
{
46+
struct lp_uart_esp32_data *data = dev->data;
47+
int inout_rd_len = 1;
48+
49+
if (uart_hal_get_rxfifo_len(&data->hal) == 0) {
50+
return -1;
51+
}
52+
53+
uart_hal_read_rxfifo(&data->hal, p_char, &inout_rd_len);
54+
55+
return 0;
56+
}
57+
58+
static void lp_uart_esp32_poll_out(const struct device *dev, unsigned char c)
59+
{
60+
struct lp_uart_esp32_data *data = dev->data;
61+
int tx_len = 0;
62+
63+
/* Wait for space in FIFO */
64+
while (uart_hal_get_txfifo_len(&data->hal) == 0) {
65+
; /* Wait */
66+
}
67+
68+
uart_hal_write_txfifo(&data->hal, (const void *)&c, 1, &tx_len);
69+
}
70+
71+
#if defined(CONFIG_SOC_ESP32C6_HPCORE)
72+
73+
static int lp_uart_esp32_param_config(const struct device *dev)
74+
{
75+
const struct lp_uart_esp32_config *const cfg = dev->config;
76+
struct lp_uart_esp32_data *data = dev->data;
77+
uint32_t sclk_freq = 0;
78+
79+
if ((cfg->rx_flow_ctrl_thresh > SOC_LP_UART_FIFO_LEN) ||
80+
(cfg->flow_ctrl > UART_CFG_FLOW_CTRL_RTS_CTS) ||
81+
(cfg->data_bits > UART_CFG_DATA_BITS_8)) {
82+
return -EINVAL;
83+
}
84+
85+
/* Get LP UART source clock frequency */
86+
switch (clk_ll_rtc_fast_get_src()) {
87+
case SOC_RTC_FAST_CLK_SRC_XTAL_DIV:
88+
#if CONFIG_SOC_SERIES_ESP32 || CONFIG_SOC_SERIES_ESP32S2 /* SOC_RTC_FAST_CLK_SRC_XTAL_D4 */
89+
sclk_freq = clk_hal_xtal_get_freq_mhz() * MHZ(1) >> 2;
90+
#else /* SOC_RTC_FAST_CLK_SRC_XTAL_D2 */
91+
sclk_freq = clk_hal_xtal_get_freq_mhz() * MHZ(1) >> 1;
92+
#endif
93+
break;
94+
case SOC_RTC_FAST_CLK_SRC_RC_FAST:
95+
sclk_freq =
96+
esp_clk_tree_rc_fast_get_freq_hz(ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED) /
97+
clk_ll_rc_fast_get_divider();
98+
break;
99+
#if SOC_CLK_LP_FAST_SUPPORT_LP_PLL
100+
case SOC_RTC_FAST_CLK_SRC_LP_PLL:
101+
sclk_freq = clk_ll_lp_pll_get_freq_mhz() * MHZ(1);
102+
break;
103+
#endif
104+
default:
105+
return -EINVAL;
106+
}
107+
108+
lp_uart_ll_enable_bus_clock(0, true);
109+
lp_uart_ll_set_source_clk(data->hal.dev, cfg->lp_uart_source_clk);
110+
lp_uart_ll_sclk_enable(0);
111+
112+
/* Initialize LP UART HAL with default parameters */
113+
uart_hal_init(&data->hal, LP_UART_NUM_0);
114+
115+
/* Set protocol parameters from the configuration */
116+
lp_uart_ll_set_baudrate(data->hal.dev, cfg->baud_rate, sclk_freq);
117+
uart_hal_set_parity(&data->hal, cfg->parity);
118+
uart_hal_set_data_bit_num(&data->hal, cfg->data_bits);
119+
uart_hal_set_stop_bits(&data->hal, cfg->stop_bits);
120+
uart_hal_set_tx_idle_num(&data->hal, ESP_LP_UART_TX_IDLE_NUM_DEFAULT);
121+
uart_hal_set_hw_flow_ctrl(&data->hal, cfg->flow_ctrl, cfg->rx_flow_ctrl_thresh);
122+
123+
/* Reset Tx/Rx FIFOs */
124+
uart_hal_rxfifo_rst(&data->hal);
125+
uart_hal_txfifo_rst(&data->hal);
126+
127+
return 0;
128+
}
129+
130+
static void lp_uart_esp32_config_io(int pin, int direction, int func)
131+
{
132+
int rtc_io_num = rtc_io_num_map[pin];
133+
134+
rtcio_hal_function_select(rtc_io_num, RTCIO_FUNC_RTC);
135+
rtcio_hal_set_direction(rtc_io_num, direction);
136+
rtcio_hal_iomux_func_sel(rtc_io_num, func);
137+
}
138+
139+
static void lp_uart_esp32_set_pin(const struct device *dev)
140+
{
141+
const struct lp_uart_esp32_config *const cfg = dev->config;
142+
143+
/* Configure Tx Pin */
144+
lp_uart_esp32_config_io(cfg->tx_io_num, RTC_GPIO_MODE_OUTPUT_ONLY, LP_U0TXD_MUX_FUNC);
145+
146+
/* Configure Rx Pin */
147+
lp_uart_esp32_config_io(cfg->rx_io_num, RTC_GPIO_MODE_INPUT_ONLY, LP_U0RXD_GPIO_NUM);
148+
149+
/* Configure RTS Pin */
150+
lp_uart_esp32_config_io(cfg->rts_io_num, RTC_GPIO_MODE_OUTPUT_ONLY, LP_U0RTS_MUX_FUNC);
151+
152+
/* Configure CTS Pin */
153+
lp_uart_esp32_config_io(cfg->cts_io_num, RTC_GPIO_MODE_INPUT_ONLY, LP_U0CTS_MUX_FUNC);
154+
}
155+
156+
static int lp_uart_esp32_init(const struct device *dev)
157+
{
158+
int ret = 0;
159+
160+
ret = lp_uart_esp32_param_config(dev);
161+
if (ret != 0) {
162+
return -EINVAL;
163+
}
164+
165+
/* Configure LP UART IO pins */
166+
lp_uart_esp32_set_pin(dev);
167+
return 0;
168+
}
169+
170+
#endif /* CONFIG_SOC_ESP32C6_HPCORE */
171+
172+
static DEVICE_API(uart, lp_uart_esp32_api) = {
173+
.poll_in = lp_uart_esp32_poll_in,
174+
.poll_out = lp_uart_esp32_poll_out,
175+
};
176+
177+
static struct lp_uart_esp32_data lp_uart_esp32_data = {
178+
.hal = {
179+
.dev = (uart_dev_t *)DT_REG_ADDR(DT_NODELABEL(lp_uart)),
180+
},
181+
};
182+
183+
static const struct lp_uart_esp32_config lp_uart_esp32_cfg = {
184+
.tx_io_num = DT_PROP(DT_NODELABEL(lp_uart), tx_pin),
185+
.rx_io_num = DT_PROP(DT_NODELABEL(lp_uart), rx_pin),
186+
.rts_io_num = DT_PROP(DT_NODELABEL(lp_uart), rts_pin),
187+
.cts_io_num = DT_PROP(DT_NODELABEL(lp_uart), cts_pin),
188+
.baud_rate = DT_PROP(DT_NODELABEL(lp_uart), current_speed),
189+
.data_bits = DT_PROP_OR(DT_NODELABEL(lp_uart), data_bits, UART_CFG_DATA_BITS_8),
190+
.parity = DT_ENUM_IDX(DT_NODELABEL(lp_uart), parity),
191+
.stop_bits = DT_PROP_OR(DT_NODELABEL(lp_uart), stop_bits, UART_CFG_STOP_BITS_1),
192+
.flow_ctrl = DT_PROP_OR(DT_NODELABEL(lp_uart), flow_ctrl, UART_CFG_FLOW_CTRL_NONE),
193+
.rx_flow_ctrl_thresh = 0,
194+
.lp_uart_source_clk = LP_UART_SCLK_DEFAULT,
195+
};
196+
197+
#if defined(CONFIG_SOC_ESP32C6_HPCORE)
198+
#define LP_UART_ESP32_INIT_FUNC lp_uart_esp32_init
199+
#else
200+
#define LP_UART_ESP32_INIT_FUNC NULL
201+
#endif
202+
203+
DEVICE_DT_DEFINE(DT_NODELABEL(lp_uart), LP_UART_ESP32_INIT_FUNC, NULL, &lp_uart_esp32_data,
204+
&lp_uart_esp32_cfg, PRE_KERNEL_1, CONFIG_SERIAL_INIT_PRIORITY, &lp_uart_esp32_api);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
description: ESP32 Low Power UART
2+
3+
compatible: "espressif,esp32-lpuart"
4+
5+
include: uart-controller.yaml
6+
7+
properties:
8+
reg:
9+
required: true
10+
11+
tx-pin:
12+
type: int
13+
default: 5
14+
enum:
15+
- 5
16+
description: In ESP32-C6, TX pin is fixed to GPIO5
17+
18+
rx-pin:
19+
type: int
20+
default: 4
21+
enum:
22+
- 4
23+
description: In ESP32-C6, RX pin is fixed to GPIO4
24+
25+
rts-pin:
26+
type: int
27+
default: 2
28+
enum:
29+
- 2
30+
description: In ESP32-C6, RTS pin is fixed to GPIO2
31+
32+
cts-pin:
33+
type: int
34+
default: 3
35+
enum:
36+
- 3
37+
description: In ESP32-C6, CTS pin is fixed to GPIO3
38+
39+
current-speed:
40+
type: int

dts/riscv/espressif/esp32c6/esp32c6_common.dtsi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@
274274
current-speed = <115200>;
275275
};
276276

277+
lp_uart: uart@600b1400 {
278+
compatible = "espressif,esp32-lpuart";
279+
reg = <0x600b1400 DT_SIZE_K(4)>;
280+
status = "disabled";
281+
current-speed = <115200>;
282+
};
283+
277284
usb_serial: uart@6000f000 {
278285
compatible = "espressif,esp32-usb-serial";
279286
reg = <0x6000F000 0x1000>;

0 commit comments

Comments
 (0)