Skip to content

Commit

Permalink
esp32/main.c: Added machine.reset_cause()
Browse files Browse the repository at this point in the history
esp32/modmachine.c: Added machine.reset_cause()
esp32/modmachine.h: Added machine.reset_cause()
  • Loading branch information
MrSurly committed Jun 13, 2017
1 parent e68bf91 commit a838127
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions esp32/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -111,6 +114,7 @@ void mp_task(void *pvParameter) {

mp_deinit();
fflush(stdout);
reset_cause = MACHINE_SOFT_RESET;
goto soft_reset;
}

Expand Down
28 changes: 28 additions & 0 deletions esp32/modmachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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) },

Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions esp32/modmachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a838127

Please sign in to comment.