Help to Make Jump from Bootloader to Application Work
Hi,
I am trying to make the jump work with an F103RC, and have read many posts in this community, but the jump simply does not work for me. The jump code is mainly from DavidNaviaux in the following post:
The code in bootloader is downloaded to 0x08000000 via ST-Link:
/* USER CODE BEGIN PD */
#define APP_ADDR 0x08010000 // MCU app code base address
#define MCU_IRQS 102u // no. of NVIC IRQ inputs
struct app_vectable_
{
uint32_t Initial_SP;
void (*Reset_Handler)(void);
};
#define APPVTAB ((struct app_vectable_ *)APP_ADDR)
/* USER CODE END PD */
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart5;
/* USER CODE BEGIN PV */
int printOnce = 1;
uint32_t startTick;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_UART5_Init(void);
/* USER CODE BEGIN PFP */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
/* USER CODE END PFP */
/**
* Bootloader Code
*/
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_UART5_Init();
/* USER CODE BEGIN 2 */
startTick = HAL_GetTick();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if (printOnce == 1)
{
printf("This is in Bootloader\n");
printOnce = 0;
}
if ((HAL_GetTick() - startTick) > 1000)
{
/* Set the clock to the default state */
HAL_UART_DeInit(&huart5);
HAL_UART_MspDeInit(&huart5);
HAL_RCC_DeInit();
HAL_DeInit();
/* Disable all interrupts */
__disable_irq();
// __disable_interrupt();
/* Disable Systick timer */
SysTick->CTRL = 0;
/* Clear Interrupt Enable Registers & Interrupt Pending Registers */
for (uint8_t i = 0; i < (MCU_IRQS + 31u) / 32; i++)
{
NVIC->ICER[i]=0xFFFFFFFF;
NVIC->ICPR[i]=0xFFFFFFFF;
}
// set the vector table address to the application vector table
SCB->VTOR = APP_ADDR;
// Set the stack pointer
__set_MSP(APPVTAB->Initial_SP);
__DSB(); // Ensure the VTOR and SP operations are complete
__ISB(); // Flush the pipeline because of SP change
/* Re-enable all interrupts */
__enable_irq();
// and now jump to the application vector
APPVTAB->Reset_Handler();
while (1)
{
}
}
}
/* USER CODE END 3 */
}
and the code in the App to which the bootloader jumps to is downloaded to 0x08010000 via ST-Link:
/* Private variables ---------------------------------------------------------*/
UART_HandleTypeDef huart5;
/* USER CODE BEGIN PV */
int tick = 0;
uint32_t startTick;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_UART5_Init(void);
/* USER CODE BEGIN PFP */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
/* USER CODE END PFP */
/**
* App Code
*/
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_UART5_Init();
/* USER CODE BEGIN 2 */
startTick = HAL_GetTick();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if ((HAL_GetTick() - startTick) > 1000)
{
tick++;
startTick = HAL_GetTick();
printf("App tick = %ds\n", tick);
}
}
/* USER CODE END 3 */
}
Please help!
