01-Jump
5/20/26About 2 min
01-Jump
Basic Info
The stm32f103c8t6 has 64KB of flash and 20KB of SRAM
Create an APP
Configure Debug

Configure the Clock


Configure the LED

Other


Code

Once you've written it, flash it directly and see whether it runs properly: the LED blinks once per second, and it sends app is running to UART1 at the same time. Once that works, move on to the next step
The code isn't finished — you have to read to the very end!!!
Create the Boot Project
Configuration
The configuration is the same as the app above, except there's one extra button
Change the Optimization
During the boot development phase you must change the optimization to -O0, and only switch it to -O3 or whatever else once things work!!!
Getting the Binary File
Use fromelf --bin -o "$L@L.bin" "#L; after each build, a binary file is generated in the build folder
Code
#define APP_ADDRESS 0x08005000U // Points to the location we will jump to later
void JumpToApplication(void) {
uint32_t app_sp = *(__IO uint32_t*)APP_ADDRESS; // Read from the specified location to check whether the SP is valid
uint32_t app_pc = *(__IO uint32_t*)(APP_ADDRESS + 4); // Read from the specified location to check whether the PC is valid
// Print them out
char data[50]="";
HAL_UART_Transmit(&huart1,(uint8_t*)"boot start run!\r\n",sizeof("boot start run!\r\n"),1000);
sprintf(data,"SP:0x%08X,PC:0x%08X\r\n",app_sp,app_pc);
HAL_UART_Transmit(&huart1,(uint8_t*)data,strlen(data),1000);
// Check whether the stack pointer is valid
if((app_sp & 0x2FFE0000) == 0x20000000) {
// 1. Completely "disarm" all peripherals and clear every potential interrupt source
HAL_RCC_DeInit();
HAL_DeInit();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
// 2. Clear the NVIC pending flags to make sure the battlefield is absolutely clean
for (uint32_t i = 0; i < 8; i++) {
NVIC->ICER[i] = 0xFFFFFFFF;
NVIC->ICPR[i] = 0xFFFFFFFF;
}
// 3. Redirect the interrupt vector table
SCB->VTOR = APP_ADDRESS;
// 4. Enable global interrupts (since the peripherals have been completely disarmed, it is absolutely safe to open the gate now, and it will save the App's HAL_Delay)
__enable_irq();
// 5. Danger zone: switch the stack pointer and jump directly (no local variables allowed)
__set_MSP(*(__IO uint32_t*)APP_ADDRESS);
((void (*)(void)) (*(__IO uint32_t*)(APP_ADDRESS + 4)))();
} else {
HAL_UART_Transmit(&huart1,(uint8_t*)"[BOOT] ERROR: SP Validation FAILED!\r\n",sizeof("ERROR: SP Validation FAILED!\r\n"),1000);
}
}Changelog
9/25/26, 12:44 PM
View All Changelog
0ca31-on