diff --git a/ports/stm32/Makefile b/ports/stm32/Makefile index e7a8f0aee6..e94e7d72f3 100644 --- a/ports/stm32/Makefile +++ b/ports/stm32/Makefile @@ -27,6 +27,10 @@ MICROPY_ROM_TEXT_COMPRESSION ?= 1 # File containing description of content to be frozen into firmware. FROZEN_MANIFEST ?= boards/manifest.py +# Location of mboot (or other bootloader) if the device has one. +# Used by machine.bootloader(). +MBOOT_TEXT0_ADDR ?= 0x08000000 + # include py core make definitions include $(TOP)/py/py.mk @@ -101,6 +105,7 @@ CFLAGS += $(CFLAGS_MCU_$(MCU_SERIES)) CFLAGS += $(COPT) CFLAGS += -I$(BOARD_DIR) CFLAGS += -DSTM32_HAL_H='' +CFLAGS += -DMBOOT_VTOR=$(MBOOT_TEXT0_ADDR) CFLAGS += -DMICROPY_HW_VTOR=$(TEXT0_ADDR) # Configure for nan-boxing object model if requested diff --git a/ports/stm32/mboot/Makefile b/ports/stm32/mboot/Makefile index de090da1c6..17de685a61 100755 --- a/ports/stm32/mboot/Makefile +++ b/ports/stm32/mboot/Makefile @@ -29,6 +29,9 @@ BUILDING_MBOOT = 1 include ../../../py/mkenv.mk include $(BOARD_DIR)/mpconfigboard.mk +# A board can set MBOOT_TEXT0_ADDR to a custom location where mboot should reside. +MBOOT_TEXT0_ADDR ?= 0x08000000 + MCU_SERIES_UPPER = $(shell echo $(MCU_SERIES) | tr '[:lower:]' '[:upper:]') CMSIS_MCU_LOWER = $(shell echo $(CMSIS_MCU) | tr '[:upper:]' '[:lower:]') @@ -77,6 +80,7 @@ CFLAGS += $(COPT) CFLAGS += -I$(BOARD_DIR) CFLAGS += -DSTM32_HAL_H='' CFLAGS += -DBOARD_$(BOARD) +CFLAGS += -DMBOOT_VTOR=$(MBOOT_TEXT0_ADDR) CFLAGS += -DAPPLICATION_ADDR=$(TEXT0_ADDR) CFLAGS += -DFFCONF_H=\"ports/stm32/mboot/ffconf.h\" CFLAGS += -DLFS1_NO_MALLOC -DLFS1_NO_DEBUG -DLFS1_NO_WARN -DLFS1_NO_ERROR -DLFS1_NO_ASSERT @@ -181,20 +185,18 @@ $(TOP)/lib/stm32lib/README.md: .PHONY: deploy deploy-stlink -FLASH_ADDR = 0x08000000 - deploy: $(BUILD)/firmware.dfu $(ECHO) "Writing $< to the board" $(Q)$(PYTHON) $(PYDFU) -u $< deploy-stlink: $(BUILD)/firmware.dfu $(ECHO) "Writing $< to the board via ST-LINK" - $(Q)$(STFLASH) write $(BUILD)/firmware.bin $(FLASH_ADDR) + $(Q)$(STFLASH) write $(BUILD)/firmware.bin $(MBOOT_TEXT0_ADDR) $(BUILD)/firmware.dfu: $(BUILD)/firmware.elf $(ECHO) "Create $@" $(Q)$(OBJCOPY) -O binary -j .isr_vector -j .text -j .data $^ $(BUILD)/firmware.bin - $(Q)$(PYTHON) $(DFU) -b $(FLASH_ADDR):$(BUILD)/firmware.bin $@ + $(Q)$(PYTHON) $(DFU) -b $(MBOOT_TEXT0_ADDR):$(BUILD)/firmware.bin $@ $(BUILD)/firmware.hex: $(BUILD)/firmware.elf $(ECHO) "Create $@" diff --git a/ports/stm32/mboot/main.c b/ports/stm32/mboot/main.c index c82e460faf..36b6890361 100644 --- a/ports/stm32/mboot/main.c +++ b/ports/stm32/mboot/main.c @@ -1358,7 +1358,7 @@ void stm32_main(int initial_r0) { #endif // Make sure IRQ vector table points to flash where this bootloader lives. - SCB->VTOR = FLASH_BASE; + SCB->VTOR = MBOOT_VTOR; // Enable 8-byte stack alignment for IRQ handlers, in accord with EABI SCB->CCR |= SCB_CCR_STKALIGN_Msk; diff --git a/ports/stm32/modmachine.c b/ports/stm32/modmachine.c index f9fd1d9a64..aee563ee88 100644 --- a/ports/stm32/modmachine.c +++ b/ports/stm32/modmachine.c @@ -270,16 +270,16 @@ STATIC NORETURN mp_obj_t machine_bootloader(size_t n_args, const mp_obj_t *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, 0x08000000); + 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 *)0x08000000); + void *mboot_region = (void *)*((volatile uint32_t *)MBOOT_VTOR); memmove(mboot_region, data, len); - powerctrl_enter_bootloader(0x70ad0080, 0x08000000); + powerctrl_enter_bootloader(0x70ad0080, MBOOT_VTOR); } #endif