8000 ports/esp32: Fix ESP32-C3 deep/light sleep wake on GPIOs support. by chihosin · Pull Request #8995 · micropython/micropython · GitHub
[go: up one dir, main page]

Skip to content

ports/esp32: Fix ESP32-C3 deep/light sleep wake on GPIOs support. #8995

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 1 commit 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
3 changes: 2 additions & 1 deletion ports/esp32/machine_rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};

machine_rtc_config_t machine_rtc_config = {
.ext1_pins = 0,
.ext0_pin = -1
.ext0_pin = -1,
.wake_pins = 0
};

STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
Expand Down
2 changes: 2 additions & 0 deletions ports/esp32/machine_rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ typedef struct {
bool ext0_level : 1;
wake_type_t ext0_wake_types;
bool ext1_level : 1;
uint64_t wake_pins;
uint64_t wake_level : 1;
} machine_rtc_config_t;

extern machine_rtc_config_t machine_rtc_config;
Expand Down
46 changes: 46 additions & 0 deletions ports/esp32/modesp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,46 @@ STATIC mp_obj_t esp32_wake_on_touch(const mp_obj_t wake) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_touch_obj, esp32_wake_on_touch);

#if CONFIG_IDF_TARGET_ESP32C3

STATIC mp_obj_t esp32_wake_on_pins(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum {ARG_pins, ARG_level};
const mp_arg_t allowed_args[] = {
{ MP_QSTR_pins, MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_level, MP_ARG_BOOL, {.u_bool = machine_rtc_config.wake_level} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
uint64_t wake_pins = machine_rtc_config.wake_pins;


// Check that all pins are allowed
if (args[ARG_pins].u_obj != mp_const_none) {
size_t len = 0;
mp_obj_t *elem;
mp_obj_get_array(args[ARG_pins].u_obj, &len, &elem);
wake_pins = 0;

for (int i = 0; i < len; i++) {

gpio_num_t pin_id = machine_pin_get_id(elem[i]);
if (!RTC_IS_VALID_EXT_PIN(pin_id)) {
mp_raise_ValueError(MP_ERROR_TEXT("invalid pin"));
break;
}
wake_pins |= (1ll << pin_id);
}
}

machine_rtc_config.wake_level = args[ARG_level].u_bool;
machine_rtc_config.wake_pins = wake_pins;

return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_wake_on_pins_obj, 0, esp32_wake_on_pins);

#else

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

if (machine_rtc_config.wake_on_touch) {
Expand Down Expand Up @@ -140,6 +180,8 @@ STATIC mp_obj_t esp32_wake_on_ulp(const mp_obj_t wake) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_wake_on_ulp_obj, esp32_wake_on_ulp);

#endif

STATIC mp_obj_t esp32_gpio_deep_sleep_hold(const mp_obj_t enable) {
if (mp_obj_is_true(enable)) {
gpio_deep_sleep_hold_en();
Expand Down Expand Up @@ -204,9 +246,13 @@ STATIC const mp_rom_map_elem_t esp32_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_esp32) },

{ MP_ROM_QSTR(MP_QSTR_wake_on_touch), MP_ROM_PTR(&esp32_wake_on_touch_obj) },
#if CONFIG_IDF_TARGET_ESP32C3
{ MP_ROM_QSTR(MP_QSTR_wake_on_pins), MP_ROM_PTR(&esp32_wake_on_pins_obj) },
#else
{ MP_ROM_QSTR(MP_QSTR_wake_on_ext0), MP_ROM_PTR(&esp32_wake_on_ext0_obj) },
{ MP_ROM_QSTR(MP_QSTR_wake_on_ext1), MP_ROM_PTR(&esp32_wake_on_ext1_obj) },
{ MP_ROM_QSTR(MP_QSTR_wake_on_ulp), MP_ROM_PTR(&esp32_wake_on_ulp_obj) },
#endif
{ MP_ROM_QSTR(MP_QSTR_gpio_deep_sleep_hold), MP_ROM_PTR(&esp32_gpio_deep_sleep_hold_obj) },
#if CONFIG_IDF_TARGET_ESP32
{ MP_ROM_QSTR(MP_QSTR_raw_temperature), MP_ROM_PTR(&esp32_raw_temperature_obj) },
Expand Down
26 changes: 26 additions & 0 deletions ports/esp32/modesp32.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@
)
#define RTC_LAST_EXT_PIN 21

#elif CONFIG_IDF_TARGET_ESP32C3

#define RTC_VALID_EXT_PINS \
( \
(1ll << 2) | \
(1ll << 3) | \
(1ll << 4) | \
(1ll << 5) | \
(1ll << 6) | \
(1ll << 7) | \
(1ll << 8) | \
(1ll << 9) | \
(1ll << 10) | \
(1ll << 11) | \
(1ll << 12) | \
(1ll << 13) | \
(1ll << 14) | \
(1ll << 15) | \
(1ll << 16) | \
(1ll << 17) | \
(1ll << 18) | \
(1ll << 19) | \
(1ll << 21) \
)
#define RTC_LAST_EXT_PIN 21

#else

#define RTC_VALID_EXT_PINS \
Expand Down
12 changes: 12 additions & 0 deletions ports/esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,18 @@ STATIC mp_obj_t machine_sleep_helper(wake_type_t wake_type, size_t n_args, const

#endif

#if CONFIG_IDF_TARGET_ESP32C3

if (machine_rtc_config.wake_pins != 0) {
if (esp_deep_sleep_enable_gpio_wakeup(
machine_rtc_config.wake_pins,
machine_rtc_config.wake_level ? ESP_GPIO_WAKEUP_GPIO_HIGH : ESP_GPIO_WAKEUP_GPIO_LOW) != ESP_OK) {
mp_raise_msg(&mp_type_RuntimeError, MP_ERROR_TEXT("esp_sleep_enable_gpio_wakeup() failed"));
}
}

#endif

switch (wake_type) {
case MACHINE_WAKE_SLEEP:
esp_light_sleep_start();
Expand Down
0