From ea4f688ba0b85db689b1104233ea3bbf30cc03fb Mon Sep 17 00:00:00 2001 From: LokLiang Date: Wed, 9 Jul 2025 14:02:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20OTA=20=E4=BD=8D=E7=BD=AE?= =?UTF-8?q?=EF=BC=8C=E7=A7=BB=E9=99=A4=E6=B5=8B=E8=AF=95=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/CMakeLists.txt | 1 + app/app_main.c | 8 ++- {main => app}/ota.c | 0 {main => app}/ota.h | 0 main/CMakeLists.txt | 1 - main/main.c | 3 - main/test.c | 161 -------------------------------------------- 7 files changed, 7 insertions(+), 167 deletions(-) rename {main => app}/ota.c (100%) rename {main => app}/ota.h (100%) delete mode 100644 main/test.c diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index 7b25bf2..c07a603 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -12,6 +12,7 @@ list(APPEND incs "lib-out") list(APPEND srcs "app_main.c") list(APPEND srcs "app_info.c") +list(APPEND srcs "ota.c") list(APPEND srcs "console.c") list(APPEND srcs "drivers/data_port/sb_data_port.c") list(APPEND srcs "drivers/data_port/uart/uart_port.c") diff --git a/app/app_main.c b/app/app_main.c index 6251437..2bcc784 100644 --- a/app/app_main.c +++ b/app/app_main.c @@ -6,6 +6,7 @@ #include /* 配置 */ +#include "ota.h" #include "config/board_config.h" #include "config/app_config.h" @@ -50,6 +51,9 @@ void work_app_main(void *arg) board_cfg_init(); // 板级配置 app_cfg_init(); // 应用配置 + /* 初始化 OTA 功能*/ + ota_partition_check(); + /* 初始化其他优先级的工作队列 */ _init_work_q(); @@ -87,9 +91,9 @@ static void _init_work_q(void) static void _init_ws2812(void) { #if (CONFIG_LED_STRIP_MAX_LEDS) - ws2812_spi_led_strip_init(SPI2_HOST, CONFIG_LED_STRIP_MAX_LEDS); + ws2812_spi_led_strip_init(g_cfg_board->led_spi.spi_id, CONFIG_LED_STRIP_MAX_LEDS); #else - ws2812_spi_led_strip_init(SPI2_HOST, 10); + ws2812_spi_led_strip_init(g_cfg_board->led_spi.spi_id, 10); #endif } diff --git a/main/ota.c b/app/ota.c similarity index 100% rename from main/ota.c rename to app/ota.c diff --git a/main/ota.h b/app/ota.h similarity index 100% rename from main/ota.h rename to app/ota.h diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 6e0a5ed..4954578 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -5,7 +5,6 @@ list(APPEND incs "../components/system/include") # SRCS list(APPEND srcs "main.c") -list(APPEND srcs "ota.c") # idf_component_register diff --git a/main/main.c b/main/main.c index e9cc13f..3879706 100755 --- a/main/main.c +++ b/main/main.c @@ -33,9 +33,6 @@ static void _init_prev_nvs(void) int app_main(void) { - /* ota */ - ota_partition_check(); - /* 初始化平台相关 */ _init_prev_nvs(); diff --git a/main/test.c b/main/test.c deleted file mode 100644 index d56d22f..0000000 --- a/main/test.c +++ /dev/null @@ -1,161 +0,0 @@ -#include -#include -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "freertos/semphr.h" -#include "esp_log.h" - -// 调试宏 -#define SHOW_TIME_TOKEN 1 // 耗时测试开关 -#ifdef SHOW_TIME_TOKEN -#define RECURSIVE_NUM 100 // 递归进入和退出临界区次数 -#endif - -#define COUT_TO_NS(x) ((x * 1000) / 160) - -static struct -{ - atomic_int suspend_count; - TaskHandle_t task_handle; -} cores_suspend_handle[portNUM_PROCESSORS] = {0}; - -void cores_suspend(void *arg) -{ - while (1) - { - while (atomic_load(&cores_suspend_handle[xPortGetCoreID()].suspend_count) > 0) - { - __asm__ __volatile__("nop"); - } - vTaskSuspend(NULL); - } -} - -void enter_critical(void) -{ - if (atomic_fetch_add(&cores_suspend_handle[xPortGetCoreID()].suspend_count, 1) == 0) - { - // 唤醒其他核的高优先级任务 - for (int i = 0; i < portNUM_PROCESSORS; i++) - { - if (i == xPortGetCoreID()) - { - continue; - } - vTaskResume(cores_suspend_handle[i].task_handle); - } - vTaskSuspendAll(); - } -} - -void exit_critical(void) -{ - if (atomic_fetch_sub(&cores_suspend_handle[xPortGetCoreID()].suspend_count, 1) == 1) - { - xTaskResumeAll(); - } -} - -/* 竞争测试,验证互斥 */ -static int shared_data = 0; - -void taskA(void *pvParameters) -{ - int cnt = 0; - while (1) - { - enter_critical(); - - int before = shared_data; - shared_data++; - - exit_critical(); - if (shared_data != before + 1) - { - ESP_LOGE("TaskA", "发生竞争"); - } - else - { - if (++cnt % 100 == 0) - { - ESP_LOGI("TaskA", "未发生竞争"); - } - } - vTaskDelay(pdMS_TO_TICKS(10)); - } -} - -void taskB(void *pvParameters) -{ - int cnt = 0; - while (1) - { - enter_critical(); - enter_critical(); - // 临界区开始 - - int before = shared_data; - shared_data++; - // 临界区结束 - - exit_critical(); - exit_critical(); - - if (shared_data != before + 1) - { - ESP_LOGE("TaskB", "发生竞争"); - } - else - { - if (++cnt % 100 == 0) - { - ESP_LOGI("TaskB", "未发生竞争"); - } - } - vTaskDelay(pdMS_TO_TICKS(1)); - } -} - -int app_main(void) -{ - // 初始化 - for (int i = 0; i < portNUM_PROCESSORS; i++) - { - xTaskCreatePinnedToCore(cores_suspend, "cores_suspend", configMINIMAL_STACK_SIZE, NULL, configMAX_PRIORITIES - 1, &cores_suspend_handle[i].task_handle, i); - } - - // 竞争测试 - xTaskCreatePinnedToCore(taskA, "TaskA", 4096, NULL, 5, NULL, 0); - xTaskCreatePinnedToCore(taskB, "TaskB", 4096, NULL, 6, NULL, 1); - -#ifdef SHOW_TIME_TOKEN - // 单次耗时测试 - uint32_t start_time = esp_cpu_get_cycle_count(); - enter_critical(); - int32_t enter_time = (esp_cpu_get_cycle_count() - start_time); - start_time = esp_cpu_get_cycle_count(); - exit_critical(); - int32_t exit_time = (esp_cpu_get_cycle_count() - start_time); - - ESP_LOGI("单次耗时", "进入时间:%ld ns, 退出时间:%ld ns", COUT_TO_NS(enter_time), COUT_TO_NS(exit_time)); - - printf("\r\n"); - - // 递归测试 - start_time = esp_cpu_get_cycle_count(); - for (int i = 0; i < RECURSIVE_NUM; i++) - { - enter_critical(); - } - enter_time = (esp_cpu_get_cycle_count() - start_time); - start_time = esp_cpu_get_cycle_count(); - for (int i = 0; i < RECURSIVE_NUM; i++) - { - exit_critical(); - } - exit_time = (esp_cpu_get_cycle_count() - start_time); - - ESP_LOGI("递归耗时", "递归次数:%d, 进入总时间:%ld ns, 退出总时间: %ld ns", RECURSIVE_NUM, COUT_TO_NS(enter_time), COUT_TO_NS(exit_time)); -#endif - return 0; -}