How do you jump to the bootloader (DFU mode) in software on the STM32 F072?

前端 未结 2 1985
挽巷
挽巷 2021-02-02 04:29

The STM32 App Note 2606 discusses this, but there is no simple code example.

2条回答
  •  暖寄归人
    2021-02-02 05:08

    In my project, I'm essentially doing the same as Brad, but without modifying the SystemInit() function.

    The CubeMX HAL defines as

    void __attribute__((weak)) __initialize_hardware_early(void);
    

    which does - in my case - nothing but calling SystemInit();

    So you can just overwrite this function:

    #include 
    #include "stm32f0xx_hal.h"
    
    #define SYSMEM_RESET_VECTOR            0x1fffC804
    #define RESET_TO_BOOTLOADER_MAGIC_CODE 0xDEADBEEF
    #define BOOTLOADER_STACK_POINTER       0x20002250
    
    uint32_t dfu_reset_to_bootloader_magic;
    
    void __initialize_hardware_early(void)
    {
        if (dfu_reset_to_bootloader_magic == RESET_TO_BOOTLOADER_MAGIC_CODE) {
            void (*bootloader)(void) = (void (*)(void)) (*((uint32_t *) SYSMEM_RESET_VECTOR));
            dfu_reset_to_bootloader_magic = 0;
            __set_MSP(BOOTLOADER_STACK_POINTER);
            bootloader();
            while (42);
        } else {
            SystemInit();
        }
    }
    
    void dfu_run_bootloader()
    {
        dfu_reset_to_bootloader_magic = RESET_TO_BOOTLOADER_MAGIC_CODE;
        NVIC_SystemReset();
    }
    

提交回复
热议问题