diff options
-rw-r--r-- | ports/stm32/boardctrl.c | 18 | ||||
-rw-r--r-- | ports/stm32/boardctrl.h | 9 | ||||
-rw-r--r-- | ports/stm32/main.c | 5 | ||||
-rw-r--r-- | ports/stm32/mboot/main.c | 7 |
4 files changed, 26 insertions, 13 deletions
diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index c82fefa97..ce04914ea 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -57,8 +57,8 @@ STATIC uint update_reset_mode(uint reset_mode) { } mp_hal_delay_ms(20); if (i % 30 == 29) { - if (++reset_mode > 3) { - reset_mode = 1; + if (++reset_mode > BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { + reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } led_state(2, reset_mode & 1); led_state(3, reset_mode & 2); @@ -97,8 +97,8 @@ STATIC uint update_reset_mode(uint reset_mode) { if (!switch_get()) { break; } - if (++reset_mode > 3) { - reset_mode = 1; + if (++reset_mode > BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { + reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } } // Flash the selected reset mode @@ -124,7 +124,7 @@ void boardctrl_before_soft_reset_loop(boardctrl_state_t *state) { #if !MICROPY_HW_USES_BOOTLOADER // Update the reset_mode via the default // method which uses the board switch/button and LEDs. - state->reset_mode = update_reset_mode(1); + state->reset_mode = update_reset_mode(BOARDCTRL_RESET_MODE_NORMAL); #endif } @@ -142,7 +142,8 @@ void boardctrl_top_soft_reset_loop(boardctrl_state_t *state) { } int boardctrl_run_boot_py(boardctrl_state_t *state) { - bool run_boot_py = state->reset_mode == 1 || state->reset_mode == 3; + bool run_boot_py = state->reset_mode == BOARDCTRL_RESET_MODE_NORMAL + || state->reset_mode == BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM; if (run_boot_py) { // Run boot.py, if it exists. @@ -174,7 +175,8 @@ int boardctrl_run_boot_py(boardctrl_state_t *state) { } int boardctrl_run_main_py(boardctrl_state_t *state) { - bool run_main_py = (state->reset_mode == 1 || state->reset_mode == 3) + bool run_main_py = (state->reset_mode == BOARDCTRL_RESET_MODE_NORMAL + || state->reset_mode == BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL; if (run_main_py) { @@ -205,5 +207,5 @@ void boardctrl_start_soft_reset(boardctrl_state_t *state) { void boardctrl_end_soft_reset(boardctrl_state_t *state) { // Set reset_mode to normal boot. - state->reset_mode = 1; + state->reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } diff --git a/ports/stm32/boardctrl.h b/ports/stm32/boardctrl.h index 33e71b65c..551be3453 100644 --- a/ports/stm32/boardctrl.h +++ b/ports/stm32/boardctrl.h @@ -60,11 +60,20 @@ #define MICROPY_BOARD_END_SOFT_RESET boardctrl_end_soft_reset #endif +// Constants to return from boardctrl_run_boot_py, boardctrl_run_main_py. enum { BOARDCTRL_CONTINUE, BOARDCTRL_GOTO_SOFT_RESET_EXIT, }; +// Constants for boardctrl_state_t.reset_mode. +enum { + BOARDCTRL_RESET_MODE_NORMAL = 1, + BOARDCTRL_RESET_MODE_SAFE_MODE = 2, + BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM = 3, + BOARDCTRL_RESET_MODE_BOOTLOADER = 4, +}; + typedef struct _boardctrl_state_t { uint8_t reset_mode; bool log_soft_reset; diff --git a/ports/stm32/main.c b/ports/stm32/main.c index bc8e6f069..ca80daace 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -156,7 +156,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(pyb_main_obj, 1, pyb_main); #if MICROPY_HW_FLASH_MOUNT_AT_BOOT // avoid inlining to avoid stack usage within main() MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { - if (reset_mode == 3) { + if (reset_mode == BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { // Asked by user to reset filesystem factory_reset_create_filesystem(); } @@ -210,7 +210,8 @@ MP_NOINLINE STATIC bool init_flash_fs(uint reset_mode) { mp_obj_t mount_point = MP_OBJ_NEW_QSTR(MP_QSTR__slash_flash); ret = mp_vfs_mount_and_chdir_protected(bdev, mount_point); - if (ret == -MP_ENODEV && bdev == MP_OBJ_FROM_PTR(&pyb_flash_obj) && reset_mode != 3) { + if (ret == -MP_ENODEV && bdev == MP_OBJ_FROM_PTR(&pyb_flash_obj) + && reset_mode != BOARDCTRL_RESET_MODE_FACTORY_FILESYSTEM) { // No filesystem, bdev is still the default (so didn't detect a possibly corrupt littlefs), // and didn't already create a filesystem, so try to create a fresh one now. ret = factory_reset_create_filesystem(); diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index 4407ca9da..a77060ab2 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -29,6 +29,7 @@ #include "py/mphal.h" #include "extmod/crypto-algorithms/sha256.c" +#include "boardctrl.h" #include "usbd_core.h" #include "storage.h" #include "flash.h" @@ -1289,7 +1290,7 @@ static int pyb_usbdd_shutdown(void) { static int get_reset_mode(void) { usrbtn_init(); - int reset_mode = 1; + int reset_mode = BOARDCTRL_RESET_MODE_NORMAL; if (usrbtn_state()) { // Cycle through reset modes while USR is held // Timeout is roughly 20s, where reset_mode=1 @@ -1299,7 +1300,7 @@ static int get_reset_mode(void) { for (int i = 0; i < (RESET_MODE_NUM_STATES * RESET_MODE_TIMEOUT_CYCLES + 1) * 32; i++) { if (i % 32 == 0) { if (++reset_mode > RESET_MODE_NUM_STATES) { - reset_mode = 1; + reset_mode = BOARDCTRL_RESET_MODE_NORMAL; } uint8_t l = RESET_MODE_LED_STATES >> ((reset_mode - 1) * 4); led_state_all(l); @@ -1396,7 +1397,7 @@ void stm32_main(int initial_r0) { int reset_mode = get_reset_mode(); uint32_t msp = *(volatile uint32_t*)APPLICATION_ADDR; - if (reset_mode != 4 && (msp & APP_VALIDITY_BITS) == 0) { + if (reset_mode != BOARDCTRL_RESET_MODE_BOOTLOADER && (msp & APP_VALIDITY_BITS) == 0) { // not DFU mode so jump to application, passing through reset_mode // undo our DFU settings // TODO probably should disable all IRQ sources first |