From dcf1e4174c1a40fa895440743a263c808817b680 Mon Sep 17 00:00:00 2001 From: ALTracer <11005378+ALTracer@users.noreply.github.com> Date: Wed, 10 Jul 2024 23:34:45 +0300 Subject: [PATCH] blackpill-f4: Unmap BootROM after ST DFU done * After using ST MaskROM for initial reprogramming of BMD bootloader (or BMF), code gets to run with `SYSCFG_MEMRMP=0b01` System memory still mapped on 0x0, and with a `SCB_VTOR` of 0 any interrupts will invoke MaskROM handlers, crashing firmware. * Unmap it both in BMD bootloader and in BMF, restoring normal operation and eliminating the need for initial manual/double reset (by pushbuttons). --- src/platforms/common/blackpill-f4/blackpill-f4.c | 6 ++++++ src/platforms/common/blackpill-f4/usbdfu.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/platforms/common/blackpill-f4/blackpill-f4.c b/src/platforms/common/blackpill-f4/blackpill-f4.c index 3c7aa0ce8ca..d05ba5b0e45 100644 --- a/src/platforms/common/blackpill-f4/blackpill-f4.c +++ b/src/platforms/common/blackpill-f4/blackpill-f4.c @@ -72,6 +72,12 @@ void platform_init(void) scb_reset_core(); } #endif + + /* Unmap ST MaskROM and map back Internal Flash */ + rcc_periph_clock_enable(RCC_SYSCFG); + if ((SYSCFG_MEMRM & 3U) == 1U) + SYSCFG_MEMRM &= ~3U; + rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[PLATFORM_CLOCK_FREQ]); /* Set up DM/DP pins. PA9/PA10 are not routed to USB-C. */ diff --git a/src/platforms/common/blackpill-f4/usbdfu.c b/src/platforms/common/blackpill-f4/usbdfu.c index d5d205ea968..66ef0649a15 100644 --- a/src/platforms/common/blackpill-f4/usbdfu.c +++ b/src/platforms/common/blackpill-f4/usbdfu.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include "usbdfu.h" @@ -54,6 +55,11 @@ int main(void) } else dfu_jump_app_if_valid(); + /* Unmap ST MaskROM and map back Internal Flash */ + rcc_periph_clock_enable(RCC_SYSCFG); + if ((SYSCFG_MEMRM & 3U) == 1U) + SYSCFG_MEMRM &= ~3U; + rcc_clock_setup_pll(&rcc_hse_25mhz_3v3[PLATFORM_CLOCK_FREQ]); /* Assert blue LED as indicator we are in the bootloader */