From 82bf6c09350d482b65b07b11a2e844720107d84f Mon Sep 17 00:00:00 2001 From: Kang Zuoling Date: Wed, 31 Mar 2021 19:57:54 +0800 Subject: [PATCH 1/3] add skip calibration and wakeup channel, fix isr in sleep mode --- components/touch_element/Kconfig | 10 + .../include/touch_element/touch_element.h | 15 + .../touch_element/touch_element_private.h | 28 ++ components/touch_element/touch_button.c | 37 +++ components/touch_element/touch_element.c | 273 +++++++++++++++++- components/touch_element/touch_matrix.c | 42 +++ components/touch_element/touch_slider.c | 42 +++ .../touch_element_sleep/CMakeLists.txt | 8 + .../touch_element_sleep/main/CMakeLists.txt | 2 + .../main/Kconfig.projbuild | 15 + .../main/touch_element_sleep.c | 143 +++++++++ .../main/tp_interrupt_main.c | 9 + 12 files changed, 612 insertions(+), 12 deletions(-) create mode 100644 components/touch_element/Kconfig create mode 100644 examples/peripherals/touch_element/touch_element_sleep/CMakeLists.txt create mode 100644 examples/peripherals/touch_element/touch_element_sleep/main/CMakeLists.txt create mode 100644 examples/peripherals/touch_element/touch_element_sleep/main/Kconfig.projbuild create mode 100644 examples/peripherals/touch_element/touch_element_sleep/main/touch_element_sleep.c diff --git a/components/touch_element/Kconfig b/components/touch_element/Kconfig new file mode 100644 index 0000000000..3aafd12e64 --- /dev/null +++ b/components/touch_element/Kconfig @@ -0,0 +1,10 @@ +menu "Touch Element" + + config TE_SKIP_DSLEEP_WAKEUP_CALIBRATION + bool "Enable skip deep sleep wakeup calibration" + default n + help + This option allows to store all Touch Sensor channels' threshold into RTC Fast Memory. So that Touch Sensor + threshold will only be configured once after Power-on Reset. + +endmenu \ No newline at end of file diff --git a/components/touch_element/include/touch_element/touch_element.h b/components/touch_element/include/touch_element/touch_element.h index 429fc2503f..c172d853b7 100644 --- a/components/touch_element/include/touch_element/touch_element.h +++ b/components/touch_element/include/touch_element/touch_element.h @@ -256,6 +256,21 @@ esp_err_t touch_element_waterproof_add(touch_elem_handle_t element_handle); */ esp_err_t touch_element_waterproof_remove(touch_elem_handle_t element_handle); +typedef struct { + uint16_t scan_time; + uint16_t sleep_time; +} touch_elem_sleep_config_t; + +esp_err_t touch_element_sleep_install(touch_elem_sleep_config_t *sleep_config); +void touch_element_sleep_uninstall(void); +esp_err_t touch_element_sleep_add_wakeup(touch_elem_handle_t element_handle); +esp_err_t touch_element_sleep_remove_wakeup(touch_elem_handle_t element_handle); +esp_err_t touch_element_sleep_add_wakeup_channel(touch_pad_t wakeup_channel); +esp_err_t touch_element_sleep_remove_wakeup_channel(touch_pad_t wakeup_channel); +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION +esp_err_t touch_element_sleep_config_wakeup_calibration(touch_elem_handle_t element_handle, bool en); +#endif + #ifdef __cplusplus } #endif diff --git a/components/touch_element/include/touch_element/touch_element_private.h b/components/touch_element/include/touch_element/touch_element_private.h index 49971ae2d4..fb287d33a1 100644 --- a/components/touch_element/include/touch_element/touch_element_private.h +++ b/components/touch_element/include/touch_element/touch_element_private.h @@ -10,6 +10,8 @@ #include "touch_element/touch_button.h" #include "touch_element/touch_slider.h" #include "touch_element/touch_matrix.h" +#include "esp_pm.h" +#include "sdkconfig.h" #ifdef __cplusplus extern "C" { @@ -54,6 +56,9 @@ typedef struct { touch_pad_t channel; //!< Touch channel number(index) te_dev_type_t type; //!< Touch channel type TODO: need to refactor as te_class_type_t te_dev_state_t state; //!< Touch channel current state +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION + bool is_use_last_threshold; +#endif } te_dev_t; typedef enum { @@ -69,6 +74,7 @@ typedef struct { esp_err_t (*set_threshold) (void); void (*process_state) (void); void (*update_state) (touch_pad_t, te_state_t); + touch_elem_handle_t (*search_channel_handle) (touch_pad_t); } te_object_methods_t; /* -------------------------------------------- Waterproof basic type --------------------------------------------- */ @@ -79,6 +85,16 @@ struct te_waterproof_s { bool is_shield_level_set; //Waterproof shield level setting bit }; typedef struct te_waterproof_s* te_waterproof_handle_t; +/* -------------------------------------------- Sleep basic type --------------------------------------------- */ +struct te_sleep_s { + touch_elem_handle_t wakeup_handle; + esp_pm_lock_handle_t pm_lock; +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION + uint32_t *non_volatile_threshold; +#endif +}; + +typedef struct te_sleep_s* te_sleep_handle_t; /* -------------------------------------------- Button basic type --------------------------------------------- */ typedef struct { touch_elem_dispatch_t dispatch_method; //Button dispatch method @@ -170,6 +186,18 @@ void te_object_method_register(te_object_methods_t *object_methods, te_class_typ void te_object_method_unregister(te_class_type_t object_type); bool te_object_check_channel(const touch_pad_t *channel_array, uint8_t channel_sum); bool waterproof_check_mask_handle(touch_elem_handle_t te_handle); +bool te_is_touch_dsleep_wakeup(void); +inline touch_pad_t te_get_sleep_channel(void); + +bool button_object_handle_check(touch_elem_handle_t element_handle); +bool slider_object_handle_check(touch_elem_handle_t element_handle); +bool matrix_object_handle_check(touch_elem_handle_t element_handle); + +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION +void button_config_wakeup_calibration(te_button_handle_t button_handle, bool en); +void slider_config_wakeup_calibration(te_slider_handle_t slider_handle, bool en); +void matrix_config_wakeup_calibration(te_matrix_handle_t matrix_handle, bool en); +#endif /* ------------------------------------------------------------------------------------------------------------------ */ #ifdef __cplusplus diff --git a/components/touch_element/touch_button.c b/components/touch_element/touch_button.c index 7a93d03f93..cf775435bd 100644 --- a/components/touch_element/touch_button.c +++ b/components/touch_element/touch_button.c @@ -40,6 +40,7 @@ static bool button_object_check_channel(touch_pad_t channel_num); static esp_err_t button_object_set_threshold(void); static void button_object_process_state(void); static void button_object_update_state(touch_pad_t channel_num, te_state_t channel_state); +static te_button_handle_t button_object_search_channel_handle(touch_pad_t channel_num); /* ------------------------------------------------------------------------------------------------------------------ */ esp_err_t touch_button_install(const touch_button_global_config_t *global_config) @@ -268,6 +269,21 @@ static void button_object_update_state(touch_pad_t channel_num, te_state_t chann } } +static te_button_handle_t button_object_search_channel_handle(touch_pad_t channel_num) +{ + te_button_handle_list_t *item; + te_button_handle_t button_handle = NULL; + SLIST_FOREACH(item, &s_te_btn_obj->handle_list, next) { + touch_pad_t button_channel = item->button_handle->device->channel; + if (channel_num == button_channel) { + button_handle = item->button_handle; + break; + } + } + + return button_handle; +} + static esp_err_t button_object_add_instance(te_button_handle_t button_handle) { te_button_handle_list_t *item = (te_button_handle_list_t *)calloc(1, sizeof(te_button_handle_list_t)); @@ -296,6 +312,20 @@ static esp_err_t button_object_remove_instance(te_button_handle_t button_handle) return ret; } +bool button_object_handle_check(touch_elem_handle_t element_handle) +{ + te_button_handle_list_t *item; + xSemaphoreTake(s_te_btn_obj->mutex, portMAX_DELAY); + SLIST_FOREACH(item, &s_te_btn_obj->handle_list, next) { + if (element_handle == item->button_handle) { + xSemaphoreGive(s_te_btn_obj->mutex); + return true; + } + } + xSemaphoreGive(s_te_btn_obj->mutex); + return false; +} + static bool button_channel_check(te_button_handle_t button_handle, touch_pad_t channel_num) { return (channel_num == button_handle->device->channel); @@ -346,6 +376,13 @@ static inline void button_dispatch(te_button_handle_t button_handle, touch_elem_ } } +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION +void button_config_wakeup_calibration(te_button_handle_t button_handle, bool en) +{ + button_handle->device->is_use_last_threshold = en; +} +#endif + /** * @brief Button process * diff --git a/components/touch_element/touch_element.c b/components/touch_element/touch_element.c index ec465c95b3..90c888ba1a 100644 --- a/components/touch_element/touch_element.c +++ b/components/touch_element/touch_element.c @@ -9,11 +9,15 @@ #include "freertos/FreeRTOS.h" #include "freertos/semphr.h" #include "freertos/queue.h" +#include "esp_sleep.h" #include "esp_timer.h" #include "esp_log.h" #include "hal/touch_sensor_hal.h" //TODO: remove hal #include "touch_element/touch_element_private.h" +#include "esp32s2/rom/rtc.h" + + #define TE_CLASS_ITEM(cls, cls_type, cls_item) ((&((cls)[cls_type]))->cls_item) #define TE_CLASS_FOREACH(cls_var, cls_start, cls_end) \ @@ -87,15 +91,21 @@ typedef struct { te_object_methods_t object_methods[TE_CLS_TYPE_MAX]; //Class(object) methods touch_elem_global_config_t *global_config; //Global initialization te_waterproof_handle_t waterproof_handle; //Waterproof configuration + te_sleep_handle_t sleep_handle; esp_timer_handle_t proc_timer; //Processing timer handle QueueHandle_t event_msg_queue; //Application event message queue (for user) QueueHandle_t intr_msg_queue; //Interrupt message (for internal) SemaphoreHandle_t mutex; //Global resource mutex bool is_set_threshold; //Threshold configuration state bit uint32_t denoise_channel_raw; //De-noise channel(TO) raw signal +// touch_elem_sleep_config_t *sleep_config; +// esp_pm_lock_handle_t pm_lock_handle; } te_obj_t; static te_obj_t *s_te_obj = NULL; +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION +RTC_FAST_ATTR uint32_t threshold_shadow[TOUCH_PAD_MAX - 1] = {0}; +#endif /** * Internal de-noise channel(Touch channel 0) equivalent capacitance table, depends on hardware design @@ -313,6 +323,36 @@ esp_err_t te_event_give(touch_elem_message_t te_message) return ESP_OK; } +uint32_t te_get_threshold(touch_pad_t channel_num) +{ + uint32_t threshold = 0; + touch_pad_sleep_channel_t sleep_channel_info; + touch_pad_sleep_channel_get_info(&sleep_channel_info); + if (channel_num != sleep_channel_info.touch_num) { + touch_pad_get_thresh(channel_num, &threshold); + } else { + touch_pad_sleep_get_threshold(channel_num, &threshold); + } + return threshold; +} + +bool te_is_touch_dsleep_wakeup(void) +{ + RESET_REASON rtc_reset_reason = rtc_get_reset_reason(0); + if (rtc_reset_reason != DEEPSLEEP_RESET) { + return false; + } + esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause(); + return wakeup_reason == ESP_SLEEP_WAKEUP_TOUCHPAD; +} + +inline touch_pad_t te_get_sleep_channel(void) +{ + touch_pad_sleep_channel_t sleep_channel_info; + touch_pad_sleep_channel_get_info(&sleep_channel_info); + return sleep_channel_info.touch_num; +} + /** * @brief Touch sensor interrupt service routine * @@ -323,22 +363,39 @@ static void te_intr_cb(void *arg) { TE_UNUSED(arg); static int scan_done_cnt = 0; + static uint32_t touch_pre_trig_status = 0; int task_awoken = pdFALSE; te_intr_msg_t te_intr_msg; /*< Figure out which touch sensor channel is triggered and the trigger type */ uint32_t intr_mask = touch_pad_read_intr_status_mask(); - te_intr_msg.channel_num = touch_pad_get_current_meas_channel(); if (intr_mask == 0x0) { //For dummy interrupt return; } + bool need_send_queue = true; - if (intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) { - te_intr_msg.channel_state = TE_STATE_PRESS; - te_intr_msg.intr_type = TE_INTR_PRESS; - } else if (intr_mask & TOUCH_PAD_INTR_MASK_INACTIVE) { - te_intr_msg.channel_state = TE_STATE_RELEASE; - te_intr_msg.intr_type = TE_INTR_RELEASE; - } else if (intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) { + uint8_t pad_num = 0; + uint32_t touch_trig_status = touch_pad_get_status(); + uint32_t touch_trig_diff = touch_trig_status ^ touch_pre_trig_status; + while (touch_trig_diff) { + if (touch_trig_diff & 0x1) { + if (touch_trig_status & BIT(pad_num)) { + if (s_te_obj->sleep_handle != NULL) { + esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock); + } + te_intr_msg.channel_state = TE_STATE_PRESS; + te_intr_msg.intr_type = TE_INTR_PRESS; + } else { + te_intr_msg.channel_state = TE_STATE_RELEASE; + te_intr_msg.intr_type = TE_INTR_RELEASE; + } + touch_pre_trig_status = touch_trig_status; + te_intr_msg.channel_num = pad_num; + } + pad_num++; + touch_trig_diff >>= 1; + } + + if (intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) { te_intr_msg.channel_state = TE_STATE_IDLE; te_intr_msg.intr_type = TE_INTR_TIMEOUT; } else if (intr_mask & TOUCH_PAD_INTR_MASK_SCAN_DONE) { @@ -355,7 +412,7 @@ static void te_intr_cb(void *arg) /*< De-noise channel signal must be read at the time between SCAN_DONE and next measurement beginning(sleep)!!! */ touch_pad_denoise_read_data(&s_te_obj->denoise_channel_raw); //Update de-noise signal } else { - te_intr_msg.intr_type = TE_INTR_MAX; // Unknown Exception +// te_intr_msg.intr_type = TE_INTR_MAX; // Unknown Exception } if (need_send_queue) { xQueueSendFromISR(s_te_obj->intr_msg_queue, &te_intr_msg, &task_awoken); @@ -385,11 +442,19 @@ static void te_proc_timer_cb(void *arg) if (ret == pdPASS) { if (te_intr_msg.intr_type == TE_INTR_PRESS || te_intr_msg.intr_type == TE_INTR_RELEASE) { te_object_update_state(te_intr_msg); + if (te_intr_msg.intr_type == TE_INTR_RELEASE) { + if (s_te_obj->sleep_handle != NULL) { + esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock); + } + } } else if (te_intr_msg.intr_type == TE_INTR_SCAN_DONE) { if (s_te_obj->is_set_threshold != true) { s_te_obj->is_set_threshold = true; te_object_set_threshold(); //TODO: add set threshold error processing ESP_LOGD(TE_DEBUG_TAG, "Set threshold"); + if (s_te_obj->sleep_handle != NULL) { + esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock); + } } if (waterproof_check_state()) { te_waterproof_handle_t waterproof_handle = s_te_obj->waterproof_handle; @@ -500,6 +565,9 @@ esp_err_t te_dev_init(te_dev_t **device, uint8_t device_num, te_dev_type_t type, device[idx]->sens = sens[idx] * divider; device[idx]->type = type; device[idx]->state = TE_STATE_IDLE; +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION + device[idx]->is_use_last_threshold = false; +#endif esp_err_t ret = touch_pad_config(device[idx]->channel); TE_CHECK(ret == ESP_OK, ret); } @@ -513,10 +581,48 @@ void te_dev_deinit(te_dev_t **device, uint8_t device_num) } } +esp_err_t te_config_thresh(touch_pad_t channel_num, uint32_t threshold) +{ + esp_err_t ret; + touch_pad_sleep_channel_t sleep_channel_info; + touch_pad_sleep_channel_get_info(&sleep_channel_info); + if (channel_num != sleep_channel_info.touch_num) { + ret = touch_pad_set_thresh(channel_num, threshold); + } else { + ret = touch_pad_sleep_set_threshold(channel_num, threshold); + } + return ret; +} + esp_err_t te_dev_set_threshold(te_dev_t *device) { - uint32_t smo_val = te_read_smooth_signal(device->channel); - esp_err_t ret = touch_pad_set_thresh(device->channel, device->sens * smo_val); + esp_err_t ret = ESP_OK; + uint32_t smo_val = 0; +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION + if (s_te_obj->sleep_handle == NULL) { + ESP_LOGE(TE_TAG, "Touch Element sleep is not installed"); + return ESP_ERR_INVALID_STATE; + } + if (device->is_use_last_threshold) { + if (te_is_touch_dsleep_wakeup()) { //Deep sleep wakeup reset + touch_pad_t sleep_channel = te_get_sleep_channel(); + ets_printf("----config rtc %ld %ld\n", s_te_obj->sleep_handle->non_volatile_threshold[device->channel - 1], sleep_channel); + ret = te_config_thresh(device->channel, s_te_obj->sleep_handle->non_volatile_threshold[device->channel - 1]); + } else { //Other reset + smo_val = te_read_smooth_signal(device->channel); + ret = te_config_thresh(device->channel, device->sens * smo_val); + uint32_t threshold = te_get_threshold(device->channel); + s_te_obj->sleep_handle->non_volatile_threshold[device->channel - 1] = threshold; //Write threshold into RTC Fast Memory + } + } else { + smo_val = te_read_smooth_signal(device->channel); + ret = te_config_thresh(device->channel, device->sens * smo_val); + } + +#else + smo_val = te_read_smooth_signal(device->channel); + ret = te_config_thresh(device->channel, device->sens * smo_val); +#endif ESP_LOGD(TE_DEBUG_TAG, "channel: %"PRIu8", smo_val: %"PRIu32, device->channel, smo_val); return ret; } @@ -648,7 +754,8 @@ static esp_err_t te_sw_init(const touch_elem_sw_config_t *software_init) const esp_timer_create_args_t te_proc_timer_args = { .name = "te_proc_timer_cb", .arg = NULL, - .callback = &te_proc_timer_cb + .callback = &te_proc_timer_cb, + .skip_unhandled_events = true, }; ret = esp_timer_create(&te_proc_timer_args, &s_te_obj->proc_timer); TE_CHECK_GOTO(ret == ESP_OK, cleanup); @@ -879,3 +986,145 @@ static void waterproof_guard_update_state(touch_pad_t current_channel, te_state_ } ESP_LOGD(TE_DEBUG_TAG, "waterproof guard state update %d", guard_device->state); } + +esp_err_t touch_element_sleep_install(touch_elem_sleep_config_t *sleep_config) +{ + TE_CHECK(s_te_obj != NULL, ESP_ERR_INVALID_STATE); + TE_CHECK(s_te_obj->sleep_handle == NULL, ESP_ERR_INVALID_STATE); + TE_CHECK(sleep_config != NULL, ESP_ERR_INVALID_ARG); + + s_te_obj->sleep_handle = calloc(1, sizeof(struct te_sleep_s)); + if (s_te_obj->sleep_handle == NULL) { + return ESP_ERR_NO_MEM; + } + + esp_err_t ret; + touch_pad_sleep_channel_set_work_time(sleep_config->sleep_time, sleep_config->scan_time); + ret = esp_sleep_enable_touchpad_wakeup(); + TE_CHECK_GOTO(ret == ESP_OK, cleanup); + +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION + ret = esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_ON); + TE_CHECK_GOTO(ret == ESP_OK, cleanup); + s_te_obj->sleep_handle->non_volatile_threshold = threshold_shadow; +#endif + + ret = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "touch_element", &s_te_obj->sleep_handle->pm_lock); + TE_CHECK_GOTO(ret == ESP_OK, cleanup); + ret = esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock); + TE_CHECK_GOTO(ret == ESP_OK, cleanup); + return ESP_OK; + +cleanup: + if (s_te_obj->sleep_handle->pm_lock != NULL) { + esp_err_t del_ret = esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock); + if (del_ret != ESP_OK) { + abort(); + } + } + TE_FREE_AND_NULL(s_te_obj->sleep_handle); + return ret; +} + +void touch_element_sleep_uninstall(void) +{ + esp_err_t ret; + if (s_te_obj->sleep_handle->pm_lock != NULL) { + ret = esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock); + if (ret != ESP_OK) { + abort(); + } + } + if (s_te_obj->sleep_handle->wakeup_handle != NULL) { + te_button_handle_t button_handle = s_te_obj->sleep_handle->wakeup_handle; + ret = touch_pad_sleep_channel_enable(button_handle->device->channel, false); + if (ret != ESP_OK) { + abort(); + } + } + s_te_obj->sleep_handle->pm_lock = NULL; + s_te_obj->sleep_handle->wakeup_handle = NULL; + TE_FREE_AND_NULL(s_te_obj->sleep_handle); +} + +esp_err_t touch_element_sleep_add_wakeup(touch_elem_handle_t element_handle) +{ + TE_CHECK(s_te_obj->sleep_handle != NULL, ESP_ERR_INVALID_STATE); + TE_CHECK(element_handle != NULL, ESP_ERR_INVALID_ARG); + if (s_te_obj->sleep_handle->wakeup_handle != NULL) { + ESP_LOGE(TE_TAG, "sleep not null"); + return ESP_ERR_NOT_SUPPORTED; //Only support one channel/element as the deep sleep wakeup channel/element + } + if (!button_object_handle_check(element_handle)) { + ESP_LOGE(TE_TAG, "not button handle"); + return ESP_ERR_NOT_SUPPORTED; //Only support button element as the deep sleep wakeup channel + } + s_te_obj->sleep_handle->wakeup_handle = element_handle; + te_button_handle_t button_handle = element_handle; + esp_err_t ret = touch_pad_sleep_channel_enable(button_handle->device->channel, true); + if (ret != ESP_OK) { + return ret; + } + return ESP_OK; +} + +esp_err_t touch_element_sleep_remove_wakeup(touch_elem_handle_t element_handle) +{ + TE_CHECK(s_te_obj->sleep_handle != NULL, ESP_ERR_INVALID_STATE); + TE_CHECK(element_handle != NULL, ESP_ERR_INVALID_ARG); + TE_CHECK(s_te_obj->sleep_handle->wakeup_handle != NULL && + s_te_obj->sleep_handle->wakeup_handle == element_handle, + ESP_ERR_NOT_FOUND); + s_te_obj->sleep_handle->wakeup_handle = NULL; + + te_button_handle_t button_handle = element_handle; //Now we are sure it's absolutely a button element + esp_err_t ret = touch_pad_sleep_channel_enable(button_handle->device->channel, false); + if (ret != ESP_OK) { + return ret; + } + return ESP_OK; +} + +esp_err_t touch_element_sleep_add_wakeup_channel(touch_pad_t wakeup_channel) +{ + TE_CHECK(s_te_obj->sleep_handle != NULL, ESP_ERR_INVALID_STATE); + TE_CHECK(wakeup_channel > TOUCH_PAD_NUM0 && wakeup_channel < TOUCH_PAD_MAX, ESP_ERR_INVALID_ARG); + touch_pad_sleep_channel_t sleep_channel_info; + touch_pad_sleep_channel_get_info(&sleep_channel_info); + if (sleep_channel_info.touch_num == wakeup_channel) { + return ESP_ERR_INVALID_ARG; + } + esp_err_t ret = touch_pad_sleep_channel_enable(wakeup_channel, true); + if (ret != ESP_OK) { + return ret; + } + return ESP_OK; +} + +esp_err_t touch_element_sleep_remove_wakeup_channel(touch_pad_t wakeup_channel) +{ + TE_CHECK(s_te_obj->sleep_handle != NULL, ESP_ERR_INVALID_STATE); + TE_CHECK(wakeup_channel > TOUCH_PAD_NUM0 && wakeup_channel < TOUCH_PAD_MAX, ESP_ERR_INVALID_ARG); + esp_err_t ret = touch_pad_sleep_channel_enable(wakeup_channel, false); + if (ret != ESP_OK) { + return ret; + } + return ESP_OK; +} + +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION +esp_err_t touch_element_sleep_config_wakeup_calibration(touch_elem_handle_t element_handle, bool en) +{ + TE_CHECK(element_handle != NULL, ESP_ERR_INVALID_ARG); + if (button_object_handle_check(element_handle)) { + button_config_wakeup_calibration(element_handle, en); + } else if (slider_object_handle_check(element_handle)) { + slider_config_wakeup_calibration(element_handle, en); + } else if (matrix_object_handle_check(element_handle)) { + matrix_config_wakeup_calibration(element_handle, en); + } else { + return ESP_ERR_NOT_FOUND; + } + return ESP_OK; +} +#endif \ No newline at end of file diff --git a/components/touch_element/touch_matrix.c b/components/touch_element/touch_matrix.c index 8b61b07baf..2cbdb7b41c 100644 --- a/components/touch_element/touch_matrix.c +++ b/components/touch_element/touch_matrix.c @@ -43,6 +43,7 @@ static bool matrix_object_check_channel(touch_pad_t channel_num); static esp_err_t matrix_object_set_threshold(void); static void matrix_object_process_state(void); static void matrix_object_update_state(touch_pad_t channel_num, te_state_t channel_state); +static te_matrix_handle_t matrix_object_search_channel_handle(touch_pad_t channel_num); /* ------------------------------------------------------------------------------------------------------------------ */ esp_err_t touch_matrix_install(const touch_matrix_global_config_t *global_config) @@ -307,6 +308,24 @@ static void matrix_object_update_state(touch_pad_t channel_num, te_state_t chann } } +static te_matrix_handle_t matrix_object_search_channel_handle(touch_pad_t channel_num) +{ + te_matrix_handle_list_t *item; + te_matrix_handle_t matrix_handle = NULL; + SLIST_FOREACH(item, &s_te_mat_obj->handle_list, next) { + for (int idx = 0; idx < item->matrix_handle->x_channel_num + item->matrix_handle->y_channel_num; idx++) { + touch_pad_t matrix_channel = item->matrix_handle->device[idx]->channel; + if (channel_num == matrix_channel) { + matrix_handle = item->matrix_handle; + goto found; + } + } + } + +found: + return matrix_handle; +} + static esp_err_t matrix_object_add_instance(te_matrix_handle_t matrix_handle) { te_matrix_handle_list_t *item = (te_matrix_handle_list_t *)calloc(1, sizeof(te_matrix_handle_list_t)); @@ -335,6 +354,20 @@ static esp_err_t matrix_object_remove_instance(te_matrix_handle_t matrix_handle) return ret; } +bool matrix_object_handle_check(touch_elem_handle_t element_handle) +{ + te_matrix_handle_list_t *item; + xSemaphoreTake(s_te_mat_obj->mutex, portMAX_DELAY); + SLIST_FOREACH(item, &s_te_mat_obj->handle_list, next) { + if (element_handle == item->matrix_handle) { + xSemaphoreGive(s_te_mat_obj->mutex); + return true; + } + } + xSemaphoreGive(s_te_mat_obj->mutex); + return false; +} + static bool matrix_channel_check(te_matrix_handle_t matrix_handle, touch_pad_t channel_num) { te_dev_t *device; @@ -403,6 +436,15 @@ static inline void matrix_dispatch(te_matrix_handle_t matrix_handle, touch_elem_ } } +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION +void matrix_config_wakeup_calibration(te_matrix_handle_t matrix_handle, bool en) +{ + for (int idx = 0; idx < matrix_handle->x_channel_num + matrix_handle->y_channel_num; ++idx) { + matrix_handle->device[idx]->is_use_last_threshold = en; + } +} +#endif + /** * @brief Scan the matrix channel * diff --git a/components/touch_element/touch_slider.c b/components/touch_element/touch_slider.c index 15acaa8802..f5a35c5dc2 100644 --- a/components/touch_element/touch_slider.c +++ b/components/touch_element/touch_slider.c @@ -49,6 +49,7 @@ static bool slider_object_check_channel(touch_pad_t channel_num); static esp_err_t slider_object_set_threshold(void); static void slider_object_process_state(void); static void slider_object_update_state(touch_pad_t channel_num, te_state_t channel_state); +static te_slider_handle_t slider_object_search_channel_handle(touch_pad_t channel_num); /* ------------------------------------------------------------------------------------------------------------------ */ esp_err_t touch_slider_install(const touch_slider_global_config_t *global_config) @@ -300,6 +301,24 @@ static void slider_object_update_state(touch_pad_t channel_num, te_state_t chann } } +static te_slider_handle_t slider_object_search_channel_handle(touch_pad_t channel_num) +{ + te_slider_handle_list_t *item; + te_slider_handle_t slider_handle = NULL; + SLIST_FOREACH(item, &s_te_sld_obj->handle_list, next) { + for (int idx = 0; idx < item->slider_handle->channel_sum; idx++) { + touch_pad_t slider_channel = item->slider_handle->device[idx]->channel; + if (channel_num == slider_channel) { + slider_handle = item->slider_handle; + goto found; + } + } + } + +found: + return slider_handle; +} + static esp_err_t slider_object_add_instance(te_slider_handle_t slider_handle) { te_slider_handle_list_t *item = (te_slider_handle_list_t *)calloc(1, sizeof(te_slider_handle_list_t)); @@ -328,6 +347,20 @@ static esp_err_t slider_object_remove_instance(te_slider_handle_t slider_handle) return ret; } +bool slider_object_handle_check(touch_elem_handle_t element_handle) +{ + te_slider_handle_list_t *item; + xSemaphoreTake(s_te_sld_obj->mutex, portMAX_DELAY); + SLIST_FOREACH(item, &s_te_sld_obj->handle_list, next) { + if (element_handle == item->slider_handle) { + xSemaphoreGive(s_te_sld_obj->mutex); + return true; + } + } + xSemaphoreGive(s_te_sld_obj->mutex); + return false; +} + static bool slider_channel_check(te_slider_handle_t slider_handle, touch_pad_t channel_num) { te_dev_t *device; @@ -406,6 +439,15 @@ static inline void slider_dispatch(te_slider_handle_t slider_handle, touch_elem_ } } +#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION +void slider_config_wakeup_calibration(te_slider_handle_t slider_handle, bool en) +{ + for (int idx = 0; idx < slider_handle->channel_sum; ++idx) { + slider_handle->device[idx]->is_use_last_threshold = en; + } +} +#endif + /** * @brief Slider process * diff --git a/examples/peripherals/touch_element/touch_element_sleep/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/CMakeLists.txt new file mode 100644 index 0000000000..1b4cd9e672 --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/CMakeLists.txt @@ -0,0 +1,8 @@ +# For more information about build system see +# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(touch_element_sleep) diff --git a/examples/peripherals/touch_element/touch_element_sleep/main/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/main/CMakeLists.txt new file mode 100644 index 0000000000..5ad924d077 --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "touch_element_sleep.c" + INCLUDE_DIRS ".") diff --git a/examples/peripherals/touch_element/touch_element_sleep/main/Kconfig.projbuild b/examples/peripherals/touch_element/touch_element_sleep/main/Kconfig.projbuild new file mode 100644 index 0000000000..2518e7429a --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/main/Kconfig.projbuild @@ -0,0 +1,15 @@ +menu "Example Configuration" + + choice TOUCH_SENSOR_EXAMPLE_TYPE + bool "Select touch element dispatch method" + default TOUCH_ELEM_EVENT + help + Select touch element dispatch method (event task or callback) for this example. + + config TOUCH_ELEM_EVENT + bool "Dispatch by event task" + config TOUCH_ELEM_CALLBACK + bool "Dispatch by callback" + endchoice + +endmenu diff --git a/examples/peripherals/touch_element/touch_element_sleep/main/touch_element_sleep.c b/examples/peripherals/touch_element/touch_element_sleep/main/touch_element_sleep.c new file mode 100644 index 0000000000..d0777077b5 --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/main/touch_element_sleep.c @@ -0,0 +1,143 @@ +/* Touch Sensor - Example + + For other examples please check: + https://github.com/espressif/esp-idf/tree/master/examples + + See README.md file to get detailed usage of this example. + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "touch_element/touch_button.h" +#include "esp_log.h" +#include "esp_sleep.h" +#include "soc/soc.h" +#include "soc/rtc_cntl_reg.h" +#include "esp_pm.h" + +static const char *TAG = "Touch Button Example"; +#define TOUCH_BUTTON_NUM 5 + +/* Touch buttons handle */ +static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM]; + +/* Touch buttons channel array */ +static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = { + TOUCH_PAD_NUM1, + TOUCH_PAD_NUM2, + TOUCH_PAD_NUM3, + TOUCH_PAD_NUM4, + TOUCH_PAD_NUM5, +}; + +/* Touch buttons channel sensitivity array */ +static const float channel_sens_array[TOUCH_BUTTON_NUM] = { + 0.03F, + 0.03F, + 0.03F, + 0.03F, + 0.03F, +}; + +#ifdef CONFIG_TOUCH_ELEM_EVENT +/* Button event handler task */ +static void button_handler_task(void *arg) +{ + (void) arg; //Unused + touch_elem_message_t element_message; + while (1) { + /* Waiting for touch element messages */ + touch_element_message_receive(&element_message, portMAX_DELAY); + if (element_message.element_type != TOUCH_ELEM_TYPE_BUTTON) { + continue; + } + /* Decode message */ + const touch_button_message_t *button_message = touch_button_get_message(&element_message); + if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) { + ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg); + } else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) { + ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg); + } else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) { + ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg); + } + } +} +#elif CONFIG_TOUCH_ELEM_CALLBACK +/* Button callback routine */ +static void button_handler(touch_button_handle_t out_handle, touch_button_message_t *out_message, void *arg) +{ + (void) out_handle; //Unused + if (out_message->event == TOUCH_BUTTON_EVT_ON_PRESS) { + ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)arg); + if (out_handle == button_handle[0]) { +// esp_deep_sleep_start(); + } else if (out_handle == button_handle[1]) { + esp_deep_sleep_start(); + } + } else if (out_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) { + ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)arg); + } else if (out_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) { + ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)arg); + } +} +#endif + +void app_main(void) +{ +// esp_pm_config_esp32s2_t pm_config = { +// .max_freq_mhz = 160, +// .min_freq_mhz = 160, +// .light_sleep_enable = true +// }; +// ESP_ERROR_CHECK( esp_pm_configure(&pm_config) ); + /* Initialize Touch Element library */ + touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG(); + ESP_ERROR_CHECK(touch_element_install(&global_config)); + ESP_LOGI(TAG, "Touch element library installed"); + + touch_button_global_config_t button_global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG(); + ESP_ERROR_CHECK(touch_button_install(&button_global_config)); + ESP_LOGI(TAG, "Touch button installed"); + for (int i = 0; i < TOUCH_BUTTON_NUM; i++) { + touch_button_config_t button_config = { + .channel_num = channel_array[i], + .channel_sens = channel_sens_array[i] + }; + /* Create Touch buttons */ + ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i])); + /* Subscribe touch button events (On Press, On Release, On LongPress) */ + ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE | TOUCH_ELEM_EVENT_ON_LONGPRESS, + (void *)channel_array[i])); +#ifdef CONFIG_TOUCH_ELEM_EVENT + /* Set EVENT as the dispatch method */ + ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT)); +#elif CONFIG_TOUCH_ELEM_CALLBACK + /* Set EVENT as the dispatch method */ + ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_CALLBACK)); + /* Register a handler function to handle event messages */ + ESP_ERROR_CHECK(touch_button_set_callback(button_handle[i], button_handler)); +#endif + /* Set LongPress event trigger threshold time */ + ESP_ERROR_CHECK(touch_button_set_longpress(button_handle[i], 1000)); + } + ESP_LOGI(TAG, "Touch buttons created"); + touch_elem_sleep_config_t sleep_config = { + .scan_time = global_config.hardware.sample_count, + .sleep_time = global_config.hardware.sleep_cycle, + }; + ESP_ERROR_CHECK(touch_element_sleep_install(&sleep_config)); + ESP_ERROR_CHECK(touch_element_sleep_add_wakeup(button_handle[0])); + ESP_ERROR_CHECK(touch_element_sleep_config_wakeup_calibration(button_handle[0], true)); + touch_pad_sleep_channel_t sleep_channel_info; + touch_pad_sleep_channel_get_info(&sleep_channel_info); + printf("----------%d\n", sleep_channel_info.touch_num); + touch_element_start(); + ESP_LOGI(TAG, "Touch element library start"); + vTaskDelay(pdMS_TO_TICKS(1000)); +} diff --git a/examples/peripherals/touch_sensor/touch_sensor_v2/touch_pad_interrupt/main/tp_interrupt_main.c b/examples/peripherals/touch_sensor/touch_sensor_v2/touch_pad_interrupt/main/tp_interrupt_main.c index 1285558143..20271deafe 100644 --- a/examples/peripherals/touch_sensor/touch_sensor_v2/touch_pad_interrupt/main/tp_interrupt_main.c +++ b/examples/peripherals/touch_sensor/touch_sensor_v2/touch_pad_interrupt/main/tp_interrupt_main.c @@ -144,6 +144,8 @@ static void tp_example_read_task(void *pvParameter) } } +#include "esp_sleep.h" +#include "hal/touch_sensor_ll.h" void app_main(void) { if (que_touch == NULL) { @@ -209,4 +211,11 @@ void app_main(void) // Start a task to show what pads have been touched xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 4096, NULL, 5, NULL); + + touch_ll_sleep_low_power(true); + while (1) { + esp_sleep_enable_timer_wakeup(100 * 1000); + esp_light_sleep_start(); + vTaskDelay(pdMS_TO_TICKS(100)); + } } From 0f1eb82acd3d810493a184ce96e8d3c2256a759e Mon Sep 17 00:00:00 2001 From: Kang Zuoling Date: Fri, 28 May 2021 17:42:38 +0800 Subject: [PATCH 2/3] add touch element deep sleep example and auto sleep example --- components/touch_element/touch_element.c | 10 ++ .../touch_element_sleep/main/CMakeLists.txt | 2 - .../main/Kconfig.projbuild | 15 -- .../CMakeLists.txt | 2 +- .../touch_elem_auto_sleep/main/CMakeLists.txt | 2 + .../main/Kconfig.projbuild | 17 +++ .../main/example_main.c} | 66 ++++----- .../touch_elem_deep_sleep/CMakeLists.txt | 8 ++ .../touch_elem_deep_sleep/main/CMakeLists.txt | 2 + .../main/Kconfig.projbuild | 69 ++++++++++ .../main/touch_elem_deep_sleep.c | 130 ++++++++++++++++++ 11 files changed, 262 insertions(+), 61 deletions(-) delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/main/CMakeLists.txt delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/main/Kconfig.projbuild rename examples/peripherals/touch_element/touch_element_sleep/{ => touch_elem_auto_sleep}/CMakeLists.txt (91%) create mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/CMakeLists.txt create mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/Kconfig.projbuild rename examples/peripherals/touch_element/touch_element_sleep/{main/touch_element_sleep.c => touch_elem_auto_sleep/main/example_main.c} (67%) create mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/CMakeLists.txt create mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/CMakeLists.txt create mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/Kconfig.projbuild create mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/touch_elem_deep_sleep.c diff --git a/components/touch_element/touch_element.c b/components/touch_element/touch_element.c index 90c888ba1a..1409dd6aa9 100644 --- a/components/touch_element/touch_element.c +++ b/components/touch_element/touch_element.c @@ -380,7 +380,9 @@ static void te_intr_cb(void *arg) if (touch_trig_diff & 0x1) { if (touch_trig_status & BIT(pad_num)) { if (s_te_obj->sleep_handle != NULL) { +#ifdef CONFIG_PM_ENABLE esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock); +#endif } te_intr_msg.channel_state = TE_STATE_PRESS; te_intr_msg.intr_type = TE_INTR_PRESS; @@ -444,7 +446,9 @@ static void te_proc_timer_cb(void *arg) te_object_update_state(te_intr_msg); if (te_intr_msg.intr_type == TE_INTR_RELEASE) { if (s_te_obj->sleep_handle != NULL) { +#ifdef CONFIG_PM_ENABLE esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock); +#endif } } } else if (te_intr_msg.intr_type == TE_INTR_SCAN_DONE) { @@ -453,7 +457,9 @@ static void te_proc_timer_cb(void *arg) te_object_set_threshold(); //TODO: add set threshold error processing ESP_LOGD(TE_DEBUG_TAG, "Set threshold"); if (s_te_obj->sleep_handle != NULL) { +#ifdef CONFIG_PM_ENABLE esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock); +#endif } } if (waterproof_check_state()) { @@ -1009,10 +1015,12 @@ esp_err_t touch_element_sleep_install(touch_elem_sleep_config_t *sleep_config) s_te_obj->sleep_handle->non_volatile_threshold = threshold_shadow; #endif +#ifdef CONFIG_PM_ENABLE ret = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "touch_element", &s_te_obj->sleep_handle->pm_lock); TE_CHECK_GOTO(ret == ESP_OK, cleanup); ret = esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock); TE_CHECK_GOTO(ret == ESP_OK, cleanup); +#endif return ESP_OK; cleanup: @@ -1030,10 +1038,12 @@ void touch_element_sleep_uninstall(void) { esp_err_t ret; if (s_te_obj->sleep_handle->pm_lock != NULL) { +#ifdef CONFIG_PM_ENABLE ret = esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock); if (ret != ESP_OK) { abort(); } +#endif } if (s_te_obj->sleep_handle->wakeup_handle != NULL) { te_button_handle_t button_handle = s_te_obj->sleep_handle->wakeup_handle; diff --git a/examples/peripherals/touch_element/touch_element_sleep/main/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/main/CMakeLists.txt deleted file mode 100644 index 5ad924d077..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/main/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -idf_component_register(SRCS "touch_element_sleep.c" - INCLUDE_DIRS ".") diff --git a/examples/peripherals/touch_element/touch_element_sleep/main/Kconfig.projbuild b/examples/peripherals/touch_element/touch_element_sleep/main/Kconfig.projbuild deleted file mode 100644 index 2518e7429a..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/main/Kconfig.projbuild +++ /dev/null @@ -1,15 +0,0 @@ -menu "Example Configuration" - - choice TOUCH_SENSOR_EXAMPLE_TYPE - bool "Select touch element dispatch method" - default TOUCH_ELEM_EVENT - help - Select touch element dispatch method (event task or callback) for this example. - - config TOUCH_ELEM_EVENT - bool "Dispatch by event task" - config TOUCH_ELEM_CALLBACK - bool "Dispatch by callback" - endchoice - -endmenu diff --git a/examples/peripherals/touch_element/touch_element_sleep/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/CMakeLists.txt similarity index 91% rename from examples/peripherals/touch_element/touch_element_sleep/CMakeLists.txt rename to examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/CMakeLists.txt index 1b4cd9e672..3c81ebef01 100644 --- a/examples/peripherals/touch_element/touch_element_sleep/CMakeLists.txt +++ b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/CMakeLists.txt @@ -5,4 +5,4 @@ cmake_minimum_required(VERSION 3.5) include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(touch_element_sleep) +project(touch_elem_auto_sleep) diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/CMakeLists.txt new file mode 100644 index 0000000000..b5b2d66b77 --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "example_main.c" + INCLUDE_DIRS ".") diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/Kconfig.projbuild b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/Kconfig.projbuild new file mode 100644 index 0000000000..c30707c9ab --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/Kconfig.projbuild @@ -0,0 +1,17 @@ +menu "Example Configuration" + + config TE_PM_ENABLE + bool "Touch Element power management" + default y + + config TE_MAX_CPU_FREQ + int "Touch Element sleep DFS maximum cpu frequency" + depends on PM_DFS_INIT_AUTO + default ESP32S2_DEFAULT_CPU_FREQ_MHZ + + config TE_MIN_CPU_FREQ + int "Touch Element sleep DFS minimum cpu frequency" + depends on PM_DFS_INIT_AUTO + default 10 + +endmenu diff --git a/examples/peripherals/touch_element/touch_element_sleep/main/touch_element_sleep.c b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/example_main.c similarity index 67% rename from examples/peripherals/touch_element/touch_element_sleep/main/touch_element_sleep.c rename to examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/example_main.c index d0777077b5..1e04204aca 100644 --- a/examples/peripherals/touch_element/touch_element_sleep/main/touch_element_sleep.c +++ b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/example_main.c @@ -14,14 +14,12 @@ #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "touch_element/touch_button.h" #include "esp_log.h" -#include "esp_sleep.h" -#include "soc/soc.h" -#include "soc/rtc_cntl_reg.h" #include "esp_pm.h" +#include "touch_element/touch_button.h" + +static const char *TAG = "Touch elem auto sleep"; -static const char *TAG = "Touch Button Example"; #define TOUCH_BUTTON_NUM 5 /* Touch buttons handle */ @@ -45,7 +43,6 @@ static const float channel_sens_array[TOUCH_BUTTON_NUM] = { 0.03F, }; -#ifdef CONFIG_TOUCH_ELEM_EVENT /* Button event handler task */ static void button_handler_task(void *arg) { @@ -68,34 +65,24 @@ static void button_handler_task(void *arg) } } } -#elif CONFIG_TOUCH_ELEM_CALLBACK -/* Button callback routine */ -static void button_handler(touch_button_handle_t out_handle, touch_button_message_t *out_message, void *arg) -{ - (void) out_handle; //Unused - if (out_message->event == TOUCH_BUTTON_EVT_ON_PRESS) { - ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)arg); - if (out_handle == button_handle[0]) { -// esp_deep_sleep_start(); - } else if (out_handle == button_handle[1]) { - esp_deep_sleep_start(); - } - } else if (out_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) { - ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)arg); - } else if (out_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) { - ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)arg); - } -} -#endif + void app_main(void) { -// esp_pm_config_esp32s2_t pm_config = { -// .max_freq_mhz = 160, -// .min_freq_mhz = 160, -// .light_sleep_enable = true -// }; -// ESP_ERROR_CHECK( esp_pm_configure(&pm_config) ); +#ifdef CONFIG_PM_ENABLE //System power management + esp_pm_config_esp32s2_t pm_config = { +#ifdef CONFIG_PM_DFS_INIT_AUTO //Power management dynamic frequency scaling + .max_freq_mhz = CONFIG_TE_MAX_CPU_FREQ, + .min_freq_mhz = CONFIG_TE_MIN_CPU_FREQ, +#endif +#ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE //FreeRTOS tickless + .light_sleep_enable = true +#else + .light_sleep_enable = false +#endif + }; + ESP_ERROR_CHECK( esp_pm_configure(&pm_config)); +#endif /* Initialize Touch Element library */ touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG(); ESP_ERROR_CHECK(touch_element_install(&global_config)); @@ -114,30 +101,23 @@ void app_main(void) /* Subscribe touch button events (On Press, On Release, On LongPress) */ ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE | TOUCH_ELEM_EVENT_ON_LONGPRESS, (void *)channel_array[i])); -#ifdef CONFIG_TOUCH_ELEM_EVENT /* Set EVENT as the dispatch method */ ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT)); -#elif CONFIG_TOUCH_ELEM_CALLBACK - /* Set EVENT as the dispatch method */ - ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_CALLBACK)); - /* Register a handler function to handle event messages */ - ESP_ERROR_CHECK(touch_button_set_callback(button_handle[i], button_handler)); -#endif /* Set LongPress event trigger threshold time */ ESP_ERROR_CHECK(touch_button_set_longpress(button_handle[i], 1000)); } ESP_LOGI(TAG, "Touch buttons created"); + +#ifdef CONFIG_TE_PM_ENABLE touch_elem_sleep_config_t sleep_config = { .scan_time = global_config.hardware.sample_count, .sleep_time = global_config.hardware.sleep_cycle, }; ESP_ERROR_CHECK(touch_element_sleep_install(&sleep_config)); - ESP_ERROR_CHECK(touch_element_sleep_add_wakeup(button_handle[0])); - ESP_ERROR_CHECK(touch_element_sleep_config_wakeup_calibration(button_handle[0], true)); - touch_pad_sleep_channel_t sleep_channel_info; - touch_pad_sleep_channel_get_info(&sleep_channel_info); - printf("----------%d\n", sleep_channel_info.touch_num); +#endif + touch_element_start(); ESP_LOGI(TAG, "Touch element library start"); + xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL); vTaskDelay(pdMS_TO_TICKS(1000)); } diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/CMakeLists.txt new file mode 100644 index 0000000000..3be0e36916 --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/CMakeLists.txt @@ -0,0 +1,8 @@ +# For more information about build system see +# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html +# The following five lines of boilerplate have to be in your project's +# CMakeLists in this exact order for cmake to work correctly +cmake_minimum_required(VERSION 3.5) + +include($ENV{IDF_PATH}/tools/cmake/project.cmake) +project(touch_elem_deep_sleep) diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/CMakeLists.txt new file mode 100644 index 0000000000..d7a6bf5299 --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/CMakeLists.txt @@ -0,0 +1,2 @@ +idf_component_register(SRCS "touch_elem_deep_sleep.c" + INCLUDE_DIRS ".") diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/Kconfig.projbuild b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/Kconfig.projbuild new file mode 100644 index 0000000000..c0558306d4 --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/Kconfig.projbuild @@ -0,0 +1,69 @@ +menu "Example Configuration" + + choice + + prompt "Select a button as Deep Sleep wakeup source" + default TE_WAKEUP_USE_BUTTON_1 + + config TE_WAKEUP_USE_BUTTON_1 + bool "Button 1" + + config TE_WAKEUP_USE_BUTTON_2 + bool "Button 2" + + config TE_WAKEUP_USE_BUTTON_3 + bool "Button 3" + + config TE_WAKEUP_USE_BUTTON_4 + bool "Button 4" + + config TE_WAKEUP_USE_BUTTON_5 + bool "Button 5" + + endchoice + + config TE_WAKEUP_BUTTON_INDEX + int + default 0 if TE_WAKEUP_USE_BUTTON_1 + default 1 if TE_WAKEUP_USE_BUTTON_2 + default 2 if TE_WAKEUP_USE_BUTTON_3 + default 3 if TE_WAKEUP_USE_BUTTON_4 + default 4 if TE_WAKEUP_USE_BUTTON_5 + + choice + + prompt "Select a button as Deep Sleep entry" + default TE_ENTRY_USE_BUTTON_1 + + config TE_ENTRY_USE_BUTTON_1 + bool "Button 1" + + config TE_ENTRY_USE_BUTTON_2 + bool "Button 2" + + config TE_ENTRY_USE_BUTTON_3 + bool "Button 3" + + config TE_ENTRY_USE_BUTTON_4 + bool "Button 4" + + config TE_ENTRY_USE_BUTTON_5 + bool "Button 5" + + endchoice + + config TE_ENTRY_BUTTON_INDEX + int + default 0 if TE_ENTRY_USE_BUTTON_1 + default 1 if TE_ENTRY_USE_BUTTON_2 + default 2 if TE_ENTRY_USE_BUTTON_3 + default 3 if TE_ENTRY_USE_BUTTON_4 + default 4 if TE_ENTRY_USE_BUTTON_5 + + config TE_SKIP_CALIBRATION + bool "Touch Element skip wakeup calibration" + depends on TE_SKIP_DSLEEP_WAKEUP_CALIBRATION + default y + + +endmenu diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/touch_elem_deep_sleep.c b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/touch_elem_deep_sleep.c new file mode 100644 index 0000000000..714f1c7c4e --- /dev/null +++ b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/touch_elem_deep_sleep.c @@ -0,0 +1,130 @@ +/* Touch Sensor - Example + + For other examples please check: + https://github.com/espressif/esp-idf/tree/master/examples + + See README.md file to get detailed usage of this example. + + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "esp_log.h" +#include "esp_sleep.h" +#include "touch_element/touch_button.h" + +static const char *TAG = "Touch Button Example"; + +#define TOUCH_BUTTON_NUM 5 + +/* Touch buttons handle */ +static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM]; +static touch_button_handle_t wakeup_button_handle = NULL; +static touch_button_handle_t entry_button_handle = NULL; + + +/* Touch buttons channel array */ +static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = { + TOUCH_PAD_NUM1, + TOUCH_PAD_NUM2, + TOUCH_PAD_NUM3, + TOUCH_PAD_NUM4, + TOUCH_PAD_NUM5, +}; + +/* Touch buttons channel sensitivity array */ +static const float channel_sens_array[TOUCH_BUTTON_NUM] = { + 0.03F, + 0.03F, + 0.03F, + 0.03F, + 0.03F, +}; + +/* Button event handler task */ +static void button_handler_task(void *arg) +{ + (void) arg; //Unused + touch_elem_message_t element_message; + while (1) { + /* Waiting for touch element messages */ + touch_element_message_receive(&element_message, portMAX_DELAY); + if (element_message.element_type != TOUCH_ELEM_TYPE_BUTTON) { + continue; + } + /* Decode message */ + const touch_button_message_t *button_message = touch_button_get_message(&element_message); + + if (element_message.handle == entry_button_handle) { + if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) { + ESP_LOGI(TAG, "Entering Deep sleep ..."); + fflush(stdout); + esp_deep_sleep_start(); + } + } + + if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) { + ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg); + } else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) { + ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg); + } else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) { + ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg); + } + } +} + +void app_main(void) +{ + esp_sleep_wakeup_cause_t wakeup_ret = esp_sleep_get_wakeup_cause(); + printf("------%d\n", wakeup_ret); + /* Initialize Touch Element library */ + touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG(); + ESP_ERROR_CHECK(touch_element_install(&global_config)); + ESP_LOGI(TAG, "Touch element library installed"); + + touch_button_global_config_t button_global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG(); + ESP_ERROR_CHECK(touch_button_install(&button_global_config)); + ESP_LOGI(TAG, "Touch button installed"); + for (int i = 0; i < TOUCH_BUTTON_NUM; i++) { + touch_button_config_t button_config = { + .channel_num = channel_array[i], + .channel_sens = channel_sens_array[i] + }; + /* Create Touch buttons */ + ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i])); + /* Subscribe touch button events (On Press, On Release, On LongPress) */ + if (i == CONFIG_TE_WAKEUP_BUTTON_INDEX || i == CONFIG_TE_ENTRY_BUTTON_INDEX) { + continue; + } else { + ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE, (void *)channel_array[i])); + } + + /* Set EVENT as the dispatch method */ + ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT)); + } + wakeup_button_handle = button_handle[CONFIG_TE_WAKEUP_BUTTON_INDEX]; + entry_button_handle = button_handle[CONFIG_TE_ENTRY_BUTTON_INDEX]; + ESP_ERROR_CHECK(touch_button_subscribe_event(entry_button_handle, TOUCH_ELEM_EVENT_ON_PRESS, (void *)channel_array[CONFIG_TE_ENTRY_BUTTON_INDEX])); + ESP_ERROR_CHECK(touch_button_set_dispatch_method(entry_button_handle, TOUCH_ELEM_DISP_EVENT)); + ESP_LOGI(TAG, "Touch buttons created"); + + touch_elem_sleep_config_t sleep_config = { + .scan_time = global_config.hardware.sample_count, + .sleep_time = global_config.hardware.sleep_cycle, + }; + ESP_ERROR_CHECK(touch_element_sleep_install(&sleep_config)); + ESP_ERROR_CHECK(touch_element_sleep_add_wakeup(wakeup_button_handle)); +#ifdef CONFIG_TE_SKIP_CALIBRATION + ESP_ERROR_CHECK(touch_element_sleep_config_wakeup_calibration(wakeup_button_handle, true)); +#endif + + touch_element_start(); + ESP_LOGI(TAG, "Touch element library start"); + xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL); + ESP_LOGI(TAG, "Press Button[%d] to enter sleep and press Button[%d] to wakeup system", channel_array[CONFIG_TE_ENTRY_BUTTON_INDEX], channel_array[CONFIG_TE_WAKEUP_BUTTON_INDEX]); +} From 62ab4456a86f1d2637f8e154b89ca015be684b61 Mon Sep 17 00:00:00 2001 From: laokaiyao Date: Fri, 26 Nov 2021 18:09:24 +0800 Subject: [PATCH 3/3] touch_sleep: complete the support for touch sleep --- components/esp_hw_support/sleep_modes.c | 1 + .../hal/esp32/include/hal/rtc_cntl_ll.h | 5 + .../hal/esp32s2/include/hal/rtc_cntl_ll.h | 5 + .../hal/esp32s3/include/hal/rtc_cntl_ll.h | 5 + components/soc/esp32s2/include/soc/soc_caps.h | 2 +- components/touch_element/Kconfig | 10 - .../include/touch_element/touch_element.h | 88 ++++++- .../touch_element/touch_element_private.h | 25 +- components/touch_element/touch_button.c | 26 +- components/touch_element/touch_element.c | 249 ++++++++---------- components/touch_element/touch_matrix.c | 29 +- components/touch_element/touch_slider.c | 29 +- .../peripherals/touch_element.rst | 28 ++ .../touch_elem_auto_sleep/CMakeLists.txt | 8 - .../touch_elem_auto_sleep/main/CMakeLists.txt | 2 - .../main/Kconfig.projbuild | 17 -- .../touch_elem_deep_sleep/CMakeLists.txt | 8 - .../touch_elem_deep_sleep/main/CMakeLists.txt | 2 - .../main/Kconfig.projbuild | 69 ----- .../main/touch_elem_deep_sleep.c | 130 --------- .../main/tp_interrupt_main.c | 9 - examples/system/light_sleep/README.md | 13 + .../system/light_sleep/main/CMakeLists.txt | 6 + .../light_sleep/main/light_sleep_example.h | 4 + .../main/light_sleep_example_main.c | 9 + .../light_sleep/main/touch_wakeup.c} | 76 ++---- 26 files changed, 314 insertions(+), 541 deletions(-) delete mode 100644 components/touch_element/Kconfig delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/CMakeLists.txt delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/CMakeLists.txt delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/Kconfig.projbuild delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/CMakeLists.txt delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/CMakeLists.txt delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/Kconfig.projbuild delete mode 100644 examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/touch_elem_deep_sleep.c rename examples/{peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/example_main.c => system/light_sleep/main/touch_wakeup.c} (58%) diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index 154b9bb649..16b886e3be 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -23,6 +23,7 @@ #include "soc/soc_caps.h" #include "driver/rtc_io.h" #include "hal/rtc_io_hal.h" +#include "hal/rtc_cntl_ll.h" #include "driver/uart.h" diff --git a/components/hal/esp32/include/hal/rtc_cntl_ll.h b/components/hal/esp32/include/hal/rtc_cntl_ll.h index 31caf551bf..19c12f69af 100644 --- a/components/hal/esp32/include/hal/rtc_cntl_ll.h +++ b/components/hal/esp32/include/hal/rtc_cntl_ll.h @@ -47,6 +47,11 @@ static inline void rtc_cntl_ll_ulp_int_clear(void) REG_SET_BIT(RTC_CNTL_INT_CLR_REG, RTC_CNTL_SAR_INT_CLR); } +static inline void rtc_cntl_ll_timer2_set_touch_wait_cycle(uint32_t wait_cycle) +{ + REG_SET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT, wait_cycle); +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32s2/include/hal/rtc_cntl_ll.h b/components/hal/esp32s2/include/hal/rtc_cntl_ll.h index ffba472c47..d70b997482 100644 --- a/components/hal/esp32s2/include/hal/rtc_cntl_ll.h +++ b/components/hal/esp32s2/include/hal/rtc_cntl_ll.h @@ -47,6 +47,11 @@ static inline void rtc_cntl_ll_ulp_int_clear(void) REG_SET_BIT(RTC_CNTL_INT_CLR_REG, RTC_CNTL_COCPU_TRAP_INT_CLR); } +static inline void rtc_cntl_ll_timer2_set_touch_wait_cycle(uint32_t wait_cycle) +{ + REG_SET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT, wait_cycle); +} + #ifdef __cplusplus } #endif diff --git a/components/hal/esp32s3/include/hal/rtc_cntl_ll.h b/components/hal/esp32s3/include/hal/rtc_cntl_ll.h index 3dfa5e3e71..bdf20068cb 100644 --- a/components/hal/esp32s3/include/hal/rtc_cntl_ll.h +++ b/components/hal/esp32s3/include/hal/rtc_cntl_ll.h @@ -127,6 +127,11 @@ static inline void rtc_cntl_ll_ulp_int_clear(void) REG_SET_BIT(RTC_CNTL_INT_CLR_REG, RTC_CNTL_COCPU_TRAP_INT_CLR); } +static inline void rtc_cntl_ll_timer2_set_touch_wait_cycle(uint32_t wait_cycle) +{ + REG_SET_FIELD(RTC_CNTL_TIMER2_REG, RTC_CNTL_ULPCP_TOUCH_START_WAIT, wait_cycle); +} + #ifdef __cplusplus } #endif diff --git a/components/soc/esp32s2/include/soc/soc_caps.h b/components/soc/esp32s2/include/soc/soc_caps.h index 5bdc088149..36509a74f9 100644 --- a/components/soc/esp32s2/include/soc/soc_caps.h +++ b/components/soc/esp32s2/include/soc/soc_caps.h @@ -292,7 +292,7 @@ /*-------------------------- TOUCH SENSOR CAPS -------------------------------*/ #define SOC_TOUCH_VERSION_2 (1) /*!handle_list, next) { - touch_pad_t button_channel = item->button_handle->device->channel; - if (channel_num == button_channel) { - button_handle = item->button_handle; - break; - } - } - - return button_handle; -} - static esp_err_t button_object_add_instance(te_button_handle_t button_handle) { te_button_handle_list_t *item = (te_button_handle_list_t *)calloc(1, sizeof(te_button_handle_list_t)); @@ -312,7 +296,7 @@ static esp_err_t button_object_remove_instance(te_button_handle_t button_handle) return ret; } -bool button_object_handle_check(touch_elem_handle_t element_handle) +bool is_button_object_handle(touch_elem_handle_t element_handle) { te_button_handle_list_t *item; xSemaphoreTake(s_te_btn_obj->mutex, portMAX_DELAY); @@ -376,12 +360,10 @@ static inline void button_dispatch(te_button_handle_t button_handle, touch_elem_ } } -#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION -void button_config_wakeup_calibration(te_button_handle_t button_handle, bool en) +void button_enable_wakeup_calibration(te_button_handle_t button_handle, bool en) { - button_handle->device->is_use_last_threshold = en; + button_handle->device->is_use_last_threshold = !en; } -#endif /** * @brief Button process diff --git a/components/touch_element/touch_element.c b/components/touch_element/touch_element.c index 1409dd6aa9..d8d656e858 100644 --- a/components/touch_element/touch_element.c +++ b/components/touch_element/touch_element.c @@ -11,11 +11,11 @@ #include "freertos/queue.h" #include "esp_sleep.h" #include "esp_timer.h" -#include "esp_log.h" +#include "esp_check.h" #include "hal/touch_sensor_hal.h" //TODO: remove hal #include "touch_element/touch_element_private.h" -#include "esp32s2/rom/rtc.h" +#include "esp_rom_sys.h" #define TE_CLASS_ITEM(cls, cls_type, cls_item) ((&((cls)[cls_type]))->cls_item) @@ -98,14 +98,10 @@ typedef struct { SemaphoreHandle_t mutex; //Global resource mutex bool is_set_threshold; //Threshold configuration state bit uint32_t denoise_channel_raw; //De-noise channel(TO) raw signal -// touch_elem_sleep_config_t *sleep_config; -// esp_pm_lock_handle_t pm_lock_handle; } te_obj_t; static te_obj_t *s_te_obj = NULL; -#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION RTC_FAST_ATTR uint32_t threshold_shadow[TOUCH_PAD_MAX - 1] = {0}; -#endif /** * Internal de-noise channel(Touch channel 0) equivalent capacitance table, depends on hardware design @@ -338,15 +334,15 @@ uint32_t te_get_threshold(touch_pad_t channel_num) bool te_is_touch_dsleep_wakeup(void) { - RESET_REASON rtc_reset_reason = rtc_get_reset_reason(0); - if (rtc_reset_reason != DEEPSLEEP_RESET) { + soc_reset_reason_t reset_reason = esp_rom_get_reset_reason(0); + if (reset_reason != RESET_REASON_CORE_DEEP_SLEEP) { return false; } esp_sleep_wakeup_cause_t wakeup_reason = esp_sleep_get_wakeup_cause(); return wakeup_reason == ESP_SLEEP_WAKEUP_TOUCHPAD; } -inline touch_pad_t te_get_sleep_channel(void) +touch_pad_t te_get_sleep_channel(void) { touch_pad_sleep_channel_t sleep_channel_info; touch_pad_sleep_channel_get_info(&sleep_channel_info); @@ -413,8 +409,6 @@ static void te_intr_cb(void *arg) } /*< De-noise channel signal must be read at the time between SCAN_DONE and next measurement beginning(sleep)!!! */ touch_pad_denoise_read_data(&s_te_obj->denoise_channel_raw); //Update de-noise signal - } else { -// te_intr_msg.intr_type = TE_INTR_MAX; // Unknown Exception } if (need_send_queue) { xQueueSendFromISR(s_te_obj->intr_msg_queue, &te_intr_msg, &task_awoken); @@ -444,12 +438,10 @@ static void te_proc_timer_cb(void *arg) if (ret == pdPASS) { if (te_intr_msg.intr_type == TE_INTR_PRESS || te_intr_msg.intr_type == TE_INTR_RELEASE) { te_object_update_state(te_intr_msg); - if (te_intr_msg.intr_type == TE_INTR_RELEASE) { - if (s_te_obj->sleep_handle != NULL) { + if ((s_te_obj->sleep_handle != NULL) && (te_intr_msg.intr_type == TE_INTR_RELEASE)) { #ifdef CONFIG_PM_ENABLE - esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock); + esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock); #endif - } } } else if (te_intr_msg.intr_type == TE_INTR_SCAN_DONE) { if (s_te_obj->is_set_threshold != true) { @@ -571,9 +563,7 @@ esp_err_t te_dev_init(te_dev_t **device, uint8_t device_num, te_dev_type_t type, device[idx]->sens = sens[idx] * divider; device[idx]->type = type; device[idx]->state = TE_STATE_IDLE; -#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION device[idx]->is_use_last_threshold = false; -#endif esp_err_t ret = touch_pad_config(device[idx]->channel); TE_CHECK(ret == ESP_OK, ret); } @@ -587,7 +577,7 @@ void te_dev_deinit(te_dev_t **device, uint8_t device_num) } } -esp_err_t te_config_thresh(touch_pad_t channel_num, uint32_t threshold) +static esp_err_t te_config_thresh(touch_pad_t channel_num, uint32_t threshold) { esp_err_t ret; touch_pad_sleep_channel_t sleep_channel_info; @@ -604,15 +594,9 @@ esp_err_t te_dev_set_threshold(te_dev_t *device) { esp_err_t ret = ESP_OK; uint32_t smo_val = 0; -#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION - if (s_te_obj->sleep_handle == NULL) { - ESP_LOGE(TE_TAG, "Touch Element sleep is not installed"); - return ESP_ERR_INVALID_STATE; - } - if (device->is_use_last_threshold) { + + if (s_te_obj->sleep_handle && device->is_use_last_threshold) { if (te_is_touch_dsleep_wakeup()) { //Deep sleep wakeup reset - touch_pad_t sleep_channel = te_get_sleep_channel(); - ets_printf("----config rtc %ld %ld\n", s_te_obj->sleep_handle->non_volatile_threshold[device->channel - 1], sleep_channel); ret = te_config_thresh(device->channel, s_te_obj->sleep_handle->non_volatile_threshold[device->channel - 1]); } else { //Other reset smo_val = te_read_smooth_signal(device->channel); @@ -624,11 +608,6 @@ esp_err_t te_dev_set_threshold(te_dev_t *device) smo_val = te_read_smooth_signal(device->channel); ret = te_config_thresh(device->channel, device->sens * smo_val); } - -#else - smo_val = te_read_smooth_signal(device->channel); - ret = te_config_thresh(device->channel, device->sens * smo_val); -#endif ESP_LOGD(TE_DEBUG_TAG, "channel: %"PRIu8", smo_val: %"PRIu32, device->channel, smo_val); return ret; } @@ -993,148 +972,146 @@ static void waterproof_guard_update_state(touch_pad_t current_channel, te_state_ ESP_LOGD(TE_DEBUG_TAG, "waterproof guard state update %d", guard_device->state); } -esp_err_t touch_element_sleep_install(touch_elem_sleep_config_t *sleep_config) +esp_err_t touch_element_enable_light_sleep(const touch_elem_sleep_config_t *sleep_config) { TE_CHECK(s_te_obj != NULL, ESP_ERR_INVALID_STATE); TE_CHECK(s_te_obj->sleep_handle == NULL, ESP_ERR_INVALID_STATE); - TE_CHECK(sleep_config != NULL, ESP_ERR_INVALID_ARG); - - s_te_obj->sleep_handle = calloc(1, sizeof(struct te_sleep_s)); - if (s_te_obj->sleep_handle == NULL) { - return ESP_ERR_NO_MEM; + uint16_t sample_count = 500; + uint16_t sleep_cycle = 0x0f; + if (sleep_config) { + sample_count = sleep_config->sample_count; + sleep_cycle = sleep_config->sleep_cycle; } - esp_err_t ret; - touch_pad_sleep_channel_set_work_time(sleep_config->sleep_time, sleep_config->scan_time); - ret = esp_sleep_enable_touchpad_wakeup(); - TE_CHECK_GOTO(ret == ESP_OK, cleanup); + s_te_obj->sleep_handle = calloc(1, sizeof(struct te_sleep_s)); + TE_CHECK(s_te_obj->sleep_handle, ESP_ERR_NO_MEM); -#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION - ret = esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_ON); - TE_CHECK_GOTO(ret == ESP_OK, cleanup); + esp_err_t ret = ESP_OK; + touch_pad_sleep_channel_set_work_time(sleep_cycle, sample_count); + TE_CHECK_GOTO(esp_sleep_enable_touchpad_wakeup() == ESP_OK, cleanup); + + TE_CHECK_GOTO(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON) == ESP_OK, cleanup); s_te_obj->sleep_handle->non_volatile_threshold = threshold_shadow; -#endif #ifdef CONFIG_PM_ENABLE - ret = esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "touch_element", &s_te_obj->sleep_handle->pm_lock); - TE_CHECK_GOTO(ret == ESP_OK, cleanup); - ret = esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock); - TE_CHECK_GOTO(ret == ESP_OK, cleanup); + TE_CHECK_GOTO(esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "touch_element", &s_te_obj->sleep_handle->pm_lock) == ESP_OK, cleanup); + TE_CHECK_GOTO(esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock) == ESP_OK, cleanup); #endif + return ESP_OK; cleanup: +#ifdef CONFIG_PM_ENABLE if (s_te_obj->sleep_handle->pm_lock != NULL) { - esp_err_t del_ret = esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock); - if (del_ret != ESP_OK) { + if (esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock) != ESP_OK) { abort(); } } +#endif TE_FREE_AND_NULL(s_te_obj->sleep_handle); return ret; } -void touch_element_sleep_uninstall(void) +esp_err_t touch_element_disable_light_sleep(void) { - esp_err_t ret; - if (s_te_obj->sleep_handle->pm_lock != NULL) { + TE_CHECK(s_te_obj->sleep_handle, ESP_ERR_INVALID_STATE); #ifdef CONFIG_PM_ENABLE - ret = esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock); - if (ret != ESP_OK) { - abort(); - } + if (s_te_obj->sleep_handle->pm_lock != NULL) { + /* Sleep channel is going to uninstall, pm lock is not needed anymore, + but we need to make sure that pm lock has been released before delete it. */ + while(esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock) == ESP_OK); + esp_err_t ret = esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock); + TE_CHECK(ret == ESP_OK, ret); + s_te_obj->sleep_handle->pm_lock = NULL; + } #endif + esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TOUCHPAD); + TE_FREE_AND_NULL(s_te_obj->sleep_handle); + return ESP_OK; +} + +esp_err_t touch_element_enable_deep_sleep(touch_elem_handle_t wakeup_elem_handle, const touch_elem_sleep_config_t *sleep_config) +{ + TE_CHECK(s_te_obj != NULL, ESP_ERR_INVALID_STATE); + TE_CHECK(s_te_obj->sleep_handle == NULL, ESP_ERR_INVALID_STATE); + TE_CHECK(wakeup_elem_handle != NULL, ESP_ERR_INVALID_ARG); + TE_CHECK(sleep_config != NULL, ESP_ERR_INVALID_ARG); + uint16_t sample_count = 500; + uint16_t sleep_cycle = 0x0f; + if (sleep_config) { + sample_count = sleep_config->sample_count; + sleep_cycle = sleep_config->sleep_cycle; } - if (s_te_obj->sleep_handle->wakeup_handle != NULL) { - te_button_handle_t button_handle = s_te_obj->sleep_handle->wakeup_handle; - ret = touch_pad_sleep_channel_enable(button_handle->device->channel, false); - if (ret != ESP_OK) { + + s_te_obj->sleep_handle = calloc(1, sizeof(struct te_sleep_s)); + TE_CHECK(s_te_obj->sleep_handle, ESP_ERR_NO_MEM); + + esp_err_t ret = ESP_OK; + touch_pad_sleep_channel_set_work_time(sleep_cycle, sample_count); + TE_CHECK_GOTO(esp_sleep_enable_touchpad_wakeup() == ESP_OK, cleanup); + + TE_CHECK_GOTO(esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_ON) == ESP_OK, cleanup); + s_te_obj->sleep_handle->non_volatile_threshold = threshold_shadow; + +#ifdef CONFIG_PM_ENABLE + TE_CHECK_GOTO(esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "touch_element", &s_te_obj->sleep_handle->pm_lock) == ESP_OK, cleanup); + TE_CHECK_GOTO(esp_pm_lock_acquire(s_te_obj->sleep_handle->pm_lock) == ESP_OK, cleanup); +#endif + //Only support one channel/element as the deep sleep wakeup channel/element + TE_CHECK(is_button_object_handle(wakeup_elem_handle), ESP_ERR_NOT_SUPPORTED); + s_te_obj->sleep_handle->wakeup_handle = wakeup_elem_handle; + te_button_handle_t button_handle = wakeup_elem_handle; + ret = touch_pad_sleep_channel_enable(button_handle->device->channel, true); + TE_CHECK(ret == ESP_OK, ret); + + return ESP_OK; + +cleanup: +#ifdef CONFIG_PM_ENABLE + if (s_te_obj->sleep_handle->pm_lock != NULL) { + if (esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock) != ESP_OK) { abort(); } } - s_te_obj->sleep_handle->pm_lock = NULL; +#endif + TE_FREE_AND_NULL(s_te_obj->sleep_handle); + return ret; +} + +esp_err_t touch_element_disable_deep_sleep(void) +{ + TE_CHECK(s_te_obj->sleep_handle, ESP_ERR_INVALID_STATE); + esp_err_t ret; +#ifdef CONFIG_PM_ENABLE + if (s_te_obj->sleep_handle->pm_lock != NULL) { + /* Sleep channel is going to uninstall, pm lock is not needed anymore, + but we need to make sure that pm lock has been released before delete it. */ + while(esp_pm_lock_release(s_te_obj->sleep_handle->pm_lock) == ESP_OK); + ret = esp_pm_lock_delete(s_te_obj->sleep_handle->pm_lock); + TE_CHECK(ret == ESP_OK, ret); + s_te_obj->sleep_handle->pm_lock = NULL; + } +#endif + te_button_handle_t button_handle = s_te_obj->sleep_handle->wakeup_handle; + ret = touch_pad_sleep_channel_enable(button_handle->device->channel, false); + TE_CHECK(ret == ESP_OK, ret); + esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_TOUCHPAD); s_te_obj->sleep_handle->wakeup_handle = NULL; TE_FREE_AND_NULL(s_te_obj->sleep_handle); -} - -esp_err_t touch_element_sleep_add_wakeup(touch_elem_handle_t element_handle) -{ - TE_CHECK(s_te_obj->sleep_handle != NULL, ESP_ERR_INVALID_STATE); - TE_CHECK(element_handle != NULL, ESP_ERR_INVALID_ARG); - if (s_te_obj->sleep_handle->wakeup_handle != NULL) { - ESP_LOGE(TE_TAG, "sleep not null"); - return ESP_ERR_NOT_SUPPORTED; //Only support one channel/element as the deep sleep wakeup channel/element - } - if (!button_object_handle_check(element_handle)) { - ESP_LOGE(TE_TAG, "not button handle"); - return ESP_ERR_NOT_SUPPORTED; //Only support button element as the deep sleep wakeup channel - } - s_te_obj->sleep_handle->wakeup_handle = element_handle; - te_button_handle_t button_handle = element_handle; - esp_err_t ret = touch_pad_sleep_channel_enable(button_handle->device->channel, true); - if (ret != ESP_OK) { - return ret; - } return ESP_OK; } -esp_err_t touch_element_sleep_remove_wakeup(touch_elem_handle_t element_handle) -{ - TE_CHECK(s_te_obj->sleep_handle != NULL, ESP_ERR_INVALID_STATE); - TE_CHECK(element_handle != NULL, ESP_ERR_INVALID_ARG); - TE_CHECK(s_te_obj->sleep_handle->wakeup_handle != NULL && - s_te_obj->sleep_handle->wakeup_handle == element_handle, - ESP_ERR_NOT_FOUND); - s_te_obj->sleep_handle->wakeup_handle = NULL; - - te_button_handle_t button_handle = element_handle; //Now we are sure it's absolutely a button element - esp_err_t ret = touch_pad_sleep_channel_enable(button_handle->device->channel, false); - if (ret != ESP_OK) { - return ret; - } - return ESP_OK; -} - -esp_err_t touch_element_sleep_add_wakeup_channel(touch_pad_t wakeup_channel) -{ - TE_CHECK(s_te_obj->sleep_handle != NULL, ESP_ERR_INVALID_STATE); - TE_CHECK(wakeup_channel > TOUCH_PAD_NUM0 && wakeup_channel < TOUCH_PAD_MAX, ESP_ERR_INVALID_ARG); - touch_pad_sleep_channel_t sleep_channel_info; - touch_pad_sleep_channel_get_info(&sleep_channel_info); - if (sleep_channel_info.touch_num == wakeup_channel) { - return ESP_ERR_INVALID_ARG; - } - esp_err_t ret = touch_pad_sleep_channel_enable(wakeup_channel, true); - if (ret != ESP_OK) { - return ret; - } - return ESP_OK; -} - -esp_err_t touch_element_sleep_remove_wakeup_channel(touch_pad_t wakeup_channel) -{ - TE_CHECK(s_te_obj->sleep_handle != NULL, ESP_ERR_INVALID_STATE); - TE_CHECK(wakeup_channel > TOUCH_PAD_NUM0 && wakeup_channel < TOUCH_PAD_MAX, ESP_ERR_INVALID_ARG); - esp_err_t ret = touch_pad_sleep_channel_enable(wakeup_channel, false); - if (ret != ESP_OK) { - return ret; - } - return ESP_OK; -} - -#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION -esp_err_t touch_element_sleep_config_wakeup_calibration(touch_elem_handle_t element_handle, bool en) +esp_err_t touch_element_sleep_enable_wakeup_calibration(touch_elem_handle_t element_handle, bool en) { TE_CHECK(element_handle != NULL, ESP_ERR_INVALID_ARG); - if (button_object_handle_check(element_handle)) { - button_config_wakeup_calibration(element_handle, en); - } else if (slider_object_handle_check(element_handle)) { - slider_config_wakeup_calibration(element_handle, en); - } else if (matrix_object_handle_check(element_handle)) { - matrix_config_wakeup_calibration(element_handle, en); + if (is_button_object_handle(element_handle)) { + button_enable_wakeup_calibration(element_handle, en); + } else if (is_slider_object_handle(element_handle)) { + slider_enable_wakeup_calibration(element_handle, en); + } else if (is_matrix_object_handle(element_handle)) { + matrix_enable_wakeup_calibration(element_handle, en); } else { return ESP_ERR_NOT_FOUND; } return ESP_OK; } -#endif \ No newline at end of file diff --git a/components/touch_element/touch_matrix.c b/components/touch_element/touch_matrix.c index 2cbdb7b41c..1bc554834e 100644 --- a/components/touch_element/touch_matrix.c +++ b/components/touch_element/touch_matrix.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -43,7 +43,6 @@ static bool matrix_object_check_channel(touch_pad_t channel_num); static esp_err_t matrix_object_set_threshold(void); static void matrix_object_process_state(void); static void matrix_object_update_state(touch_pad_t channel_num, te_state_t channel_state); -static te_matrix_handle_t matrix_object_search_channel_handle(touch_pad_t channel_num); /* ------------------------------------------------------------------------------------------------------------------ */ esp_err_t touch_matrix_install(const touch_matrix_global_config_t *global_config) @@ -308,24 +307,6 @@ static void matrix_object_update_state(touch_pad_t channel_num, te_state_t chann } } -static te_matrix_handle_t matrix_object_search_channel_handle(touch_pad_t channel_num) -{ - te_matrix_handle_list_t *item; - te_matrix_handle_t matrix_handle = NULL; - SLIST_FOREACH(item, &s_te_mat_obj->handle_list, next) { - for (int idx = 0; idx < item->matrix_handle->x_channel_num + item->matrix_handle->y_channel_num; idx++) { - touch_pad_t matrix_channel = item->matrix_handle->device[idx]->channel; - if (channel_num == matrix_channel) { - matrix_handle = item->matrix_handle; - goto found; - } - } - } - -found: - return matrix_handle; -} - static esp_err_t matrix_object_add_instance(te_matrix_handle_t matrix_handle) { te_matrix_handle_list_t *item = (te_matrix_handle_list_t *)calloc(1, sizeof(te_matrix_handle_list_t)); @@ -354,7 +335,7 @@ static esp_err_t matrix_object_remove_instance(te_matrix_handle_t matrix_handle) return ret; } -bool matrix_object_handle_check(touch_elem_handle_t element_handle) +bool is_matrix_object_handle(touch_elem_handle_t element_handle) { te_matrix_handle_list_t *item; xSemaphoreTake(s_te_mat_obj->mutex, portMAX_DELAY); @@ -436,14 +417,12 @@ static inline void matrix_dispatch(te_matrix_handle_t matrix_handle, touch_elem_ } } -#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION -void matrix_config_wakeup_calibration(te_matrix_handle_t matrix_handle, bool en) +void matrix_enable_wakeup_calibration(te_matrix_handle_t matrix_handle, bool en) { for (int idx = 0; idx < matrix_handle->x_channel_num + matrix_handle->y_channel_num; ++idx) { - matrix_handle->device[idx]->is_use_last_threshold = en; + matrix_handle->device[idx]->is_use_last_threshold = !en; } } -#endif /** * @brief Scan the matrix channel diff --git a/components/touch_element/touch_slider.c b/components/touch_element/touch_slider.c index f5a35c5dc2..ad506597e0 100644 --- a/components/touch_element/touch_slider.c +++ b/components/touch_element/touch_slider.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2016-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -49,7 +49,6 @@ static bool slider_object_check_channel(touch_pad_t channel_num); static esp_err_t slider_object_set_threshold(void); static void slider_object_process_state(void); static void slider_object_update_state(touch_pad_t channel_num, te_state_t channel_state); -static te_slider_handle_t slider_object_search_channel_handle(touch_pad_t channel_num); /* ------------------------------------------------------------------------------------------------------------------ */ esp_err_t touch_slider_install(const touch_slider_global_config_t *global_config) @@ -301,24 +300,6 @@ static void slider_object_update_state(touch_pad_t channel_num, te_state_t chann } } -static te_slider_handle_t slider_object_search_channel_handle(touch_pad_t channel_num) -{ - te_slider_handle_list_t *item; - te_slider_handle_t slider_handle = NULL; - SLIST_FOREACH(item, &s_te_sld_obj->handle_list, next) { - for (int idx = 0; idx < item->slider_handle->channel_sum; idx++) { - touch_pad_t slider_channel = item->slider_handle->device[idx]->channel; - if (channel_num == slider_channel) { - slider_handle = item->slider_handle; - goto found; - } - } - } - -found: - return slider_handle; -} - static esp_err_t slider_object_add_instance(te_slider_handle_t slider_handle) { te_slider_handle_list_t *item = (te_slider_handle_list_t *)calloc(1, sizeof(te_slider_handle_list_t)); @@ -347,7 +328,7 @@ static esp_err_t slider_object_remove_instance(te_slider_handle_t slider_handle) return ret; } -bool slider_object_handle_check(touch_elem_handle_t element_handle) +bool is_slider_object_handle(touch_elem_handle_t element_handle) { te_slider_handle_list_t *item; xSemaphoreTake(s_te_sld_obj->mutex, portMAX_DELAY); @@ -439,14 +420,12 @@ static inline void slider_dispatch(te_slider_handle_t slider_handle, touch_elem_ } } -#ifdef CONFIG_TE_SKIP_DSLEEP_WAKEUP_CALIBRATION -void slider_config_wakeup_calibration(te_slider_handle_t slider_handle, bool en) +void slider_enable_wakeup_calibration(te_slider_handle_t slider_handle, bool en) { for (int idx = 0; idx < slider_handle->channel_sum; ++idx) { - slider_handle->device[idx]->is_use_last_threshold = en; + slider_handle->device[idx]->is_use_last_threshold = !en; } } -#endif /** * @brief Slider process diff --git a/docs/en/api-reference/peripherals/touch_element.rst b/docs/en/api-reference/peripherals/touch_element.rst index 869f950c0d..86797227c0 100644 --- a/docs/en/api-reference/peripherals/touch_element.rst +++ b/docs/en/api-reference/peripherals/touch_element.rst @@ -355,6 +355,34 @@ In code, the waterproof configuration may look like as follows: ... } +Wakeup from Light/Deep Sleep +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Only Touch Button can be configured as wake up source. + +Light or deep sleep are both supported to be waken up by touch sensor. For the light sleep, any installed touch button can wake it up. But only the sleep button can wake up from deep sleep, and the touch sensor will do a calibration immediately, the reference value will be calibrated to a wrong value if our finger doesn't remove timely. Though the wrong reference value will recover after the finger remove away and have no affect to the driver logic, if you don't want to see a wrong reference value while waking up from deep sleep, you can call :cpp:func:`touch_element_sleep_enable_wakeup_calibration` to disable the wakeup calibration. + +The Touch Element Wakeup example is available in `example/system/light_sleep` directory. + +.. code-block:: c + + void app_main() + { + ... + touch_element_install(); + touch_button_install(); //Initialize the touch button + touch_button_create(&element_handle); //Create a new Touch element + + ... + + // ESP_ERROR_CHECK(touch_element_enable_light_sleep(&sleep_config)); + ESP_ERROR_CHECK(touch_element_enable_deep_sleep(button_handle[0], &sleep_config)); + // ESP_ERROR_CHECK(touch_element_sleep_enable_wakeup_calibration(button_handle[0], false)); // (optional) Disable wakeup calibration to prevent updating the base line to a wrong value + + touch_element_start(); + + ... + } Application Example ------------------- diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/CMakeLists.txt deleted file mode 100644 index 3c81ebef01..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# For more information about build system see -# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html -# The following five lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(touch_elem_auto_sleep) diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/CMakeLists.txt deleted file mode 100644 index b5b2d66b77..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -idf_component_register(SRCS "example_main.c" - INCLUDE_DIRS ".") diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/Kconfig.projbuild b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/Kconfig.projbuild deleted file mode 100644 index c30707c9ab..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/Kconfig.projbuild +++ /dev/null @@ -1,17 +0,0 @@ -menu "Example Configuration" - - config TE_PM_ENABLE - bool "Touch Element power management" - default y - - config TE_MAX_CPU_FREQ - int "Touch Element sleep DFS maximum cpu frequency" - depends on PM_DFS_INIT_AUTO - default ESP32S2_DEFAULT_CPU_FREQ_MHZ - - config TE_MIN_CPU_FREQ - int "Touch Element sleep DFS minimum cpu frequency" - depends on PM_DFS_INIT_AUTO - default 10 - -endmenu diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/CMakeLists.txt deleted file mode 100644 index 3be0e36916..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# For more information about build system see -# https://docs.espressif.com/projects/esp-idf/en/latest/api-guides/build-system.html -# The following five lines of boilerplate have to be in your project's -# CMakeLists in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) - -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -project(touch_elem_deep_sleep) diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/CMakeLists.txt b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/CMakeLists.txt deleted file mode 100644 index d7a6bf5299..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/CMakeLists.txt +++ /dev/null @@ -1,2 +0,0 @@ -idf_component_register(SRCS "touch_elem_deep_sleep.c" - INCLUDE_DIRS ".") diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/Kconfig.projbuild b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/Kconfig.projbuild deleted file mode 100644 index c0558306d4..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/Kconfig.projbuild +++ /dev/null @@ -1,69 +0,0 @@ -menu "Example Configuration" - - choice - - prompt "Select a button as Deep Sleep wakeup source" - default TE_WAKEUP_USE_BUTTON_1 - - config TE_WAKEUP_USE_BUTTON_1 - bool "Button 1" - - config TE_WAKEUP_USE_BUTTON_2 - bool "Button 2" - - config TE_WAKEUP_USE_BUTTON_3 - bool "Button 3" - - config TE_WAKEUP_USE_BUTTON_4 - bool "Button 4" - - config TE_WAKEUP_USE_BUTTON_5 - bool "Button 5" - - endchoice - - config TE_WAKEUP_BUTTON_INDEX - int - default 0 if TE_WAKEUP_USE_BUTTON_1 - default 1 if TE_WAKEUP_USE_BUTTON_2 - default 2 if TE_WAKEUP_USE_BUTTON_3 - default 3 if TE_WAKEUP_USE_BUTTON_4 - default 4 if TE_WAKEUP_USE_BUTTON_5 - - choice - - prompt "Select a button as Deep Sleep entry" - default TE_ENTRY_USE_BUTTON_1 - - config TE_ENTRY_USE_BUTTON_1 - bool "Button 1" - - config TE_ENTRY_USE_BUTTON_2 - bool "Button 2" - - config TE_ENTRY_USE_BUTTON_3 - bool "Button 3" - - config TE_ENTRY_USE_BUTTON_4 - bool "Button 4" - - config TE_ENTRY_USE_BUTTON_5 - bool "Button 5" - - endchoice - - config TE_ENTRY_BUTTON_INDEX - int - default 0 if TE_ENTRY_USE_BUTTON_1 - default 1 if TE_ENTRY_USE_BUTTON_2 - default 2 if TE_ENTRY_USE_BUTTON_3 - default 3 if TE_ENTRY_USE_BUTTON_4 - default 4 if TE_ENTRY_USE_BUTTON_5 - - config TE_SKIP_CALIBRATION - bool "Touch Element skip wakeup calibration" - depends on TE_SKIP_DSLEEP_WAKEUP_CALIBRATION - default y - - -endmenu diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/touch_elem_deep_sleep.c b/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/touch_elem_deep_sleep.c deleted file mode 100644 index 714f1c7c4e..0000000000 --- a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_deep_sleep/main/touch_elem_deep_sleep.c +++ /dev/null @@ -1,130 +0,0 @@ -/* Touch Sensor - Example - - For other examples please check: - https://github.com/espressif/esp-idf/tree/master/examples - - See README.md file to get detailed usage of this example. - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ - -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "esp_log.h" -#include "esp_sleep.h" -#include "touch_element/touch_button.h" - -static const char *TAG = "Touch Button Example"; - -#define TOUCH_BUTTON_NUM 5 - -/* Touch buttons handle */ -static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM]; -static touch_button_handle_t wakeup_button_handle = NULL; -static touch_button_handle_t entry_button_handle = NULL; - - -/* Touch buttons channel array */ -static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = { - TOUCH_PAD_NUM1, - TOUCH_PAD_NUM2, - TOUCH_PAD_NUM3, - TOUCH_PAD_NUM4, - TOUCH_PAD_NUM5, -}; - -/* Touch buttons channel sensitivity array */ -static const float channel_sens_array[TOUCH_BUTTON_NUM] = { - 0.03F, - 0.03F, - 0.03F, - 0.03F, - 0.03F, -}; - -/* Button event handler task */ -static void button_handler_task(void *arg) -{ - (void) arg; //Unused - touch_elem_message_t element_message; - while (1) { - /* Waiting for touch element messages */ - touch_element_message_receive(&element_message, portMAX_DELAY); - if (element_message.element_type != TOUCH_ELEM_TYPE_BUTTON) { - continue; - } - /* Decode message */ - const touch_button_message_t *button_message = touch_button_get_message(&element_message); - - if (element_message.handle == entry_button_handle) { - if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) { - ESP_LOGI(TAG, "Entering Deep sleep ..."); - fflush(stdout); - esp_deep_sleep_start(); - } - } - - if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) { - ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg); - } else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) { - ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg); - } else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) { - ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg); - } - } -} - -void app_main(void) -{ - esp_sleep_wakeup_cause_t wakeup_ret = esp_sleep_get_wakeup_cause(); - printf("------%d\n", wakeup_ret); - /* Initialize Touch Element library */ - touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG(); - ESP_ERROR_CHECK(touch_element_install(&global_config)); - ESP_LOGI(TAG, "Touch element library installed"); - - touch_button_global_config_t button_global_config = TOUCH_BUTTON_GLOBAL_DEFAULT_CONFIG(); - ESP_ERROR_CHECK(touch_button_install(&button_global_config)); - ESP_LOGI(TAG, "Touch button installed"); - for (int i = 0; i < TOUCH_BUTTON_NUM; i++) { - touch_button_config_t button_config = { - .channel_num = channel_array[i], - .channel_sens = channel_sens_array[i] - }; - /* Create Touch buttons */ - ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i])); - /* Subscribe touch button events (On Press, On Release, On LongPress) */ - if (i == CONFIG_TE_WAKEUP_BUTTON_INDEX || i == CONFIG_TE_ENTRY_BUTTON_INDEX) { - continue; - } else { - ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE, (void *)channel_array[i])); - } - - /* Set EVENT as the dispatch method */ - ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT)); - } - wakeup_button_handle = button_handle[CONFIG_TE_WAKEUP_BUTTON_INDEX]; - entry_button_handle = button_handle[CONFIG_TE_ENTRY_BUTTON_INDEX]; - ESP_ERROR_CHECK(touch_button_subscribe_event(entry_button_handle, TOUCH_ELEM_EVENT_ON_PRESS, (void *)channel_array[CONFIG_TE_ENTRY_BUTTON_INDEX])); - ESP_ERROR_CHECK(touch_button_set_dispatch_method(entry_button_handle, TOUCH_ELEM_DISP_EVENT)); - ESP_LOGI(TAG, "Touch buttons created"); - - touch_elem_sleep_config_t sleep_config = { - .scan_time = global_config.hardware.sample_count, - .sleep_time = global_config.hardware.sleep_cycle, - }; - ESP_ERROR_CHECK(touch_element_sleep_install(&sleep_config)); - ESP_ERROR_CHECK(touch_element_sleep_add_wakeup(wakeup_button_handle)); -#ifdef CONFIG_TE_SKIP_CALIBRATION - ESP_ERROR_CHECK(touch_element_sleep_config_wakeup_calibration(wakeup_button_handle, true)); -#endif - - touch_element_start(); - ESP_LOGI(TAG, "Touch element library start"); - xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL); - ESP_LOGI(TAG, "Press Button[%d] to enter sleep and press Button[%d] to wakeup system", channel_array[CONFIG_TE_ENTRY_BUTTON_INDEX], channel_array[CONFIG_TE_WAKEUP_BUTTON_INDEX]); -} diff --git a/examples/peripherals/touch_sensor/touch_sensor_v2/touch_pad_interrupt/main/tp_interrupt_main.c b/examples/peripherals/touch_sensor/touch_sensor_v2/touch_pad_interrupt/main/tp_interrupt_main.c index 20271deafe..1285558143 100644 --- a/examples/peripherals/touch_sensor/touch_sensor_v2/touch_pad_interrupt/main/tp_interrupt_main.c +++ b/examples/peripherals/touch_sensor/touch_sensor_v2/touch_pad_interrupt/main/tp_interrupt_main.c @@ -144,8 +144,6 @@ static void tp_example_read_task(void *pvParameter) } } -#include "esp_sleep.h" -#include "hal/touch_sensor_ll.h" void app_main(void) { if (que_touch == NULL) { @@ -211,11 +209,4 @@ void app_main(void) // Start a task to show what pads have been touched xTaskCreate(&tp_example_read_task, "touch_pad_read_task", 4096, NULL, 5, NULL); - - touch_ll_sleep_low_power(true); - while (1) { - esp_sleep_enable_timer_wakeup(100 * 1000); - esp_light_sleep_start(); - vTaskDelay(pdMS_TO_TICKS(100)); - } } diff --git a/examples/system/light_sleep/README.md b/examples/system/light_sleep/README.md index 1a24c4ebce..70ada8ab88 100644 --- a/examples/system/light_sleep/README.md +++ b/examples/system/light_sleep/README.md @@ -61,6 +61,14 @@ Note #2: only UART0 and UART1 (if has) are supported to be configured as wake up Note #3: due to limitation of the HW, the bytes that received during light sleep is only used for waking up, and it will not be received by UART peripheral or passed to the driver. +### Wake-up by Touch Pad + +For this example, pressing any registered touch buttons can wake up the chip. + +Note #1: For light sleep, all registered touch buttons can wake up the chip. But only the channel which is configured as wake up channel can wake up the chip from deep sleep. + +Note #2: Waking-up by touch pad relies on 'touch_element' driver, which can only support ESP32-S2 and ESP32-S3 currently. + ``` Entering light sleep Returned from light sleep, reason: timer, t=2713 ms, slept for 1999 ms @@ -83,6 +91,11 @@ Entering light sleep Returned from light sleep, reason: pin, t=12564 ms, slept for 1 ms Waiting for GPIO9 to go high... Entering light sleep +... +I (361) touch_wakeup: Button[1] Press +Returned from light sleep, reason: touch, t=14471 ms, slept for 467 ms +Entering light sleep + ... ``` diff --git a/examples/system/light_sleep/main/CMakeLists.txt b/examples/system/light_sleep/main/CMakeLists.txt index d5b05c298c..25d2112b95 100644 --- a/examples/system/light_sleep/main/CMakeLists.txt +++ b/examples/system/light_sleep/main/CMakeLists.txt @@ -3,5 +3,11 @@ set(srcs "light_sleep_example_main.c" "timer_wakeup.c" "uart_wakeup.c") +set(TOUCH_ELEMENT_COMPATIBLE_TARGETS "esp32s2" "esp32s3") + +if(IDF_TARGET IN_LIST TOUCH_ELEMENT_COMPATIBLE_TARGETS) + list(APPEND srcs "touch_wakeup.c") +endif() + idf_component_register(SRCS ${srcs} INCLUDE_DIRS ".") diff --git a/examples/system/light_sleep/main/light_sleep_example.h b/examples/system/light_sleep/main/light_sleep_example.h index b3faf43f57..c3401c4aed 100644 --- a/examples/system/light_sleep/main/light_sleep_example.h +++ b/examples/system/light_sleep/main/light_sleep_example.h @@ -18,6 +18,10 @@ esp_err_t example_register_timer_wakeup(void); esp_err_t example_register_uart_wakeup(void); +#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 +void example_register_touch_wakeup(void); +#endif + #ifdef __cplusplus } #endif diff --git a/examples/system/light_sleep/main/light_sleep_example_main.c b/examples/system/light_sleep/main/light_sleep_example_main.c index fccb36883f..4beb322e20 100644 --- a/examples/system/light_sleep/main/light_sleep_example_main.c +++ b/examples/system/light_sleep/main/light_sleep_example_main.c @@ -50,6 +50,11 @@ static void light_sleep_task(void *args) * Otherwise the chip may fall sleep again before running uart task */ vTaskDelay(1); break; +#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + case ESP_SLEEP_WAKEUP_TOUCHPAD: + wakeup_reason = "touch"; + break; +#endif default: wakeup_reason = "other"; break; @@ -72,6 +77,10 @@ void app_main(void) example_register_timer_wakeup(); /* Enable wakeup from light sleep by uart */ example_register_uart_wakeup(); +#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 + /* Enable wakeup from light sleep by touch element */ + example_register_touch_wakeup(); +#endif xTaskCreate(light_sleep_task, "light_sleep_task", 4096, NULL, 6, NULL); } diff --git a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/example_main.c b/examples/system/light_sleep/main/touch_wakeup.c similarity index 58% rename from examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/example_main.c rename to examples/system/light_sleep/main/touch_wakeup.c index 1e04204aca..92d0668c10 100644 --- a/examples/peripherals/touch_element/touch_element_sleep/touch_elem_auto_sleep/main/example_main.c +++ b/examples/system/light_sleep/main/touch_wakeup.c @@ -1,30 +1,22 @@ -/* Touch Sensor - Example - - For other examples please check: - https://github.com/espressif/esp-idf/tree/master/examples - - See README.md file to get detailed usage of this example. - - This example code is in the Public Domain (or CC0 licensed, at your option.) - - Unless required by applicable law or agreed to in writing, this - software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - CONDITIONS OF ANY KIND, either express or implied. -*/ - +/* + * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" -#include "esp_pm.h" +#include "esp_sleep.h" #include "touch_element/touch_button.h" -static const char *TAG = "Touch elem auto sleep"; +static const char *TAG = "touch_wakeup"; -#define TOUCH_BUTTON_NUM 5 +#define TOUCH_BUTTON_NUM 5 /* Touch buttons handle */ static touch_button_handle_t button_handle[TOUCH_BUTTON_NUM]; + /* Touch buttons channel array */ static const touch_pad_t channel_array[TOUCH_BUTTON_NUM] = { TOUCH_PAD_NUM1, @@ -57,32 +49,18 @@ static void button_handler_task(void *arg) /* Decode message */ const touch_button_message_t *button_message = touch_button_get_message(&element_message); if (button_message->event == TOUCH_BUTTON_EVT_ON_PRESS) { - ESP_LOGI(TAG, "Button[%d] Press", (uint32_t)element_message.arg); + ESP_LOGI(TAG, "Button[%"PRIu32"] Press", (uint32_t)element_message.arg); } else if (button_message->event == TOUCH_BUTTON_EVT_ON_RELEASE) { - ESP_LOGI(TAG, "Button[%d] Release", (uint32_t)element_message.arg); + ESP_LOGI(TAG, "Button[%"PRIu32"] Release", (uint32_t)element_message.arg); } else if (button_message->event == TOUCH_BUTTON_EVT_ON_LONGPRESS) { - ESP_LOGI(TAG, "Button[%d] LongPress", (uint32_t)element_message.arg); + ESP_LOGI(TAG, "Button[%"PRIu32"] LongPress", (uint32_t)element_message.arg); } } + vTaskDelete(NULL); } - -void app_main(void) +esp_err_t example_register_touch_wakeup(void) { -#ifdef CONFIG_PM_ENABLE //System power management - esp_pm_config_esp32s2_t pm_config = { -#ifdef CONFIG_PM_DFS_INIT_AUTO //Power management dynamic frequency scaling - .max_freq_mhz = CONFIG_TE_MAX_CPU_FREQ, - .min_freq_mhz = CONFIG_TE_MIN_CPU_FREQ, -#endif -#ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE //FreeRTOS tickless - .light_sleep_enable = true -#else - .light_sleep_enable = false -#endif - }; - ESP_ERROR_CHECK( esp_pm_configure(&pm_config)); -#endif /* Initialize Touch Element library */ touch_elem_global_config_t global_config = TOUCH_ELEM_GLOBAL_DEFAULT_CONFIG(); ESP_ERROR_CHECK(touch_element_install(&global_config)); @@ -98,26 +76,26 @@ void app_main(void) }; /* Create Touch buttons */ ESP_ERROR_CHECK(touch_button_create(&button_config, &button_handle[i])); - /* Subscribe touch button events (On Press, On Release, On LongPress) */ - ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], TOUCH_ELEM_EVENT_ON_PRESS | TOUCH_ELEM_EVENT_ON_RELEASE | TOUCH_ELEM_EVENT_ON_LONGPRESS, - (void *)channel_array[i])); /* Set EVENT as the dispatch method */ ESP_ERROR_CHECK(touch_button_set_dispatch_method(button_handle[i], TOUCH_ELEM_DISP_EVENT)); - /* Set LongPress event trigger threshold time */ - ESP_ERROR_CHECK(touch_button_set_longpress(button_handle[i], 1000)); + /* Subscribe touch button events (On Press, On Release, On LongPress) */ + ESP_ERROR_CHECK(touch_button_subscribe_event(button_handle[i], + TOUCH_ELEM_EVENT_ON_PRESS | + TOUCH_ELEM_EVENT_ON_RELEASE | + TOUCH_ELEM_EVENT_ON_LONGPRESS, + (void *)channel_array[i])); } ESP_LOGI(TAG, "Touch buttons created"); -#ifdef CONFIG_TE_PM_ENABLE touch_elem_sleep_config_t sleep_config = { - .scan_time = global_config.hardware.sample_count, - .sleep_time = global_config.hardware.sleep_cycle, + .sample_count = global_config.hardware.sample_count, + .sleep_cycle = global_config.hardware.sleep_cycle, }; - ESP_ERROR_CHECK(touch_element_sleep_install(&sleep_config)); -#endif + /* Enable one of registered touch button as light/deep sleep wake-up source */ + ESP_ERROR_CHECK(touch_element_enable_light_sleep(&sleep_config)); touch_element_start(); - ESP_LOGI(TAG, "Touch element library start"); - xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 5, NULL); - vTaskDelay(pdMS_TO_TICKS(1000)); + xTaskCreate(&button_handler_task, "button_handler_task", 4 * 1024, NULL, 6, NULL); + ESP_LOGI(TAG, "touch wakeup source is ready"); + return ESP_OK; }