更新 OTA 位置,移除测试文件

This commit is contained in:
LokLiang
2025-07-09 14:02:53 +08:00
parent 9110aeabf0
commit ea4f688ba0
7 changed files with 7 additions and 167 deletions

View File

@@ -5,7 +5,6 @@ list(APPEND incs "../components/system/include")
# SRCS
list(APPEND srcs "main.c")
list(APPEND srcs "ota.c")
# idf_component_register

View File

@@ -33,9 +33,6 @@ static void _init_prev_nvs(void)
int app_main(void)
{
/* ota */
ota_partition_check();
/* 初始化平台相关 */
_init_prev_nvs();

View File

@@ -1,75 +0,0 @@
#include "ota.h"
#include "esp_partition.h"
#include "esp_ota_ops.h"
#include "esp_flash_partitions.h"
#define CONFIG_SYS_LOG_LEVEL SYS_LOG_LEVEL_WRN
#define SYS_LOG_DOMAIN "OTA"
#define CONS_ABORT()
#include "sys_log.h"
#define HASH_LEN 32 /* SHA-256 digest length */
static void _print_sha256(const uint8_t *image_hash, const char *label)
{
char hash_print[HASH_LEN * 2 + 1];
hash_print[HASH_LEN * 2] = 0;
for (int i = 0; i < HASH_LEN; ++i)
{
sprintf(&hash_print[i * 2], "%02x", image_hash[i]);
}
SYS_LOG_INF("%s %s", label, hash_print);
}
void ota_partition_check()
{
uint8_t sha_256[HASH_LEN] = {0};
esp_partition_t partition;
// get sha256 digest for the partition table
partition.address = ESP_PARTITION_TABLE_OFFSET;
partition.size = ESP_PARTITION_TABLE_MAX_LEN;
partition.type = ESP_PARTITION_TYPE_DATA;
esp_partition_get_sha256(&partition, sha_256);
_print_sha256(sha_256, "SHA-256 for the partition table:");
// get sha256 digest for bootloader
partition.address = ESP_BOOTLOADER_OFFSET;
partition.size = ESP_PARTITION_TABLE_OFFSET;
partition.type = ESP_PARTITION_TYPE_APP;
esp_partition_get_sha256(&partition, sha_256);
_print_sha256(sha_256, "SHA-256 for bootloader: ");
// get sha256 digest for running partition
esp_partition_get_sha256(esp_ota_get_running_partition(), sha_256);
_print_sha256(sha_256, "SHA-256 for current firmware: ");
const esp_partition_t *running = esp_ota_get_running_partition();
esp_ota_img_states_t ota_state;
if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK)
{
if (ota_state == ESP_OTA_IMG_PENDING_VERIFY)
{
// run diagnostic function ...
bool diagnostic_is_ok = true; // diagnostic();
if (diagnostic_is_ok)
{
SYS_LOG_INF("Diagnostics completed successfully! Continuing execution ...");
esp_ota_mark_app_valid_cancel_rollback();
}
else
{
SYS_LOG_ERR("Diagnostics failed! Start rollback to the previous version ...");
esp_ota_mark_app_invalid_rollback_and_reboot();
}
}
else
{
SYS_LOG_INF("");
}
}
else
{
SYS_LOG_INF("");
}
}

View File

@@ -1,3 +0,0 @@
#pragma once
void ota_partition_check(void);

View File

@@ -1,161 +0,0 @@
#include <stdio.h>
#include <stdatomic.h>
#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;
}