#include "bootloader.h" #if BOOTLOADER_CODE_FLAG #include "stm32f10x_flash.h" const uint8_t BootloaderSwVersion[] = BOOTLOADER_SOFTWARE_VERSION; #endif uint8_t updataflag = false; uint8_t Checksum(uint32_t address, uint32_t length) { //uint32_t checksum = 0; return 1; } typedef void (*pFunction)(void); static pFunction Jump_To_Application; static uint32_t JumpAddress = 0; void BootloaderInit(void) { } void JumpToApplicationSW(void) { #if BOOTLOADER_CODE_FLAG __disable_irq();//关全局中断 if (IsExternProgrammingRequestValid()) { updataflag = true; ClearExternProgrammingRequest(); } else if (((*(__IO uint32_t *)APP_FlashBaseAddress) & 0x2FFE0000) == MCU_RAM_StartAddress) { SetBootloaderSwVersionAddress(); JumpAddress = *(__IO uint32_t *)(APP_FlashBaseAddress + 4); Jump_To_Application = (pFunction) JumpAddress; __set_MSP(*(__IO uint32_t *) APP_FlashBaseAddress); Jump_To_Application(); } __enable_irq(); #endif } #if BOOTLOADER_CODE_FLAG void RAMFLASHInit(void) { } status_t EraseApplicationSoftwareCode(uint32_t address, uint32_t length) { status_t ret = STATUS_SUCCESS; uint32_t page_address = 0; if (address % FMC_PAGE_SIZE) { return STATUS_ERROR; } FLASH_Unlock(); /* 清空所有标志位 */ FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); page_address = address; while(page_address < address+length) { if(FLASH_COMPLETE != FLASH_ErasePage(page_address)) { ret = STATUS_ERROR; break; } page_address += FMC_PAGE_SIZE; } FLASH_Lock(); return ret; } status_t ProgramApplicationSoftware(uint32_t address, uint8_t *pdata, uint32_t length) { status_t ret = STATUS_SUCCESS; uint32_t program_address = 0; FLASH_Unlock(); /* 清空所有标志位 */ FLASH_ClearFlag(FLASH_FLAG_BSY | FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR); /* Program Flash Bank1 */ program_address = address; while(program_address < (address + length)) { FLASH_ProgramWord(program_address, *(uint32_t *)pdata); if (*(uint32_t*)program_address != *(uint32_t*)pdata) { ret = STATUS_ERROR; // 读出校验 break; } program_address += 4; pdata += 4; } FLASH_Lock(); return ret; } #endif uint8_t GetUdsUpdataFlag(void) { if (updataflag) { updataflag = 0; return 1; } return 0; } void ClearExternProgrammingRequest(void) { BKP->DR39 = 0x00; BKP->DR40 = 0x00; } void SetExternProgrammingRequest(void) { BKP->DR39 = (uint16_t)(BOOTLOADER_PROGRAMMING_FLAG >> 0); BKP->DR40 = (uint16_t)(BOOTLOADER_PROGRAMMING_FLAG >> 16); } bool IsExternProgrammingRequestValid(void) { bool status = false; uint32_t flag; flag = BKP->DR40; flag = (flag << 16) | BKP->DR39; if (flag == BOOTLOADER_PROGRAMMING_FLAG) { status = true; } return status; } #if BOOTLOADER_CODE_FLAG void SetBootloaderSwVersionAddress(void) { BKP->DR41 = (uint16_t)((uint32_t)BootloaderSwVersion >> 0); BKP->DR42 = (uint16_t)((uint32_t)BootloaderSwVersion >> 16); } #endif uint32_t GetBootloaderSwVersionAddress(void) { static uint32_t bl_sw_addr; bl_sw_addr = BKP->DR42; bl_sw_addr = (bl_sw_addr << 16) | BKP->DR41; return bl_sw_addr; }