From 1636a9759bc2d5f162312ac8bf5823e95e0ad053 Mon Sep 17 00:00:00 2001 From: BIGTREETECH <38851044+bigtreetech@users.noreply.github.com> Date: Fri, 1 Jul 2022 01:58:00 +0800 Subject: stm32: stm32g0/h7 usb_dfu_bootloader support (#5596) Signed-off-by: Alan.Ma from BigTreeTech --- src/stm32/stm32h7.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'src/stm32/stm32h7.c') diff --git a/src/stm32/stm32h7.c b/src/stm32/stm32h7.c index d6a32d96..bb0fe454 100644 --- a/src/stm32/stm32h7.c +++ b/src/stm32/stm32h7.c @@ -6,6 +6,7 @@ #include "autoconf.h" // CONFIG_CLOCK_REF_FREQ #include "board/armcm_boot.h" // VectorTable +#include "board/irq.h" // irq_disable #include "board/armcm_reset.h" // try_request_canboot #include "command.h" // DECL_CONSTANT_STR #include "internal.h" // get_pclock_frequency @@ -187,11 +188,36 @@ clock_setup(void) * USB bootloader ****************************************************************/ +#define USB_BOOT_FLAG_ADDR (CONFIG_RAM_START + CONFIG_RAM_SIZE - 1024) +#define USB_BOOT_FLAG 0x55534220424f4f54 // "USB BOOT" + +// Flag that bootloader is desired and reboot +static void +usb_reboot_for_dfu_bootloader(void) +{ + irq_disable(); + *(uint64_t*)USB_BOOT_FLAG_ADDR = USB_BOOT_FLAG; + NVIC_SystemReset(); +} + +// Check if rebooting into system DFU Bootloader +static void +check_usb_dfu_bootloader(void) +{ + if (!CONFIG_USBSERIAL || *(uint64_t*)USB_BOOT_FLAG_ADDR != USB_BOOT_FLAG) + return; + *(uint64_t*)USB_BOOT_FLAG_ADDR = 0; + uint32_t *sysbase = (uint32_t*)0x1FF09800; + asm volatile("mov sp, %0\n bx %1" + : : "r"(sysbase[0]), "r"(sysbase[1])); +} + // Handle USB reboot requests void usb_request_bootloader(void) { try_request_canboot(); + usb_reboot_for_dfu_bootloader(); } @@ -205,8 +231,14 @@ armcm_main(void) { // Run SystemInit() and then restore VTOR SystemInit(); + RCC->D1CCIPR = 0x00000000; + RCC->D2CCIP1R = 0x00000000; + RCC->D2CCIP2R = 0x00000000; + RCC->D3CCIPR = 0x00000000; SCB->VTOR = (uint32_t)VectorTable; + check_usb_dfu_bootloader(); + clock_setup(); sched_main(); -- cgit v1.2.3-70-g09d2