summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2021-04-08 21:11:24 +1000
committerDamien George <damien@micropython.org>2021-04-09 12:44:19 +1000
commit7b41d7f187d0bbfdda66db06b523c8829f198998 (patch)
tree95ca9a32bdcc2e50ee38397c45664ba46f7c1048
parent4d9e657f0ee881f4a41093ab89ec91d03613744d (diff)
stm32/boardctrl: Give boards control over execution of boot.py,main.py.
This commit simplifies the customisation of the main MicroPython execution loop (4 macros are reduced to 2), and allows a board to have full control over the execution (or not) of boot.py and main.py. For boards that use the default start-up code, there is no functional change in this commit. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/stm32/boardctrl.c50
-rw-r--r--ports/stm32/boardctrl.h30
-rw-r--r--ports/stm32/main.c38
3 files changed, 56 insertions, 62 deletions
diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c
index 188068d70..c82fefa97 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;
+
+ if (run_boot_py) {
+ // Run boot.py, if it exists.
+ const char *boot_py = "boot.py";
+ int ret = pyexec_file_if_exists(boot_py);
-void boardctrl_after_boot_py(boardctrl_state_t *state) {
- if (state->run_boot_py && !state->last_ret) {
- flash_error(4);
+ // 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 f1cbc3b95..33e71b65c 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 888b20513..bc8e6f069 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.