From 05093f7f54d5f0a375d120df40e3335fa92e0a22 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Tue, 14 Jan 2020 15:50:00 -0500 Subject: [PATCH] Fix VTOR relocate, add bootloader makefile handling --- ports/stm32f4/Makefile | 6 +++- .../boards/meowbit_v121/mpconfigboard.h | 1 + .../boards/meowbit_v121/mpconfigboard.mk | 2 ++ ports/stm32f4/system_stm32f4xx.c | 35 ++++++++++++++++--- 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/ports/stm32f4/Makefile b/ports/stm32f4/Makefile index c3eb4aa3b0..d948996ea4 100755 --- a/ports/stm32f4/Makefile +++ b/ports/stm32f4/Makefile @@ -90,6 +90,10 @@ else ### CFLAGS += -flto endif +ifndef BOOTLOADER_OFFSET + BOOTLOADER_OFFSET := 0x8000000 +endif + C_DEFS = -DMCU_PACKAGE=$(MCU_PACKAGE) -DUSE_HAL_DRIVER -DUSE_FULL_LL_DRIVER -D$(CMSIS_MCU) CFLAGS += $(INC) -Werror -Wall -std=gnu11 -nostdlib $(BASE_CFLAGS) $(C_DEFS) $(CFLAGS_MOD) $(COPT) @@ -257,7 +261,7 @@ $(BUILD)/firmware.hex: $(BUILD)/firmware.elf $(BUILD)/firmware.uf2: $(BUILD)/firmware.hex $(ECHO) "Create $@" - $(PYTHON3) $(TOP)/tools/uf2/utils/uf2conv.py -f 0x57755a57 -b 0x08010000 -c -o "$(BUILD)/firmware.uf2" $^ + $(PYTHON3) $(TOP)/tools/uf2/utils/uf2conv.py -f 0x57755a57 -b $(BOOTLOADER_OFFSET) -c -o "$(BUILD)/firmware.uf2" $^ include $(TOP)/py/mkrules.mk diff --git a/ports/stm32f4/boards/meowbit_v121/mpconfigboard.h b/ports/stm32f4/boards/meowbit_v121/mpconfigboard.h index 0371685d8b..0de74a946c 100644 --- a/ports/stm32f4/boards/meowbit_v121/mpconfigboard.h +++ b/ports/stm32f4/boards/meowbit_v121/mpconfigboard.h @@ -37,6 +37,7 @@ #define BOARD_OSC_DIV 12 #define BOARD_NO_VBUS_SENSE +#define BOARD_VTOR_DEFER //Leave VTOR relocation to bootloader // On-board flash #define SPI_FLASH_MOSI_PIN (&pin_PB15) diff --git a/ports/stm32f4/boards/meowbit_v121/mpconfigboard.mk b/ports/stm32f4/boards/meowbit_v121/mpconfigboard.mk index c033d1d4bc..1d6f68b13c 100644 --- a/ports/stm32f4/boards/meowbit_v121/mpconfigboard.mk +++ b/ports/stm32f4/boards/meowbit_v121/mpconfigboard.mk @@ -9,6 +9,8 @@ EXTERNAL_FLASH_DEVICE_COUNT = 1 EXTERNAL_FLASH_DEVICES = W25Q16JV_IQ LONGINT_IMPL = MPZ +BOOTLOADER_OFFSET = 0x8010000 + # INTERNAL_FLASH_FILESYSTEM = 1 # LONGINT_IMPL = NONE diff --git a/ports/stm32f4/system_stm32f4xx.c b/ports/stm32f4/system_stm32f4xx.c index 3303f969d9..bce420056d 100644 --- a/ports/stm32f4/system_stm32f4xx.c +++ b/ports/stm32f4/system_stm32f4xx.c @@ -1,3 +1,27 @@ +/* + * Taken from ST Cube library and modified. See below for original header. + * + * Modifications copyright (c) 2019 Lucian Copeland for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + /** ****************************************************************************** * @file system_stm32f4xx.c @@ -63,6 +87,7 @@ #include "stm32f4xx.h" +#include "py/mpconfig.h" #if !defined (HSE_VALUE) #define HSE_VALUE ((uint32_t)25000000) /*!< Default value of the External oscillator in Hz */ @@ -193,10 +218,12 @@ void SystemInit(void) #endif /* DATA_IN_ExtSRAM || DATA_IN_ExtSDRAM */ /* Configure the Vector Table location add offset address ------------------*/ -#ifdef VECT_TAB_SRAM - SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ -#else - SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ +#if !defined(BOARD_VTOR_DEFER) //only set VTOR if the bootloader hasn't already + #ifdef VECT_TAB_SRAM + SCB->VTOR = SRAM_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal SRAM */ + #else + SCB->VTOR = FLASH_BASE | VECT_TAB_OFFSET; /* Vector Table Relocation in Internal FLASH */ + #endif #endif }