From 985e94139da179ac520ec6f3eb10cd6bf3be6ccc Mon Sep 17 00:00:00 2001 From: fuzhibo Date: Mon, 26 Oct 2020 16:10:37 +0800 Subject: [PATCH] driver(touch): fix touch sensor false trigger in sleep mode --- components/esp_system/sleep_modes.c | 41 ++++++++++++------- .../hal/esp32s2/include/hal/touch_sensor_ll.h | 19 +++++---- .../hal/esp32s3/include/hal/touch_sensor_ll.h | 19 +++++---- 3 files changed, 50 insertions(+), 29 deletions(-) diff --git a/components/esp_system/sleep_modes.c b/components/esp_system/sleep_modes.c index a708667f23..20b9097f14 100644 --- a/components/esp_system/sleep_modes.c +++ b/components/esp_system/sleep_modes.c @@ -29,7 +29,6 @@ #include "driver/touch_sensor.h" #include "driver/touch_sensor_common.h" #include "soc/soc_caps.h" - #include "driver/rtc_io.h" #include "hal/rtc_io_hal.h" @@ -166,9 +165,7 @@ static uint32_t get_power_down_flags(void); static void ext0_wakeup_prepare(void); static void ext1_wakeup_prepare(void); static void timer_wakeup_prepare(void); - - -#ifdef CONFIG_IDF_TARGET_ESP32S2 +#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 static void touch_wakeup_prepare(void); #endif @@ -313,10 +310,22 @@ static uint32_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags) } #endif -#ifdef CONFIG_IDF_TARGET_ESP32S2 - // Enable Touch wakeup - if (s_config.wakeup_triggers & RTC_TOUCH_TRIG_EN) { - touch_wakeup_prepare(); +#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + if (deep_sleep) { + if (s_config.wakeup_triggers & RTC_TOUCH_TRIG_EN) { + touch_wakeup_prepare(); + /* Workaround: In deep sleep, for ESP32S2, Power down the RTC_PERIPH will change the slope configuration of Touch sensor sleep pad. + * The configuration change will change the reading of the sleep pad, which will cause the touch wake-up sensor to trigger falsely. + */ + pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH; + } + } else { + /* In light sleep, the RTC_PERIPH power domain should be in the power-on state (Power on the touch circuit in light sleep), + * otherwise the touch sensor FSM will be cleared, causing touch sensor false triggering. + */ + if (touch_ll_get_fsm_state()) { // Check if the touch sensor is working properly. + pd_flags &= ~RTC_SLEEP_PD_RTC_PERIPH; + } } #endif uint32_t reject_triggers = 0; @@ -603,16 +612,18 @@ static void timer_wakeup_prepare(void) } -#ifdef CONFIG_IDF_TARGET_ESP32S2 +#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 /* In deep sleep mode, only the sleep channel is supported, and other touch channels should be turned off. */ static void touch_wakeup_prepare(void) { - touch_pad_sleep_channel_t slp_config; - touch_pad_fsm_stop(); - touch_pad_clear_channel_mask(TOUCH_PAD_BIT_MASK_ALL); - touch_pad_sleep_channel_get_info(&slp_config); - touch_pad_set_channel_mask(BIT(slp_config.touch_num)); - touch_pad_fsm_start(); + touch_pad_t touch_num = TOUCH_PAD_NUM0; + touch_ll_sleep_get_channel_num(&touch_num); // Check if the sleep pad is enabled. + if ((touch_num > TOUCH_PAD_NUM0) && (touch_num < TOUCH_PAD_MAX) && touch_ll_get_fsm_state()) { + touch_ll_stop_fsm(); + touch_ll_clear_channel_mask(TOUCH_PAD_BIT_MASK_ALL); + touch_ll_set_channel_mask(BIT(touch_num)); + touch_ll_start_fsm(); + } } #endif diff --git a/components/hal/esp32s2/include/hal/touch_sensor_ll.h b/components/hal/esp32s2/include/hal/touch_sensor_ll.h index d0c990f6f7..f4c915b435 100644 --- a/components/hal/esp32s2/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32s2/include/hal/touch_sensor_ll.h @@ -289,6 +289,17 @@ static inline void touch_ll_stop_fsm(void) RTCCNTL.touch_ctrl2.touch_timer_force_done = TOUCH_LL_TIMER_DONE; } +/** + * Get touch sensor FSM timer state. + * @return + * - true: FSM enabled + * - false: FSM disabled + */ +static inline bool touch_ll_get_fsm_state(void) +{ + return (bool)RTCCNTL.touch_ctrl2.touch_slp_timer_en; +} + /** * Trigger a touch sensor measurement, only support in SW mode of FSM. */ @@ -298,18 +309,12 @@ static inline void touch_ll_start_sw_meas(void) RTCCNTL.touch_ctrl2.touch_start_en = 1; } -/** - * Set touch sensor interrupt threshold. - * - * @param touch_num touch pad index. - * @param threshold threshold of touchpad count. - */ /** * Set the trigger threshold of touch sensor. * The threshold determines the sensitivity of the touch sensor. * The threshold is the original value of the trigger state minus the benchmark value. * - * @note If set "TOUCH_PAD_THRESHOLD_MAX", the touch is never be trigered. + * @note If set "TOUCH_PAD_THRESHOLD_MAX", the touch is never be triggered. * @param touch_num touch pad index * @param threshold threshold of touch sensor. */ diff --git a/components/hal/esp32s3/include/hal/touch_sensor_ll.h b/components/hal/esp32s3/include/hal/touch_sensor_ll.h index d16b09a1f6..5bc5ebb56d 100644 --- a/components/hal/esp32s3/include/hal/touch_sensor_ll.h +++ b/components/hal/esp32s3/include/hal/touch_sensor_ll.h @@ -289,6 +289,17 @@ static inline void touch_ll_stop_fsm(void) RTCCNTL.touch_ctrl2.touch_timer_force_done = TOUCH_LL_TIMER_DONE; } +/** + * Get touch sensor FSM timer state. + * @return + * - true: FSM enabled + * - false: FSM disabled + */ +static inline bool touch_ll_get_fsm_state(void) +{ + return (bool)RTCCNTL.touch_ctrl2.touch_slp_timer_en; +} + /** * Trigger a touch sensor measurement, only support in SW mode of FSM. */ @@ -298,18 +309,12 @@ static inline void touch_ll_start_sw_meas(void) RTCCNTL.touch_ctrl2.touch_start_en = 1; } -/** - * Set touch sensor interrupt threshold. - * - * @param touch_num touch pad index. - * @param threshold threshold of touchpad count. - */ /** * Set the trigger threshold of touch sensor. * The threshold determines the sensitivity of the touch sensor. * The threshold is the original value of the trigger state minus the benchmark value. * - * @note If set "TOUCH_PAD_THRESHOLD_MAX", the touch is never be trigered. + * @note If set "TOUCH_PAD_THRESHOLD_MAX", the touch is never be triggered. * @param touch_num touch pad index * @param threshold threshold of touch sensor. */