diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index 188068d705..c82fefa97c 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -24,6 +24,7 @@ * THE SOFTWARE. */ +#include "py/runtime.h" #include "py/mphal.h" #include "lib/utils/pyexec.h" #include "boardctrl.h" @@ -140,13 +141,21 @@ void boardctrl_top_soft_reset_loop(boardctrl_state_t *state) { led_state(4, 0); } -void boardctrl_before_boot_py(boardctrl_state_t *state) { - state->run_boot_py = state->reset_mode == 1 || state->reset_mode == 3; -} +int boardctrl_run_boot_py(boardctrl_state_t *state) { + bool run_boot_py = state->reset_mode == 1 || state->reset_mode == 3; -void boardctrl_after_boot_py(boardctrl_state_t *state) { - if (state->run_boot_py && !state->last_ret) { - flash_error(4); + if (run_boot_py) { + // Run boot.py, if it exists. + const char *boot_py = "boot.py"; + int ret = pyexec_file_if_exists(boot_py); + + // Take action based on the execution result. + if (ret & PYEXEC_FORCED_EXIT) { + return BOARDCTRL_GOTO_SOFT_RESET_EXIT; + } + if (!ret) { + flash_error(4); + } } // Turn boot-up LEDs off @@ -160,17 +169,34 @@ void boardctrl_after_boot_py(boardctrl_state_t *state) { led_state(2, 0); led_state(3, 0); led_state(4, 0); + + return BOARDCTRL_CONTINUE; } -void boardctrl_before_main_py(boardctrl_state_t *state) { - state->run_main_py = (state->reset_mode == 1 || state->reset_mode == 3) +int boardctrl_run_main_py(boardctrl_state_t *state) { + bool run_main_py = (state->reset_mode == 1 || state->reset_mode == 3) && pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL; -} -void boardctrl_after_main_py(boardctrl_state_t *state) { - if (state->run_main_py && !state->last_ret) { - flash_error(3); + if (run_main_py) { + // Run main.py (or what it was configured to be), if it exists. + const char *main_py; + if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) { + main_py = "main.py"; + } else { + main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main)); + } + int ret = pyexec_file_if_exists(main_py); + + // Take action based on the execution result. + if (ret & PYEXEC_FORCED_EXIT) { + return BOARDCTRL_GOTO_SOFT_RESET_EXIT; + } + if (!ret) { + flash_error(3); + } } + + return BOARDCTRL_CONTINUE; } void boardctrl_start_soft_reset(boardctrl_state_t *state) { diff --git a/ports/stm32/boardctrl.h b/ports/stm32/boardctrl.h index f1cbc3b955..33e71b65c9 100644 --- a/ports/stm32/boardctrl.h +++ b/ports/stm32/boardctrl.h @@ -44,20 +44,12 @@ #define MICROPY_BOARD_TOP_SOFT_RESET_LOOP boardctrl_top_soft_reset_loop #endif -#ifndef MICROPY_BOARD_BEFORE_BOOT_PY -#define MICROPY_BOARD_BEFORE_BOOT_PY boardctrl_before_boot_py +#ifndef MICROPY_BOARD_RUN_BOOT_PY +#define MICROPY_BOARD_RUN_BOOT_PY boardctrl_run_boot_py #endif -#ifndef MICROPY_BOARD_AFTER_BOOT_PY -#define MICROPY_BOARD_AFTER_BOOT_PY boardctrl_after_boot_py -#endif - -#ifndef MICROPY_BOARD_BEFORE_MAIN_PY -#define MICROPY_BOARD_BEFORE_MAIN_PY boardctrl_before_main_py -#endif - -#ifndef MICROPY_BOARD_AFTER_MAIN_PY -#define MICROPY_BOARD_AFTER_MAIN_PY boardctrl_after_main_py +#ifndef MICROPY_BOARD_RUN_MAIN_PY +#define MICROPY_BOARD_RUN_MAIN_PY boardctrl_run_main_py #endif #ifndef MICROPY_BOARD_START_SOFT_RESET @@ -68,20 +60,20 @@ #define MICROPY_BOARD_END_SOFT_RESET boardctrl_end_soft_reset #endif +enum { + BOARDCTRL_CONTINUE, + BOARDCTRL_GOTO_SOFT_RESET_EXIT, +}; + typedef struct _boardctrl_state_t { uint8_t reset_mode; - bool run_boot_py; - bool run_main_py; bool log_soft_reset; - int last_ret; } boardctrl_state_t; void boardctrl_before_soft_reset_loop(boardctrl_state_t *state); void boardctrl_top_soft_reset_loop(boardctrl_state_t *state); -void boardctrl_before_boot_py(boardctrl_state_t *state); -void boardctrl_after_boot_py(boardctrl_state_t *state); -void boardctrl_before_main_py(boardctrl_state_t *state); -void boardctrl_after_main_py(boardctrl_state_t *state); +int boardctrl_run_boot_py(boardctrl_state_t *state); +int boardctrl_run_main_py(boardctrl_state_t *state); void boardctrl_start_soft_reset(boardctrl_state_t *state); void boardctrl_end_soft_reset(boardctrl_state_t *state); diff --git a/ports/stm32/main.c b/ports/stm32/main.c index 888b20513c..bc8e6f0691 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -469,9 +469,7 @@ void stm32_main(uint32_t reset_mode) { boardctrl_state_t state; state.reset_mode = reset_mode; - state.run_boot_py = false; - state.run_main_py = false; - state.last_ret = 0; + state.log_soft_reset = false; MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP(&state); @@ -566,20 +564,11 @@ soft_reset: // reset config variables; they should be set by boot.py MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL; - MICROPY_BOARD_BEFORE_BOOT_PY(&state); - - // run boot.py, if it exists - // TODO perhaps have pyb.reboot([bootpy]) function to soft-reboot and execute custom boot.py - if (state.run_boot_py) { - const char *boot_py = "boot.py"; - state.last_ret = pyexec_file_if_exists(boot_py); - if (state.last_ret & PYEXEC_FORCED_EXIT) { - goto soft_reset_exit; - } + // Run boot.py (or whatever else a board configures at this stage). + if (MICROPY_BOARD_RUN_BOOT_PY(&state) == BOARDCTRL_GOTO_SOFT_RESET_EXIT) { + goto soft_reset_exit; } - MICROPY_BOARD_AFTER_BOOT_PY(&state); - // Now we initialise sub-systems that need configuration from boot.py, // or whose initialisation can be safely deferred until after running // boot.py. @@ -613,24 +602,11 @@ soft_reset: // At this point everything is fully configured and initialised. - MICROPY_BOARD_BEFORE_MAIN_PY(&state); - - // Run the main script from the current directory. - if (state.run_main_py) { - const char *main_py; - if (MP_STATE_PORT(pyb_config_main) == MP_OBJ_NULL) { - main_py = "main.py"; - } else { - main_py = mp_obj_str_get_str(MP_STATE_PORT(pyb_config_main)); - } - state.last_ret = pyexec_file_if_exists(main_py); - if (state.last_ret & PYEXEC_FORCED_EXIT) { - goto soft_reset_exit; - } + // Run main.py (or whatever else a board configures at this stage). + if (MICROPY_BOARD_RUN_MAIN_PY(&state) == BOARDCTRL_GOTO_SOFT_RESET_EXIT) { + goto soft_reset_exit; } - MICROPY_BOARD_AFTER_MAIN_PY(&state); - #if MICROPY_ENABLE_COMPILER // Main script is finished, so now go into REPL mode. // The REPL mode can change, or it can request a soft reset.