//定义一个函数类型的参数.这里iapfun是一个函数指针
typedef void (*iapfun)(void);
//跳转到应用程序段
//appxaddr:用户代码起始地址.
void iap_load_app(uint32_t appxaddr)
{
iapfun jump2app;//定义一个函数指针变量
__disable_irq();
if(((*(__IO uint32_t*)appxaddr)&0x2FFE0000)==0x20000000) //检查栈顶地址是否合法.
{
//HAL_NVIC_DisableIRQ(DMA1_Channel2_3_IRQn);
//HAL_NVIC_DisableIRQ(DMA1_Channel4_5_6_7_IRQn);
//HAL_NVIC_DisableIRQ(USART1_IRQn);
//HAL_NVIC_DisableIRQ(USART2_IRQn);
//HAL_NVIC_DisableIRQ(USART3_4_IRQn);
//HAL_NVIC_DisableIRQ(SysTick_IRQn);
//函数指针实例化,给指针赋值,APP代码区第二个字内存的数据为程序开始地址(复位地址)
jump2app=(iapfun)*(__IO uint32_t*)(appxaddr+4);
//初始化APP堆栈指针(APP代码区的第一个字内存的数据为栈顶地址)
__set_MSP(*(__IO uint32_t*)appxaddr);
//跳转到APP.函数指针在前面已经实例化,这里直接用
jump2app();
}
}
/**
\brief Set Main Stack Pointer
\details Assigns the given value to the Main Stack Pointer (MSP).
\param [in] topOfMainStack Main Stack Pointer value to set
*/
__STATIC_INLINE void __set_MSP(uint32_t topOfMainStack)
{
register uint32_t __regMainStackPointer __ASM("msp");
__regMainStackPointer = topOfMainStack;
}/**
|