8000 esp32: Make machine.soft_reset() work in main.py and reset_cause(). · larsks/micropython@d28dbcd · GitHub
  • [go: up one dir, main page]

    Skip to content

    Commit d28dbcd

    Browse files
    tvedpgeorge
    authored andcommitted
    esp32: Make machine.soft_reset() work in main.py and reset_cause().
    This commit fixes two issues on the esp32: - it enables machine.soft_reset() to be called in main.py; - it enables machine.reset_cause() to correctly identify a soft reset. The former is useful in that it enables soft resets in applications that are started at boot time. The support is patterned after the stm32 port.
    1 parent c10d431 commit d28dbcd

    File tree

    3 files changed

    +24
    -1
    lines changed

    3 files changed

    +24
    -1
    lines changed

    ports/esp32/main.c

    Lines changed: 8 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -73,6 +73,7 @@ void mp_task(void *pvParameter) {
    7373
    mp_thread_init(pxTaskGetStackStart(NULL), MP_TASK_STACK_SIZE / sizeof(uintptr_t));
    7474
    #endif
    7575
    uart_init();
    76+
    machine_init();
    7677

    7778
    // TODO: CONFIG_SPIRAM_SUPPORT is for 3.3 compatibility, remove after move to 4.0.
    7879
    #if CONFIG_ESP32_SPIRAM_SUPPORT || CONFIG_SPIRAM_SUPPORT
    @@ -118,7 +119,10 @@ void mp_task(void *pvParameter) {
    118119
    pyexec_frozen_module("_boot.py");
    119120
    pyexec_file_if_exists("boot.py");
    120121
    if (pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL) {
    121-
    pyexec_file_if_exists("main.py");
    122+
    int ret = pyexec_file_if_exists("main.py");
    123+
    if (ret & PYEXEC_FORCED_EXIT) {
    124+
    goto soft_reset_exit;
    125+
    }
    122126
    }
    123127

    124128
    for (;;) {
    @@ -135,6 +139,8 @@ void mp_task(void *pvParameter) {
    135139
    }
    136140
    }
    137141

    142+
    soft_reset_exit:
    143+
    138144
    #if MICROPY_BLUETOOTH_NIMBLE
    139145
    mp_bluetooth_deinit();
    140146
    #endif
    @@ -151,6 +157,7 @@ void mp_task(void *pvParameter) {
    151157

    152158
    // deinitialise peripherals
    153159
    machine_pins_deinit();
    160+
    machine_deinit();
    154161
    usocket_events_deinit();
    155162

    156163
    mp_deinit();

    ports/esp32/modmachine.c

    Lines changed: 14 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -59,6 +59,8 @@ typedef enum {
    5959
    MP_SOFT_RESET
    6060
    } reset_reason_t;
    6161

    62+
    STATIC bool is_soft_reset = 0;
    63+
    6264
    STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
    6365
    if (n_args == 0) {
    6466
    // get
    @@ -140,6 +142,9 @@ STATIC mp_obj_t machine_deepsleep(size_t n_args, const mp_obj_t *pos_args, mp_ma
    140142
    STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_deepsleep_obj, 0, machine_deepsleep);
    141143

    142144
    STATIC mp_obj_t machine_reset_cause(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    145+
    if (is_soft_reset) {
    146+
    return MP_OBJ_NEW_SMALL_INT(MP_SOFT_RESET);
    147+
    }
    143148
    switch (esp_reset_reason()) {
    144149
    case ESP_RST_POWERON:
    145150
    case ESP_RST_BROWNOUT:
    @@ -171,6 +176,15 @@ STATIC mp_obj_t machine_reset_cause(size_t n_args, const mp_obj_t *pos_args, mp_
    171176
    }
    172177
    STATIC MP_DEFINE_CONST_FUN_OBJ_KW(machine_reset_cause_obj, 0, machine_reset_cause);
    173178

    179+
    void machine_init(void) {
    180+
    is_soft_reset = 0;
    181+
    }
    182+
    183+
    void machine_deinit(void) {
    184+
    // we are doing a soft-reset so change the reset_cause
    185+
    is_soft_reset = 1;
    186+
    }
    187+
    174188
    STATIC mp_obj_t machine_wake_reason(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
    175189
    return MP_OBJ_NEW_SMALL_INT(esp_sleep_get_wakeup_cause());
    176190
    }

    ports/esp32/modmachine.h

    Lines changed: 2 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -22,6 +22,8 @@ extern const mp_obj_type_t machine_uart_type;
    2222
    extern const mp_obj_type_t machine_rtc_type;
    2323
    extern const mp_obj_type_t machine_sdcard_type;
    2424

    25+
    void machine_init(void);
    26+
    void machine_deinit(void);
    2527
    void machine_pins_init(void);
    2628
    void machine_pins_deinit(void);
    2729
    void machine_timer_deinit_all(void);

    0 commit comments

    Comments
     (0)
    0