diff --git a/esp32/main.c b/esp32/main.c index 091cbddc9..e21bb4bb9 100644 --- a/esp32/main.c +++ b/esp32/main.c @@ -59,12 +59,15 @@ STATIC StaticTask_t mp_task_tcb; STATIC StackType_t mp_task_stack[MP_TASK_STACK_LEN] __attribute__((aligned (8))); STATIC uint8_t mp_task_heap[MP_TASK_HEAP_SIZE]; +extern uint32_t reset_cause; + void mp_task(void *pvParameter) { volatile uint32_t sp = (uint32_t)get_sp(); #if MICROPY_PY_THREAD mp_thread_init(&mp_task_stack[0], MP_TASK_STACK_LEN); #endif uart_init(); + machine_init(); soft_reset: // initialise the stack pointer for the main thread @@ -111,6 +114,7 @@ void mp_task(void *pvParameter) { mp_deinit(); fflush(stdout); + reset_cause = MACHINE_SOFT_RESET; goto soft_reset; } diff --git a/esp32/modmachine.c b/esp32/modmachine.c index 58bffc4de..b97b1501b 100644 --- a/esp32/modmachine.c +++ b/esp32/modmachine.c @@ -34,6 +34,7 @@ #include "freertos/task.h" #include "rom/ets_sys.h" #include "esp_system.h" +#include "rom/rtc.h" #include "py/obj.h" #include "py/runtime.h" @@ -45,6 +46,12 @@ #if MICROPY_PY_MACHINE +uint32_t reset_cause; + +void machine_init() { + reset_cause = rtc_get_reset_reason(0); +} + STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) { if (n_args == 0) { // get @@ -70,6 +77,11 @@ STATIC mp_obj_t machine_reset(void) { } STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); +STATIC mp_obj_t machine_reset_cause(void) { + return mp_obj_new_int(reset_cause); +} +STATIC MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_cause_obj, machine_reset_cause); + STATIC mp_obj_t machine_unique_id(void) { uint8_t chipid[6]; esp_efuse_mac_get_default(chipid); @@ -105,6 +117,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_freq_obj) }, { MP_ROM_QSTR(MP_QSTR_reset), MP_ROM_PTR(&machine_reset_obj) }, + { MP_ROM_QSTR(MP_QSTR_reset_cause), MP_ROM_PTR(&machine_reset_cause_obj) }, { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, { MP_ROM_QSTR(MP_QSTR_idle), MP_ROM_PTR(&machine_idle_obj) }, @@ -121,6 +134,21 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_PWM), MP_ROM_PTR(&machine_pwm_type) }, { MP_ROM_QSTR(MP_QSTR_SPI), MP_ROM_PTR(&mp_machine_soft_spi_type) }, { MP_ROM_QSTR(MP_QSTR_UART), MP_ROM_PTR(&machine_uart_type) }, + + // These are conflated, unfortunately + // https://github.com/espressif/esp-idf/issues/494#issuecomment-291921540 + // + // Per @igrr: + // "ESP32 doesn't have a dedicated reset line, instead CHIP_PU pin is + // used both as a power down pin and a reset pin. So a low pulse on + // CHIP_PU is equivalent to a power off followed by power on. + // This is why an external reset is logged as a power on reset." + + { MP_ROM_QSTR(MP_QSTR_PWRON_RESET), MP_OBJ_NEW_SMALL_INT(POWERON_RESET) }, + { MP_ROM_QSTR(MP_QSTR_HARD_RESET), MP_OBJ_NEW_SMALL_INT(POWERON_RESET) }, + { MP_ROM_QSTR(MP_QSTR_WDT_RESET), MP_OBJ_NEW_SMALL_INT(RTCWDT_CPU_RESET) }, + { MP_ROM_QSTR(MP_QSTR_DEEPSLEEP_RESET), MP_OBJ_NEW_SMALL_INT(RTCWDT_RTC_RESET) }, + { MP_ROM_QSTR(MP_QSTR_SOFT_RESET), MP_OBJ_NEW_SMALL_INT(MACHINE_SOFT_RESET) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/esp32/modmachine.h b/esp32/modmachine.h index 3dace35d4..1123ec4e9 100644 --- a/esp32/modmachine.h +++ b/esp32/modmachine.h @@ -3,6 +3,10 @@ #include "py/obj.h" +#define MACHINE_SOFT_RESET 0 + +void machine_init(); + extern const mp_obj_type_t machine_pin_type; extern const mp_obj_type_t machine_touchpad_type; extern const mp_obj_type_t machine_adc_type;