diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 800370e0ad..abde70f12c 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -136,6 +136,7 @@ SRC_HAL_IMX_C += \ $(MCU_DIR)/drivers/fsl_lpuart.c \ $(MCU_DIR)/drivers/fsl_pit.c \ $(MCU_DIR)/drivers/fsl_pwm.c \ + $(MCU_DIR)/drivers/fsl_romapi.c \ $(MCU_DIR)/drivers/fsl_sai.c \ $(MCU_DIR)/drivers/fsl_snvs_lp.c \ $(MCU_DIR)/drivers/fsl_wdog.c \ diff --git a/ports/mimxrt/modmachine.c b/ports/mimxrt/modmachine.c index 9097289e79..5c897578d4 100644 --- a/ports/mimxrt/modmachine.c +++ b/ports/mimxrt/modmachine.c @@ -38,6 +38,7 @@ #include "pin.h" #include "modmachine.h" #include "fsl_wdog.h" +#include "fsl_romapi.h" #if MICROPY_PY_MACHINE @@ -108,6 +109,24 @@ STATIC mp_obj_t machine_enable_irq(mp_obj_t state_in) { } MP_DEFINE_CONST_FUN_OBJ_1(machine_enable_irq_obj, machine_enable_irq); +NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *args) { + #if defined(MICROPY_BOARD_ENTER_BOOTLOADER) + // If a board has a custom bootloader, call it first. + MICROPY_BOARD_ENTER_BOOTLOADER(n_args, args); + #elif FSL_ROM_HAS_RUNBOOTLOADER_API + // If not, enter ROM bootloader in serial downloader / USB mode. + uint32_t arg = 0xEB110000; + ROM_RunBootloader(&arg); + #else + // No custom bootloader, or run bootloader API, then just reset. + WDOG_TriggerSystemSoftwareReset(WDOG1); + #endif + while (1) { + ; + } +} +STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_bootloader_obj, 0, 1, machine_bootloader); + STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_umachine) }, { MP_ROM_QSTR(MP_QSTR_unique_id), MP_ROM_PTR(&machine_unique_id_obj) }, @@ -144,6 +163,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, + { MP_ROM_QSTR(MP_QSTR_bootloader), MP_ROM_PTR(&machine_bootloader_obj) }, #if MICROPY_PY_MACHINE_BITSTREAM { MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },