From d8a1e3ea2b2a843d59e45befb94a09dd82e73e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 7 Nov 2022 14:02:41 +0100 Subject: [PATCH 1/6] touch_sleep_wakeup_api --- cores/esp32/esp32-hal-touch.c | 23 +++++++++++++++++++++++ cores/esp32/esp32-hal-touch.h | 5 +++++ 2 files changed, 28 insertions(+) diff --git a/cores/esp32/esp32-hal-touch.c b/cores/esp32/esp32-hal-touch.c index 7360e77a67d..476df60850c 100644 --- a/cores/esp32/esp32-hal-touch.c +++ b/cores/esp32/esp32-hal-touch.c @@ -186,6 +186,7 @@ static touch_value_t __touchRead(uint8_t pin) { int8_t pad = digitalPinToTouchChannel(pin); if(pad < 0){ + log_e(" No touch pad on selected pin!"); return 0; } @@ -202,6 +203,7 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar { int8_t pad = digitalPinToTouchChannel(pin); if(pad < 0){ + log_e(" No touch pad on selected pin!"); return; } @@ -264,6 +266,27 @@ bool touchInterruptGetLastStatus(uint8_t pin) { } #endif +void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold) +{ + int8_t pad = digitalPinToTouchChannel(pin); + if(pad < 0){ + log_e(" No touch pad on selected pin!"); + return; + } + __touchInit(); + __touchChannelInit(pad); + + #if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC + touch_pad_set_thresh(pad, threshold); + + #elif SOC_TOUCH_VERSION_2 + touch_pad_sleep_channel_enable(pad, true); + touch_pad_sleep_set_threshold(pad, threshold); + + #endif + esp_sleep_enable_touchpad_wakeup(); +} + extern touch_value_t touchRead(uint8_t) __attribute__ ((weak, alias("__touchRead"))); extern void touchAttachInterrupt(uint8_t, voidFuncPtr, touch_value_t) __attribute__ ((weak, alias("__touchAttachInterrupt"))); extern void touchAttachInterruptArg(uint8_t, voidArgFuncPtr, void *, touch_value_t) __attribute__ ((weak, alias("__touchAttachArgsInterrupt"))); diff --git a/cores/esp32/esp32-hal-touch.h b/cores/esp32/esp32-hal-touch.h index e62a4166206..305a8b49f8b 100644 --- a/cores/esp32/esp32-hal-touch.h +++ b/cores/esp32/esp32-hal-touch.h @@ -89,6 +89,11 @@ void touchInterruptSetThresholdDirection(bool mustbeLower); bool touchInterruptGetLastStatus(uint8_t pin); #endif +/* + * Setup touch pad wake up from deep sleep with given threshold. +**/ +void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold); + #endif // SOC_TOUCH_SENSOR_NUM > 0 #ifdef __cplusplus From 2f33ba8f87f816cda7aa902c7063dcae598071dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 7 Nov 2022 14:02:55 +0100 Subject: [PATCH 2/6] update example --- .../DeepSleep/TouchWakeUp/TouchWakeUp.ino | 60 +++++++++++-------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino b/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino index 76f48b757a4..103546d3c7f 100644 --- a/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino +++ b/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino @@ -11,7 +11,13 @@ Author: Pranav Cherukupalli */ -#define Threshold 40 /* Greater the value, more the sensitivity */ +#if CONFIG_IDF_TARGET_ESP32 + #define THRESHOLD 40 /* Greater the value, more the sensitivity */ +#elif CONFIG_IDF_TARGET_ESP32S2 + #define THRESHOLD 30000 /* Lower the value, more the sensitivity */ +#else //CONFIG_IDF_TARGET_ESP32S3 + default for other chips (to be adjusted) */ + #define THRESHOLD 80000 /* Lower the value, more the sensitivity */ +#endif RTC_DATA_ATTR int bootCount = 0; touch_pad_t touchPin; @@ -42,24 +48,31 @@ has been awaken from sleep void print_wakeup_touchpad(){ touchPin = esp_sleep_get_touchpad_wakeup_status(); - switch(touchPin) - { - case 0 : Serial.println("Touch detected on GPIO 4"); break; - case 1 : Serial.println("Touch detected on GPIO 0"); break; - case 2 : Serial.println("Touch detected on GPIO 2"); break; - case 3 : Serial.println("Touch detected on GPIO 15"); break; - case 4 : Serial.println("Touch detected on GPIO 13"); break; - case 5 : Serial.println("Touch detected on GPIO 12"); break; - case 6 : Serial.println("Touch detected on GPIO 14"); break; - case 7 : Serial.println("Touch detected on GPIO 27"); break; - case 8 : Serial.println("Touch detected on GPIO 33"); break; - case 9 : Serial.println("Touch detected on GPIO 32"); break; - default : Serial.println("Wakeup not by touchpad"); break; - } -} - -void callback(){ - //placeholder callback function + #if CONFIG_IDF_TARGET_ESP32 + switch(touchPin) + { + case 0 : Serial.println("Touch detected on GPIO 4"); break; + case 1 : Serial.println("Touch detected on GPIO 0"); break; + case 2 : Serial.println("Touch detected on GPIO 2"); break; + case 3 : Serial.println("Touch detected on GPIO 15"); break; + case 4 : Serial.println("Touch detected on GPIO 13"); break; + case 5 : Serial.println("Touch detected on GPIO 12"); break; + case 6 : Serial.println("Touch detected on GPIO 14"); break; + case 7 : Serial.println("Touch detected on GPIO 27"); break; + case 8 : Serial.println("Touch detected on GPIO 33"); break; + case 9 : Serial.println("Touch detected on GPIO 32"); break; + default : Serial.println("Wakeup not by touchpad"); break; + } + #else + if(touchPin < TOUCH_PAD_MAX) + { + Serial.printf("Touch detected on GPIO %d\n", touchPin); + } + else + { + Serial.println("Wakeup not by touchpad"); + } + #endif } void setup(){ @@ -74,11 +87,8 @@ void setup(){ print_wakeup_reason(); print_wakeup_touchpad(); - //Setup interrupt on Touch Pad 3 (GPIO15) - touchAttachInterrupt(T3, callback, Threshold); - - //Configure Touchpad as wakeup source - esp_sleep_enable_touchpad_wakeup(); + //Setup sleep wakeup on Touch Pad 3 (GPIO15 for ESP32) / (GPIO3 for ESP32-S2 and S3) + touchSleepWakeUpEnable(T3,THRESHOLD); //Go to sleep now Serial.println("Going to sleep now"); @@ -88,4 +98,4 @@ void setup(){ void loop(){ //This will never be reached -} +} \ No newline at end of file From d0a7b2657e8d3121ea8bb6d1cd0ee434470b01b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Mon, 7 Nov 2022 14:43:17 +0100 Subject: [PATCH 3/6] update touch docs --- cores/esp32/esp32-hal-touch.h | 2 +- docs/source/api/touch.rst | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/cores/esp32/esp32-hal-touch.h b/cores/esp32/esp32-hal-touch.h index 305a8b49f8b..235b8f86d63 100644 --- a/cores/esp32/esp32-hal-touch.h +++ b/cores/esp32/esp32-hal-touch.h @@ -91,7 +91,7 @@ bool touchInterruptGetLastStatus(uint8_t pin); /* * Setup touch pad wake up from deep sleep with given threshold. -**/ + **/ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold); #endif // SOC_TOUCH_SENSOR_NUM > 0 diff --git a/docs/source/api/touch.rst b/docs/source/api/touch.rst index 75715ffed70..371f66dc1aa 100644 --- a/docs/source/api/touch.rst +++ b/docs/source/api/touch.rst @@ -85,6 +85,18 @@ This function is used to detach interrupt from the touch pad. * ``pin`` GPIO TOUCH pad pin. +touchSleepWakeUpEnable +^^^^^^^^^^^^^^^^^^^^^^ + +This function is used to setup touch pad as the wake up source from the deep sleep. + +.. code-block:: arduino + + void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold); + +* ``pin`` GPIO TOUCH pad pin +* ``threshold`` Sets the threshold when to wake up + TOUCH API specific for ESP32 chip (TOUCH_V1) ******************************************** From ecefe759b32d7a529947f8005e033d5984059029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 9 Nov 2022 10:50:17 +0100 Subject: [PATCH 4/6] add note to docs --- docs/source/api/touch.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/api/touch.rst b/docs/source/api/touch.rst index 371f66dc1aa..9dd23a8d017 100644 --- a/docs/source/api/touch.rst +++ b/docs/source/api/touch.rst @@ -90,6 +90,8 @@ touchSleepWakeUpEnable This function is used to setup touch pad as the wake up source from the deep sleep. +.. note:: ESP32-S2 and ESP32-S3 only support one sleep wake up touch pad. + .. code-block:: arduino void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold); From 5f908100eb34c70d2e3f399e9a9976286b0ecf1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 9 Nov 2022 13:41:13 +0100 Subject: [PATCH 5/6] added multiple touchpads + edit threshold --- .../DeepSleep/TouchWakeUp/TouchWakeUp.ino | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino b/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino index 103546d3c7f..812a9bf53b4 100644 --- a/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino +++ b/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino @@ -5,6 +5,9 @@ This code displays how to use deep sleep with a touch as a wake up source and how to store data in RTC memory to use it over reboots +ESP32 can have multiple touch pads enabled as wakeup source +ESP32-S2 and ESP32-S3 supports only 1 touch pad as wakeup source enabled + This code is under Public Domain License. Author: @@ -12,11 +15,9 @@ Pranav Cherukupalli */ #if CONFIG_IDF_TARGET_ESP32 - #define THRESHOLD 40 /* Greater the value, more the sensitivity */ -#elif CONFIG_IDF_TARGET_ESP32S2 - #define THRESHOLD 30000 /* Lower the value, more the sensitivity */ -#else //CONFIG_IDF_TARGET_ESP32S3 + default for other chips (to be adjusted) */ - #define THRESHOLD 80000 /* Lower the value, more the sensitivity */ + #define THRESHOLD 40 /* Greater the value, more the sensitivity */ +#else //ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted) */ + #define THRESHOLD 5000 /* Lower the value, more the sensitivity */ #endif RTC_DATA_ATTR int bootCount = 0; @@ -87,8 +88,14 @@ void setup(){ print_wakeup_reason(); print_wakeup_touchpad(); - //Setup sleep wakeup on Touch Pad 3 (GPIO15 for ESP32) / (GPIO3 for ESP32-S2 and S3) - touchSleepWakeUpEnable(T3,THRESHOLD); + #if CONFIG_IDF_TARGET_ESP32 + //Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27) + touchSleepWakeUpEnable(T3,THRESHOLD); + touchSleepWakeUpEnable(T7,THRESHOLD); + + #else //ESP32-S2 + ESP32-S3 + //Setup sleep wakeup on Touch Pad 3 (GPIO3) + touchSleepWakeUpEnable(T3,THRESHOLD); //Go to sleep now Serial.println("Going to sleep now"); From 3903189d80eee2948d6c6817b6aafc45113dd178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Procha=CC=81zka?= <90197375+P-R-O-C-H-Y@users.noreply.github.com> Date: Wed, 9 Nov 2022 13:54:13 +0100 Subject: [PATCH 6/6] fixed missing #endif --- libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino | 2 ++ 1 file changed, 2 insertions(+) diff --git a/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino b/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino index 812a9bf53b4..94f2dc735a2 100644 --- a/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino +++ b/libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino @@ -97,6 +97,8 @@ void setup(){ //Setup sleep wakeup on Touch Pad 3 (GPIO3) touchSleepWakeUpEnable(T3,THRESHOLD); + #endif + //Go to sleep now Serial.println("Going to sleep now"); esp_deep_sleep_start();