Files
ESP32_COMM/main/core/main.cpp
T
小龙虾 2138a572ee feat(esp32): 添加多电机OTA中继功能并优化调试日志系统
- 新增多电机状态、版本和OTA中继监控模块(monitor/motor_state.cpp,
  monitor/motor_version.cpp, monitor/motor_ota_relay.cpp)
- 在CAN反馈任务中添加0xC5/0xC6/0xD2类型消息处理,支持多电机状态推送
- 重构调试日志系统,将一次性JSON分配改为512B栈缓冲流式发送,降低内存峰值
- 调试UART波特率从115200提升至30000
- 修改版本管理方式,从自动生version.h改为手动维护
- 更新VSCode设置以支持ESP-IDF开发环境配置
- 在OTA协调器中添加电机OTA互斥机制,防止并发冲突
- 完善中继状态机异常处理,添加远程取消通知机制
2026-07-26 20:40:16 +08:00

148 lines
4.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* 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"
#include "monitor/motor_ota_relay.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);
}
motorOtaRelayInit();
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();
}
}