Skip to content

Commit 7205a66

Browse files
committed
esp32/modmachine: Return GPIOs causing wakeup - wake_ext1_pins().
1 parent 9e6885a commit 7205a66

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

docs/library/machine.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ Power related functions
166166

167167
Availability: ESP32, WiPy.
168168

169+
.. function:: wake_ext1_pins()
170+
171+
Returns the GPIO pin numbers of those pins which caused wakeup from deep sleep as a
172+
tuple of integers.
173+
174+
This is a direct interface to the ESP-IDF `esp_sleep_get_ext1_wakeup_status()` function.
175+
176+
Availability: ESP32 (except ESP32C3).
177+
169178
Miscellaneous functions
170179
-----------------------
171180

ports/esp32/modmachine.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,33 @@ STATIC mp_obj_t machine_wake_reason(size_t n_args, const mp_obj_t *pos_args, mp_
231231
}
232232
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_wake_reason_obj, 0, machine_wake_reason);
233233

234+
#if !CONFIG_IDF_TARGET_ESP32C3
235+
STATIC mp_obj_t machine_wake_ext1_pins(void) {
236+
uint64_t status = esp_sleep_get_ext1_wakeup_status();
237+
int len, index;
238+
mp_obj_t *tuple = NULL;
239+
240+
// Only a few (~8) GPIOs might cause EXT1 wakeup.
241+
// Therefore, we don't allocate 64*4 = 256 bytes on the stack and calculate the
242+
// required space in a first pass.
243+
for (index = 0, len = 0; index < 64; index++) {
244+
len += (status & ((uint64_t)1 << index)) ? 1 : 0;
245+
}
246+
if (len) {
247+
tuple = alloca(len * sizeof(*tuple));
248+
249+
for (index = 0, len = 0; index < 64; index++) {
250+
if (status & ((uint64_t)1 << index)) {
251+
tuple[len++] = MP_OBJ_NEW_SMALL_INT(index);
252+
}
253+
}
254+
}
255+
return mp_obj_new_tuple(len, tuple);
256+
}
257+
258+
STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_wake_ext1_pins_obj, machine_wake_ext1_pins);
259+
#endif
260+
234261
STATIC mp_obj_t machine_reset(void) {
235262
esp_restart();
236263
return mp_const_none;
@@ -344,6 +371,10 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
344371
{ MP_ROM_QSTR(MP_QSTR_TIMER_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_TIMER) },
345372
{ MP_ROM_QSTR(MP_QSTR_TOUCHPAD_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_TOUCHPAD) },
346373
{ MP_ROM_QSTR(MP_QSTR_ULP_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_ULP) },
374+
#if !CONFIG_IDF_TARGET_ESP32C3
375+
// ext1 wakeup pins
376+
{ MP_ROM_QSTR(MP_QSTR_wake_ext1_pins), MP_ROM_PTR(&machine_wake_ext1_pins_obj) },
377+
#endif
347378
};
348379

349380
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy