#include "main.h" #include "Can.h" #include "config.h" #include "gd32c10x.h" #include "initial.h" #include "sys/OTA.h" #include "sys/initial.h" #include "sys/systick.h" #include #include extern CAN_transmiter CAN0_transmiter; // CAN初始化 BootloaderSystem sys; extern OTAHandler ota; void BootloaderSystem::initHardware() { systick_config(); // 系统时钟初始化 // USART0_init(); // 调试串口初始化 CAN0_transmiter.init(); check_sleep(); } void BootloaderSystem::run() { volatile u32 *flash_ptr = (volatile u32 *)n_APP_LATEST_EN_ADDR; u32 app_enable = *flash_ptr; flash_ptr = (volatile u32 *)n_APP_LATEST_CHECK_ADDR; u32 app_check = *flash_ptr; if (app_check == OTA_AVAL and app_enable == OTA_AVAL) { // APP已确认且激活,直接进入 #if OTA_DEBUG printf("Normal Mode\r\n"); #endif // 正常模式:跳转到APP ota.jumpToApplication(); } else if (app_check == OTA_AVAL and app_enable == OTA_NOT_AVAL) { // 手动禁用APP,进入OTA模式 #if OTA_DEBUG printf("OTA Mode1\r\n"); #endif // OTA模式 while (1) { CAN0_transmiter.communication_check(); ota.process(); } } else { // APP无效,确认backup volatile u32 *flash_ptr = (volatile u32 *)n_APP_BACKUP_AVAL_ADDR; u32 back_aval = *flash_ptr; flash_ptr = (volatile u32 *)n_APP_BACKUP_CHECK_ADDR; u32 back_check = *flash_ptr; if (back_check == OTA_AVAL and back_aval == OTA_AVAL) { // backup已确认且激活,复制到主APP区域 #if OTA_DEBUG printf("OTA Mode3\r\n"); #endif CAN0_transmiter.sendBackup(); move_app_data(); // 恢复BACKUP ota.jumpToApplication(); // 跳转到APP } else { // backup无效,进入OTA模式 #if OTA_DEBUG printf("OTA Mode4\r\n"); #endif // OTA模式 while (1) { CAN0_transmiter.communication_check(); ota.process(); } } } } int main(void) { sys.initHardware(); // scanAppFlashContent(); // test_decrypt(); sys.run(); while (1) ; // 不应该执行到这里 }