diff --git a/components/esp_driver_touch_sens/hw_ver1/include/driver/touch_version_types.h b/components/esp_driver_touch_sens/hw_ver1/include/driver/touch_version_types.h index 08db408e59..877a698f80 100644 --- a/components/esp_driver_touch_sens/hw_ver1/include/driver/touch_version_types.h +++ b/components/esp_driver_touch_sens/hw_ver1/include/driver/touch_version_types.h @@ -19,9 +19,6 @@ extern "C" { #endif -#define TOUCH_MIN_CHAN_ID 0 /*!< The minimum available channel id of the touch pad */ -#define TOUCH_MAX_CHAN_ID 9 /*!< The maximum available channel id of the touch pad */ - /** * @brief Helper macro to the default configurations of the touch sensor controller * diff --git a/components/esp_driver_touch_sens/hw_ver2/include/driver/touch_version_types.h b/components/esp_driver_touch_sens/hw_ver2/include/driver/touch_version_types.h index a0c1f595a5..6d92c5273d 100644 --- a/components/esp_driver_touch_sens/hw_ver2/include/driver/touch_version_types.h +++ b/components/esp_driver_touch_sens/hw_ver2/include/driver/touch_version_types.h @@ -19,9 +19,6 @@ extern "C" { #endif -#define TOUCH_MIN_CHAN_ID 1 /*!< The minimum available channel id of the touch pad */ -#define TOUCH_MAX_CHAN_ID 14 /*!< The maximum available channel id of the touch pad */ - #define TOUCH_SHIELD_CHAN_ID 14 /*!< The touch channel that can be used as the shield channel */ /** diff --git a/components/esp_driver_touch_sens/hw_ver3/include/driver/touch_version_types.h b/components/esp_driver_touch_sens/hw_ver3/include/driver/touch_version_types.h index cb9e0bfb62..078fd7d1c1 100644 --- a/components/esp_driver_touch_sens/hw_ver3/include/driver/touch_version_types.h +++ b/components/esp_driver_touch_sens/hw_ver3/include/driver/touch_version_types.h @@ -19,9 +19,6 @@ extern "C" { #endif -#define TOUCH_MIN_CHAN_ID 0 /*!< The minimum available channel id of the touch pad */ -#define TOUCH_MAX_CHAN_ID 13 /*!< The maximum available channel id of the touch pad */ - /** * @brief Helper macro to the default configurations of the touch sensor controller * diff --git a/components/esp_driver_touch_sens/include/driver/touch_sens_types.h b/components/esp_driver_touch_sens/include/driver/touch_sens_types.h index 97e6bf9886..5d7e2d548d 100644 --- a/components/esp_driver_touch_sens/include/driver/touch_sens_types.h +++ b/components/esp_driver_touch_sens/include/driver/touch_sens_types.h @@ -22,6 +22,9 @@ extern "C" { #define TOUCH_PROXIMITY_CHAN_NUM SOC_TOUCH_PROXIMITY_CHANNEL_NUM /*!< The supported proximity channel number in proximity sensing mode */ #endif +#define TOUCH_MIN_CHAN_ID SOC_TOUCH_MIN_CHAN_ID /*!< The minimum available channel id of the touch pad */ +#define TOUCH_MAX_CHAN_ID SOC_TOUCH_MAX_CHAN_ID /*!< The maximum available channel id of the touch pad */ + /** * @brief The chip sleep level that allows the touch sensor to wake-up * diff --git a/components/hal/esp32p4/include/hal/touch_sensor_ll.h b/components/hal/esp32p4/include/hal/touch_sensor_ll.h index 76639412b9..95592ad332 100644 --- a/components/hal/esp32p4/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32p4/include/hal/touch_sensor_ll.h @@ -584,6 +584,18 @@ static inline void touch_ll_sample_cfg_set_engaged_num(uint8_t sample_cfg_num) LP_ANA_PERI.touch_scan_ctrl2.freq_scan_cnt_limit = sample_cfg_num ? sample_cfg_num : 1; } +/** + * Get the engaged sample configuration number + * + * @return The engaged sample configuration number, range 0~3. + */ +static inline uint32_t touch_ll_sample_cfg_get_engaged_num(void) +{ + uint32_t sample_cfg_num = LP_ANA_PERI.touch_scan_ctrl2.freq_scan_cnt_limit; + return sample_cfg_num ? sample_cfg_num : 1; +} + + /** * Set capacitance and resistance of the RC filter of the sampling frequency. * diff --git a/components/hal/esp32s2/include/hal/touch_sensor_ll.h b/components/hal/esp32s2/include/hal/touch_sensor_ll.h index 2ce086cc6f..a1d70d4197 100644 --- a/components/hal/esp32s2/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32s2/include/hal/touch_sensor_ll.h @@ -1682,6 +1682,29 @@ static inline void touch_ll_sleep_read_data(uint32_t *raw_data) *raw_data = SENS.sar_touch_status[touch_num - 1].touch_pad_data; } +/** + * Get the data of the touch channel according to the types + * + * @param sample_cfg_id The sample configuration index + * @param type data type + * 0/1: TOUCH_LL_READ_RAW, the raw data of the touch channel + * 2: TOUCH_LL_READ_BENCHMARK, benchmark value of touch channel, + * the benchmark value is the maximum during the first measurement period + * 3: TOUCH_LL_READ_SMOOTH, the smoothed data that obtained by filtering the raw data. + * @param smooth_data pointer to smoothed data + */ +__attribute__((always_inline)) +static inline void touch_ll_sleep_read_chan_data(uint8_t type, uint32_t *data) +{ + SENS.sar_touch_conf.touch_data_sel = type; + if (type == TOUCH_LL_READ_RAW) { + uint32_t touch_num = RTCCNTL.touch_slp_thres.touch_slp_pad; + *data = SENS.sar_touch_status[touch_num - 1].touch_pad_data; + } else { + *data = SENS.sar_touch_slp_status.touch_slp_data; + } +} + /** * Select touch sensor dbias to save power in sleep mode. * diff --git a/components/hal/esp32s3/include/hal/touch_sensor_ll.h b/components/hal/esp32s3/include/hal/touch_sensor_ll.h index d57129e5c0..3a9e1824f7 100644 --- a/components/hal/esp32s3/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32s3/include/hal/touch_sensor_ll.h @@ -1718,6 +1718,29 @@ static inline void touch_ll_sleep_read_data(uint32_t *raw_data) *raw_data = SENS.sar_touch_status[touch_num - 1].touch_pad_data; } +/** + * Get the data of the touch channel according to the types + * + * @param sample_cfg_id The sample configuration index + * @param type data type + * 0/1: TOUCH_LL_READ_RAW, the raw data of the touch channel + * 2: TOUCH_LL_READ_BENCHMARK, benchmark value of touch channel, + * the benchmark value is the maximum during the first measurement period + * 3: TOUCH_LL_READ_SMOOTH, the smoothed data that obtained by filtering the raw data. + * @param smooth_data pointer to smoothed data + */ +__attribute__((always_inline)) +static inline void touch_ll_sleep_read_chan_data(uint8_t type, uint32_t *data) +{ + SENS.sar_touch_conf.touch_data_sel = type; + if (type == TOUCH_LL_READ_RAW) { + uint32_t touch_num = RTCCNTL.touch_slp_thres.touch_slp_pad; + *data = SENS.sar_touch_status[touch_num - 1].touch_pad_data; + } else { + *data = SENS.sar_touch_slp_status.touch_slp_data; + } +} + /** * Select touch sensor dbias to save power in sleep mode. diff --git a/components/soc/esp32/include/soc/Kconfig.soc_caps.in b/components/soc/esp32/include/soc/Kconfig.soc_caps.in index 7d90fdcc62..d580321c10 100644 --- a/components/soc/esp32/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32/include/soc/Kconfig.soc_caps.in @@ -735,6 +735,14 @@ config SOC_TOUCH_SENSOR_NUM int default 10 +config SOC_TOUCH_MIN_CHAN_ID + bool + default n + +config SOC_TOUCH_MAX_CHAN_ID + int + default 9 + config SOC_TOUCH_SUPPORT_SLEEP_WAKEUP bool default y diff --git a/components/soc/esp32/include/soc/soc_caps.h b/components/soc/esp32/include/soc/soc_caps.h index 1273854c26..f82090f463 100644 --- a/components/soc/esp32/include/soc/soc_caps.h +++ b/components/soc/esp32/include/soc/soc_caps.h @@ -336,6 +336,8 @@ /*-------------------------- TOUCH SENSOR CAPS -------------------------------*/ #define SOC_TOUCH_SENSOR_VERSION (1U) /*!= SOC_TOUCH_MAX_CHAN_ID || \ + channel < SOC_TOUCH_MIN_CHAN_ID) { \ + return ESP_ERR_INVALID_ARG; \ + } \ +} \ + +esp_err_t ulp_riscv_touch_pad_read_raw_data(touch_pad_t touch_num, uint32_t *raw_data) +{ + /* Check Arguments */ + if (!raw_data) { + return ESP_ERR_INVALID_ARG; + } + ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); + + /* Read raw touch data */ + uint32_t engaged_sampler_cnt = touch_ll_sample_cfg_get_engaged_num(); + for (int i = 0; i < engaged_sampler_cnt; i++) { + // Only have smooth data on V3, no raw data + touch_ll_read_chan_data((int)touch_num, i + 1, TOUCH_LL_READ_SMOOTH, &raw_data[i]); + } + + return ESP_OK; +} + +esp_err_t ulp_riscv_touch_pad_read_benchmark(touch_pad_t touch_num, uint32_t *benchmark) +{ + /* Check Arguments */ + if (!benchmark) { + return ESP_ERR_INVALID_ARG; + } + ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); + + /* Read benchmark data */ + uint32_t engaged_sampler_cnt = touch_ll_sample_cfg_get_engaged_num(); + for (int i = 0; i < engaged_sampler_cnt; i++) { + touch_ll_read_chan_data((int)touch_num, i + 1, TOUCH_LL_READ_BENCHMARK, &benchmark[i]); + } + + return ESP_OK; +} + +esp_err_t ulp_riscv_touch_pad_filter_read_smooth(touch_pad_t touch_num, uint32_t *smooth_data) +{ + /* Check Arguments */ + if (!smooth_data) { + return ESP_ERR_INVALID_ARG; + } + ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); + + /* Read smoothened touch sensor data */ + uint32_t engaged_sampler_cnt = touch_ll_sample_cfg_get_engaged_num(); + for (int i = 0; i < engaged_sampler_cnt; i++) { + touch_ll_read_chan_data((int)touch_num, i + 1, TOUCH_LL_READ_SMOOTH, &smooth_data[i]); + } + + return ESP_OK; +} + +esp_err_t ulp_riscv_touch_pad_reset_benchmark(touch_pad_t touch_num) +{ + /* Check Arguments */ + if (touch_num > TOUCH_PAD_MAX || touch_num < 0) { + return ESP_ERR_INVALID_ARG; + } + + /* Reset benchmark */ + touch_ll_reset_chan_benchmark(BIT(touch_num)); + + return ESP_OK; +} + +esp_err_t ulp_riscv_touch_pad_sleep_channel_read_data(touch_pad_t touch_num, uint32_t *raw_data) +{ + /* Check Arguments */ + if (!raw_data) { + return ESP_ERR_INVALID_ARG; + } + ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); + + /* Read raw touch data */ + uint32_t engaged_sampler_cnt = touch_ll_sample_cfg_get_engaged_num(); + for (int i = 0; i < engaged_sampler_cnt; i++) { + // Only have smooth data on V3, no raw data + touch_ll_sleep_read_chan_data(TOUCH_LL_READ_SMOOTH, i + 1, &raw_data[i]); + } + + return ESP_OK; +} + +esp_err_t ulp_riscv_touch_pad_sleep_channel_read_benchmark(touch_pad_t touch_num, uint32_t *benchmark) +{ + /* Check Arguments */ + if (!benchmark) { + return ESP_ERR_INVALID_ARG; + } + ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); + + /* Read benchmark data */ + uint32_t engaged_sampler_cnt = touch_ll_sample_cfg_get_engaged_num(); + for (int i = 0; i < engaged_sampler_cnt; i++) { + touch_ll_sleep_read_chan_data(TOUCH_LL_READ_BENCHMARK, i + 1, &benchmark[i]); + } + + return ESP_OK; +} + +esp_err_t ulp_riscv_touch_pad_sleep_channel_read_smooth(touch_pad_t touch_num, uint32_t *smooth_data) +{ + /* Check Arguments */ + if (!smooth_data) { + return ESP_ERR_INVALID_ARG; + } + ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); + + /* Read smoothened touch sensor data */ + uint32_t engaged_sampler_cnt = touch_ll_sample_cfg_get_engaged_num(); + for (int i = 0; i < engaged_sampler_cnt; i++) { + touch_ll_sleep_read_chan_data(TOUCH_LL_READ_SMOOTH, i + 1, &smooth_data[i]); + } + + return ESP_OK; +} + +esp_err_t ulp_riscv_touch_pad_sleep_channel_reset_benchmark(void) +{ + /* Reset benchmark */ + touch_ll_sleep_reset_benchmark(); + + return ESP_OK; +} diff --git a/components/ulp/ulp_riscv/ulp_core/ulp_riscv_touch.c b/components/ulp/ulp_riscv/ulp_core/ulp_riscv_touch.c index 8ced246058..aafc715380 100644 --- a/components/ulp/ulp_riscv/ulp_core/ulp_riscv_touch.c +++ b/components/ulp/ulp_riscv/ulp_core/ulp_riscv_touch.c @@ -1,20 +1,18 @@ /* - * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include "ulp_riscv_touch_ulp_core.h" #include "soc/soc_caps.h" -#include "soc/touch_sensor_pins.h" -#include "hal/touch_sensor_hal.h" +#include "hal/touch_sensor_ll.h" /* Check Touch Channel correctness */ #define ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(channel) \ { \ - if (channel >= SOC_TOUCH_SENSOR_NUM || \ - channel < 0 || \ - channel == SOC_TOUCH_DENOISE_CHANNEL) { \ + if (channel >= SOC_TOUCH_MAX_CHAN_ID || \ + channel < SOC_TOUCH_MIN_CHAN_ID) { \ return ESP_ERR_INVALID_ARG; \ } \ } \ @@ -28,7 +26,7 @@ esp_err_t ulp_riscv_touch_pad_read_raw_data(touch_pad_t touch_num, uint32_t *raw ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); /* Read raw touch data */ - *raw_data = touch_hal_read_raw_data(touch_num); + touch_ll_read_chan_data((int)touch_num, TOUCH_LL_READ_RAW, raw_data); return ESP_OK; } @@ -42,7 +40,7 @@ esp_err_t ulp_riscv_touch_pad_read_benchmark(touch_pad_t touch_num, uint32_t *be ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); /* Read benchmark data */ - touch_hal_read_benchmark(touch_num, benchmark); + touch_ll_read_chan_data((int)touch_num, TOUCH_LL_READ_BENCHMARK, benchmark); return ESP_OK; } @@ -56,7 +54,7 @@ esp_err_t ulp_riscv_touch_pad_filter_read_smooth(touch_pad_t touch_num, uint32_t ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); /* Read smoothened touch sensor data */ - touch_hal_filter_read_smooth(touch_num, smooth_data); + touch_ll_read_chan_data((int)touch_num, TOUCH_LL_READ_SMOOTH, smooth_data); return ESP_OK; } @@ -69,7 +67,7 @@ esp_err_t ulp_riscv_touch_pad_reset_benchmark(touch_pad_t touch_num) } /* Reset benchmark */ - touch_hal_reset_benchmark(touch_num); + touch_ll_reset_chan_benchmark(BIT(touch_num)); return ESP_OK; } @@ -83,7 +81,7 @@ esp_err_t ulp_riscv_touch_pad_sleep_channel_read_data(touch_pad_t touch_num, uin ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); /* Read raw touch data */ - touch_hal_sleep_read_data(raw_data); + touch_ll_sleep_read_chan_data(TOUCH_LL_READ_RAW, raw_data); return ESP_OK; } @@ -97,7 +95,7 @@ esp_err_t ulp_riscv_touch_pad_sleep_channel_read_benchmark(touch_pad_t touch_num ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); /* Read benchmark data */ - touch_hal_sleep_read_benchmark(benchmark); + touch_ll_sleep_read_chan_data(TOUCH_LL_READ_BENCHMARK, benchmark); return ESP_OK; } @@ -111,7 +109,7 @@ esp_err_t ulp_riscv_touch_pad_sleep_channel_read_smooth(touch_pad_t touch_num, u ULP_RISCV_TOUCH_CHANNEL_CHECK_AND_RETURN(touch_num); /* Read smoothened touch sensor data */ - touch_hal_sleep_read_smooth(smooth_data); + touch_ll_sleep_read_chan_data(TOUCH_LL_READ_SMOOTH, smooth_data); return ESP_OK; } @@ -119,7 +117,7 @@ esp_err_t ulp_riscv_touch_pad_sleep_channel_read_smooth(touch_pad_t touch_num, u esp_err_t ulp_riscv_touch_pad_sleep_channel_reset_benchmark(void) { /* Reset benchmark */ - touch_hal_sleep_reset_benchmark(); + touch_ll_sleep_reset_benchmark(); return ESP_OK; }