Skip to content

Implemented GPIO wake-up for ESP32-C3 #13333

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 4 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 @@ -63,6 +63,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 @@ -89,6 +95,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
32 changes: 31 additions & 1 deletion ports/esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
{ 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) }, \
{ MP_ROM_QSTR(MP_QSTR_EXT1_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_EXT1) }, \
{ MP_ROM_QSTR(MP_QSTR_GPIO_WAKE), MP_ROM_INT(ESP_SLEEP_WAKEUP_GPIO) }, \
{ 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 Expand Up @@ -146,7 +147,36 @@ static void machine_sleep_helper(wake_type_t wake_type, size_t n_args, const mp_
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) {
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
Loading