diff --git a/src/main/drivers/system.c b/src/main/drivers/system.c index 18a569857e2..f5c28cdadf0 100644 --- a/src/main/drivers/system.c +++ b/src/main/drivers/system.c @@ -104,6 +104,7 @@ void systemResetToBootloader(void) systemResetRequest(RESET_BOOTLOADER_REQUEST_ROM); } + typedef void resetHandler_t(void); typedef struct isrVector_s { @@ -119,14 +120,29 @@ void checkForBootLoaderRequest(void) if (bootloaderRequest != RESET_BOOTLOADER_REQUEST_ROM) { return; } + + // Clear the reset reason before jumping persistentObjectWrite(PERSISTENT_OBJECT_RESET_REASON, RESET_NONE); +#if defined(STM32H7) + + // Enable SYSCFG clock (required for bootloader) + __HAL_RCC_SYSCFG_CLK_ENABLE(); + + // Jump to bootloader volatile isrVector_t *bootloaderVector = (isrVector_t *)systemBootloaderAddress(); __set_MSP(bootloaderVector->stackEnd); bootloaderVector->resetHandler(); + while (1); +#else + // On F4/F7, jump to bootloader + volatile isrVector_t *bootloaderVector = (isrVector_t *)systemBootloaderAddress(); + __set_MSP(bootloaderVector->stackEnd); + bootloaderVector->resetHandler(); while (1); +#endif } #define SHORT_FLASH_DURATION 50 diff --git a/src/main/drivers/system_stm32h7xx.c b/src/main/drivers/system_stm32h7xx.c index af3ea72e2a2..000fe74d828 100644 --- a/src/main/drivers/system_stm32h7xx.c +++ b/src/main/drivers/system_stm32h7xx.c @@ -62,8 +62,6 @@ uint32_t systemBootloaderAddress(void) void systemInit(void) { - checkForBootLoaderRequest(); - // Configure NVIC preempt/priority groups HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITY_GROUPING); diff --git a/src/main/target/system_stm32h7xx.c b/src/main/target/system_stm32h7xx.c index cc580320b5a..6ec0d1c4002 100644 --- a/src/main/target/system_stm32h7xx.c +++ b/src/main/target/system_stm32h7xx.c @@ -584,6 +584,9 @@ void SystemInit (void) initialiseMemorySections(); + // Check for bootloader request BEFORE any clock/peripheral configuration + checkForBootLoaderRequest(); + // FPU settings #if (__FPU_PRESENT == 1) && (__FPU_USED == 1) SCB->CPACR |= ((3UL << 10*2)|(3UL << 11*2)); // Set CP10 and CP11 Full Access