- 新增 SystemBoot 类用于跟踪各模块初始化状态,提供位图标记与报告功能 - 在 main.cpp 中重构初始化流程,按顺序初始化各模块并记录状态 - 添加 system_boot.cpp 模块文件到构建配置 - CAN反馈任务增加冷启动延迟,避免与GD32初始化窗口冲突 refactor(ota): 优化OTA协调器线程安全机制 - 修改 lock()/unlock() 方法返回take结果,确保mutex正确释放 - 修复多任务并发时mutex死锁问题,避免vTaskPriorityDisinherit断言 - 统一finalize失败后的状态复位逻辑,确保协调器回到IDLE状态 - 优化忙状态超时检测与自愈机制 perf(boot): 优化冷启动时序与资源管理 - 简化README中冷启动优化说明,聚焦核心逻辑 - 优化BOD阈值设置为LVL5,改进电源稳定性 - 增加BLE OTA工作线程优先级,减少丢包情况 - 优化堆告警节流机制,在OTA期间降低告警频率 fix(relay): 改进GD32应用存活检测逻辑 - 调整GD32 APP存活验证时序,增加会话建立验证 - 修复HELLO建立session后未验证role的问题 - 优化FINISH后等待APP启动的静默时间与重试机制 - 统一GD32探测逻辑到OTA中继任务 refactor(server): 优化HTTP服务端内存与性能 - 使用静态缓冲区替代动态分配,减少堆碎片化风险 - 优化CAN批量发送期间暂停反馈任务的机制 - 调整BLE连接参数请求逻辑,提升传输性能 - 改进错误处理流程,避免堆紧张时的额外分配 bump(version): 更新固件版本至0.3.50 - 更新版本号从0.3.49到0.3.50 - 更新编译时间戳与Git哈希值
145 lines
4.4 KiB
C++
145 lines
4.4 KiB
C++
/*
|
||
* ESP32-C3FH4 OTA 中继 - 程序入口
|
||
*
|
||
* 初始化顺序(每位成功后 SystemBoot::mark=1,末尾统一 report):
|
||
* 1. 冷启动延时
|
||
* 2. netif / event_loop / debug_log
|
||
* 3. wifi earlyInit
|
||
* 4. health_mon + task
|
||
* 5. cpu_stats(可选)
|
||
* 6. protocol / ota_coord
|
||
* 7. spi init + L0 本机自检 → mark spi;HELLO 对端轻检 → mark can_link(非关键)
|
||
* 8. ota_relay / wifi_ota / ble_ota 任务
|
||
* 9. can_fb 任务(依赖 spi L0)
|
||
* 10. ota_mark_valid(可选)
|
||
* 11. SystemBoot::report()
|
||
*/
|
||
#include "freertos/FreeRTOS.h"
|
||
#include "freertos/task.h"
|
||
#include "esp_system.h"
|
||
#include "esp_netif.h"
|
||
#include "esp_event.h"
|
||
#include "esp_ota_ops.h"
|
||
|
||
#include "system_boot.h"
|
||
#include "monitor/health_monitor.h"
|
||
#include "monitor/cpu_stats.h"
|
||
#include "relay/ota_relay.h"
|
||
#include "ota/ota_coordinator.h"
|
||
#include "transport/spi_master.h"
|
||
#include "server/wifi_ota.h"
|
||
#include "server/ble_ota.h"
|
||
#include "transport/protocol.h"
|
||
#include "monitor/debug_log.h"
|
||
#include "app_config.h"
|
||
#include "ota/esp32_ota_core.h"
|
||
#include "monitor/can_feedback_task.h"
|
||
|
||
/** @brief 创建关键任务;成功返回 true。 */
|
||
static bool createCriticalTask(TaskFunction_t entry, const char *name,
|
||
uint32_t stack, UBaseType_t prio)
|
||
{
|
||
return xTaskCreatePinnedToCore(entry, name, stack, NULL, prio, NULL, 0) == pdPASS;
|
||
}
|
||
|
||
/**
|
||
* @brief 程序主入口:按模块初始化并汇总结果。
|
||
*/
|
||
extern "C" void app_main(void)
|
||
{
|
||
SystemBoot::reset();
|
||
|
||
/* 上电/欠压复位后短延时再初始化子系统 */
|
||
{
|
||
esp_reset_reason_t rr = esp_reset_reason();
|
||
if (rr == ESP_RST_POWERON || rr == ESP_RST_BROWNOUT) {
|
||
vTaskDelay(pdMS_TO_TICKS(200));
|
||
}
|
||
}
|
||
|
||
Esp32OtaCore::initMutex();
|
||
|
||
if (esp_netif_init() == ESP_OK) {
|
||
SystemBoot::mark(BOOT_NETIF);
|
||
}
|
||
|
||
if (esp_event_loop_create_default() == ESP_OK) {
|
||
SystemBoot::mark(BOOT_EVENT_LOOP);
|
||
}
|
||
|
||
DebugLog::instance().init();
|
||
SystemBoot::mark(BOOT_DEBUG_LOG);
|
||
|
||
/* earlyInit:true=已启动 STA 连接流程;false=将走 AP */
|
||
const bool wifi_early_started_sta = WifiOta::instance().earlyInit();
|
||
SystemBoot::mark(BOOT_WIFI_EARLY);
|
||
|
||
HealthMonitor::instance().init();
|
||
SystemBoot::mark(BOOT_HEALTH_MON);
|
||
|
||
if (createCriticalTask(HealthMonitor::taskEntry, "health_mon",
|
||
TASK_STACK_HEALTH_MON, configMAX_PRIORITIES - 4)) {
|
||
SystemBoot::mark(BOOT_HEALTH_TASK);
|
||
}
|
||
|
||
CpuStats::instance().start();
|
||
SystemBoot::mark(BOOT_CPU_STATS);
|
||
|
||
Protocol::instance().init();
|
||
SystemBoot::mark(BOOT_PROTOCOL);
|
||
|
||
OtaCoordinator::instance().init();
|
||
OtaCoordinator::instance().setBleCoexListener(&WifiOta::instance());
|
||
SystemBoot::mark(BOOT_OTA_COORD);
|
||
|
||
bool spi_l0_ok = false;
|
||
{
|
||
esp_err_t ret = SpiMaster::instance().init();
|
||
if (ret == ESP_OK && SpiMaster::instance().selfTestL0()) {
|
||
SystemBoot::mark(BOOT_SPI);
|
||
spi_l0_ok = true;
|
||
/* 对端轻检:GD32 可能仍在启动延时,失败不拉垮关键启动 */
|
||
if (SpiMaster::instance().selfTestPeerL0()) {
|
||
SystemBoot::mark(BOOT_CAN_LINK);
|
||
}
|
||
}
|
||
}
|
||
|
||
if (createCriticalTask(OtaRelay::taskEntry, "ota_relay",
|
||
TASK_STACK_OTA_RELAY, configMAX_PRIORITIES - 2)) {
|
||
SystemBoot::mark(BOOT_OTA_RELAY_TASK);
|
||
}
|
||
|
||
if (createCriticalTask(WifiOta::taskEntry, "wifi_ota",
|
||
TASK_STACK_WIFI_OTA, configMAX_PRIORITIES - 3)) {
|
||
SystemBoot::mark(BOOT_WIFI_TASK);
|
||
}
|
||
|
||
if (createCriticalTask(BleOta::taskEntry, "ble_ota",
|
||
TASK_STACK_BLE_OTA, configMAX_PRIORITIES - 3)) {
|
||
SystemBoot::mark(BOOT_BLE_TASK);
|
||
}
|
||
|
||
/* CAN 反馈依赖 SPI 本机 L0 */
|
||
if (spi_l0_ok && startCanFeedbackTask()) {
|
||
SystemBoot::mark(BOOT_CAN_FB_TASK);
|
||
}
|
||
|
||
if (esp_ota_mark_app_valid_cancel_rollback() == ESP_OK) {
|
||
SystemBoot::mark(BOOT_OTA_VALID);
|
||
}
|
||
|
||
DEBUG_INFO("MAIN", "ESP32-C3 OTA Relay %s, heap=%lu, wifi_early=%s",
|
||
FW_VERSION,
|
||
(unsigned long)esp_get_free_heap_size(),
|
||
wifi_early_started_sta ? "STA" : "AP/other");
|
||
|
||
const bool critical_ok = SystemBoot::report();
|
||
|
||
if (!critical_ok) {
|
||
DEBUG_ERR("MAIN", "critical init FAIL, restart in 3s");
|
||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||
esp_restart();
|
||
}
|
||
}
|