Skip to content

Commit 7a76ade

Browse files
authored
Add touch sleep wakeup API (espressif#7439)
1 parent bd71449 commit 7a76ade

File tree

4 files changed

+85
-24
lines changed

4 files changed

+85
-24
lines changed

Diff for: cores/esp32/esp32-hal-touch.c

+23
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ static touch_value_t __touchRead(uint8_t pin)
186186
{
187187
int8_t pad = digitalPinToTouchChannel(pin);
188188
if(pad < 0){
189+
log_e(" No touch pad on selected pin!");
189190
return 0;
190191
}
191192

@@ -202,6 +203,7 @@ static void __touchConfigInterrupt(uint8_t pin, void (*userFunc)(void), void *Ar
202203
{
203204
int8_t pad = digitalPinToTouchChannel(pin);
204205
if(pad < 0){
206+
log_e(" No touch pad on selected pin!");
205207
return;
206208
}
207209

@@ -264,6 +266,27 @@ bool touchInterruptGetLastStatus(uint8_t pin) {
264266
}
265267
#endif
266268

269+
void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold)
270+
{
271+
int8_t pad = digitalPinToTouchChannel(pin);
272+
if(pad < 0){
273+
log_e(" No touch pad on selected pin!");
274+
return;
275+
}
276+
__touchInit();
277+
__touchChannelInit(pad);
278+
279+
#if SOC_TOUCH_VERSION_1 // Only for ESP32 SoC
280+
touch_pad_set_thresh(pad, threshold);
281+
282+
#elif SOC_TOUCH_VERSION_2
283+
touch_pad_sleep_channel_enable(pad, true);
284+
touch_pad_sleep_set_threshold(pad, threshold);
285+
286+
#endif
287+
esp_sleep_enable_touchpad_wakeup();
288+
}
289+
267290
extern touch_value_t touchRead(uint8_t) __attribute__ ((weak, alias("__touchRead")));
268291
extern void touchAttachInterrupt(uint8_t, voidFuncPtr, touch_value_t) __attribute__ ((weak, alias("__touchAttachInterrupt")));
269292
extern void touchAttachInterruptArg(uint8_t, voidArgFuncPtr, void *, touch_value_t) __attribute__ ((weak, alias("__touchAttachArgsInterrupt")));

Diff for: cores/esp32/esp32-hal-touch.h

+5
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ void touchInterruptSetThresholdDirection(bool mustbeLower);
8989
bool touchInterruptGetLastStatus(uint8_t pin);
9090
#endif
9191

92+
/*
93+
* Setup touch pad wake up from deep sleep with given threshold.
94+
**/
95+
void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold);
96+
9297
#endif // SOC_TOUCH_SENSOR_NUM > 0
9398

9499
#ifdef __cplusplus

Diff for: docs/source/api/touch.rst

+14
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ This function is used to detach interrupt from the touch pad.
8585
8686
* ``pin`` GPIO TOUCH pad pin.
8787

88+
touchSleepWakeUpEnable
89+
^^^^^^^^^^^^^^^^^^^^^^
90+
91+
This function is used to setup touch pad as the wake up source from the deep sleep.
92+
93+
.. note:: ESP32-S2 and ESP32-S3 only support one sleep wake up touch pad.
94+
95+
.. code-block:: arduino
96+
97+
void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold);
98+
99+
* ``pin`` GPIO TOUCH pad pin
100+
* ``threshold`` Sets the threshold when to wake up
101+
88102
TOUCH API specific for ESP32 chip (TOUCH_V1)
89103
********************************************
90104

Diff for: libraries/ESP32/examples/DeepSleep/TouchWakeUp/TouchWakeUp.ino

+43-24
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ This code displays how to use deep sleep with
55
a touch as a wake up source and how to store data in
66
RTC memory to use it over reboots
77
8+
ESP32 can have multiple touch pads enabled as wakeup source
9+
ESP32-S2 and ESP32-S3 supports only 1 touch pad as wakeup source enabled
10+
811
This code is under Public Domain License.
912
1013
Author:
1114
Pranav Cherukupalli <cherukupallip@gmail.com>
1215
*/
1316

14-
#define Threshold 40 /* Greater the value, more the sensitivity */
17+
#if CONFIG_IDF_TARGET_ESP32
18+
#define THRESHOLD 40 /* Greater the value, more the sensitivity */
19+
#else //ESP32-S2 and ESP32-S3 + default for other chips (to be adjusted) */
20+
#define THRESHOLD 5000 /* Lower the value, more the sensitivity */
21+
#endif
1522

1623
RTC_DATA_ATTR int bootCount = 0;
1724
touch_pad_t touchPin;
@@ -42,24 +49,31 @@ has been awaken from sleep
4249
void print_wakeup_touchpad(){
4350
touchPin = esp_sleep_get_touchpad_wakeup_status();
4451

45-
switch(touchPin)
46-
{
47-
case 0 : Serial.println("Touch detected on GPIO 4"); break;
48-
case 1 : Serial.println("Touch detected on GPIO 0"); break;
49-
case 2 : Serial.println("Touch detected on GPIO 2"); break;
50-
case 3 : Serial.println("Touch detected on GPIO 15"); break;
51-
case 4 : Serial.println("Touch detected on GPIO 13"); break;
52-
case 5 : Serial.println("Touch detected on GPIO 12"); break;
53-
case 6 : Serial.println("Touch detected on GPIO 14"); break;
54-
case 7 : Serial.println("Touch detected on GPIO 27"); break;
55-
case 8 : Serial.println("Touch detected on GPIO 33"); break;
56-
case 9 : Serial.println("Touch detected on GPIO 32"); break;
57-
default : Serial.println("Wakeup not by touchpad"); break;
58-
}
59-
}
60-
61-
void callback(){
62-
//placeholder callback function
52+
#if CONFIG_IDF_TARGET_ESP32
53+
switch(touchPin)
54+
{
55+
case 0 : Serial.println("Touch detected on GPIO 4"); break;
56+
case 1 : Serial.println("Touch detected on GPIO 0"); break;
57+
case 2 : Serial.println("Touch detected on GPIO 2"); break;
58+
case 3 : Serial.println("Touch detected on GPIO 15"); break;
59+
case 4 : Serial.println("Touch detected on GPIO 13"); break;
60+
case 5 : Serial.println("Touch detected on GPIO 12"); break;
61+
case 6 : Serial.println("Touch detected on GPIO 14"); break;
62+
case 7 : Serial.println("Touch detected on GPIO 27"); break;
63+
case 8 : Serial.println("Touch detected on GPIO 33"); break;
64+
case 9 : Serial.println("Touch detected on GPIO 32"); break;
65+
default : Serial.println("Wakeup not by touchpad"); break;
66+
}
67+
#else
68+
if(touchPin < TOUCH_PAD_MAX)
69+
{
70+
Serial.printf("Touch detected on GPIO %d\n", touchPin);
71+
}
72+
else
73+
{
74+
Serial.println("Wakeup not by touchpad");
75+
}
76+
#endif
6377
}
6478

6579
void setup(){
@@ -74,11 +88,16 @@ void setup(){
7488
print_wakeup_reason();
7589
print_wakeup_touchpad();
7690

77-
//Setup interrupt on Touch Pad 3 (GPIO15)
78-
touchAttachInterrupt(T3, callback, Threshold);
91+
#if CONFIG_IDF_TARGET_ESP32
92+
//Setup sleep wakeup on Touch Pad 3 + 7 (GPIO15 + GPIO 27)
93+
touchSleepWakeUpEnable(T3,THRESHOLD);
94+
touchSleepWakeUpEnable(T7,THRESHOLD);
95+
96+
#else //ESP32-S2 + ESP32-S3
97+
//Setup sleep wakeup on Touch Pad 3 (GPIO3)
98+
touchSleepWakeUpEnable(T3,THRESHOLD);
7999

80-
//Configure Touchpad as wakeup source
81-
esp_sleep_enable_touchpad_wakeup();
100+
#endif
82101

83102
//Go to sleep now
84103
Serial.println("Going to sleep now");
@@ -88,4 +107,4 @@ void setup(){
88107

89108
void loop(){
90109
//This will never be reached
91-
}
110+
}

0 commit comments

Comments
 (0)