Error jumping from bootloader to application with STM32F412

  Kiến thức lập trình

I have an issue with jumping from the bootloader to the application.
If I make the jump before entering the infinite loop, everything works, but if I do it from inside the loop, it doesn’t work.
I also tried putting a condition that makes it exit the loop and then execute the jump outside after passing through the loop, but even in this case, it doesn’t work.
If I step through the bootloader code, I reach the last instruction (pmain_app()) without any problems.
I have conducted various tests, and the only way to make it work inside the loop is by not initializing any peripherals.
I don’t know what else to try.

#include "main.h"
#include "dma.h"
#include "fatfs.h"
#include "spi.h"
#include "usb_host.h"
#include "gpio.h"
#include "crc.h"

extern TIM_HandleTypeDef htim11;
extern const uint32_t   app_vector;  //app_vector is defined in ld file

#define APPLICATION_START_ADDRESS       (uint32_t *)&app_vector 

void SystemClock_Config(void);
void MX_USB_HOST_Process(void);
void JumpToApplication(void);


int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_GPIO_Init();
    __enable_irq();

    volatile static int xxx;

    //JumpToApplication() //Here works

    while(true)
    {
        retStatus_t ret = retNothing;

        if(xxx++ >= 10000) ret = retJump;

        switch(ret)
        {
            case retNothing:
            break;

            case retJump:
                __disable_irq();
                JumpToApplication(); //Here it only works if I don't initialize any peripherals.
            break;

            case retReset:
                NVIC_SystemReset();
            break;
        }
    }
    //JumpToApplication() //Here doesn't work
}

void JumpToApplication(void)
{
    __HAL_RCC_GPIOA_CLK_DISABLE();
    __HAL_RCC_GPIOB_CLK_DISABLE();
    __HAL_RCC_GPIOC_CLK_DISABLE();
    __HAL_RCC_GPIOD_CLK_DISABLE();

    HAL_TIM_Base_DeInit(&htim11);

    void (*pmain_app)(void);

    __disable_irq();

    __HAL_RCC_CRC_CLK_DISABLE();

    SCB->VTOR = (uint32_t)APPLICATION_START_ADDRESS;
    __set_MSP((uint32_t)*APPLICATION_START_ADDRESS);
    pmain_app = (void (*)(void)) * (APPLICATION_START_ADDRESS + 4);

    __DSB();
    __ISB();

    pmain_app();
}

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT