Files
GD32_BOOT/Robot_V2/main.cpp
T

111 lines
2.8 KiB
C++
Raw Normal View History

2025-12-27 19:20:23 +08:00
#include "main.h"
2025-12-25 12:07:06 +08:00
#include "Can.h"
2026-01-05 00:52:20 +08:00
#include "config.h"
#include "gd32c10x.h"
2025-12-27 19:20:23 +08:00
#include "initial.h"
#include "sys/OTA.h"
#include "sys/initial.h"
2025-12-25 12:07:06 +08:00
#include "sys/systick.h"
2025-12-27 19:20:23 +08:00
#include <cstdio>
2026-01-05 00:52:20 +08:00
#include <gd32c10x_fmc.h>
2025-12-27 19:20:23 +08:00
extern CAN_transmiter CAN0_transmiter; // CAN初始化
BootloaderSystem sys;
extern OTAHandler ota;
2025-12-27 19:20:23 +08:00
void BootloaderSystem::initHardware() {
systick_config(); // 系统时钟初始化
USART0_init(); // 调试串口初始化
2025-12-27 19:20:23 +08:00
CAN0_transmiter.init();
check_sleep();
2025-12-27 19:20:23 +08:00
}
2025-12-27 19:20:23 +08:00
void BootloaderSystem::run() {
2026-01-05 00:52:20 +08:00
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 DEBUG_OTA
2026-01-05 00:52:20 +08:00
printf("Normal Mode\r\n");
#endif
2026-01-05 00:52:20 +08:00
// 正常模式:跳转到APP
ota.jumpToApplication();
} else if (app_check == OTA_AVAL and
app_enable == OTA_NOT_AVAL) { // APP可用但未激活,延迟进入APP
#if DEBUG_OTA
printf("Delay Boot Mode: APP available but not enabled, jumping in %d ms...\r\n", APP_DELAY_BOOT_TIME);
#endif
// 延迟启动模式:倒计时期间监听CAN,收到OTA请求则进入OTA模式
u32 delay_count = 0;
bool ota_requested = false;
while (delay_count < APP_DELAY_BOOT_TIME) {
// 检查CAN通信并处理接收到的消息
2025-12-27 19:20:23 +08:00
CAN0_transmiter.communication_check();
// 调用ota.process()发送心跳消息(IDAEL状态下会发送CANFD_OTA_WAIT_ID)
2025-12-27 19:20:23 +08:00
ota.process();
// 检查是否收到OTA请求
if (ota.isActive()) {
ota_requested = true;
break;
}
delay_count+=10;
}
if (ota_requested) {
#if DEBUG_OTA
printf("OTA request received during delay, entering OTA mode...\r\n");
#endif
// 收到OTA请求,进入OTA模式
while (1) {
CAN0_transmiter.communication_check();
ota.process();
}
} else {
#if DEBUG_OTA
printf("Delay timeout, jumping to APP...\r\n");
#endif
// 超时未收到OTA请求,跳转APP
ota.jumpToApplication();
2025-12-25 12:07:06 +08:00
}
2026-01-05 00:52:20 +08:00
} 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 DEBUG_OTA
printf("OTA Mode3\r\n");
#endif
CAN0_transmiter.sendBackup();
2026-01-05 00:52:20 +08:00
move_app_data(); // 恢复BACKUP
ota.jumpToApplication(); // 跳转到APP
} else { // backup无效,进入OTA模式
#if DEBUG_OTA
printf("OTA Mode4\r\n");
#endif
2026-01-05 00:52:20 +08:00
// OTA模式
while (1) {
CAN0_transmiter.communication_check();
ota.process();
}
}
2025-12-25 12:07:06 +08:00
}
2025-12-27 19:20:23 +08:00
}
2025-12-25 12:07:06 +08:00
int main(void) {
sys.initHardware();
// test_decrypt();
2025-12-25 12:07:06 +08:00
sys.run();
while (1)
; // 不应该执行到这里
}