From 1cd9dd5001b8afed71216acc32e79caaff925976 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Thu, 17 Oct 2024 18:17:36 +0800 Subject: [PATCH] refactor(touch): refactor touch low-level for s2 & s3 --- components/hal/CMakeLists.txt | 7 +- .../hal/esp32p4/include/hal/touch_sensor_ll.h | 40 +- .../esp32s2/include/hal/touch_sensor_hal.h | 26 +- .../hal/esp32s2/include/hal/touch_sensor_ll.h | 913 +++++++++++++---- components/hal/esp32s2/touch_sensor_hal.c | 14 +- .../esp32s3/include/hal/touch_sensor_hal.h | 26 +- .../hal/esp32s3/include/hal/touch_sensor_ll.h | 930 ++++++++++++++---- components/hal/esp32s3/touch_sensor_hal.c | 15 +- .../hal/touch_sens_hal.h} | 24 +- components/hal/include/hal/touch_sens_types.h | 131 +++ components/hal/include/hal/touch_sensor_hal.h | 1 + .../hal/include/hal/touch_sensor_types.h | 19 +- .../touch_sensor_hal.c => touch_sens_hal.c} | 32 +- .../esp32s2/include/soc/Kconfig.soc_caps.in | 4 + components/soc/esp32s2/include/soc/soc_caps.h | 1 + .../esp32s3/include/soc/Kconfig.soc_caps.in | 4 + components/soc/esp32s3/include/soc/soc_caps.h | 1 + .../esp32s3/register/soc/rtc_cntl_struct.h | 8 +- 18 files changed, 1658 insertions(+), 538 deletions(-) rename components/hal/{esp32p4/include/hal/touch_sensor_hal.h => include/hal/touch_sens_hal.h} (76%) create mode 100644 components/hal/include/hal/touch_sens_types.h rename components/hal/{esp32p4/touch_sensor_hal.c => touch_sens_hal.c} (75%) diff --git a/components/hal/CMakeLists.txt b/components/hal/CMakeLists.txt index ba107dbf84..698bd8adff 100644 --- a/components/hal/CMakeLists.txt +++ b/components/hal/CMakeLists.txt @@ -282,10 +282,15 @@ if(NOT BOOTLOADER_BUILD) endif() if(CONFIG_SOC_TOUCH_SENSOR_SUPPORTED) - list(APPEND srcs "${target}/touch_sensor_hal.c") + # Source files for legacy touch driver if(CONFIG_SOC_TOUCH_SENSOR_VERSION LESS 3) + list(APPEND srcs "${target}/touch_sensor_hal.c") list(APPEND srcs "touch_sensor_hal.c") endif() + # Source files for touch driver-ng (currently only support ver2 & ver3) + if(CONFIG_SOC_TOUCH_SENSOR_VERSION GREATER 1) + list(APPEND srcs "touch_sens_hal.c") + endif() endif() if(${target} STREQUAL "esp32") diff --git a/components/hal/esp32p4/include/hal/touch_sensor_ll.h b/components/hal/esp32p4/include/hal/touch_sensor_ll.h index 37280c3b5d..8425e15b30 100644 --- a/components/hal/esp32p4/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32p4/include/hal/touch_sensor_ll.h @@ -25,6 +25,7 @@ #include "soc/pmu_struct.h" #include "soc/soc_caps.h" #include "hal/touch_sensor_types.h" +#include "hal/touch_sens_types.h" #ifdef __cplusplus extern "C" { @@ -222,10 +223,9 @@ static inline void touch_ll_force_done_curr_measurement(void) * The measurement action can be triggered by the hardware timer, as well as by the software instruction. * @note * The timer should be triggered - * @param is_sleep Whether in sleep state */ __attribute__((always_inline)) -static inline void touch_ll_start_fsm_repeated_timer(bool is_sleep) +static inline void touch_ll_start_fsm_repeated_timer(void) { /** * Touch timer trigger measurement and always wait measurement done. @@ -238,10 +238,9 @@ static inline void touch_ll_start_fsm_repeated_timer(bool is_sleep) /** * Stop touch sensor FSM timer. * The measurement action can be triggered by the hardware timer, as well as by the software instruction. - * @param is_sleep Whether in sleep state */ __attribute__((always_inline)) -static inline void touch_ll_stop_fsm_repeated_timer(bool is_sleep) +static inline void touch_ll_stop_fsm_repeated_timer(void) { PMU.touch_pwr_cntl.sleep_timer_en = 0; touch_ll_force_done_curr_measurement(); @@ -326,7 +325,7 @@ static inline void touch_ll_enable_scan_mask(uint16_t chan_mask, bool enable) * @return * - ESP_OK on success */ -static inline void touch_ll_set_channel_mask(uint16_t enable_mask) +static inline void touch_ll_enable_channel_mask(uint16_t enable_mask) { // Channel shift workaround: the lowest bit takes no effect uint16_t mask = (enable_mask << 1) & TOUCH_PAD_BIT_MASK_ALL; @@ -502,7 +501,7 @@ static inline uint32_t touch_ll_get_current_meas_channel(void) * * @param int_mask interrupt mask */ -static inline void touch_ll_intr_enable(uint32_t int_mask) +static inline void touch_ll_interrupt_enable(uint32_t int_mask) { uint32_t mask = LP_TOUCH.int_ena.val; mask |= (int_mask & TOUCH_LL_INTR_MASK_ALL); @@ -514,7 +513,7 @@ static inline void touch_ll_intr_enable(uint32_t int_mask) * * @param int_mask interrupt mask */ -static inline void touch_ll_intr_disable(uint32_t int_mask) +static inline void touch_ll_interrupt_disable(uint32_t int_mask) { uint32_t mask = LP_TOUCH.int_ena.val; mask &= ~(int_mask & TOUCH_LL_INTR_MASK_ALL); @@ -527,7 +526,7 @@ static inline void touch_ll_intr_disable(uint32_t int_mask) * @param int_mask Pad mask to clear interrupts */ __attribute__((always_inline)) -static inline void touch_ll_intr_clear(touch_pad_intr_mask_t int_mask) +static inline void touch_ll_interrupt_clear(touch_pad_intr_mask_t int_mask) { LP_TOUCH.int_clr.val = int_mask; } @@ -545,28 +544,23 @@ static inline uint32_t touch_ll_get_intr_status_mask(void) } /** - * Enable the timeout check for all touch sensor channels measurements. + * Set the timeout to enable or disable the check for all touch sensor channels measurements. * When the touch reading of a touch channel exceeds the measurement threshold, * If enable: a timeout interrupt will be generated and it will go to the next channel measurement. * If disable: the FSM is always on the channel, until the measurement of this channel is over. * * @param timeout_cycles The maximum time cycles of the measurement on one channel. + * Set to 0 to disable the timeout. + * Set to non-zero to enable the timeout and set the timeout cycles. */ -static inline void touch_ll_timeout_enable(uint32_t timeout_cycles) +static inline void touch_ll_set_timeout(uint32_t timeout_cycles) { - LP_ANA_PERI.touch_scan_ctrl2.touch_timeout_num = timeout_cycles; - LP_ANA_PERI.touch_scan_ctrl2.touch_timeout_en = 1; -} - -/** - * Disable the timeout check for all touch sensor channels measurements. - * When the touch reading of a touch channel exceeds the measurement threshold, - * If enable: a timeout interrupt will be generated and it will go to the next channel measurement. - * If disable: the FSM is always on the channel, until the measurement of this channel is over. - */ -static inline void touch_ll_timeout_disable(void) -{ - LP_ANA_PERI.touch_scan_ctrl2.touch_timeout_en = 0; + if (timeout_cycles) { + LP_ANA_PERI.touch_scan_ctrl2.touch_timeout_num = timeout_cycles; + LP_ANA_PERI.touch_scan_ctrl2.touch_timeout_en = 1; + } else { + LP_ANA_PERI.touch_scan_ctrl2.touch_timeout_en = 0; + } } /** diff --git a/components/hal/esp32s2/include/hal/touch_sensor_hal.h b/components/hal/esp32s2/include/hal/touch_sensor_hal.h index b42532c78f..382a52f937 100644 --- a/components/hal/esp32s2/include/hal/touch_sensor_hal.h +++ b/components/hal/esp32s2/include/hal/touch_sensor_hal.h @@ -278,14 +278,10 @@ void touch_hal_filter_get_config(touch_filter_config_t *filter_info); /** * Enable touch sensor filter and detection algorithm. * For more details on the detection algorithm, please refer to the application documentation. + * + * @param enable set true to enable the filter */ -#define touch_hal_filter_enable() touch_ll_filter_enable() - -/** - * Disable touch sensor filter and detection algorithm. - * For more details on the detection algorithm, please refer to the application documentation. - */ -#define touch_hal_filter_disable() touch_ll_filter_disable() +#define touch_hal_filter_enable(enable) touch_ll_filter_enable(enable) /************************ Denoise register setting ************************/ @@ -326,7 +322,7 @@ void touch_hal_denoise_enable(void); * This denoise function filters out interference introduced on all channels, * such as noise introduced by the power supply and external EMI. */ -#define touch_hal_denoise_disable() touch_ll_denoise_disable() +#define touch_hal_denoise_disable() touch_ll_denoise_enable(false) /** * Set internal reference capacitance of denoise channel. @@ -391,7 +387,7 @@ void touch_hal_denoise_enable(void); * * @param pad_num Touch sensor channel number. */ -#define touch_hal_waterproof_set_sheild_driver(driver_level) touch_ll_waterproof_set_sheild_driver(driver_level) +#define touch_hal_waterproof_set_sheild_driver(driver_level) touch_ll_waterproof_set_shield_driver(driver_level) /** * Get max equivalent capacitance for shield channel. @@ -400,7 +396,7 @@ void touch_hal_denoise_enable(void); * * @param pad_num Touch sensor channel number. */ -#define touch_hal_waterproof_get_sheild_driver(driver_level) touch_ll_waterproof_get_sheild_driver(driver_level) +#define touch_hal_waterproof_get_sheild_driver(driver_level) touch_ll_waterproof_get_shield_driver(driver_level) /** * Set parameter of waterproof function. @@ -430,7 +426,7 @@ void touch_hal_waterproof_enable(void); /** * Disable parameter of waterproof function. */ -#define touch_hal_waterproof_disable() touch_ll_waterproof_disable() +#define touch_hal_waterproof_disable() touch_ll_waterproof_enable(false) /************************ Proximity register setting ************************/ @@ -550,13 +546,9 @@ void touch_hal_sleep_channel_enable(touch_pad_t pad_num, bool enable); /** * Enable proximity function for sleep pad. + * @param enable the proximity sensing */ -#define touch_hal_sleep_enable_approach() touch_ll_sleep_enable_proximity_sensing() - -/** - * Disable proximity function for sleep pad. - */ -#define touch_hal_sleep_disable_approach() touch_ll_sleep_disable_proximity_sensing() +#define touch_hal_sleep_enable_approach(enable) touch_ll_sleep_enable_proximity_sensing(enable) /** * Read benchmark of touch sensor for sleep pad. diff --git a/components/hal/esp32s2/include/hal/touch_sensor_ll.h b/components/hal/esp32s2/include/hal/touch_sensor_ll.h index 0cc5069d2f..bf4628cee0 100644 --- a/components/hal/esp32s2/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32s2/include/hal/touch_sensor_ll.h @@ -17,25 +17,714 @@ #include #include #include "hal/misc.h" +#include "hal/assert.h" #include "soc/touch_sensor_periph.h" #include "soc/soc_caps.h" #include "soc/sens_struct.h" #include "soc/rtc_cntl_struct.h" #include "soc/rtc_io_struct.h" #include "hal/touch_sensor_types.h" +#include "hal/touch_sens_types.h" #ifdef __cplusplus extern "C" { #endif #define TOUCH_LL_READ_RAW 0x0 -#define TOUCH_LL_READ_BENCHMARK 0x2 +#define TOUCH_LL_READ_BENCHMARK 0x2 #define TOUCH_LL_READ_SMOOTH 0x3 + #define TOUCH_LL_TIMER_FORCE_DONE 0x3 #define TOUCH_LL_TIMER_DONE 0x0 -#define TOUCH_LL_PAD_MEASURE_WAIT_MAX (0xFF) /*! 0); + SENS.touch_thresh[touch_num - 1].thresh = thresh; +} + +/** + * Set the power on wait cycle + * + * @param wait_cycles + */ +static inline void touch_ll_set_power_on_wait_cycle(uint32_t wait_cycles) +{ + //the waiting cycles (in 8MHz) between TOUCH_START and TOUCH_XPD + HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_ctrl2, touch_xpd_wait, wait_cycles); //wait volt stable +} + +// HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_ctrl1, touch_meas_num, meas_time); +/** + * Set touch sensor touch sensor charge and discharge times of every measurement on a pad. + * + * @param charge_times The times of charge and discharge in each measure process of touch channels. + * The timer frequency is RTC_FAST (about 16M). Range: 0 ~ 0xffff. + */ +static inline void touch_ll_set_charge_times( uint16_t charge_times) +{ + HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_ctrl1, touch_meas_num, charge_times); +} + +/** + * Set touch sensor sleep time. + * + * @param interval_ticks The touch sensor will sleep for some cycles after each measurement. + * interval_ticks decide the interval between each measurement. + * t_sleep = interval_ticks / (RTC_SLOW_CLK frequency). + * The approximate frequency value of RTC_SLOW_CLK can be obtained using rtc_clk_slow_freq_get_hz function. + */ +static inline void touch_ll_set_measure_interval_ticks(uint16_t interval_ticks) +{ + // touch sensor sleep cycle Time = interval_ticks / RTC_SLOW_CLK + HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_ctrl1, touch_sleep_cycles, interval_ticks); +} + +static inline void touch_ll_set_charge_speed(uint32_t touch_num, touch_charge_speed_t charge_speed) +{ + RTCIO.touch_pad[touch_num].dac = charge_speed; +} + +static inline void touch_ll_set_charge_voltage_high_limit(touch_volt_lim_h_t high_lim) +{ + RTCCNTL.touch_ctrl2.touch_drefh = (uint32_t)high_lim & 0x3; + RTCCNTL.touch_ctrl2.touch_drange = (uint32_t)high_lim >> 2; +} + +static inline void touch_ll_set_charge_voltage_low_limit(touch_volt_lim_l_t low_lim) +{ + RTCCNTL.touch_ctrl2.touch_drefl = low_lim; +} + +static inline void touch_ll_set_init_charge_voltage(uint32_t touch_num, touch_init_charge_volt_t init_charge_volt) +{ + RTCIO.touch_pad[touch_num].tie_opt = init_charge_volt; +} + +static inline void touch_ll_set_idle_channel_connection(touch_idle_conn_t idle_conn) +{ + RTCCNTL.touch_scan_ctrl.touch_inactive_connection = idle_conn; +} + +/** + * Enable touch sensor channel. Register touch channel into touch sensor measurement group. + * The working mode of the touch sensor is simultaneous measurement. + * This function will set the measure bits according to the given bitmask. + * + * @note If set this mask, the FSM timer should be stop firstly. + * @note The touch sensor that in scan map, should be deinit GPIO function firstly. + * @param enable_mask bitmask of touch sensor scan group. + * e.g. TOUCH_PAD_NUM1 -> BIT(1) + * @return + * - ESP_OK on success + */ +static inline void touch_ll_enable_channel_mask(uint16_t enable_mask) +{ + RTCCNTL.touch_scan_ctrl.touch_scan_pad_map = enable_mask; + SENS.sar_touch_conf.touch_outen = enable_mask; +} + +/** + * Set the timeout to enable or disable the check for all touch sensor channels measurements. + * When the touch reading of a touch channel exceeds the measurement threshold, + * If enable: a timeout interrupt will be generated and it will go to the next channel measurement. + * If disable: the FSM is always on the channel, until the measurement of this channel is over. + * + * @param timeout_cycles The maximum time cycles of the measurement on one channel. + * Set to 0 to disable the timeout. + * Set to non-zero to enable the timeout and set the timeout cycles. + */ +static inline void touch_ll_set_timeout(uint32_t timeout_cycles) +{ + if (timeout_cycles) { + RTCCNTL.touch_timeout_ctrl.touch_timeout_num = timeout_cycles; + RTCCNTL.touch_timeout_ctrl.touch_timeout_en = 1; + } else { + RTCCNTL.touch_timeout_ctrl.touch_timeout_en = 0; + } +} + +/** + * Clear all touch sensor channels active status. + * + * @note Generally no manual removal is required. + */ +static inline void touch_ll_clear_active_channel_status(void) +{ + SENS.sar_touch_conf.touch_status_clr = 1; +} + +/** + * Select touch sensor dbias to save power in sleep mode. + * + * @note If change the dbias, the reading of touch sensor will changed. Users should make sure the threshold. + */ +static inline void touch_ll_set_bias_type(touch_bias_type_t bias_type) +{ + RTCCNTL.touch_ctrl2.touch_dbias = bias_type; +} + +/********************************* FSM Operation ******************************/ +/** + * Touch timer trigger measurement and always wait measurement done. + * Force done for touch timer ensures that the timer always can get the measurement done signal. + * @note The `force done` signal should last as least one slow clock tick + */ +__attribute__((always_inline)) +static inline void touch_ll_force_done_curr_measurement(void) +{ + RTCCNTL.touch_ctrl2.touch_timer_force_done = TOUCH_LL_TIMER_FORCE_DONE; + RTCCNTL.touch_ctrl2.touch_timer_force_done = TOUCH_LL_TIMER_DONE; +} + +/** + * Enable touch sensor FSM timer trigger (continuous) mode or software trigger (oneshot) mode. + * + * @param enable Enable FSM timer mode. + * True: the FSM will trigger scanning repeatedly under the control of the hardware timer (continuous mode) + * False: the FSM will trigger scanning once under the control of the software (continuous mode) + */ +__attribute__((always_inline)) +static inline void touch_ll_enable_fsm_timer(bool enable) +{ + touch_ll_force_done_curr_measurement(); + // Set 0 to start by timer, otherwise by software + RTCCNTL.touch_ctrl2.touch_start_force = !enable; +} + +/** + * Start touch sensor FSM timer to run FSM repeatedly + * The measurement action can be triggered by the hardware timer, as well as by the software instruction. + * @note + * The timer should be triggered + */ +__attribute__((always_inline)) +static inline void touch_ll_start_fsm_repeated_timer(void) +{ + /** + * Touch timer trigger measurement and always wait measurement done. + * Force done for touch timer ensures that the timer always can get the measurement done signal. + */ + touch_ll_force_done_curr_measurement(); + RTCCNTL.touch_ctrl2.touch_slp_timer_en = 1; +} + +/** + * Stop touch sensor FSM timer. + * The measurement action can be triggered by the hardware timer, as well as by the software instruction. + */ +__attribute__((always_inline)) +static inline void touch_ll_stop_fsm_repeated_timer(void) +{ + RTCCNTL.touch_ctrl2.touch_slp_timer_en = 0; + touch_ll_force_done_curr_measurement(); +} + +/** + * Is the FSM repeated timer enabled. + * @note when the timer is enabled, RTC clock should not be power down + * + * @return + * - true: enabled + * - false: disabled + */ +static inline bool touch_ll_is_fsm_repeated_timer_enabled(void) +{ + return (bool)RTCCNTL.touch_ctrl2.touch_slp_timer_en; +} + + +/** + * Enable the touch sensor FSM start signal from software + */ +__attribute__((always_inline)) +static inline void touch_ll_trigger_oneshot_measurement(void) +{ + RTCCNTL.touch_ctrl2.touch_start_en = 1; + RTCCNTL.touch_ctrl2.touch_start_en = 0; +} + +/** + * @brief Power on the channel by mask + * + * @param chan_mask The channel mask that needs to power on + */ +__attribute__((always_inline)) +static inline void touch_ll_channel_sw_measure_mask(uint16_t chan_mask) +{ + (void) chan_mask; + // Only for compatibility +} + +/****************************** Benchmark Operation ***************************/ +/** + * Force reset benchmark to raw data of touch sensor. + * + * @note If call this API, make sure enable clock gate(`touch_ll_clkgate`) first. + * @param chan_mask touch channel mask + */ +__attribute__((always_inline)) +static inline void touch_ll_reset_chan_benchmark(uint32_t chan_mask) +{ + SENS.sar_touch_chn_st.touch_channel_clr = chan_mask; +} + +static inline void touch_ll_sleep_reset_benchmark(void) +{ + RTCCNTL.touch_approach.touch_slp_channel_clr = 1; +} + +/************************************** Data **********************************/ +/** + * Get the data of the touch channel according to the types + * + * @param touch_num touch pad index + * @param type data type + * 0/1: TOUCH_LL_READ_RAW, raw data of 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 data pointer to the data + */ +__attribute__((always_inline)) +static inline void touch_ll_read_chan_data(uint32_t touch_num, uint8_t type, uint32_t *data) +{ + HAL_ASSERT(type <= TOUCH_LL_READ_SMOOTH); + HAL_ASSERT(touch_num > 0); + SENS.sar_touch_conf.touch_data_sel = type; + *data = SENS.sar_touch_status[touch_num - 1].touch_pad_data; +} + +/****************************** Filter Configuration **************************/ + +/** + * Enable or disable touch sensor filter and detection algorithm. + * For more details on the detection algorithm, please refer to the application documentation. + */ +static inline void touch_ll_filter_enable(bool enable) +{ + RTCCNTL.touch_filter_ctrl.touch_filter_en = enable; +} + +/** + * Set filter mode. The input of the filter is the raw value of touch reading, + * and the output of the filter is involved in the judgment of the touch state. + * + * @param mode Filter mode type. Refer to ``touch_filter_mode_t``. + */ +static inline void touch_ll_filter_set_filter_mode(touch_filter_mode_t mode) +{ + RTCCNTL.touch_filter_ctrl.touch_filter_mode = mode; +} + +/** + * Set jitter filter step size. + * If filter mode is jitter, should set filter step for jitter. + * Range: 0 ~ 15 + * + * @param step The step size of the data change. + */ +static inline void touch_ll_filter_set_jitter_step(uint32_t step) +{ + RTCCNTL.touch_filter_ctrl.touch_jitter_step = step; +} + +/** + * Set the denoise coefficient regarding the denoise level. + * + * @param denoise_lvl Range [0 ~ 4]. 0 = no noise resistance, otherwise higher denoise_lvl means more noise resistance. + */ +static inline void touch_ll_filter_set_denoise_level(int denoise_lvl) +{ + HAL_ASSERT(denoise_lvl >= 0 && denoise_lvl <= 4); + bool always_update = denoise_lvl == 0; + // Map denoise level to actual noise threshold coefficients + uint32_t noise_thresh = denoise_lvl == 4 ? 3 : 3 - denoise_lvl; + + RTCCNTL.touch_filter_ctrl.touch_noise_thres = always_update ? 0 : noise_thresh; + RTCCNTL.touch_filter_ctrl.config2 = always_update ? 0 : noise_thresh; + RTCCNTL.touch_filter_ctrl.config1 = 0xF; +} + +/** + * Set the hysteresis value of the active threshold + * While the touch data is greater than active_threshold + hysteresis and last for several ticks, the channel is activated, + * and while the touch data is smaller than active_threshold - hysteresis and last for several ticks, the channel is inactivated + * + * @param hysteresis The hysteresis value of active threshold + */ +static inline void touch_ll_filter_set_active_hysteresis(uint32_t hysteresis) +{ + RTCCNTL.touch_filter_ctrl.config3 = hysteresis; +} + +/** + * Set filter mode. The input to the filter is raw data and the output is the smooth data. + * The smooth data is used to determine the touch status. + * + * @param mode Filter mode type. Refer to ``touch_smooth_mode_t``. + */ +static inline void touch_ll_filter_set_smooth_mode(touch_smooth_mode_t mode) +{ + RTCCNTL.touch_filter_ctrl.touch_smooth_lvl = mode; +} + +/** + * Set debounce count, such as `n`. If the measured values continue to exceed + * the threshold for `n+1` times, it is determined that the touch sensor state changes. + * + * @param dbc_cnt Debounce count value. + */ +static inline void touch_ll_filter_set_debounce(uint32_t dbc_cnt) +{ + RTCCNTL.touch_filter_ctrl.touch_debounce = dbc_cnt; +} + +/**************************** Sleep Configurations ****************************/ +/** + * Set the trigger threshold of touch sensor in deep sleep. + * The threshold determines the sensitivity of the touch sensor. + * The threshold is the original value of the trigger state minus the benchmark value. + * + * @note In general, the touch threshold during sleep can use the threshold parameter parameters before sleep. + */ +static inline void touch_ll_sleep_set_threshold(uint32_t touch_thres) +{ + RTCCNTL.touch_slp_thres.touch_slp_th = touch_thres; +} + +/** + * Set touch channel number for sleep channel. + * + * @note Only one touch sensor channel is supported in deep sleep mode. + * @param touch_num Touch sensor channel number. + */ +static inline void touch_ll_sleep_set_channel_num(uint32_t touch_num) +{ + RTCCNTL.touch_slp_thres.touch_slp_pad = touch_num; +} + +/** + * Enable proximity sensing function for sleep channel. + */ +static inline void touch_ll_sleep_enable_proximity_sensing(bool enable) +{ + RTCCNTL.touch_slp_thres.touch_slp_approach_en = enable; +} + +/************************* Waterproof Configurations **************************/ +/** + * Enable parameter of waterproof function. + * + * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel. + * Guard pad is used to detect the large area of water covering the touch panel. + * Shield pad is used to shield the influence of water droplets covering the touch panel. + * It is generally designed as a grid and is placed around the touch buttons. + * @param enable Enable or disable waterproof function. + */ +static inline void touch_ll_waterproof_enable(bool enable) +{ + RTCCNTL.touch_scan_ctrl.touch_shield_pad_en = enable; +} + +/** + * Set touch channel use for guard channel. + * + * @param pad_num Touch sensor channel number. + */ +static inline void touch_ll_waterproof_set_guard_chan(uint32_t pad_num) +{ + RTCCNTL.touch_scan_ctrl.touch_out_ring = pad_num; +} + +/** + * Set max equivalent capacitance for shield channel. + * The equivalent capacitance of the shielded channel can be calculated + * from the reading of denoise channel. + * + * @param pad_num Touch sensor channel number. + */ +static inline void touch_ll_waterproof_set_shield_driver(touch_pad_shield_driver_t driver_level) +{ + RTCCNTL.touch_scan_ctrl.touch_bufdrv = driver_level; +} + +/****************************** Proximity Sensing *****************************/ +/** + * Set the proximity sensing channel to the specific touch channel + * To disable the proximity channel, point this pad to `TOUCH_LL_NULL_CHANNEL` + * + * @param prox_chan proximity sensing channel. + * @param touch_num The touch channel that supposed to be used as proximity sensing channel + */ +static inline void touch_ll_set_proximity_sensing_channel(uint8_t prox_chan, uint32_t touch_num) +{ + switch (prox_chan) { + case 0: + SENS.sar_touch_conf.touch_approach_pad0 = touch_num; + break; + case 1: + SENS.sar_touch_conf.touch_approach_pad1 = touch_num; + break; + case 2: + SENS.sar_touch_conf.touch_approach_pad2 = touch_num; + break; + default: + // invalid proximity channel + abort(); + } +} + +/** + * Set the total scan times of the proximity sensing channel. + * + * @param scan_times The total scan times of the proximity sensing channel + */ +static inline void touch_ll_proximity_set_total_scan_times(uint32_t scan_times) +{ + HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_approach, touch_approach_meas_time, scan_times); +} + +/** + * Get the total scan times of the proximity sensing channel. + * + * @return + * - The total scan times of the proximity sensing channel + */ +__attribute__((always_inline)) +static inline uint32_t touch_ll_proximity_get_total_scan_times(void) +{ + return HAL_FORCE_READ_U32_REG_FIELD(RTCCNTL.touch_approach, touch_approach_meas_time); +} + +/** + * Get the current scan count for proximity channel. + * + * @param touch_num Touch channel number. + * @return + * - Current scan count for proximity channel + */ +__attribute__((always_inline)) +static inline uint32_t touch_ll_proximity_get_curr_scan_cnt(uint32_t touch_num) +{ + if (SENS.sar_touch_conf.touch_approach_pad0 == touch_num) { + return HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad0_cnt); + } else if (SENS.sar_touch_conf.touch_approach_pad1 == touch_num) { + return HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad1_cnt); + } else if (SENS.sar_touch_conf.touch_approach_pad2 == touch_num) { + return HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad2_cnt); + } + return 0; +} + +/******************************* Denoise Channel ******************************/ +/** + * Enable denoise function. + * T0 is an internal channel that does not have a corresponding external GPIO. + * T0 will work simultaneously with the measured channel Tn. Finally, the actual + * measured value of Tn is the value after subtracting lower bits of T0. + * This denoise function filters out interference introduced on all channels, + * such as noise introduced by the power supply and external EMI. + * @param enable enable the denoise channel + */ +static inline void touch_ll_denoise_enable(bool enable) +{ + RTCCNTL.touch_scan_ctrl.touch_denoise_en = enable; +} + +/** + * Set internal reference capacitance of denoise channel. + * Select the appropriate internal reference capacitance value so that + * the reading of denoise channel is closest to the reading of the channel being measured. + * + * @param capacitance Reference capacitance level. + */ +static inline void touch_ll_denoise_set_reference_cap(touch_denoise_chan_cap_t capacitance) +{ + RTCCNTL.touch_ctrl2.touch_refc = capacitance; +} + +/** + * Set denoise resolution of denoise channel. + * Determined by measuring the noise amplitude of the denoise channel. + * + * @param resolution Denoise resolution of denoise channel. + */ +static inline void touch_ll_denoise_set_resolution(touch_denoise_chan_res_t resolution) +{ + RTCCNTL.touch_scan_ctrl.touch_denoise_res = resolution; +} + +/** + * Read denoise measure value (TOUCH_PAD_NUM0). + * + * @param denoise value of denoise. + */ +static inline void touch_ll_denoise_read_data(uint32_t *data) +{ + *data = SENS.sar_touch_status0.touch_denoise_data; +} + +/******************************************************************************/ +/* Legacy APIs */ +/******************************************************************************/ /** * Set touch sensor touch sensor times of charge and discharge. * @@ -85,11 +774,11 @@ static inline void touch_ll_get_sleep_time(uint16_t *sleep_time) } /** - * Set touch sensor high voltage threshold of chanrge. + * Set touch sensor high voltage threshold of charge. * The touch sensor measures the channel capacitance value by charging and discharging the channel. * So the high threshold should be less than the supply voltage. * - * @param refh The high voltage threshold of chanrge. + * @param refh The high voltage threshold of charge. */ static inline void touch_ll_set_voltage_high(touch_high_volt_t refh) { @@ -97,11 +786,11 @@ static inline void touch_ll_set_voltage_high(touch_high_volt_t refh) } /** - * Get touch sensor high voltage threshold of chanrge. + * Get touch sensor high voltage threshold of charge. * The touch sensor measures the channel capacitance value by charging and discharging the channel. * So the high threshold should be less than the supply voltage. * - * @param refh The high voltage threshold of chanrge. + * @param refh The high voltage threshold of charge. */ static inline void touch_ll_get_voltage_high(touch_high_volt_t *refh) { @@ -131,11 +820,11 @@ static inline void touch_ll_get_voltage_low(touch_low_volt_t *refl) } /** - * Set touch sensor high voltage attenuation of chanrge. The actual charge threshold is high voltage threshold minus attenuation value. + * Set touch sensor high voltage attenuation of charge. The actual charge threshold is high voltage threshold minus attenuation value. * The touch sensor measures the channel capacitance value by charging and discharging the channel. * So the high threshold should be less than the supply voltage. * - * @param refh The high voltage threshold of chanrge. + * @param refh The high voltage threshold of charge. */ static inline void touch_ll_set_voltage_attenuation(touch_volt_atten_t atten) { @@ -143,11 +832,11 @@ static inline void touch_ll_set_voltage_attenuation(touch_volt_atten_t atten) } /** - * Get touch sensor high voltage attenuation of chanrge. The actual charge threshold is high voltage threshold minus attenuation value. + * Get touch sensor high voltage attenuation of charge. The actual charge threshold is high voltage threshold minus attenuation value. * The touch sensor measures the channel capacitance value by charging and discharging the channel. * So the high threshold should be less than the supply voltage. * - * @param refh The high voltage threshold of chanrge. + * @param refh The high voltage threshold of charge. */ static inline void touch_ll_get_voltage_attenuation(touch_volt_atten_t *atten) { @@ -412,20 +1101,6 @@ static inline uint32_t IRAM_ATTR touch_ll_read_raw_data(touch_pad_t touch_num) return SENS.sar_touch_status[touch_num - 1].touch_pad_data; } -/** - * Get touch sensor measure status. No block. - * - * @return - * - If touch sensors measure done. - */ -__attribute__((always_inline)) -static inline bool touch_ll_is_measure_done(void) -{ - return (bool)SENS.sar_touch_chn_st.touch_meas_done; -} - -/************************* esp32s2 only *************************/ - /** * Reset the whole of touch module. * @@ -468,17 +1143,6 @@ static inline void touch_ll_get_idle_channel_connect(touch_pad_conn_type_t *type *type = (touch_pad_conn_type_t)(RTCCNTL.touch_scan_ctrl.touch_inactive_connection); } -/** - * Get the current measure channel. Touch sensor measurement is cyclic scan mode. - * - * @return - * - touch channel number - */ -static inline touch_pad_t IRAM_ATTR touch_ll_get_current_meas_channel(void) -{ - return (touch_pad_t)(SENS.sar_touch_status0.touch_scan_curr); -} - /** * Enable touch sensor interrupt by bitmask. * @@ -673,17 +1337,6 @@ static inline void touch_ll_reset_benchmark(touch_pad_t touch_num) } } -/** - * Set filter mode. The input of the filter is the raw value of touch reading, - * and the output of the filter is involved in the judgment of the touch state. - * - * @param mode Filter mode type. Refer to ``touch_filter_mode_t``. - */ -static inline void touch_ll_filter_set_filter_mode(touch_filter_mode_t mode) -{ - RTCCNTL.touch_filter_ctrl.touch_filter_mode = mode; -} - /** * Get filter mode. The input of the filter is the raw value of touch reading, * and the output of the filter is involved in the judgment of the touch state. @@ -695,17 +1348,6 @@ static inline void touch_ll_filter_get_filter_mode(touch_filter_mode_t *mode) *mode = (touch_filter_mode_t)RTCCNTL.touch_filter_ctrl.touch_filter_mode; } -/** - * Set filter mode. The input to the filter is raw data and the output is the smooth data. - * The smooth data is used to determine the touch status. - * - * @param mode Filter mode type. Refer to `touch_smooth_mode_t`. - */ -static inline void touch_ll_filter_set_smooth_mode(touch_smooth_mode_t mode) -{ - RTCCNTL.touch_filter_ctrl.touch_smooth_lvl = mode; -} - /** * Get filter mode. The smooth data is used to determine the touch status. * @@ -716,17 +1358,6 @@ static inline void touch_ll_filter_get_smooth_mode(touch_smooth_mode_t *mode) *mode = (touch_smooth_mode_t)(RTCCNTL.touch_filter_ctrl.touch_smooth_lvl); } -/** - * Set debounce count, such as `n`. If the measured values continue to exceed - * the threshold for `n+1` times, it is determined that the touch sensor state changes. - * - * @param dbc_cnt Debounce count value. - */ -static inline void touch_ll_filter_set_debounce(uint32_t dbc_cnt) -{ - RTCCNTL.touch_filter_ctrl.touch_debounce = dbc_cnt; -} - /** * Get debounce count. * @@ -764,18 +1395,6 @@ static inline void touch_ll_filter_get_noise_thres(uint32_t *noise_thr) *noise_thr = RTCCNTL.touch_filter_ctrl.touch_noise_thres; } -/** - * Set jitter filter step size. - * If filter mode is jitter, should set filter step for jitter. - * Range: 0 ~ 15 - * - * @param step The step size of the data change. - */ -static inline void touch_ll_filter_set_jitter_step(uint32_t step) -{ - RTCCNTL.touch_filter_ctrl.touch_jitter_step = step; -} - /** * Get jitter filter step size. * If filter mode is jitter, should set filter step for jitter. @@ -788,52 +1407,8 @@ static inline void touch_ll_filter_get_jitter_step(uint32_t *step) *step = RTCCNTL.touch_filter_ctrl.touch_jitter_step; } -/** - * Enable touch sensor filter and detection algorithm. - * For more details on the detection algorithm, please refer to the application documentation. - */ -static inline void touch_ll_filter_enable(void) -{ - RTCCNTL.touch_filter_ctrl.touch_filter_en = 1; -} - -/** - * Disable touch sensor filter and detection algorithm. - * For more details on the detection algorithm, please refer to the application documentation. - */ -static inline void touch_ll_filter_disable(void) -{ - RTCCNTL.touch_filter_ctrl.touch_filter_en = 0; -} - /************************ Denoise register setting ************************/ -/** - * Enable denoise function. - * T0 is an internal channel that does not have a corresponding external GPIO. - * T0 will work simultaneously with the measured channel Tn. Finally, the actual - * measured value of Tn is the value after subtracting lower bits of T0. - * This denoise function filters out interference introduced on all channels, - * such as noise introduced by the power supply and external EMI. - */ -static inline void touch_ll_denoise_enable(void) -{ - RTCCNTL.touch_scan_ctrl.touch_denoise_en = 1; -} - -/** - * Enable denoise function. - * T0 is an internal channel that does not have a corresponding external GPIO. - * T0 will work simultaneously with the measured channel Tn. Finally, the actual - * measured value of Tn is the value after subtracting lower bits of T0. - * This denoise function filters out interference introduced on all channels, - * such as noise introduced by the power supply and external EMI. - */ -static inline void touch_ll_denoise_disable(void) -{ - RTCCNTL.touch_scan_ctrl.touch_denoise_en = 0; -} - /** * Set internal reference capacitance of denoise channel. * Select the appropriate internal reference capacitance value so that @@ -880,16 +1455,6 @@ static inline void touch_ll_denoise_get_grade(touch_pad_denoise_grade_t *grade) *grade = (touch_pad_denoise_grade_t)(RTCCNTL.touch_scan_ctrl.touch_denoise_res); } -/** - * Read denoise measure value (TOUCH_PAD_NUM0). - * - * @param denoise value of denoise. - */ -static inline void touch_ll_denoise_read_data(uint32_t *data) -{ - *data = SENS.sar_touch_status0.touch_denoise_data; -} - /************************ Waterproof register setting ************************/ /** @@ -912,18 +1477,6 @@ static inline void touch_ll_waterproof_get_guard_pad(touch_pad_t *pad_num) *pad_num = (touch_pad_t)(RTCCNTL.touch_scan_ctrl.touch_out_ring); } -/** - * Set max equivalent capacitance for shield channel. - * The equivalent capacitance of the shielded channel can be calculated - * from the reading of denoise channel. - * - * @param pad_num Touch sensor channel number. - */ -static inline void touch_ll_waterproof_set_sheild_driver(touch_pad_shield_driver_t driver_level) -{ - RTCCNTL.touch_scan_ctrl.touch_bufdrv = driver_level; -} - /** * Get max equivalent capacitance for shield channel. * The equivalent capacitance of the shielded channel can be calculated @@ -931,32 +1484,11 @@ static inline void touch_ll_waterproof_set_sheild_driver(touch_pad_shield_driver * * @param pad_num Touch sensor channel number. */ -static inline void touch_ll_waterproof_get_sheild_driver(touch_pad_shield_driver_t *driver_level) +static inline void touch_ll_waterproof_get_shield_driver(touch_pad_shield_driver_t *driver_level) { *driver_level = (touch_pad_shield_driver_t)(RTCCNTL.touch_scan_ctrl.touch_bufdrv); } -/** - * Enable parameter of waterproof function. - * - * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel. - * Guard pad is used to detect the large area of water covering the touch panel. - * Shield pad is used to shield the influence of water droplets covering the touch panel. - * It is generally designed as a grid and is placed around the touch buttons. - */ -static inline void touch_ll_waterproof_enable(void) -{ - RTCCNTL.touch_scan_ctrl.touch_shield_pad_en = 1; -} - -/** - * Disable parameter of waterproof function. - */ -static inline void touch_ll_waterproof_disable(void) -{ - RTCCNTL.touch_scan_ctrl.touch_shield_pad_en = 0; -} - /************************ Proximity register setting ************************/ /** @@ -1038,18 +1570,6 @@ static inline bool touch_ll_proximity_pad_check(touch_pad_t touch_num) } /************** sleep pad setting ***********************/ - -/** - * Set touch channel number for sleep pad. - * - * @note Only one touch sensor channel is supported in deep sleep mode. - * @param touch_num Touch sensor channel number. - */ -static inline void touch_ll_sleep_set_channel_num(touch_pad_t touch_num) -{ - RTCCNTL.touch_slp_thres.touch_slp_pad = touch_num; -} - /** * Get touch channel number for sleep pad. * @@ -1061,18 +1581,6 @@ static inline void touch_ll_sleep_get_channel_num(touch_pad_t *touch_num) *touch_num = (touch_pad_t)(RTCCNTL.touch_slp_thres.touch_slp_pad); } -/** - * Set the trigger threshold of touch sensor in deep sleep. - * The threshold determines the sensitivity of the touch sensor. - * The threshold is the original value of the trigger state minus the benchmark value. - * - * @note In general, the touch threshold during sleep can use the threshold parameter parameters before sleep. - */ -static inline void touch_ll_sleep_set_threshold(uint32_t touch_thres) -{ - RTCCNTL.touch_slp_thres.touch_slp_th = touch_thres; -} - /** * Get the trigger threshold of touch sensor in deep sleep. * The threshold determines the sensitivity of the touch sensor. @@ -1085,22 +1593,6 @@ static inline void touch_ll_sleep_get_threshold(uint32_t *touch_thres) *touch_thres = RTCCNTL.touch_slp_thres.touch_slp_th; } -/** - * Enable proximity function for sleep pad. - */ -static inline void touch_ll_sleep_enable_proximity_sensing(void) -{ - RTCCNTL.touch_slp_thres.touch_slp_approach_en = 1; -} - -/** - * Disable proximity function for sleep pad. - */ -static inline void touch_ll_sleep_disable_proximity_sensing(void) -{ - RTCCNTL.touch_slp_thres.touch_slp_approach_en = 0; -} - /** * Get proximity function status for sleep pad. */ @@ -1134,11 +1626,6 @@ static inline void touch_ll_sleep_read_data(uint32_t *raw_data) *raw_data = SENS.sar_touch_status[touch_num - 1].touch_pad_data; } -static inline void touch_ll_sleep_reset_benchmark(void) -{ - RTCCNTL.touch_approach.touch_slp_channel_clr = 1; -} - /** * Select touch sensor dbias to save power in sleep mode. * diff --git a/components/hal/esp32s2/touch_sensor_hal.c b/components/hal/esp32s2/touch_sensor_hal.c index 78bc17819e..dd1d1c400f 100644 --- a/components/hal/esp32s2/touch_sensor_hal.c +++ b/components/hal/esp32s2/touch_sensor_hal.c @@ -48,12 +48,12 @@ void touch_hal_deinit(void) touch_ll_clear_trigger_status_mask(); touch_ll_intr_disable(TOUCH_PAD_INTR_MASK_ALL); touch_ll_timeout_disable(); - touch_ll_waterproof_disable(); - touch_ll_denoise_disable(); + touch_ll_waterproof_enable(false); + touch_ll_denoise_enable(false); touch_pad_t prox_pad[SOC_TOUCH_PROXIMITY_CHANNEL_NUM] = {[0 ... (SOC_TOUCH_PROXIMITY_CHANNEL_NUM - 1)] = 0}; touch_ll_proximity_set_channel_num((const touch_pad_t *)prox_pad); touch_ll_sleep_set_channel_num(0); - touch_ll_sleep_disable_proximity_sensing(); + touch_ll_sleep_enable_proximity_sensing(false); touch_ll_reset(); // Reset the touch sensor FSM. } @@ -90,25 +90,25 @@ void touch_hal_denoise_get_config(touch_pad_denoise_t *denoise) void touch_hal_denoise_enable(void) { touch_ll_clear_channel_mask(1U << SOC_TOUCH_DENOISE_CHANNEL); - touch_ll_denoise_enable(); + touch_ll_denoise_enable(true); } void touch_hal_waterproof_set_config(const touch_pad_waterproof_t *waterproof) { touch_ll_waterproof_set_guard_pad(waterproof->guard_ring_pad); - touch_ll_waterproof_set_sheild_driver(waterproof->shield_driver); + touch_ll_waterproof_set_shield_driver(waterproof->shield_driver); } void touch_hal_waterproof_get_config(touch_pad_waterproof_t *waterproof) { touch_ll_waterproof_get_guard_pad(&waterproof->guard_ring_pad); - touch_ll_waterproof_get_sheild_driver(&waterproof->shield_driver); + touch_ll_waterproof_get_shield_driver(&waterproof->shield_driver); } void touch_hal_waterproof_enable(void) { touch_ll_clear_channel_mask(1U << SOC_TOUCH_SHIELD_CHANNEL); - touch_ll_waterproof_enable(); + touch_ll_waterproof_enable(true); } bool touch_hal_enable_proximity(touch_pad_t touch_num, bool enabled) diff --git a/components/hal/esp32s3/include/hal/touch_sensor_hal.h b/components/hal/esp32s3/include/hal/touch_sensor_hal.h index c80f624fde..e9edc58c51 100644 --- a/components/hal/esp32s3/include/hal/touch_sensor_hal.h +++ b/components/hal/esp32s3/include/hal/touch_sensor_hal.h @@ -278,14 +278,10 @@ void touch_hal_filter_get_config(touch_filter_config_t *filter_info); /** * Enable touch sensor filter and detection algorithm. * For more details on the detection algorithm, please refer to the application documentation. + * + * @param enable set true to enable the filter */ -#define touch_hal_filter_enable() touch_ll_filter_enable() - -/** - * Disable touch sensor filter and detection algorithm. - * For more details on the detection algorithm, please refer to the application documentation. - */ -#define touch_hal_filter_disable() touch_ll_filter_disable() +#define touch_hal_filter_enable(enable) touch_ll_filter_enable(enable) /************************ Denoise register setting ************************/ @@ -326,7 +322,7 @@ void touch_hal_denoise_enable(void); * This denoise function filters out interference introduced on all channels, * such as noise introduced by the power supply and external EMI. */ -#define touch_hal_denoise_disable() touch_ll_denoise_disable() +#define touch_hal_denoise_disable() touch_ll_denoise_enable(false) /** * Set internal reference capacitance of denoise channel. @@ -391,7 +387,7 @@ void touch_hal_denoise_enable(void); * * @param pad_num Touch sensor channel number. */ -#define touch_hal_waterproof_set_sheild_driver(driver_level) touch_ll_waterproof_set_sheild_driver(driver_level) +#define touch_hal_waterproof_set_sheild_driver(driver_level) touch_ll_waterproof_set_shield_driver(driver_level) /** * Get max equivalent capacitance for shield channel. @@ -400,7 +396,7 @@ void touch_hal_denoise_enable(void); * * @param pad_num Touch sensor channel number. */ -#define touch_hal_waterproof_get_sheild_driver(driver_level) touch_ll_waterproof_get_sheild_driver(driver_level) +#define touch_hal_waterproof_get_sheild_driver(driver_level) touch_ll_waterproof_get_shield_driver(driver_level) /** * Set parameter of waterproof function. @@ -430,7 +426,7 @@ void touch_hal_waterproof_enable(void); /** * Disable parameter of waterproof function. */ -#define touch_hal_waterproof_disable() touch_ll_waterproof_disable() +#define touch_hal_waterproof_disable() touch_ll_waterproof_enable(false) /************************ Proximity register setting ************************/ @@ -550,13 +546,9 @@ void touch_hal_sleep_channel_enable(touch_pad_t pad_num, bool enable); /** * Enable proximity function for sleep pad. + * @param enable the proximity sensing */ -#define touch_hal_sleep_enable_approach() touch_ll_sleep_enable_proximity_sensing() - -/** - * Disable proximity function for sleep pad. - */ -#define touch_hal_sleep_disable_approach() touch_ll_sleep_disable_proximity_sensing() +#define touch_hal_sleep_enable_approach(enable) touch_ll_sleep_enable_proximity_sensing(enable) /** * Read benchmark of touch sensor for sleep pad. diff --git a/components/hal/esp32s3/include/hal/touch_sensor_ll.h b/components/hal/esp32s3/include/hal/touch_sensor_ll.h index d3dde4edd3..0e22458f8b 100644 --- a/components/hal/esp32s3/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32s3/include/hal/touch_sensor_ll.h @@ -17,25 +17,724 @@ #include #include #include "hal/misc.h" +#include "hal/assert.h" #include "soc/touch_sensor_periph.h" #include "soc/rtc_cntl_struct.h" #include "soc/rtc_io_struct.h" #include "soc/sens_struct.h" #include "soc/soc_caps.h" #include "hal/touch_sensor_types.h" +#include "hal/touch_sens_types.h" #ifdef __cplusplus extern "C" { #endif - #define TOUCH_LL_READ_RAW 0x0 #define TOUCH_LL_READ_BENCHMARK 0x2 #define TOUCH_LL_READ_SMOOTH 0x3 + #define TOUCH_LL_TIMER_FORCE_DONE 0x3 #define TOUCH_LL_TIMER_DONE 0x0 -#define TOUCH_LL_PAD_MEASURE_WAIT_MAX (0xFF) /*! 0); + SENS.touch_thresh[touch_num - 1].thresh = thresh; +} + +/** + * Set the power on wait cycle + * + * @param wait_cycles + */ +static inline void touch_ll_set_power_on_wait_cycle(uint32_t wait_cycles) +{ + //the waiting cycles (in 8MHz) between TOUCH_START and TOUCH_XPD + HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_ctrl2, touch_xpd_wait, wait_cycles); //wait volt stable +} + +// HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_ctrl1, touch_meas_num, meas_time); +/** + * Set touch sensor touch sensor charge and discharge times of every measurement on a pad. + * + * @param charge_times The times of charge and discharge in each measure process of touch channels. + * The timer frequency is RTC_FAST (about 16M). Range: 0 ~ 0xffff. + */ +static inline void touch_ll_set_charge_times( uint16_t charge_times) +{ + HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_ctrl1, touch_meas_num, charge_times); +} + +/** + * Set touch sensor sleep time. + * + * @param interval_ticks The touch sensor will sleep for some cycles after each measurement. + * interval_ticks decide the interval between each measurement. + * t_sleep = interval_ticks / (RTC_SLOW_CLK frequency). + * The approximate frequency value of RTC_SLOW_CLK can be obtained using rtc_clk_slow_freq_get_hz function. + */ +static inline void touch_ll_set_measure_interval_ticks(uint16_t interval_ticks) +{ + // touch sensor sleep cycle Time = interval_ticks / RTC_SLOW_CLK + HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_ctrl1, touch_sleep_cycles, interval_ticks); +} + +static inline void touch_ll_set_charge_speed(uint32_t touch_num, touch_charge_speed_t charge_speed) +{ +#define CHARGE_SPEED_MASK(val, num) ((val) << (29 - (num) * 3)) + uint32_t speed_mask = 0; + if (touch_num < 10) { + speed_mask = RTCCNTL.touch_dac.val; + speed_mask &= ~CHARGE_SPEED_MASK(0x07, touch_num); // clear the old value + RTCCNTL.touch_dac.val = speed_mask | CHARGE_SPEED_MASK(charge_speed, touch_num); + } else { + speed_mask = RTCCNTL.touch_dac1.val; + speed_mask &= ~CHARGE_SPEED_MASK(0x07, touch_num - 10); // clear the old value + RTCCNTL.touch_dac1.val = speed_mask | CHARGE_SPEED_MASK(charge_speed, touch_num - 10); + } +#undef CHARGE_SPEED_MASK +} + +static inline void touch_ll_set_charge_voltage_high_limit(touch_volt_lim_h_t high_lim) +{ + RTCCNTL.touch_ctrl2.touch_drefh = (uint32_t)high_lim & 0x3; + RTCCNTL.touch_ctrl2.touch_drange = (uint32_t)high_lim >> 2; +} + +static inline void touch_ll_set_charge_voltage_low_limit(touch_volt_lim_l_t low_lim) +{ + RTCCNTL.touch_ctrl2.touch_drefl = low_lim; +} + +static inline void touch_ll_set_init_charge_voltage(uint32_t touch_num, touch_init_charge_volt_t init_charge_volt) +{ + RTCIO.touch_pad[touch_num].tie_opt = init_charge_volt; +} + +static inline void touch_ll_set_idle_channel_connection(touch_idle_conn_t idle_conn) +{ + RTCCNTL.touch_scan_ctrl.touch_inactive_connection = idle_conn; +} + +/** + * Enable touch sensor channel. Register touch channel into touch sensor measurement group. + * The working mode of the touch sensor is simultaneous measurement. + * This function will set the measure bits according to the given bitmask. + * + * @note If set this mask, the FSM timer should be stop firstly. + * @note The touch sensor that in scan map, should be deinit GPIO function firstly. + * @param enable_mask bitmask of touch sensor scan group. + * e.g. TOUCH_PAD_NUM1 -> BIT(1) + * @return + * - ESP_OK on success + */ +static inline void touch_ll_enable_channel_mask(uint16_t enable_mask) +{ + RTCCNTL.touch_scan_ctrl.touch_scan_pad_map = enable_mask; + SENS.sar_touch_conf.touch_outen = enable_mask; +} + +/** + * Set the timeout to enable or disable the check for all touch sensor channels measurements. + * When the touch reading of a touch channel exceeds the measurement threshold, + * If enable: a timeout interrupt will be generated and it will go to the next channel measurement. + * If disable: the FSM is always on the channel, until the measurement of this channel is over. + * + * @param timeout_cycles The maximum time cycles of the measurement on one channel. + * Set to 0 to disable the timeout. + * Set to non-zero to enable the timeout and set the timeout cycles. + */ +static inline void touch_ll_set_timeout(uint32_t timeout_cycles) +{ + if (timeout_cycles) { + RTCCNTL.touch_timeout_ctrl.touch_timeout_num = timeout_cycles; + RTCCNTL.touch_timeout_ctrl.touch_timeout_en = 1; + } else { + RTCCNTL.touch_timeout_ctrl.touch_timeout_en = 0; + } +} + +/** + * Clear all touch sensor channels active status. + * + * @note Generally no manual removal is required. + */ +static inline void touch_ll_clear_active_channel_status(void) +{ + SENS.sar_touch_conf.touch_status_clr = 1; +} + + +/** + * Select touch sensor dbias to save power in sleep mode. + * + * @note If change the dbias, the reading of touch sensor will changed. Users should make sure the threshold. + */ +static inline void touch_ll_set_bias_type(touch_bias_type_t bias_type) +{ + RTCCNTL.touch_ctrl2.touch_dbias = bias_type; +} + +/********************************* FSM Operation ******************************/ +/** + * Touch timer trigger measurement and always wait measurement done. + * Force done for touch timer ensures that the timer always can get the measurement done signal. + * @note The `force done` signal should last as least one slow clock tick + */ +__attribute__((always_inline)) +static inline void touch_ll_force_done_curr_measurement(void) +{ + RTCCNTL.touch_ctrl2.touch_timer_force_done = TOUCH_LL_TIMER_FORCE_DONE; + RTCCNTL.touch_ctrl2.touch_timer_force_done = TOUCH_LL_TIMER_DONE; +} + +/** + * Enable touch sensor FSM timer trigger (continuous) mode or software trigger (oneshot) mode. + * + * @param enable Enable FSM timer mode. + * True: the FSM will trigger scanning repeatedly under the control of the hardware timer (continuous mode) + * False: the FSM will trigger scanning once under the control of the software (continuous mode) + */ +__attribute__((always_inline)) +static inline void touch_ll_enable_fsm_timer(bool enable) +{ + touch_ll_force_done_curr_measurement(); + // Set 0 to start by timer, otherwise by software + RTCCNTL.touch_ctrl2.touch_start_force = !enable; +} + +/** + * Start touch sensor FSM timer to run FSM repeatedly + * The measurement action can be triggered by the hardware timer, as well as by the software instruction. + * @note + * The timer should be triggered + */ +__attribute__((always_inline)) +static inline void touch_ll_start_fsm_repeated_timer(void) +{ + /** + * Touch timer trigger measurement and always wait measurement done. + * Force done for touch timer ensures that the timer always can get the measurement done signal. + */ + touch_ll_force_done_curr_measurement(); + RTCCNTL.touch_ctrl2.touch_slp_timer_en = 1; +} + +/** + * Stop touch sensor FSM timer. + * The measurement action can be triggered by the hardware timer, as well as by the software instruction. + */ +__attribute__((always_inline)) +static inline void touch_ll_stop_fsm_repeated_timer(void) +{ + RTCCNTL.touch_ctrl2.touch_slp_timer_en = 0; + touch_ll_force_done_curr_measurement(); +} + +/** + * Is the FSM repeated timer enabled. + * @note when the timer is enabled, RTC clock should not be power down + * + * @return + * - true: enabled + * - false: disabled + */ +static inline bool touch_ll_is_fsm_repeated_timer_enabled(void) +{ + return (bool)RTCCNTL.touch_ctrl2.touch_slp_timer_en; +} + +/** + * Enable the touch sensor FSM start signal from software + */ +__attribute__((always_inline)) +static inline void touch_ll_trigger_oneshot_measurement(void) +{ + RTCCNTL.touch_ctrl2.touch_start_en = 1; + RTCCNTL.touch_ctrl2.touch_start_en = 0; +} + +/** + * @brief Power on the channel by mask + * + * @param chan_mask The channel mask that needs to power on + */ +__attribute__((always_inline)) +static inline void touch_ll_channel_sw_measure_mask(uint16_t chan_mask) +{ + (void) chan_mask; + // Only for compatibility +} + +/****************************** Benchmark Operation ***************************/ +/** + * Force reset benchmark to raw data of touch sensor. + * + * @note If call this API, make sure enable clock gate(`touch_ll_clkgate`) first. + * @param chan_mask touch channel mask + */ +__attribute__((always_inline)) +static inline void touch_ll_reset_chan_benchmark(uint32_t chan_mask) +{ + SENS.sar_touch_chn_st.touch_channel_clr = chan_mask; +} + +static inline void touch_ll_sleep_reset_benchmark(void) +{ + RTCCNTL.touch_approach.touch_slp_channel_clr = 1; +} + +/************************************** Data **********************************/ +/** + * Get the data of the touch channel according to the types + * + * @param touch_num touch pad index + * @param type data type + * 0/1: TOUCH_LL_READ_RAW, raw data of 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 data pointer to the data + */ +__attribute__((always_inline)) +static inline void touch_ll_read_chan_data(uint32_t touch_num, uint8_t type, uint32_t *data) +{ + HAL_ASSERT(type <= TOUCH_LL_READ_SMOOTH); + HAL_ASSERT(touch_num > 0); + SENS.sar_touch_conf.touch_data_sel = type; + *data = SENS.sar_touch_status[touch_num - 1].touch_pad_data; +} + +/****************************** Filter Configuration **************************/ + +/** + * Enable or disable touch sensor filter and detection algorithm. + * For more details on the detection algorithm, please refer to the application documentation. + */ +static inline void touch_ll_filter_enable(bool enable) +{ + RTCCNTL.touch_filter_ctrl.touch_filter_en = enable; +} + + +/** + * Set filter mode. The input of the filter is the raw value of touch reading, + * and the output of the filter is involved in the judgment of the touch state. + * + * @param mode Filter mode type. Refer to ``touch_filter_mode_t``. + */ +static inline void touch_ll_filter_set_filter_mode(touch_filter_mode_t mode) +{ + RTCCNTL.touch_filter_ctrl.touch_filter_mode = mode; +} + +/** + * Set jitter filter step size. + * If filter mode is jitter, should set filter step for jitter. + * Range: 0 ~ 15 + * + * @param step The step size of the data change. + */ +static inline void touch_ll_filter_set_jitter_step(uint32_t step) +{ + RTCCNTL.touch_filter_ctrl.touch_jitter_step = step; +} + +/** + * Set the denoise coefficient regarding the denoise level. + * + * @param denoise_lvl Range [0 ~ 4]. 0 = no noise resistance, otherwise higher denoise_lvl means more noise resistance. + */ +static inline void touch_ll_filter_set_denoise_level(int denoise_lvl) +{ + HAL_ASSERT(denoise_lvl >= 0 && denoise_lvl <= 4); + bool always_update = denoise_lvl == 0; + // Map denoise level to actual noise threshold coefficients + uint32_t noise_thresh = denoise_lvl == 4 ? 3 : 3 - denoise_lvl; + + RTCCNTL.touch_filter_ctrl.touch_bypass_noise_thres = always_update; + RTCCNTL.touch_filter_ctrl.touch_noise_thres = always_update ? 0 : noise_thresh; + + RTCCNTL.touch_filter_ctrl.touch_bypass_nn_thres = always_update; + RTCCNTL.touch_filter_ctrl.config2 = always_update ? 0 : noise_thresh; + RTCCNTL.touch_filter_ctrl.config1 = 0xF; +} + +/** + * Set the hysteresis value of the active threshold + * While the touch data is greater than active_threshold + hysteresis and last for several ticks, the channel is activated, + * and while the touch data is smaller than active_threshold - hysteresis and last for several ticks, the channel is inactivated + * + * @param hysteresis The hysteresis value of active threshold + */ +static inline void touch_ll_filter_set_active_hysteresis(uint32_t hysteresis) +{ + RTCCNTL.touch_filter_ctrl.config3 = hysteresis; +} + +/** + * Set filter mode. The input to the filter is raw data and the output is the smooth data. + * The smooth data is used to determine the touch status. + * + * @param mode Filter mode type. Refer to ``touch_smooth_mode_t``. + */ +static inline void touch_ll_filter_set_smooth_mode(touch_smooth_mode_t mode) +{ + RTCCNTL.touch_filter_ctrl.touch_smooth_lvl = mode; +} + +/** + * Set debounce count, such as `n`. If the measured values continue to exceed + * the threshold for `n+1` times, it is determined that the touch sensor state changes. + * + * @param dbc_cnt Debounce count value. + */ +static inline void touch_ll_filter_set_debounce(uint32_t dbc_cnt) +{ + RTCCNTL.touch_filter_ctrl.touch_debounce = dbc_cnt; +} + +/**************************** Sleep Configurations ****************************/ +/** + * Set the trigger threshold of touch sensor in deep sleep. + * The threshold determines the sensitivity of the touch sensor. + * The threshold is the original value of the trigger state minus the benchmark value. + * + * @note In general, the touch threshold during sleep can use the threshold parameter parameters before sleep. + */ +static inline void touch_ll_sleep_set_threshold(uint32_t touch_thres) +{ + RTCCNTL.touch_slp_thres.touch_slp_th = touch_thres; +} + +/** + * Set touch channel number for sleep channel. + * + * @note Only one touch sensor channel is supported in deep sleep mode. + * @param touch_num Touch sensor channel number. + */ +static inline void touch_ll_sleep_set_channel_num(uint32_t touch_num) +{ + RTCCNTL.touch_slp_thres.touch_slp_pad = touch_num; +} + +/** + * Enable proximity sensing function for sleep channel. + */ +static inline void touch_ll_sleep_enable_proximity_sensing(bool enable) +{ + RTCCNTL.touch_slp_thres.touch_slp_approach_en = enable; +} + +/************************* Waterproof Configurations **************************/ +/** + * Enable parameter of waterproof function. + * + * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel. + * Guard pad is used to detect the large area of water covering the touch panel. + * Shield pad is used to shield the influence of water droplets covering the touch panel. + * It is generally designed as a grid and is placed around the touch buttons. + * @param enable Enable or disable waterproof function. + */ +static inline void touch_ll_waterproof_enable(bool enable) +{ + RTCCNTL.touch_scan_ctrl.touch_shield_pad_en = enable; +} + +/** + * Set touch channel use for guard channel. + * + * @param pad_num Touch sensor channel number. + */ +static inline void touch_ll_waterproof_set_guard_chan(uint32_t pad_num) +{ + RTCCNTL.touch_scan_ctrl.touch_out_ring = pad_num; +} + +/** + * Set max equivalent capacitance for shield channel. + * The equivalent capacitance of the shielded channel can be calculated + * from the reading of denoise channel. + * + * @param pad_num Touch sensor channel number. + */ +static inline void touch_ll_waterproof_set_shield_driver(touch_pad_shield_driver_t driver_level) +{ + RTCCNTL.touch_scan_ctrl.touch_bufdrv = driver_level; +} + +/****************************** Proximity Sensing *****************************/ +/** + * Set the proximity sensing channel to the specific touch channel + * To disable the proximity channel, point this pad to `TOUCH_LL_NULL_CHANNEL` + * + * @param prox_chan proximity sensing channel. + * @param touch_num The touch channel that supposed to be used as proximity sensing channel + */ +static inline void touch_ll_set_proximity_sensing_channel(uint8_t prox_chan, uint32_t touch_num) +{ + switch (prox_chan) { + case 0: + SENS.sar_touch_conf.touch_approach_pad0 = touch_num; + break; + case 1: + SENS.sar_touch_conf.touch_approach_pad1 = touch_num; + break; + case 2: + SENS.sar_touch_conf.touch_approach_pad2 = touch_num; + break; + default: + // invalid proximity channel + abort(); + } +} + +/** + * Set the total scan times of the proximity sensing channel. + * + * @param scan_times The total scan times of the proximity sensing channel + */ +static inline void touch_ll_proximity_set_total_scan_times(uint32_t scan_times) +{ + HAL_FORCE_MODIFY_U32_REG_FIELD(RTCCNTL.touch_approach, touch_approach_meas_time, scan_times); +} + +/** + * Get the total scan times of the proximity sensing channel. + * + * @return + * - The total scan times of the proximity sensing channel + */ +__attribute__((always_inline)) +static inline uint32_t touch_ll_proximity_get_total_scan_times(void) +{ + return HAL_FORCE_READ_U32_REG_FIELD(RTCCNTL.touch_approach, touch_approach_meas_time); +} + +/** + * Get the current scan count for proximity channel. + * + * @param touch_num Touch channel number. + * @return + * - Current scan count for proximity channel + */ +__attribute__((always_inline)) +static inline uint32_t touch_ll_proximity_get_curr_scan_cnt(uint32_t touch_num) +{ + if (SENS.sar_touch_conf.touch_approach_pad0 == touch_num) { + return HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad0_cnt); + } else if (SENS.sar_touch_conf.touch_approach_pad1 == touch_num) { + return HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad1_cnt); + } else if (SENS.sar_touch_conf.touch_approach_pad2 == touch_num) { + return HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad2_cnt); + } + return 0; +} + +/******************************* Denoise Channel ******************************/ +/** + * Enable denoise function. + * T0 is an internal channel that does not have a corresponding external GPIO. + * T0 will work simultaneously with the measured channel Tn. Finally, the actual + * measured value of Tn is the value after subtracting lower bits of T0. + * This denoise function filters out interference introduced on all channels, + * such as noise introduced by the power supply and external EMI. + * @param enable enable the denoise channel + */ +static inline void touch_ll_denoise_enable(bool enable) +{ + RTCCNTL.touch_scan_ctrl.touch_denoise_en = enable; +} + +/** + * Set internal reference capacitance of denoise channel. + * Select the appropriate internal reference capacitance value so that + * the reading of denoise channel is closest to the reading of the channel being measured. + * + * @param capacitance Reference capacitance level. + */ +static inline void touch_ll_denoise_set_reference_cap(touch_denoise_chan_cap_t capacitance) +{ + RTCCNTL.touch_ctrl2.touch_refc = capacitance; +} + +/** + * Set denoise resolution of denoise channel. + * Determined by measuring the noise amplitude of the denoise channel. + * + * @param resolution Denoise resolution of denoise channel. + */ +static inline void touch_ll_denoise_set_resolution(touch_denoise_chan_res_t resolution) +{ + RTCCNTL.touch_scan_ctrl.touch_denoise_res = resolution; +} + +/** + * Read denoise measure value (TOUCH_PAD_NUM0). + * + * @param denoise value of denoise. + */ +static inline void touch_ll_denoise_read_data(uint32_t *data) +{ + *data = SENS.sar_touch_denoise.touch_denoise_data; +} + +/******************************************************************************/ +/* Legacy APIs */ +/******************************************************************************/ /** * Set touch sensor touch sensor times of charge and discharge. * @@ -85,11 +784,11 @@ static inline void touch_ll_get_sleep_time(uint16_t *sleep_time) } /** - * Set touch sensor high voltage threshold of chanrge. + * Set touch sensor high voltage threshold of charge. * The touch sensor measures the channel capacitance value by charging and discharging the channel. * So the high threshold should be less than the supply voltage. * - * @param refh The high voltage threshold of chanrge. + * @param refh The high voltage threshold of charge. */ static inline void touch_ll_set_voltage_high(touch_high_volt_t refh) { @@ -97,11 +796,11 @@ static inline void touch_ll_set_voltage_high(touch_high_volt_t refh) } /** - * Get touch sensor high voltage threshold of chanrge. + * Get touch sensor high voltage threshold of charge. * The touch sensor measures the channel capacitance value by charging and discharging the channel. * So the high threshold should be less than the supply voltage. * - * @param refh The high voltage threshold of chanrge. + * @param refh The high voltage threshold of charge. */ static inline void touch_ll_get_voltage_high(touch_high_volt_t *refh) { @@ -131,11 +830,11 @@ static inline void touch_ll_get_voltage_low(touch_low_volt_t *refl) } /** - * Set touch sensor high voltage attenuation of chanrge. The actual charge threshold is high voltage threshold minus attenuation value. + * Set touch sensor high voltage attenuation of charge. The actual charge threshold is high voltage threshold minus attenuation value. * The touch sensor measures the channel capacitance value by charging and discharging the channel. * So the high threshold should be less than the supply voltage. * - * @param refh The high voltage threshold of chanrge. + * @param refh The high voltage threshold of charge. */ static inline void touch_ll_set_voltage_attenuation(touch_volt_atten_t atten) { @@ -143,11 +842,11 @@ static inline void touch_ll_set_voltage_attenuation(touch_volt_atten_t atten) } /** - * Get touch sensor high voltage attenuation of chanrge. The actual charge threshold is high voltage threshold minus attenuation value. + * Get touch sensor high voltage attenuation of charge. The actual charge threshold is high voltage threshold minus attenuation value. * The touch sensor measures the channel capacitance value by charging and discharging the channel. * So the high threshold should be less than the supply voltage. * - * @param refh The high voltage threshold of chanrge. + * @param refh The high voltage threshold of charge. */ static inline void touch_ll_get_voltage_attenuation(touch_volt_atten_t *atten) { @@ -427,20 +1126,6 @@ static inline uint32_t IRAM_ATTR touch_ll_read_raw_data(touch_pad_t touch_num) return SENS.sar_touch_status[touch_num - 1].touch_pad_data; } -/** - * Get touch sensor measure status. No block. - * - * @return - * - If touch sensors measure done. - */ -__attribute__((always_inline)) -static inline bool touch_ll_is_measure_done(void) -{ - return (bool)SENS.sar_touch_chn_st.touch_meas_done; -} - -/************************* esp32s2 only *************************/ - /** * Reset the whole of touch module. * @@ -483,17 +1168,6 @@ static inline void touch_ll_get_idle_channel_connect(touch_pad_conn_type_t *type *type = (touch_pad_conn_type_t)(RTCCNTL.touch_scan_ctrl.touch_inactive_connection); } -/** - * Get the current measure channel. Touch sensor measurement is cyclic scan mode. - * - * @return - * - touch channel number - */ -static inline touch_pad_t IRAM_ATTR touch_ll_get_current_meas_channel(void) -{ - return (touch_pad_t)(SENS.sar_touch_status0.touch_scan_curr); -} - /** * Enable touch sensor interrupt by bitmask. * @@ -700,17 +1374,6 @@ static inline void touch_ll_reset_benchmark(touch_pad_t touch_num) } } -/** - * Set filter mode. The input of the filter is the raw value of touch reading, - * and the output of the filter is involved in the judgment of the touch state. - * - * @param mode Filter mode type. Refer to ``touch_filter_mode_t``. - */ -static inline void touch_ll_filter_set_filter_mode(touch_filter_mode_t mode) -{ - RTCCNTL.touch_filter_ctrl.touch_filter_mode = mode; -} - /** * Get filter mode. The input of the filter is the raw value of touch reading, * and the output of the filter is involved in the judgment of the touch state. @@ -722,17 +1385,6 @@ static inline void touch_ll_filter_get_filter_mode(touch_filter_mode_t *mode) *mode = (touch_filter_mode_t)RTCCNTL.touch_filter_ctrl.touch_filter_mode; } -/** - * Set filter mode. The input to the filter is raw data and the output is the smooth data. - * The smooth data is used to determine the touch status. - * - * @param mode Filter mode type. Refer to ``touch_smooth_mode_t``. - */ -static inline void touch_ll_filter_set_smooth_mode(touch_smooth_mode_t mode) -{ - RTCCNTL.touch_filter_ctrl.touch_smooth_lvl = mode; -} - /** * Get filter mode. The smooth data is used to determine the touch status. * @@ -743,17 +1395,6 @@ static inline void touch_ll_filter_get_smooth_mode(touch_smooth_mode_t *mode) *mode = (touch_smooth_mode_t)(RTCCNTL.touch_filter_ctrl.touch_smooth_lvl); } -/** - * Set debounce count, such as `n`. If the measured values continue to exceed - * the threshold for `n+1` times, it is determined that the touch sensor state changes. - * - * @param dbc_cnt Debounce count value. - */ -static inline void touch_ll_filter_set_debounce(uint32_t dbc_cnt) -{ - RTCCNTL.touch_filter_ctrl.touch_debounce = dbc_cnt; -} - /** * Get debounce count. * @@ -791,18 +1432,6 @@ static inline void touch_ll_filter_get_noise_thres(uint32_t *noise_thr) *noise_thr = RTCCNTL.touch_filter_ctrl.touch_noise_thres; } -/** - * Set jitter filter step size. - * If filter mode is jitter, should set filter step for jitter. - * Range: 0 ~ 15 - * - * @param step The step size of the data change. - */ -static inline void touch_ll_filter_set_jitter_step(uint32_t step) -{ - RTCCNTL.touch_filter_ctrl.touch_jitter_step = step; -} - /** * Get jitter filter step size. * If filter mode is jitter, should set filter step for jitter. @@ -815,52 +1444,8 @@ static inline void touch_ll_filter_get_jitter_step(uint32_t *step) *step = RTCCNTL.touch_filter_ctrl.touch_jitter_step; } -/** - * Enable touch sensor filter and detection algorithm. - * For more details on the detection algorithm, please refer to the application documentation. - */ -static inline void touch_ll_filter_enable(void) -{ - RTCCNTL.touch_filter_ctrl.touch_filter_en = 1; -} - -/** - * Disable touch sensor filter and detection algorithm. - * For more details on the detection algorithm, please refer to the application documentation. - */ -static inline void touch_ll_filter_disable(void) -{ - RTCCNTL.touch_filter_ctrl.touch_filter_en = 0; -} - /************************ Denoise register setting ************************/ -/** - * Enable denoise function. - * T0 is an internal channel that does not have a corresponding external GPIO. - * T0 will work simultaneously with the measured channel Tn. Finally, the actual - * measured value of Tn is the value after subtracting lower bits of T0. - * This denoise function filters out interference introduced on all channels, - * such as noise introduced by the power supply and external EMI. - */ -static inline void touch_ll_denoise_enable(void) -{ - RTCCNTL.touch_scan_ctrl.touch_denoise_en = 1; -} - -/** - * Enable denoise function. - * T0 is an internal channel that does not have a corresponding external GPIO. - * T0 will work simultaneously with the measured channel Tn. Finally, the actual - * measured value of Tn is the value after subtracting lower bits of T0. - * This denoise function filters out interference introduced on all channels, - * such as noise introduced by the power supply and external EMI. - */ -static inline void touch_ll_denoise_disable(void) -{ - RTCCNTL.touch_scan_ctrl.touch_denoise_en = 0; -} - /** * Set internal reference capacitance of denoise channel. * Select the appropriate internal reference capacitance value so that @@ -907,16 +1492,6 @@ static inline void touch_ll_denoise_get_grade(touch_pad_denoise_grade_t *grade) *grade = (touch_pad_denoise_grade_t)(RTCCNTL.touch_scan_ctrl.touch_denoise_res); } -/** - * Read denoise measure value (TOUCH_PAD_NUM0). - * - * @param denoise value of denoise. - */ -static inline void touch_ll_denoise_read_data(uint32_t *data) -{ - *data = SENS.sar_touch_denoise.touch_denoise_data; -} - /************************ Waterproof register setting ************************/ /** @@ -939,18 +1514,6 @@ static inline void touch_ll_waterproof_get_guard_pad(touch_pad_t *pad_num) *pad_num = (touch_pad_t)(RTCCNTL.touch_scan_ctrl.touch_out_ring); } -/** - * Set max equivalent capacitance for shield channel. - * The equivalent capacitance of the shielded channel can be calculated - * from the reading of denoise channel. - * - * @param pad_num Touch sensor channel number. - */ -static inline void touch_ll_waterproof_set_sheild_driver(touch_pad_shield_driver_t driver_level) -{ - RTCCNTL.touch_scan_ctrl.touch_bufdrv = driver_level; -} - /** * Get max equivalent capacitance for shield channel. * The equivalent capacitance of the shielded channel can be calculated @@ -958,32 +1521,11 @@ static inline void touch_ll_waterproof_set_sheild_driver(touch_pad_shield_driver * * @param pad_num Touch sensor channel number. */ -static inline void touch_ll_waterproof_get_sheild_driver(touch_pad_shield_driver_t *driver_level) +static inline void touch_ll_waterproof_get_shield_driver(touch_pad_shield_driver_t *driver_level) { *driver_level = (touch_pad_shield_driver_t)(RTCCNTL.touch_scan_ctrl.touch_bufdrv); } -/** - * Enable parameter of waterproof function. - * - * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel. - * Guard pad is used to detect the large area of water covering the touch panel. - * Shield pad is used to shield the influence of water droplets covering the touch panel. - * It is generally designed as a grid and is placed around the touch buttons. - */ -static inline void touch_ll_waterproof_enable(void) -{ - RTCCNTL.touch_scan_ctrl.touch_shield_pad_en = 1; -} - -/** - * Disable parameter of waterproof function. - */ -static inline void touch_ll_waterproof_disable(void) -{ - RTCCNTL.touch_scan_ctrl.touch_shield_pad_en = 0; -} - /************************ Proximity register setting ************************/ /** @@ -1040,11 +1582,11 @@ static inline void touch_ll_proximity_get_meas_times(uint32_t *times) static inline void touch_ll_proximity_read_meas_cnt(touch_pad_t touch_num, uint32_t *cnt) { if (SENS.sar_touch_conf.touch_approach_pad0 == touch_num) { - *cnt = SENS.sar_touch_appr_status.touch_approach_pad0_cnt; + *cnt = HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad0_cnt); } else if (SENS.sar_touch_conf.touch_approach_pad1 == touch_num) { - *cnt = SENS.sar_touch_appr_status.touch_approach_pad1_cnt; + *cnt = HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad1_cnt); } else if (SENS.sar_touch_conf.touch_approach_pad2 == touch_num) { - *cnt = SENS.sar_touch_appr_status.touch_approach_pad2_cnt; + *cnt = HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_approach_pad2_cnt); } } @@ -1065,18 +1607,6 @@ static inline bool touch_ll_proximity_pad_check(touch_pad_t touch_num) } /************** sleep pad setting ***********************/ - -/** - * Set touch channel number for sleep pad. - * - * @note Only one touch sensor channel is supported in deep sleep mode. - * @param touch_num Touch sensor channel number. - */ -static inline void touch_ll_sleep_set_channel_num(touch_pad_t touch_num) -{ - RTCCNTL.touch_slp_thres.touch_slp_pad = touch_num; -} - /** * Get touch channel number for sleep pad. * @@ -1088,18 +1618,6 @@ static inline void touch_ll_sleep_get_channel_num(touch_pad_t *touch_num) *touch_num = (touch_pad_t)(RTCCNTL.touch_slp_thres.touch_slp_pad); } -/** - * Set the trigger threshold of touch sensor in deep sleep. - * The threshold determines the sensitivity of the touch sensor. - * The threshold is the original value of the trigger state minus the benchmark value. - * - * @note In general, the touch threshold during sleep can use the threshold parameter parameters before sleep. - */ -static inline void touch_ll_sleep_set_threshold(uint32_t touch_thres) -{ - RTCCNTL.touch_slp_thres.touch_slp_th = touch_thres; -} - /** * Get the trigger threshold of touch sensor in deep sleep. * The threshold determines the sensitivity of the touch sensor. @@ -1112,22 +1630,6 @@ static inline void touch_ll_sleep_get_threshold(uint32_t *touch_thres) *touch_thres = RTCCNTL.touch_slp_thres.touch_slp_th; } -/** - * Enable proximity function for sleep pad. - */ -static inline void touch_ll_sleep_enable_proximity_sensing(void) -{ - RTCCNTL.touch_slp_thres.touch_slp_approach_en = 1; -} - -/** - * Disable proximity function for sleep pad. - */ -static inline void touch_ll_sleep_disable_proximity_sensing(void) -{ - RTCCNTL.touch_slp_thres.touch_slp_approach_en = 0; -} - /** * Get proximity function status for sleep pad. */ @@ -1161,10 +1663,6 @@ static inline void touch_ll_sleep_read_data(uint32_t *raw_data) *raw_data = SENS.sar_touch_status[touch_num - 1].touch_pad_data; } -static inline void touch_ll_sleep_reset_benchmark(void) -{ - RTCCNTL.touch_approach.touch_slp_channel_clr = 1; -} /** * Select touch sensor dbias to save power in sleep mode. @@ -1192,7 +1690,7 @@ static inline void touch_ll_sleep_read_debounce(uint32_t *debounce) */ static inline void touch_ll_sleep_read_proximity_cnt(uint32_t *prox_cnt) { - *prox_cnt = SENS.sar_touch_appr_status.touch_slp_approach_cnt; + *prox_cnt = HAL_FORCE_READ_U32_REG_FIELD(SENS.sar_touch_appr_status, touch_slp_approach_cnt); } /** diff --git a/components/hal/esp32s3/touch_sensor_hal.c b/components/hal/esp32s3/touch_sensor_hal.c index 83dcc05cd5..c68e4d094e 100644 --- a/components/hal/esp32s3/touch_sensor_hal.c +++ b/components/hal/esp32s3/touch_sensor_hal.c @@ -8,6 +8,7 @@ #include "soc/soc_pins.h" #include "hal/touch_sensor_hal.h" +#include "hal/touch_sensor_ll.h" #include "hal/touch_sensor_types.h" #include "soc/soc_caps.h" @@ -48,12 +49,12 @@ void touch_hal_deinit(void) touch_ll_clear_trigger_status_mask(); touch_ll_intr_disable(TOUCH_PAD_INTR_MASK_ALL); touch_ll_timeout_disable(); - touch_ll_waterproof_disable(); - touch_ll_denoise_disable(); + touch_ll_waterproof_enable(false); + touch_ll_denoise_enable(false); touch_pad_t prox_pad[SOC_TOUCH_PROXIMITY_CHANNEL_NUM] = {[0 ... (SOC_TOUCH_PROXIMITY_CHANNEL_NUM - 1)] = 0}; touch_ll_proximity_set_channel_num((const touch_pad_t *)prox_pad); touch_ll_sleep_set_channel_num(0); - touch_ll_sleep_disable_proximity_sensing(); + touch_ll_sleep_enable_proximity_sensing(false); touch_ll_reset(); // Reset the touch sensor FSM. } @@ -90,25 +91,25 @@ void touch_hal_denoise_get_config(touch_pad_denoise_t *denoise) void touch_hal_denoise_enable(void) { touch_ll_clear_channel_mask(1U << SOC_TOUCH_DENOISE_CHANNEL); - touch_ll_denoise_enable(); + touch_ll_denoise_enable(true); } void touch_hal_waterproof_set_config(const touch_pad_waterproof_t *waterproof) { touch_ll_waterproof_set_guard_pad(waterproof->guard_ring_pad); - touch_ll_waterproof_set_sheild_driver(waterproof->shield_driver); + touch_ll_waterproof_set_shield_driver(waterproof->shield_driver); } void touch_hal_waterproof_get_config(touch_pad_waterproof_t *waterproof) { touch_ll_waterproof_get_guard_pad(&waterproof->guard_ring_pad); - touch_ll_waterproof_get_sheild_driver(&waterproof->shield_driver); + touch_ll_waterproof_get_shield_driver(&waterproof->shield_driver); } void touch_hal_waterproof_enable(void) { touch_ll_clear_channel_mask(1U << SOC_TOUCH_SHIELD_CHANNEL); - touch_ll_waterproof_enable(); + touch_ll_waterproof_enable(true); } bool touch_hal_enable_proximity(touch_pad_t touch_num, bool enabled) diff --git a/components/hal/esp32p4/include/hal/touch_sensor_hal.h b/components/hal/include/hal/touch_sens_hal.h similarity index 76% rename from components/hal/esp32p4/include/hal/touch_sensor_hal.h rename to components/hal/include/hal/touch_sens_hal.h index bc6d03f503..a86953acbc 100644 --- a/components/hal/esp32p4/include/hal/touch_sensor_hal.h +++ b/components/hal/include/hal/touch_sens_hal.h @@ -10,22 +10,35 @@ * See readme.md in hal/include/hal/readme.md ******************************************************************************/ -// The HAL layer for touch sensor (ESP32-P4 specific part) #pragma once +#include "soc/soc_caps.h" +#if SOC_TOUCH_SENSOR_VERSION > 1 #include "hal/touch_sensor_ll.h" -#include "hal/touch_sensor_types.h" +#include "hal/touch_sens_types.h" +#endif // SOC_TOUCH_SENSOR_SUPPORTED #ifdef __cplusplus extern "C" { #endif +#if SOC_TOUCH_SENSOR_VERSION > 1 + /** * @brief Sample configurations of the touch sensor * */ typedef struct { +#if SOC_TOUCH_SENSOR_VERSION == 2 // S2/S3 + uint32_t charge_times; /*!< The charge and discharge times of this sample configuration, the read data are positive correlation to the charge_times */ + touch_volt_lim_h_t charge_volt_lim_h; /*!< The upper voltage limit while charging a touch pad */ + touch_volt_lim_l_t charge_volt_lim_l; /*!< The lower voltage limit while charging a touch pad */ + touch_idle_conn_t idle_conn; /*!< The connection of the idle touch channels. + * The idle touch channel is a channel which is enabled but not under measuring. + */ + touch_bias_type_t bias_type; +#elif SOC_TOUCH_SENSOR_VERSION == 3 // P4 uint32_t div_num; /*!< The division from the source clock to the sampling frequency */ uint32_t charge_times; /*!< The charge and discharge times of the sample configuration, the read data are positive correlation to the charge_times */ uint8_t rc_filter_res; /*!< The resistance of the RC filter of the sample configuration, range [0, 3], while 0 = 0K, 1 = 1.5K, 2 = 3K, 3 = 4.5K */ @@ -34,6 +47,9 @@ typedef struct { uint8_t high_drv; /*!< High speed touch driver */ uint8_t bias_volt; /*!< The Internal LDO voltage, which decide the bias voltage of the sample wave, range [0,15] */ bool bypass_shield_output; /*!< Whether to bypass the shield output */ +#else +#error "Unsupported touch sensor version" +#endif } touch_hal_sample_config_t; /** @@ -47,7 +63,9 @@ typedef struct { * Set to '0' to ignore the measurement time limitation, otherwise please set a proper time considering the configurations * of the sample configurations below. */ +#if SOC_TOUCH_SENSOR_VERSION == 3 touch_out_mode_t output_mode; /*!< Touch channel counting mode of the binarized touch output */ +#endif // SOC_TOUCH_SENSOR_VERSION == 3 uint32_t sample_cfg_num; /*!< The sample configuration number that used for sampling */ touch_hal_sample_config_t *sample_cfg; /*!< The array of the sample configuration configurations, the length should be specified in `touch_hal_sample_config_t::sample_cfg_num` */ } touch_hal_config_t; @@ -74,6 +92,8 @@ void touch_hal_save_sleep_config(int deep_slp_chan, const touch_hal_config_t *de */ void touch_hal_prepare_deep_sleep(void); +#endif // SOC_TOUCH_SENSOR_SUPPORTED + #ifdef __cplusplus } #endif diff --git a/components/hal/include/hal/touch_sens_types.h b/components/hal/include/hal/touch_sens_types.h new file mode 100644 index 0000000000..d763a748a7 --- /dev/null +++ b/components/hal/include/hal/touch_sens_types.h @@ -0,0 +1,131 @@ +/* + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once + +#include "esp_bit_defs.h" +#include "soc/soc_caps.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief Touch sensor upper charging voltage limit + */ +typedef enum { + TOUCH_VOLT_LIM_H_0V9, /*!< Touch sensor upper voltage limit is 0.9V while charging a touch pad */ + TOUCH_VOLT_LIM_H_1V0, /*!< Touch sensor upper voltage limit is 1.0V while charging a touch pad */ + TOUCH_VOLT_LIM_H_1V1, /*!< Touch sensor upper voltage limit is 1.1V while charging a touch pad */ + TOUCH_VOLT_LIM_H_1V2, /*!< Touch sensor upper voltage limit is 1.2V while charging a touch pad */ + // No 1V3 + TOUCH_VOLT_LIM_H_1V4, /*!< Touch sensor upper voltage limit is 1.4V while charging a touch pad */ + TOUCH_VOLT_LIM_H_1V5, /*!< Touch sensor upper voltage limit is 1.5V while charging a touch pad */ + TOUCH_VOLT_LIM_H_1V6, /*!< Touch sensor upper voltage limit is 1.6V while charging a touch pad */ + TOUCH_VOLT_LIM_H_1V7, /*!< Touch sensor upper voltage limit is 1.7V while charging a touch pad */ + // No 1V8 + TOUCH_VOLT_LIM_H_1V9, /*!< Touch sensor upper voltage limit is 1.9V while charging a touch pad */ + TOUCH_VOLT_LIM_H_2V0, /*!< Touch sensor upper voltage limit is 2.0V while charging a touch pad */ + TOUCH_VOLT_LIM_H_2V1, /*!< Touch sensor upper voltage limit is 2.1V while charging a touch pad */ + TOUCH_VOLT_LIM_H_2V2, /*!< Touch sensor upper voltage limit is 2.2V while charging a touch pad */ + // No 2V3 + TOUCH_VOLT_LIM_H_2V4, /*!< Touch sensor upper voltage limit is 2.4V while charging a touch pad */ + TOUCH_VOLT_LIM_H_2V5, /*!< Touch sensor upper voltage limit is 2.5V while charging a touch pad */ + TOUCH_VOLT_LIM_H_2V6, /*!< Touch sensor upper voltage limit is 2.6V while charging a touch pad */ + TOUCH_VOLT_LIM_H_2V7, /*!< Touch sensor upper voltage limit is 2.7V while charging a touch pad */ +} touch_volt_lim_h_t; + +/** + * @brief Touch sensor lower discharging voltage limit + */ +typedef enum { + TOUCH_VOLT_LIM_L_0V5, /*!< Touch sensor lower voltage limit is 0.5V while charging a touch pad */ + TOUCH_VOLT_LIM_L_0V6, /*!< Touch sensor lower voltage limit is 0.5V while charging a touch pad */ + TOUCH_VOLT_LIM_L_0V7, /*!< Touch sensor lower voltage limit is 0.5V while charging a touch pad */ + TOUCH_VOLT_LIM_L_0V8, /*!< Touch sensor lower voltage limit is 0.5V while charging a touch pad */ +} touch_volt_lim_l_t; + +/** + * @brief Touch sensor charge and discharge speed + */ +typedef enum { + TOUCH_CHARGE_SPEED_0 = 0, /*!< Touch sensor charge and discharge speed, no charge, always zero */ + TOUCH_CHARGE_SPEED_1 = 1, /*!< Touch sensor charge and discharge speed, slowest */ + TOUCH_CHARGE_SPEED_2 = 2, /*!< Touch sensor charge and discharge speed */ + TOUCH_CHARGE_SPEED_3 = 3, /*!< Touch sensor charge and discharge speed */ + TOUCH_CHARGE_SPEED_4 = 4, /*!< Touch sensor charge and discharge speed */ + TOUCH_CHARGE_SPEED_5 = 5, /*!< Touch sensor charge and discharge speed */ + TOUCH_CHARGE_SPEED_6 = 6, /*!< Touch sensor charge and discharge speed */ + TOUCH_CHARGE_SPEED_7 = 7, /*!< Touch sensor charge and discharge speed, fastest */ +} touch_charge_speed_t; + +/** + * @brief Touch sensor initial voltage before charging + */ +typedef enum { + TOUCH_INIT_CHARGE_VOLT_LOW = 0, /*!< Tie the initial charge voltage to low */ + TOUCH_INIT_CHARGE_VOLT_HIGH = 1,/*!< Tie the initial charge voltage to high */ +} touch_init_charge_volt_t; + +/** Touch channel idle state configuration */ +typedef enum { + TOUCH_IDLE_CONN_HIGHZ = 0, /*!< The idle (enabled but not measuring) touch channel is in high resistance state */ + TOUCH_IDLE_CONN_GND = 1, /*!< The idle (enabled but not measuring) touch channel is connected to the ground */ +} touch_idle_conn_t; + +/** + * @brief Touch sensor denoise channel internal reference capacitance + * + */ +typedef enum { + TOUCH_DENOISE_CHAN_CAP_L0 = 0, /*!< Denoise channel internal reference capacitance is 5pf */ + TOUCH_DENOISE_CHAN_CAP_L1 = 1, /*!< Denoise channel internal reference capacitance is 6.4pf */ + TOUCH_DENOISE_CHAN_CAP_L2 = 2, /*!< Denoise channel internal reference capacitance is 7.8pf */ + TOUCH_DENOISE_CHAN_CAP_L3 = 3, /*!< Denoise channel internal reference capacitance is 9.2pf */ + TOUCH_DENOISE_CHAN_CAP_L4 = 4, /*!< Denoise channel internal reference capacitance is 10.6pf */ + TOUCH_DENOISE_CHAN_CAP_L5 = 5, /*!< Denoise channel internal reference capacitance is 12.0pf */ + TOUCH_DENOISE_CHAN_CAP_L6 = 6, /*!< Denoise channel internal reference capacitance is 13.4pf */ + TOUCH_DENOISE_CHAN_CAP_L7 = 7, /*!< Denoise channel internal reference capacitance is 14.8pf */ +} touch_denoise_chan_cap_t; + +/** + * @brief Touch sensor denoise channel noise suppression resolution + * + */ +typedef enum { + TOUCH_DENOISE_CHAN_RES_BIT12 = 0, /*!< Denoise channel noise suppression resolution is 12bit */ + TOUCH_DENOISE_CHAN_RES_BIT10 = 1, /*!< Denoise channel noise suppression resolution is 10bit */ + TOUCH_DENOISE_CHAN_RES_BIT8 = 2, /*!< Denoise channel noise suppression resolution is 8bit */ + TOUCH_DENOISE_CHAN_RES_BIT4 = 3, /*!< Denoise channel noise suppression resolution is 4bit */ +} touch_denoise_chan_res_t; + +/** + * @brief Touch sensor bias type + * + */ +typedef enum { + TOUCH_BIAS_TYPE_BANDGAP, /*!< Use bandgap-bias to charge/discharge the touch channel, which is more stable but power-consuming */ + TOUCH_BIAS_TYPE_SELF, /*!< Use self-bias to charge/discharge the touch channel, which is less stable but power-saving */ +} touch_bias_type_t; + +/** + * @brief Touch channel counting mode of the binarized touch output + * + */ +typedef enum { + TOUCH_PAD_OUT_AS_DATA, /*!< Counting the output of touch channel as data. + * The value will be smaller than actual value but more sensitive when the frequency of touch_out is close to the source clock + * Normally we treat the output as data when it is lower than the sample clock + */ + TOUCH_PAD_OUT_AS_CLOCK, /*!< Counting the output of touch channel as clock. + * The value is accurate but less sensitive when the frequency of touch_out is close to the source clock + * Normally we treat the output as clock when it is higher than the sample clock + */ +} touch_out_mode_t; + +#ifdef __cplusplus +} +#endif diff --git a/components/hal/include/hal/touch_sensor_hal.h b/components/hal/include/hal/touch_sensor_hal.h index be45ca89ab..a0352a864d 100644 --- a/components/hal/include/hal/touch_sensor_hal.h +++ b/components/hal/include/hal/touch_sensor_hal.h @@ -14,6 +14,7 @@ #pragma once +#include "soc/soc_caps.h" #if SOC_TOUCH_SENSOR_SUPPORTED #include "hal/touch_sensor_ll.h" #include "hal/touch_sensor_types.h" diff --git a/components/hal/include/hal/touch_sensor_types.h b/components/hal/include/hal/touch_sensor_types.h index dac11762f3..c5428372c9 100644 --- a/components/hal/include/hal/touch_sensor_types.h +++ b/components/hal/include/hal/touch_sensor_types.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -275,23 +275,6 @@ typedef enum { TOUCH_PAD_SMOOTH_MAX, } touch_smooth_mode_t; -#if SOC_TOUCH_SENSOR_VERSION == 3 -/** - * @brief Touch channel counting mode of the binarized touch output - * - */ -typedef enum { - TOUCH_PAD_OUT_AS_DATA, /*!< Counting the output of touch channel as data. - * The value will be smaller than actual value but more sensitive when the frequency of touch_out is close to the source clock - * Normally we treat the output as data when it is lower than the sample clock - */ - TOUCH_PAD_OUT_AS_CLOCK, /*!< Counting the output of touch channel as clock. - * The value is accurate but less sensitive when the frequency of touch_out is close to the source clock - * Normally we treat the output as clock when it is higher than the sample clock - */ -} touch_out_mode_t; -#endif - /** Touch sensor filter configuration */ typedef struct touch_filter_config { touch_filter_mode_t mode; /*! -#include "soc/soc_pins.h" -#include "hal/touch_sensor_ll.h" -#include "hal/touch_sensor_hal.h" -#include "hal/touch_sensor_types.h" #include "soc/soc_caps.h" +#include "hal/touch_sensor_ll.h" +#include "hal/touch_sens_hal.h" +#include "hal/touch_sens_types.h" + typedef struct { int deep_slp_chan; @@ -26,16 +26,19 @@ void touch_hal_config_controller(const touch_hal_config_t *cfg) { HAL_ASSERT(cfg); touch_ll_sleep_set_channel_num(TOUCH_LL_NULL_CHANNEL); - touch_ll_set_out_mode(cfg->output_mode); touch_ll_set_power_on_wait_cycle(cfg->power_on_wait_ticks); touch_ll_set_measure_interval_ticks(cfg->meas_interval_ticks); - if (cfg->timeout_ticks) { - touch_ll_timeout_enable(cfg->timeout_ticks); - } else { - touch_ll_timeout_disable(); - } + touch_ll_set_timeout(cfg->timeout_ticks); +#if SOC_TOUCH_SENSOR_VERSION == 2 + touch_ll_set_charge_times(cfg->sample_cfg->charge_times); + touch_ll_set_charge_voltage_high_limit(cfg->sample_cfg->charge_volt_lim_h); + touch_ll_set_charge_voltage_low_limit(cfg->sample_cfg->charge_volt_lim_l); + touch_ll_set_idle_channel_connection(cfg->sample_cfg->idle_conn); + touch_ll_set_bias_type(cfg->sample_cfg->bias_type); +#elif SOC_TOUCH_SENSOR_VERSION == 3 touch_ll_sample_cfg_set_engaged_num(cfg->sample_cfg_num); + touch_ll_set_out_mode(cfg->output_mode); for (int i = 0; i < cfg->sample_cfg_num; i++) { touch_ll_set_clock_div(i, cfg->sample_cfg[i].div_num); touch_ll_set_charge_times(i, cfg->sample_cfg[i].charge_times); @@ -44,6 +47,9 @@ void touch_hal_config_controller(const touch_hal_config_t *cfg) touch_ll_sample_cfg_bypass_shield_output(i, cfg->sample_cfg[i].bypass_shield_output); touch_ll_sample_cfg_set_bias_voltage(i, cfg->sample_cfg[i].bias_volt); } +#else + HAL_ASSERT(0); // Unsupported touch sensor version +#endif } void touch_hal_save_sleep_config(int deep_slp_chan, const touch_hal_config_t *deep_slp_cfg) @@ -67,8 +73,8 @@ static void s_touch_hal_apply_sleep_config(void) } /* Whether to enable touch sensor wake-up the chip from deep sleep */ if (s_touch_slp_obj.deep_slp_chan >= 0) { - touch_ll_sleep_set_channel_num(s_touch_slp_obj.deep_slp_chan); - touch_ll_set_channel_mask(BIT(s_touch_slp_obj.deep_slp_chan)); + // touch_ll_sleep_set_channel_num(s_touch_slp_obj.deep_slp_chan); + // touch_ll_enable_channel_mask(BIT(s_touch_slp_obj.deep_slp_chan)); } else { touch_ll_sleep_set_channel_num(TOUCH_LL_NULL_CHANNEL); } @@ -78,5 +84,5 @@ void touch_hal_prepare_deep_sleep(void) { s_touch_hal_apply_sleep_config(); touch_ll_sleep_reset_benchmark(); - touch_ll_intr_clear(TOUCH_LL_INTR_MASK_ALL); + touch_ll_interrupt_clear(TOUCH_LL_INTR_MASK_ALL); } diff --git a/components/soc/esp32s2/include/soc/Kconfig.soc_caps.in b/components/soc/esp32s2/include/soc/Kconfig.soc_caps.in index 686747c9f8..506be9789f 100644 --- a/components/soc/esp32s2/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32s2/include/soc/Kconfig.soc_caps.in @@ -827,6 +827,10 @@ config SOC_TOUCH_SUPPORT_PROX_SENSING bool default y +config SOC_TOUCH_SUPPORT_DENOISE_CHAN + bool + default y + config SOC_TOUCH_PROXIMITY_CHANNEL_NUM int default 3 diff --git a/components/soc/esp32s2/include/soc/soc_caps.h b/components/soc/esp32s2/include/soc/soc_caps.h index 3e50662f73..bc81df1436 100644 --- a/components/soc/esp32s2/include/soc/soc_caps.h +++ b/components/soc/esp32s2/include/soc/soc_caps.h @@ -345,6 +345,7 @@ #define SOC_TOUCH_SUPPORT_SLEEP_WAKEUP (1) /*!< Touch sensor supports sleep awake */ #define SOC_TOUCH_SUPPORT_WATERPROOF (1) /*!< Touch sensor supports waterproof */ #define SOC_TOUCH_SUPPORT_PROX_SENSING (1) /*!< Touch sensor supports proximity sensing */ +#define SOC_TOUCH_SUPPORT_DENOISE_CHAN (1) /*!< Touch sensor supports denoise channel */ #define SOC_TOUCH_PROXIMITY_CHANNEL_NUM (3) /*!< Support touch proximity channel number. */ #define SOC_TOUCH_SAMPLE_CFG_NUM (1U) /*!< The sample configuration number in total, each sampler can be used to sample on one frequency */ diff --git a/components/soc/esp32s3/include/soc/Kconfig.soc_caps.in b/components/soc/esp32s3/include/soc/Kconfig.soc_caps.in index 0c228dc841..f87cc98a22 100644 --- a/components/soc/esp32s3/include/soc/Kconfig.soc_caps.in +++ b/components/soc/esp32s3/include/soc/Kconfig.soc_caps.in @@ -1003,6 +1003,10 @@ config SOC_TOUCH_SUPPORT_PROX_SENSING bool default y +config SOC_TOUCH_SUPPORT_DENOISE_CHAN + bool + default y + config SOC_TOUCH_PROXIMITY_CHANNEL_NUM int default 3 diff --git a/components/soc/esp32s3/include/soc/soc_caps.h b/components/soc/esp32s3/include/soc/soc_caps.h index 6e8194fbf9..d87dbe088c 100644 --- a/components/soc/esp32s3/include/soc/soc_caps.h +++ b/components/soc/esp32s3/include/soc/soc_caps.h @@ -385,6 +385,7 @@ #define SOC_TOUCH_SUPPORT_SLEEP_WAKEUP (1) /*!< Touch sensor supports sleep awake */ #define SOC_TOUCH_SUPPORT_WATERPROOF (1) /*!< Touch sensor supports waterproof */ #define SOC_TOUCH_SUPPORT_PROX_SENSING (1) /*!< Touch sensor supports proximity sensing */ +#define SOC_TOUCH_SUPPORT_DENOISE_CHAN (1) /*!< Touch sensor supports denoise channel */ #define SOC_TOUCH_PROXIMITY_CHANNEL_NUM (3) /*!< Support touch proximity sensing channel number. */ #define SOC_TOUCH_PROXIMITY_MEAS_DONE_SUPPORTED (1) /*!< Support touch proximity sensing measure done interrupt type. */ diff --git a/components/soc/esp32s3/register/soc/rtc_cntl_struct.h b/components/soc/esp32s3/register/soc/rtc_cntl_struct.h index 7ef1825ebd..a92633ec86 100644 --- a/components/soc/esp32s3/register/soc/rtc_cntl_struct.h +++ b/components/soc/esp32s3/register/soc/rtc_cntl_struct.h @@ -798,14 +798,14 @@ typedef volatile struct rtc_cntl_dev_s { union { struct { uint32_t reserved0 : 7; - uint32_t touch_bypass_neg_noise_thres : 1; + uint32_t touch_bypass_nn_thres : 1; uint32_t touch_bypass_noise_thres : 1; uint32_t touch_smooth_lvl : 2; uint32_t touch_jitter_step : 4; /*touch jitter step*/ - uint32_t config1: 4; - uint32_t config2: 2; + uint32_t config1 : 4; + uint32_t config2 : 2; uint32_t touch_noise_thres : 2; - uint32_t config3: 2; + uint32_t config3 : 2; uint32_t touch_debounce : 3; /*debounce counter*/ uint32_t touch_filter_mode : 3; /*0: IIR ? 1: IIR ? 2: IIR 1/8 3: Jitter*/ uint32_t touch_filter_en : 1; /*touch filter enable*/