From 6f044e6e81cb07c555f795ab037181a26c9b8fe9 Mon Sep 17 00:00:00 2001 From: Sudeep Mohanty Date: Wed, 29 Apr 2026 16:13:43 +0200 Subject: [PATCH] refactor(ulp): use lp_gpio_matrix_* for LP core peripheral IO on matrix targets On SOC_LP_GPIO_MATRIX_SUPPORTED targets, use lp_gpio_matrix_input and lp_gpio_matrix_output instead of lp_gpio_connect_* so matrix outputs use rtcio_hal_matrix_out (LP GPIO IOMUX applied in HAL). LP UART remapped pins: drop rtc_gpio_iomux_func_sel(pin, 1); rtc_gpio_init already selects RTCIO_LL_PIN_FUNC. Non-matrix LP I2C: select dedicated LP I2C RTC IOMUX via rtc_gpio_iomux_output (same mux register write as rtc_gpio_iomux_func_sel; matches HAL naming for peripheral-owned output enable on dedicated mux functions). --- components/ulp/lp_core/lp_core_i2c.c | 19 ++++++++++--------- components/ulp/lp_core/lp_core_spi.c | 9 +++------ components/ulp/lp_core/lp_core_uart.c | 21 +++++++-------------- 3 files changed, 20 insertions(+), 29 deletions(-) diff --git a/components/ulp/lp_core/lp_core_i2c.c b/components/ulp/lp_core/lp_core_i2c.c index 7761d8f28ba..2733126d1d2 100644 --- a/components/ulp/lp_core/lp_core_i2c.c +++ b/components/ulp/lp_core/lp_core_i2c.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -89,17 +89,18 @@ static esp_err_t lp_i2c_set_pin(const lp_core_i2c_cfg_t *cfg) ESP_RETURN_ON_ERROR(lp_i2c_configure_io(sda_io_num, sda_pullup_en), LPI2C_TAG, "LP I2C SDA pin config failed"); #if !SOC_LP_GPIO_MATRIX_SUPPORTED + /* On targets that do not support the LP IO Matrix: assign SDA and SCL to the LP I2C + * RTC IOMUX function. lp_i2c_configure_io() already initialized the pads and open-drain direction. */ const i2c_signal_conn_t *p_i2c_pin = &i2c_periph_signal[LP_I2C_NUM_0]; - ret = rtc_gpio_iomux_func_sel(sda_io_num, p_i2c_pin->iomux_func); - ret = rtc_gpio_iomux_func_sel(scl_io_num, p_i2c_pin->iomux_func); + ret = rtc_gpio_iomux_output(sda_io_num, p_i2c_pin->iomux_func); + ret = rtc_gpio_iomux_output(scl_io_num, p_i2c_pin->iomux_func); #else - /* Connect the SDA pin of the LP_I2C peripheral to the LP_IO Matrix */ - ret = lp_gpio_connect_out_signal(sda_io_num, LP_I2C_SDA_PAD_OUT_IDX, 0, 0); - ret = lp_gpio_connect_in_signal(sda_io_num, LP_I2C_SDA_PAD_IN_IDX, 0); + /* On targets with the LP IO Matrix: route SDA and SCL to the LP I2C pad signals. */ + ret = lp_gpio_matrix_output(sda_io_num, LP_I2C_SDA_PAD_OUT_IDX, false, false); + ret = lp_gpio_matrix_input(sda_io_num, LP_I2C_SDA_PAD_IN_IDX, false); - /* Connect the SCL pin of the LP_I2C peripheral to the LP_IO Matrix */ - ret = lp_gpio_connect_out_signal(scl_io_num, LP_I2C_SCL_PAD_OUT_IDX, 0, 0); - ret = lp_gpio_connect_in_signal(scl_io_num, LP_I2C_SCL_PAD_IN_IDX, 0); + ret = lp_gpio_matrix_output(scl_io_num, LP_I2C_SCL_PAD_OUT_IDX, false, false); + ret = lp_gpio_matrix_input(scl_io_num, LP_I2C_SCL_PAD_IN_IDX, false); #endif /* !SOC_LP_GPIO_MATRIX_SUPPORTED */ return ret; diff --git a/components/ulp/lp_core/lp_core_spi.c b/components/ulp/lp_core/lp_core_spi.c index 21a3cdd97c9..acc9225c81f 100644 --- a/components/ulp/lp_core/lp_core_spi.c +++ b/components/ulp/lp_core/lp_core_spi.c @@ -32,12 +32,9 @@ static esp_err_t lp_spi_config_io(gpio_num_t pin, rtc_gpio_mode_t direction, uin /* Initialize LP_IO */ ESP_RETURN_ON_ERROR(rtc_gpio_init(pin), LP_SPI_TAG, "LP IO Init failed for GPIO %d", pin); - /* Set LP_IO direction */ - ESP_RETURN_ON_ERROR(rtc_gpio_set_direction(pin, direction), LP_SPI_TAG, "LP IO Set direction failed for %d", pin); - - /* Connect the LP SPI signals to the LP_IO Matrix */ - ESP_RETURN_ON_ERROR(lp_gpio_connect_out_signal(pin, out_pad_idx, 0, 0), LP_SPI_TAG, "LP IO Matrix connect out signal failed for %d", pin); - ESP_RETURN_ON_ERROR(lp_gpio_connect_in_signal(pin, in_pad_idx, 0), LP_SPI_TAG, "LP IO Matrix connect in signal failed for %d", pin); + /* Connect this LP_IO to the LP SPI pad-out and pad-in indices on the LP IO Matrix. */ + ESP_RETURN_ON_ERROR(lp_gpio_matrix_output(pin, out_pad_idx, false, false), LP_SPI_TAG, "LP IO matrix output failed for %d", pin); + ESP_RETURN_ON_ERROR(lp_gpio_matrix_input(pin, in_pad_idx, false), LP_SPI_TAG, "LP IO matrix input failed for %d", pin); return ret; } diff --git a/components/ulp/lp_core/lp_core_uart.c b/components/ulp/lp_core/lp_core_uart.c index 95933304b6d..7052021a3e1 100644 --- a/components/ulp/lp_core/lp_core_uart.c +++ b/components/ulp/lp_core/lp_core_uart.c @@ -94,35 +94,28 @@ static esp_err_t lp_uart_config_io(gpio_num_t pin, rtc_gpio_mode_t direction, ui /* Connect pins */ const uart_periph_sig_t *upin = &uart_periph_signal[LP_UART_PORT_NUM].pins[idx]; #if !SOC_LP_GPIO_MATRIX_SUPPORTED - /* On non-matrix chips, LP UART pins are always the default IOMUX pins. - * Use the all-in-one rtc_gpio_iomux APIs which handle func sel and direction. */ + /* On targets that do not support the LP IO Matrix, LP UART uses fixed pads. + * rtc_gpio_iomux_output / rtc_gpio_iomux_input select the UART RTC IOMUX function and direction. */ if (direction == RTC_GPIO_MODE_OUTPUT_ONLY) { ret = rtc_gpio_iomux_output(pin, upin->iomux_func); } else { ret = rtc_gpio_iomux_input(pin, upin->iomux_func, UART_PERIPH_SIGNAL(LP_UART_PORT_NUM, idx)); } #else - /* If the configured pin is the default LP_IO Mux pin for LP UART, then set the LP_IO MUX function */ + /* Default LP UART pin: same RTC IOMUX path as on targets that do not support the LP IO Matrix. */ if (upin->default_gpio == pin) { - /* rtc_gpio_iomux_input/output are all-in-one APIs that handle func sel, - * direction, and LP GPIO Matrix bypass in a single call. */ + /* Select LP UART on this pin including direction and LP IO Matrix bypass. */ if (direction == RTC_GPIO_MODE_OUTPUT_ONLY) { ret = rtc_gpio_iomux_output(pin, upin->iomux_func); } else { ret = rtc_gpio_iomux_input(pin, upin->iomux_func, UART_PERIPH_SIGNAL(LP_UART_PORT_NUM, idx)); } } else { - ret = rtc_gpio_set_direction(pin, direction); - if (ret != ESP_OK) { - return ESP_FAIL; - } - /* Select FUNC1 for LP_IO Matrix */ - ret = rtc_gpio_iomux_func_sel(pin, 1); - /* Connect the LP_IO to the LP UART peripheral signal */ + /* Non-default pin: route LP UART TX/RX through the LP IO Matrix. */ if (direction == RTC_GPIO_MODE_OUTPUT_ONLY) { - ret = lp_gpio_connect_out_signal(pin, UART_PERIPH_SIGNAL(LP_UART_PORT_NUM, idx), 0, 0); + ret = lp_gpio_matrix_output(pin, UART_PERIPH_SIGNAL(LP_UART_PORT_NUM, idx), false, false); } else { - ret = lp_gpio_connect_in_signal(pin, UART_PERIPH_SIGNAL(LP_UART_PORT_NUM, idx), 0); + ret = lp_gpio_matrix_input(pin, UART_PERIPH_SIGNAL(LP_UART_PORT_NUM, idx), false); } } #endif /* SOC_LP_GPIO_MATRIX_SUPPORTED */