Skip to content

Commit

Permalink
blackpill-f4: Unmap BootROM after ST DFU done
Browse files Browse the repository at this point in the history
* 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).
  • Loading branch information
ALTracer committed Jul 11, 2024
1 parent bd92e74 commit dcf1e41
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/platforms/common/blackpill-f4/blackpill-f4.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
6 changes: 6 additions & 0 deletions src/platforms/common/blackpill-f4/usbdfu.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <libopencm3/cm3/scb.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/syscfg.h>
#include <libopencm3/usb/dwc/otg_fs.h>

#include "usbdfu.h"
Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit dcf1e41

Please sign in to comment.