From 979233482c5ac026928c4eeca0fbe4800f188cd7 Mon Sep 17 00:00:00 2001 From: Thomas Wenrich Date: Sun, 14 Nov 2021 10:44:08 +0100 Subject: [PATCH] esp32/modmachine: Return GPIOs causing wakeup - wake_ext1_pins(). Signed-off-by: Thomas Wenrich --- docs/library/machine.rst | 9 +++++++++ ports/esp32/modmachine.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/docs/library/machine.rst b/docs/library/machine.rst index 3f5cd6f13c72b..db7ed9038ee78 100644 --- a/docs/library/machine.rst +++ b/docs/library/machine.rst @@ -166,6 +166,15 @@ Power related functions Availability: ESP32, WiPy. +.. function:: wake_ext1_pins() + + Returns the GPIO pin numbers of those pins which caused wakeup from deep sleep as a + tuple of integers. + + This is a direct interface to the ESP-IDF ``esp_sleep_get_ext1_wakeup_status()`` function. + + Availability: ESP32 (except ESP32C3). + Miscellaneous functions ----------------------- diff --git a/ports/esp32/modmachine.c b/ports/esp32/modmachine.c index 37d4f424d4216..1f8250ce41925 100644 --- a/ports/esp32/modmachine.c +++ b/ports/esp32/modmachine.c @@ -231,6 +231,33 @@ STATIC mp_obj_t machine_wake_reason(size_t n_args, const mp_obj_t *pos_args, mp_ } STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_wake_reason_obj, 0, machine_wake_reason); +#if !CONFIG_IDF_TARGET_ESP32C3 +STATIC mp_obj_t machine_wake_ext1_pins(void) { + uint64_t status = esp_sleep_get_ext1_wakeup_status(); + int len, index; + mp_obj_t *tuple = NULL; + + // Only a few (~8) GPIOs might cause EXT1 wakeup. + // Therefore, we don't allocate 64*4 = 256 bytes on the stack and calculate the + // required space in a first pass. + for (index = 0, len = 0; index < 64; index++) { + len += (status & ((uint64_t)1 << index)) ? 1 : 0; + } + if (len) { + tuple = alloca(len * sizeof(*tuple)); + + for (index = 0, len = 0; index < 64; index++) { + if (status & ((uint64_t)1 << index)) { + tuple[len++] = MP_OBJ_NEW_SMALL_INT(index); + } + } + } + return mp_obj_new_tuple(len, tuple); +} + +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_wake_ext1_pins_obj, machine_wake_ext1_pins); +#endif + STATIC mp_obj_t machine_reset(void) { esp_restart(); return mp_const_none; @@ -344,6 +371,10 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_TIMER_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_TIMER) }, { MP_ROM_QSTR(MP_QSTR_TOUCHPAD_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_TOUCHPAD) }, { MP_ROM_QSTR(MP_QSTR_ULP_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_ULP) }, + #if !CONFIG_IDF_TARGET_ESP32C3 + // ext1 wakeup pins + { MP_ROM_QSTR(MP_QSTR_wake_ext1_pins), MP_ROM_PTR(&machine_wake_ext1_pins_obj) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);