Skip to content

esp32: make machine.soft_reset() work in main.py #6782

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
9 changes: 8 additions & 1 deletion ports/esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void mp_task(void *pvParameter) {
mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t));
#endif
uart_init();
machine_init();

// TODO: CONFIG_SPIRAM_SUPPORT is for 3.3 compatibility, remove after move to 4.0.
#if CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_SPIRAM_SUPPORT
Expand Down Expand Up @@ -122,7 +123,10 @@ void mp_task(void *pvParameter) {
pyexec_frozen_module("_boot.py");
pyexec_file_if_exists("boot.py");
if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
pyexec_file_if_exists("main.py");
int ret = pyexec_file_if_exists("main.py");
if (ret & PYEXEC_FORCED_EXIT) {
goto soft_reset_exit;
}
}

for (;;) {
Expand All @@ -139,6 +143,8 @@ void mp_task(void *pvParameter) {
}
}

soft_reset_exit:

#if MICROPY_BLUETOOTH_NIMBLE
mp_bluetooth_deinit();
#endif
Expand All @@ -155,6 +161,7 @@ void mp_task(void *pvParameter) {

// deinitialise peripherals
machine_pins_deinit();
machine_deinit();
usocket_events_deinit();

mp_deinit();
Expand Down
14 changes: 14 additions & 0 deletions ports/esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ typedef enum {
MP_SOFT_RESET
} reset_reason_t;

STATIC bool is_soft_reset = 0;

STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
if (n_args == 0) {
// get
Expand Down Expand Up @@ -146,6 +148,9 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *pos_args, mp_ma
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_deepsleep_obj, 0, machine_deepsleep);

STATIC mp_obj_t machine_reset_cause(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
if (is_soft_reset) {
return MP_OBJ_NEW_SMALL_INT(MP_SOFT_RESET);
}
switch (esp_reset_reason()) {
case ESP_RST_POWERON:
case ESP_RST_BROWNOUT:
Expand Down Expand Up @@ -177,6 +182,15 @@ STATIC mp_obj_t machine_reset_cause(size_t n_args, const mp_obj_t *pos_args, mp_
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_reset_cause_obj, 0, machine_reset_cause);

void machine_init(void) {
is_soft_reset = 0;
}

void machine_deinit(void) {
// we are doing a soft-reset so change the reset_cause
is_soft_reset = 1;
}

STATIC mp_obj_t machine_wake_reason(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
return MP_OBJ_NEW_SMALL_INT(esp_sleep_get_wakeup_cause());
}
Expand Down
2 changes: 2 additions & 0 deletions ports/esp32/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ extern const mp_obj_type_t machine_uart_type;
extern const mp_obj_type_t machine_rtc_type;
extern const mp_obj_type_t machine_sdcard_type;

void machine_init(void);
void machine_deinit(void);
void machine_pins_init(void);
void machine_pins_deinit(void);
void machine_timer_deinit_all(void);
Expand Down