Skip to content

modmachine: Implemented GPIO wake-up for ESP32-C3. #9583

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ports/esp32/modesp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_touch_obj, esp32_wake_on_touch);

STATIC mp_obj_t esp32_wake_on_ext0(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {

#if CONFIG_IDF_TARGET_ESP32C3

mp_raise_ValueError(MP_ERROR_TEXT("not supported"));

#else

if (machine_rtc_config.wake_on_touch) {
mp_raise_ValueError(MP_ERROR_TEXT("no resources"));
}
Expand All @@ -91,6 +97,8 @@ STATIC mp_obj_t esp32_wake_on_ext0(size_t n_args, const mp_obj_t *pos_args, mp_m
machine_rtc_config.ext0_level = args[ARG_level].u_bool;
machine_rtc_config.ext0_wake_types = MACHINE_WAKE_SLEEP | MACHINE_WAKE_DEEPSLEEP;

#endif

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_ext0_obj, 0, esp32_wake_on_ext0);
Expand Down
2 changes: 1 addition & 1 deletion ports/esp32/modesp32.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef MICROPY_INCLUDED_ESP32_MODESP32_H
#define MICROPY_INCLUDED_ESP32_MODESP32_H

#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32C3

#define RTC_VALID_EXT_PINS \
( \
Expand Down
35 changes: 34 additions & 1 deletion ports/esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,36 @@ STATIC mp_obj_t machine_sleep_helper(wake_type_t wake_type, size_t n_args, const
esp_sleep_enable_timer_wakeup(((uint64_t)expiry) * 1000);
}

#if !CONFIG_IDF_TARGET_ESP32C3
#if CONFIG_IDF_TARGET_ESP32C3

if (machine_rtc_config.ext1_pins != 0) {
gpio_int_type_t intr_type = machine_rtc_config.ext1_level ? GPIO_INTR_HIGH_LEVEL : GPIO_INTR_LOW_LEVEL;

for (int i = 0; i < GPIO_NUM_MAX; ++i) {
gpio_num_t gpio = (gpio_num_t)i;
uint64_t bm = 1ULL << i;

if (machine_rtc_config.ext1_pins & bm) {
gpio_sleep_set_direction(gpio, GPIO_MODE_INPUT);

if (MACHINE_WAKE_SLEEP == wake_type) {
gpio_wakeup_enable(gpio, intr_type);
}
}
}

if (MACHINE_WAKE_DEEPSLEEP == wake_type) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we should use esp_sleep_is_valid_wakeup_gpio to check if pin used for deepsleep is rtc gpio pin(wake from deepsleep can only triggered from rtc gpio)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked ESPIDF code and esp_deep_sleep_enable_gpio_wakeup does this check internally as one of first operation.

However there are also some other operation which may also return failure and there is no diostinguish between failure type (it unfortunatelly returns always the same error code). This means that reason of failure can be caused also by other failure then use of incorrect wake-up GPIO and then the message about incorrect pin in exception can be missleading.

if (ESP_OK != esp_deep_sleep_enable_gpio_wakeup(
machine_rtc_config.ext1_pins,
machine_rtc_config.ext1_level ? ESP_GPIO_WAKEUP_GPIO_HIGH : ESP_GPIO_WAKEUP_GPIO_LOW)) {
mp_raise_ValueError(MP_ERROR_TEXT("wake-up pin not supported"));
}
} else {
esp_sleep_enable_gpio_wakeup();
}
}

#else

if (machine_rtc_config.ext0_pin != -1 && (machine_rtc_config.ext0_wake_types & wake_type)) {
esp_sleep_enable_ext0_wakeup(machine_rtc_config.ext0_pin, machine_rtc_config.ext0_level ? 1 : 0);
Expand Down Expand Up @@ -338,7 +367,11 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_wake_reason), MP_ROM_PTR(&machine_wake_reason_obj) },
{ MP_ROM_QSTR(MP_QSTR_PIN_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_EXT0) },
{ MP_ROM_QSTR(MP_QSTR_EXT0_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_EXT0) },
#if CONFIG_IDF_TARGET_ESP32C3
{ MP_ROM_QSTR(MP_QSTR_EXT1_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_GPIO) },
#else
{ MP_ROM_QSTR(MP_QSTR_EXT1_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_EXT1) },
#endif
{ 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) },
Expand Down