2026-05-31 03:20:26 +08:00
|
|
|
|
/*
|
2026-04-27 00:42:26 +08:00
|
|
|
|
* ESP32-C3FH4 OTA 中继 - 程序入口
|
2026-05-31 03:20:26 +08:00
|
|
|
|
* 四个 FreeRTOS 任务:健康监控 / OTA Relay / WiFi Server / BLE GATT
|
|
|
|
|
|
*
|
|
|
|
|
|
* 使用各模块的单例类进行初始化和任务创建。
|
2026-04-27 00:42:26 +08:00
|
|
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
|
#include "freertos/task.h"
|
|
|
|
|
|
#include "esp_chip_info.h"
|
2026-04-28 01:06:17 +08:00
|
|
|
|
#include "esp_system.h"
|
|
|
|
|
|
#include "esp_netif.h"
|
2026-05-05 03:28:44 +08:00
|
|
|
|
#include "esp_event.h"
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-04-27 02:34:47 +08:00
|
|
|
|
#include "monitor/health_monitor.h"
|
|
|
|
|
|
#include "relay/ota_relay.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"
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
2026-05-05 04:48:31 +08:00
|
|
|
|
/**
|
2026-05-31 03:20:26 +08:00
|
|
|
|
* @brief 程序主入口,初始化系统组件并创建各 FreeRTOS 任务。
|
2026-05-05 04:48:31 +08:00
|
|
|
|
*/
|
2026-04-27 02:34:47 +08:00
|
|
|
|
extern "C" void app_main(void)
|
2026-04-27 00:42:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
esp_chip_info_t chip_info;
|
|
|
|
|
|
esp_chip_info(&chip_info);
|
2026-05-05 03:28:44 +08:00
|
|
|
|
|
|
|
|
|
|
// 初始化网络接口 + 事件循环(必须最早)
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_netif_init());
|
|
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化调试日志(UART0 先就绪,UDP socket 延迟到首次发送时创建)
|
2026-05-31 03:20:26 +08:00
|
|
|
|
DebugLog::instance().init();
|
2026-05-05 03:28:44 +08:00
|
|
|
|
|
|
|
|
|
|
// ★ WiFi 提前初始化(调度器启动前)
|
2026-05-31 03:20:26 +08:00
|
|
|
|
bool wifi_sta_ok = WifiOta::instance().earlyInit();
|
2026-05-05 03:28:44 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
HealthMonitor::instance().init();
|
2026-05-05 03:28:44 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "健康监控状态已初始化");
|
2026-04-27 00:42:26 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "========================================");
|
|
|
|
|
|
DEBUG_INFO("MAIN", "ESP32-C3 OTA Relay v1.0 启动");
|
|
|
|
|
|
DEBUG_INFO("MAIN", "Chip: %s, Rev %d", CONFIG_IDF_TARGET, chip_info.revision);
|
|
|
|
|
|
DEBUG_INFO("MAIN", "Free heap: %lu bytes", esp_get_free_heap_size());
|
2026-04-28 01:06:17 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "CPU Freq: %d MHz", CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ);
|
2026-05-05 03:28:44 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "WiFi: %s", wifi_sta_ok ? "STA 连接中" : "将使用 AP 模式");
|
2026-04-27 00:42:26 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "========================================");
|
|
|
|
|
|
|
2026-05-05 21:55:29 +08:00
|
|
|
|
// 健康监控任务(温度 + WiFi RSSI)
|
2026-04-28 01:06:17 +08:00
|
|
|
|
BaseType_t xRet;
|
|
|
|
|
|
xRet = xTaskCreatePinnedToCore(
|
2026-05-31 03:20:26 +08:00
|
|
|
|
HealthMonitor::taskEntry,
|
2026-04-27 02:34:47 +08:00
|
|
|
|
"health_mon",
|
|
|
|
|
|
4096,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
configMAX_PRIORITIES - 4,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
0
|
|
|
|
|
|
);
|
2026-04-28 01:06:17 +08:00
|
|
|
|
if (xRet != pdPASS) {
|
|
|
|
|
|
DEBUG_ERR("MAIN", "创建 health_monitor_task 失败");
|
|
|
|
|
|
} else {
|
|
|
|
|
|
DEBUG_INFO("MAIN", "健康监控任务已创建");
|
|
|
|
|
|
}
|
2026-04-27 02:34:47 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
Protocol::instance().init();
|
2026-04-27 00:42:26 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "协议层初始化完成");
|
|
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
esp_err_t ret = SpiMaster::instance().init();
|
2026-04-27 00:42:26 +08:00
|
|
|
|
if (ret != ESP_OK) {
|
|
|
|
|
|
DEBUG_ERR("MAIN", "SPI 总线初始化失败: %d", ret);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
DEBUG_INFO("MAIN", "SPI 总线初始化完成");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// OTA 中继任务(中高优先级)
|
|
|
|
|
|
xRet = xTaskCreatePinnedToCore(
|
2026-05-31 03:20:26 +08:00
|
|
|
|
OtaRelay::taskEntry,
|
2026-04-27 00:42:26 +08:00
|
|
|
|
"ota_relay",
|
|
|
|
|
|
8192,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
configMAX_PRIORITIES - 2,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
0 // 核0
|
|
|
|
|
|
);
|
|
|
|
|
|
if (xRet != pdPASS) {
|
|
|
|
|
|
DEBUG_ERR("MAIN", "创建 ota_relay_task 失败");
|
2026-04-28 01:06:17 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
DEBUG_INFO("MAIN", "OTA 中继任务已创建");
|
2026-04-27 00:42:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WiFi OTA 服务器任务
|
|
|
|
|
|
xRet = xTaskCreatePinnedToCore(
|
2026-05-31 03:20:26 +08:00
|
|
|
|
WifiOta::taskEntry,
|
2026-04-27 00:42:26 +08:00
|
|
|
|
"wifi_ota",
|
|
|
|
|
|
8192,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
configMAX_PRIORITIES - 3,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
0
|
|
|
|
|
|
);
|
|
|
|
|
|
if (xRet != pdPASS) {
|
|
|
|
|
|
DEBUG_ERR("MAIN", "创建 wifi_ota_task 失败");
|
2026-04-28 01:06:17 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
DEBUG_INFO("MAIN", "WiFi OTA 任务已创建");
|
2026-04-27 00:42:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// BLE OTA 任务
|
|
|
|
|
|
xRet = xTaskCreatePinnedToCore(
|
2026-05-31 03:20:26 +08:00
|
|
|
|
BleOta::taskEntry,
|
2026-04-27 00:42:26 +08:00
|
|
|
|
"ble_ota",
|
|
|
|
|
|
8192,
|
|
|
|
|
|
NULL,
|
|
|
|
|
|
configMAX_PRIORITIES - 3,
|
|
|
|
|
|
NULL,
|
2026-04-27 02:34:47 +08:00
|
|
|
|
0 // ESP32-C3 单核,固定核0
|
2026-04-27 00:42:26 +08:00
|
|
|
|
);
|
|
|
|
|
|
if (xRet != pdPASS) {
|
|
|
|
|
|
DEBUG_ERR("MAIN", "创建 ble_ota_task 失败");
|
2026-04-28 01:06:17 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
DEBUG_INFO("MAIN", "BLE OTA 任务已创建");
|
2026-04-27 00:42:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-28 01:06:17 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "========================================");
|
2026-04-27 00:42:26 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "所有任务创建完成,OTA Relay 运行中");
|
2026-05-31 03:20:26 +08:00
|
|
|
|
DEBUG_INFO("MAIN", "调试日志输出: UDP(%s:%d) + UART0(GPIO21/TX, 115200)",
|
2026-04-28 01:06:17 +08:00
|
|
|
|
DEBUG_LOG_IP, DEBUG_LOG_PORT);
|
|
|
|
|
|
DEBUG_INFO("MAIN", "========================================");
|
2026-05-31 03:20:26 +08:00
|
|
|
|
}
|