#include #include "esp_log.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_task_wdt.h" static const char* TAG = "RustApp"; extern void rust_app_main(); void rust_app_task(void* pvParameters) { ESP_LOGI(TAG, "Rust application task started"); // 订阅看门狗,防止任务被复位 ESP_ERROR_CHECK(esp_task_wdt_add(NULL)); rust_app_main(); // 如果 rust_app_main 返回,记录错误并进入无限循环 ESP_LOGE(TAG, "Rust application task unexpectedly returned!"); while (1) { vTaskDelay(1000 / portTICK_PERIOD_MS); } } static TaskHandle_t rust_app_task_handle = NULL; void rust_app_wrapper_init() { // 使用静态变量确保函数只被调用一次 static bool initialized = false; if (initialized) { ESP_LOGI(TAG, "Rust application already initialized"); return; } initialized = true; ESP_LOGI(TAG, "Initializing Rust application..."); // 创建一个新的任务来运行 Rust 应用 // 增加栈大小到 16384 (16KB) 以防止栈溢出 BaseType_t result = xTaskCreate(rust_app_task, "rust_app_task", 16384, NULL, 5, &rust_app_task_handle); if (result != pdPASS) { ESP_LOGE(TAG, "Failed to create Rust application task!"); } else { ESP_LOGI(TAG, "Rust application task created successfully"); } }