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>
This commit is contained in:
parent
4d9e657f0e
commit
7b41d7f187
@ -24,6 +24,7 @@
|
|||||||
* THE SOFTWARE.
|
* THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include "py/runtime.h"
|
||||||
#include "py/mphal.h"
|
#include "py/mphal.h"
|
||||||
#include "lib/utils/pyexec.h"
|
#include "lib/utils/pyexec.h"
|
||||||
#include "boardctrl.h"
|
#include "boardctrl.h"
|
||||||
@ -140,14 +141,22 @@ void boardctrl_top_soft_reset_loop(boardctrl_state_t *state) {
|
|||||||
led_state(4, 0);
|
led_state(4, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void boardctrl_before_boot_py(boardctrl_state_t *state) {
|
int boardctrl_run_boot_py(boardctrl_state_t *state) {
|
||||||
state->run_boot_py = state->reset_mode == 1 || state->reset_mode == 3;
|
bool run_boot_py = state->reset_mode == 1 || state->reset_mode == 3;
|
||||||
}
|
|
||||||
|
|
||||||
void boardctrl_after_boot_py(boardctrl_state_t *state) {
|
if (run_boot_py) {
|
||||||
if (state->run_boot_py && !state->last_ret) {
|
// 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);
|
flash_error(4);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Turn boot-up LEDs off
|
// Turn boot-up LEDs off
|
||||||
|
|
||||||
@ -160,17 +169,34 @@ void boardctrl_after_boot_py(boardctrl_state_t *state) {
|
|||||||
led_state(2, 0);
|
led_state(2, 0);
|
||||||
led_state(3, 0);
|
led_state(3, 0);
|
||||||
led_state(4, 0);
|
led_state(4, 0);
|
||||||
|
|
||||||
|
return BOARDCTRL_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void boardctrl_before_main_py(boardctrl_state_t *state) {
|
int boardctrl_run_main_py(boardctrl_state_t *state) {
|
||||||
state->run_main_py = (state->reset_mode == 1 || state->reset_mode == 3)
|
bool run_main_py = (state->reset_mode == 1 || state->reset_mode == 3)
|
||||||
&& pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL;
|
&& pyexec_mode_kind == PYEXEC_MODE_FRIENDLY_REPL;
|
||||||
}
|
|
||||||
|
|
||||||
void boardctrl_after_main_py(boardctrl_state_t *state) {
|
if (run_main_py) {
|
||||||
if (state->run_main_py && !state->last_ret) {
|
// 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);
|
flash_error(3);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return BOARDCTRL_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void boardctrl_start_soft_reset(boardctrl_state_t *state) {
|
void boardctrl_start_soft_reset(boardctrl_state_t *state) {
|
||||||
|
@ -44,20 +44,12 @@
|
|||||||
#define MICROPY_BOARD_TOP_SOFT_RESET_LOOP boardctrl_top_soft_reset_loop
|
#define MICROPY_BOARD_TOP_SOFT_RESET_LOOP boardctrl_top_soft_reset_loop
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MICROPY_BOARD_BEFORE_BOOT_PY
|
#ifndef MICROPY_BOARD_RUN_BOOT_PY
|
||||||
#define MICROPY_BOARD_BEFORE_BOOT_PY boardctrl_before_boot_py
|
#define MICROPY_BOARD_RUN_BOOT_PY boardctrl_run_boot_py
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MICROPY_BOARD_AFTER_BOOT_PY
|
#ifndef MICROPY_BOARD_RUN_MAIN_PY
|
||||||
#define MICROPY_BOARD_AFTER_BOOT_PY boardctrl_after_boot_py
|
#define MICROPY_BOARD_RUN_MAIN_PY boardctrl_run_main_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
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef MICROPY_BOARD_START_SOFT_RESET
|
#ifndef MICROPY_BOARD_START_SOFT_RESET
|
||||||
@ -68,20 +60,20 @@
|
|||||||
#define MICROPY_BOARD_END_SOFT_RESET boardctrl_end_soft_reset
|
#define MICROPY_BOARD_END_SOFT_RESET boardctrl_end_soft_reset
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
enum {
|
||||||
|
BOARDCTRL_CONTINUE,
|
||||||
|
BOARDCTRL_GOTO_SOFT_RESET_EXIT,
|
||||||
|
};
|
||||||
|
|
||||||
typedef struct _boardctrl_state_t {
|
typedef struct _boardctrl_state_t {
|
||||||
uint8_t reset_mode;
|
uint8_t reset_mode;
|
||||||
bool run_boot_py;
|
|
||||||
bool run_main_py;
|
|
||||||
bool log_soft_reset;
|
bool log_soft_reset;
|
||||||
int last_ret;
|
|
||||||
} boardctrl_state_t;
|
} boardctrl_state_t;
|
||||||
|
|
||||||
void boardctrl_before_soft_reset_loop(boardctrl_state_t *state);
|
void boardctrl_before_soft_reset_loop(boardctrl_state_t *state);
|
||||||
void boardctrl_top_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);
|
int boardctrl_run_boot_py(boardctrl_state_t *state);
|
||||||
void boardctrl_after_boot_py(boardctrl_state_t *state);
|
int boardctrl_run_main_py(boardctrl_state_t *state);
|
||||||
void boardctrl_before_main_py(boardctrl_state_t *state);
|
|
||||||
void boardctrl_after_main_py(boardctrl_state_t *state);
|
|
||||||
void boardctrl_start_soft_reset(boardctrl_state_t *state);
|
void boardctrl_start_soft_reset(boardctrl_state_t *state);
|
||||||
void boardctrl_end_soft_reset(boardctrl_state_t *state);
|
void boardctrl_end_soft_reset(boardctrl_state_t *state);
|
||||||
|
|
||||||
|
@ -469,9 +469,7 @@ void stm32_main(uint32_t reset_mode) {
|
|||||||
|
|
||||||
boardctrl_state_t state;
|
boardctrl_state_t state;
|
||||||
state.reset_mode = reset_mode;
|
state.reset_mode = reset_mode;
|
||||||
state.run_boot_py = false;
|
state.log_soft_reset = false;
|
||||||
state.run_main_py = false;
|
|
||||||
state.last_ret = 0;
|
|
||||||
|
|
||||||
MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP(&state);
|
MICROPY_BOARD_BEFORE_SOFT_RESET_LOOP(&state);
|
||||||
|
|
||||||
@ -566,19 +564,10 @@ soft_reset:
|
|||||||
// reset config variables; they should be set by boot.py
|
// reset config variables; they should be set by boot.py
|
||||||
MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;
|
MP_STATE_PORT(pyb_config_main) = MP_OBJ_NULL;
|
||||||
|
|
||||||
MICROPY_BOARD_BEFORE_BOOT_PY(&state);
|
// Run boot.py (or whatever else a board configures at this stage).
|
||||||
|
if (MICROPY_BOARD_RUN_BOOT_PY(&state) == BOARDCTRL_GOTO_SOFT_RESET_EXIT) {
|
||||||
// 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;
|
goto soft_reset_exit;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
MICROPY_BOARD_AFTER_BOOT_PY(&state);
|
|
||||||
|
|
||||||
// Now we initialise sub-systems that need configuration from boot.py,
|
// Now we initialise sub-systems that need configuration from boot.py,
|
||||||
// or whose initialisation can be safely deferred until after running
|
// or whose initialisation can be safely deferred until after running
|
||||||
@ -613,23 +602,10 @@ soft_reset:
|
|||||||
|
|
||||||
// At this point everything is fully configured and initialised.
|
// At this point everything is fully configured and initialised.
|
||||||
|
|
||||||
MICROPY_BOARD_BEFORE_MAIN_PY(&state);
|
// Run main.py (or whatever else a board configures at this stage).
|
||||||
|
if (MICROPY_BOARD_RUN_MAIN_PY(&state) == BOARDCTRL_GOTO_SOFT_RESET_EXIT) {
|
||||||
// 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;
|
goto soft_reset_exit;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
MICROPY_BOARD_AFTER_MAIN_PY(&state);
|
|
||||||
|
|
||||||
#if MICROPY_ENABLE_COMPILER
|
#if MICROPY_ENABLE_COMPILER
|
||||||
// Main script is finished, so now go into REPL mode.
|
// Main script is finished, so now go into REPL mode.
|
||||||
|
Loading…
Reference in New Issue
Block a user