目录
一、bootloader关键代码
#define APPLICATION_ADDRESS (uint32_t)0x80004000 // app所在地址
typedef void (*pFunction)(void);
pFunction JumpToApplication;
uint32_t JumpAddress;
int main(void)
{
...
if (((*(__IO uint32_t *)APPLICATION_ADDRESS) & 0xFFFF8000) == 0x20000000) {
JumpAddress = *(__IO uint32_t *)(APPLICATION_ADDRESS + 4);
JumpToApplication = (pFunction)JumpAddress;
// 设置堆栈
__set_MSP(*(__IO uint32_t *)APPLICATION_ADDRESS);
// 跳转到app
JumpToApplication();
}
return 0;
}
这里if条件判断主要是为了确保堆栈的地址在内存大小的地址范围内。
二、application关键代码
#define APPLICATION_ADDRESS (uint32_t)0x08004000
#if (defined ( __CC_ARM ))
__IO uint32_t VectorTable[48] __attribute__((at(0x20000000)));
#elif (defined (__ICCARM__))
#pragma location = 0x20000000
__no_init __IO uint32_t VectorTable[48];
#elif defined ( __GNUC__ )
__IO uint32_t VectorTable[48] __attribute__((section(".RAMVectorTable")));
#endif
int main(void)
{
...
/* Copy the vector table from the Flash (mapped at the base of the application
load address 0x08004000) to the base address of the SRAM at 0x20000000. */
for(i = 0; i < 48; i++) {
VectorTable[i] = *(__IO uint32_t*)(APPLICATION_ADDRESS + (i<<2));
}
/* Enable the SYSCFG peripheral clock*/
__HAL_RCC_SYSCFG_CLK_ENABLE();
/* Remap SRAM at 0x00000000 */
__HAL_SYSCFG_REMAPMEMORY_SRAM();
...
return 0;
}
这里比较关键的是中断向量表的搬移,因为Cortex M0没有VTOR这个寄存器,所以需要将__Vectors至__Vectors_End之间的内容拷贝到0x20000000处。
再一个就是将内存映射到0x00000000处,即执行__HAL_SYSCFG_REMAPMEMORY_SRAM()调用。