diff --git a/ports/stm32/boardctrl.c b/ports/stm32/boardctrl.c index 922f218e92..02a3759991 100644 --- a/ports/stm32/boardctrl.c +++ b/ports/stm32/boardctrl.c @@ -25,9 +25,11 @@ */ #include "py/runtime.h" +#include "py/objstr.h" #include "py/mphal.h" #include "shared/runtime/pyexec.h" #include "boardctrl.h" +#include "powerctrl.h" #include "led.h" #include "usrsw.h" @@ -43,6 +45,26 @@ STATIC void flash_error(int n) { led_state(PYB_LED_GREEN, 0); } +#if MICROPY_HW_USES_BOOTLOADER +void boardctrl_maybe_enter_mboot(size_t n_args, const void *args_in) { + const mp_obj_t *args = args_in; + + if (n_args == 0 || !mp_obj_is_true(args[0])) { + // By default, with no args given, we enter the custom bootloader (mboot) + powerctrl_enter_bootloader(0x70ad0000, MBOOT_VTOR); + } + + if (n_args == 1 && mp_obj_is_str_or_bytes(args[0])) { + // With a string/bytes given, pass its data to the custom bootloader + size_t len; + const char *data = mp_obj_str_get_data(args[0], &len); + void *mboot_region = (void *)*((volatile uint32_t *)MBOOT_VTOR); + memmove(mboot_region, data, len); + powerctrl_enter_bootloader(0x70ad0080, MBOOT_VTOR); + } +} +#endif + #if !MICROPY_HW_USES_BOOTLOADER STATIC uint update_reset_mode(uint reset_mode) { #if MICROPY_HW_HAS_SWITCH diff --git a/ports/stm32/boardctrl.h b/ports/stm32/boardctrl.h index bc901f1c37..afd4ab8510 100644 --- a/ports/stm32/boardctrl.h +++ b/ports/stm32/boardctrl.h @@ -38,8 +38,12 @@ #endif #ifndef MICROPY_BOARD_ENTER_BOOTLOADER +#if MICROPY_HW_USES_BOOTLOADER +#define MICROPY_BOARD_ENTER_BOOTLOADER(nargs, args) boardctrl_maybe_enter_mboot(nargs, args) +#else #define MICROPY_BOARD_ENTER_BOOTLOADER(nargs, args) #endif +#endif #ifndef MICROPY_BOARD_EARLY_INIT #define MICROPY_BOARD_EARLY_INIT() @@ -106,6 +110,7 @@ typedef struct _boardctrl_state_t { bool log_soft_reset; } boardctrl_state_t; +void boardctrl_maybe_enter_mboot(size_t n_args, const void *args); void boardctrl_before_soft_reset_loop(boardctrl_state_t *state); void boardctrl_top_soft_reset_loop(boardctrl_state_t *state); int boardctrl_run_boot_py(boardctrl_state_t *state); diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index 91061a3109..34e78d2616 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -30,7 +30,6 @@ #include "modmachine.h" #include "py/gc.h" #include "py/runtime.h" -#include "py/objstr.h" #include "py/mperrno.h" #include "py/mphal.h" #include "extmod/machine_bitstream.h" @@ -283,22 +282,6 @@ NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *args) { MICROPY_BOARD_ENTER_BOOTLOADER(n_args, args); - #if MICROPY_HW_USES_BOOTLOADER - if (n_args == 0 || !mp_obj_is_true(args[0])) { - // By default, with no args given, we enter the custom bootloader (mboot) - powerctrl_enter_bootloader(0x70ad0000, MBOOT_VTOR); - } - - if (n_args == 1 && mp_obj_is_str_or_bytes(args[0])) { - // With a string/bytes given, pass its data to the custom bootloader - size_t len; - const char *data = mp_obj_str_get_data(args[0], &len); - void *mboot_region = (void *)*((volatile uint32_t *)MBOOT_VTOR); - memmove(mboot_region, data, len); - powerctrl_enter_bootloader(0x70ad0080, MBOOT_VTOR); - } - #endif - #if defined(STM32F7) || defined(STM32H7) powerctrl_enter_bootloader(0, 0x1ff00000); #else