/******************************************************************************* * the includes ******************************************************************************/ #include "bootapp.h" #include "norflash.h" #include "uartapp.h" /******************************************************************************* * the defines ******************************************************************************/ #define ENABLE_INT() __set_PRIMASK(0) /* 使能全局中断 */ #define DISABLE_INT() __set_PRIMASK(1) /* 禁止全局中断 */ /******************************************************************************* * the typedefs ******************************************************************************/ typedef struct { uint32_t startflag; uint32_t len; uint32_t AppCRC; uint32_t reverse; } APPCRC_type; /******************************************************************************* * the globals ******************************************************************************/ /******************************************************************************* * the const ******************************************************************************/ /******************************************************************************* * the functions ******************************************************************************/ void BspQspiBoot_JumpToApp(void) { uint32_t i = 0; void (*AppJump)(void); /* 声明一个函数指针 */ __IO uint32_t AppAddr = 0x90000000; /* APP 地址 */ /* 关闭全局中断 */ DISABLE_INT(); /* 设置所有时钟到默认状态,使用HSI时钟 */ HAL_RCC_DeInit(); /* 关闭滴答定时器,复位到默认值 */ SysTick->CTRL = 0; SysTick->LOAD = 0; SysTick->VAL = 0; /* 关闭所有中断,清除所有中断挂起标志 */ for (i = 0; i < 8; i++) { NVIC->ICER[i] = 0xFFFFFFFF; NVIC->ICPR[i] = 0xFFFFFFFF; } /* 使能全局中断 */ ENABLE_INT(); /* 跳转到应用程序,首地址是MSP,地址+4是复位中断服务程序地址 */ AppJump = (void (*)(void))(*((uint32_t*)(AppAddr + 4))); /* 设置主堆栈指针 */ __set_MSP(*(uint32_t*)AppAddr); /* 在RTOS工程,这条语句很重要,设置为特权级模式,使用MSP指针 */ __set_CONTROL(0); /* 跳转到系统BootLoader */ AppJump(); /* 跳转成功的话,不会执行到这里,用户可以在这里添加代码 */ } void WriteCRCInfo(uint32_t len, uint32_t crc) { APPCRC_type appcrc; appcrc.startflag = 0xAA5555AA; appcrc.len = len; appcrc.AppCRC = crc; appcrc.reverse = 0xAAAAAAAA; NORFLASH_Write_NoCheck((uint8_t*)&appcrc, EX_FLASH_CRC_ADDR, 0x10); } uint8_t ReadCRCInfo(uint32_t* len, uint32_t* crc) { APPCRC_type* appcrc = (APPCRC_type*)EX_FLASH_CRC_ADDR; if (appcrc->startflag == 0xAA5555AA) { *len = appcrc->len; *crc = appcrc->AppCRC; return 1; } else { return 0; } } uint8_t Boot_checkApp(void) { uint32_t len, crc; if (ReadCRCInfo(&len, &crc) == 1) { if (crc_calc(EX_FLASH_START_ADDR, len) == crc) { return 1; } else { return 0; } } return 0; }