|
| 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); |
0 commit comments