参考代码
This commit is contained in:
47
main/CMakeLists.txt
Normal file
47
main/CMakeLists.txt
Normal file
@@ -0,0 +1,47 @@
|
||||
|
||||
set(srcs-app
|
||||
)
|
||||
|
||||
# 基础程序
|
||||
set(srcs-common
|
||||
"ota.c"
|
||||
"../app/app_main.c"
|
||||
"../app/console.c"
|
||||
"../app/drivers/data_port/sb_data_port.c"
|
||||
"../app/drivers/data_port/uart/uart_port.c"
|
||||
"../app/button/button_event.c"
|
||||
)
|
||||
|
||||
# 自定义框架抽象层
|
||||
set(srcs-components
|
||||
"../components/system/source/k_kit/k_kit.c"
|
||||
"../components/system/source/shell/sh_vt100.c"
|
||||
"../components/system/source/shell/sh_vset.c"
|
||||
"../components/system/source/shell/sh.c"
|
||||
"../sal/esp32s3/kernel/os_heap.c"
|
||||
"../sal/esp32s3/kernel/os_mutex.c"
|
||||
"../sal/esp32s3/kernel/os_hook.c"
|
||||
"../sal/esp32s3/kernel/os_timer.c"
|
||||
"../sal/esp32s3/kernel/os_semaphore.c"
|
||||
"../sal/esp32s3/kernel/os_thread.c"
|
||||
"../sal/esp32s3/kernel/os_kit.c"
|
||||
"../sal/esp32s3/kernel/os_service.c"
|
||||
"../sal/esp32s3/chip/uart_esp32.c"
|
||||
"../sal/esp32s3/soc_shell.c"
|
||||
"../app/config/board_config.c"
|
||||
)
|
||||
set(incs
|
||||
"../app"
|
||||
"../components/system/include"
|
||||
"../components/system/source"
|
||||
"../components/system/source/k_kit"
|
||||
"../components/system/source/shell"
|
||||
"../sal/esp32s3"
|
||||
"../sal/esp32s3/kernel"
|
||||
"${IDF_PATH}/components/freertos/FreeRTOS-Kernel/include/freertos"
|
||||
)
|
||||
|
||||
idf_component_register(SRCS "main.c"
|
||||
${srcs-components}
|
||||
${srcs-common}
|
||||
INCLUDE_DIRS "." ${incs})
|
||||
116
main/main.c
Executable file
116
main/main.c
Executable file
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* @file main.c
|
||||
* @author LokLiang
|
||||
* @brief 根据实际平台启动并初始化,用户 APP 入口为 void work_app_main(void *arg)
|
||||
* @version 0.1
|
||||
* @date 2023-11-24
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#include "os/os.h"
|
||||
#include "app_main.h"
|
||||
|
||||
#include "ota.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
#undef SYS_LOG_DOMAIN
|
||||
#define SYS_LOG_DOMAIN "OS"
|
||||
#include "os_util.h"
|
||||
|
||||
#ifndef CONFIG_OS_THREAD_MAIN_STACK_SIZE
|
||||
#define CONFIG_OS_THREAD_MAIN_STACK_SIZE 0x1C00
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_OS_THREAD_MAIN_PRIORITY
|
||||
#define CONFIG_OS_THREAD_MAIN_PRIORITY OS_PRIORITY_NORMAL
|
||||
#endif
|
||||
|
||||
static unsigned s_int_nest;
|
||||
|
||||
static unsigned _port_interrupt_save(void)
|
||||
{
|
||||
if (s_int_nest == 0)
|
||||
{
|
||||
os_interrupt_disable();
|
||||
}
|
||||
return s_int_nest++;
|
||||
}
|
||||
|
||||
static void _port_interrupt_restore(unsigned nest)
|
||||
{
|
||||
s_int_nest = nest;
|
||||
if (nest == 0)
|
||||
{
|
||||
os_interrupt_enable();
|
||||
}
|
||||
}
|
||||
|
||||
static k_work_q_t *_port_get_work_q_hdl(void)
|
||||
{
|
||||
struct os_thread_handle *thread_handle = os_thread_get_self();
|
||||
os_work_q_list_t *work_q_list = thread_handle->work_q_list;
|
||||
return &work_q_list->work_q_handle;
|
||||
}
|
||||
|
||||
static void _port_thread_sleep(k_tick_t sleep_ticks)
|
||||
{
|
||||
vTaskDelay(sleep_ticks);
|
||||
}
|
||||
|
||||
static void _os_init(void)
|
||||
{
|
||||
static k_init_t const init_struct = {
|
||||
.malloc = os_malloc,
|
||||
.free = os_free,
|
||||
.get_sys_ticks = os_get_sys_ticks,
|
||||
.interrupt_save = _port_interrupt_save,
|
||||
.interrupt_restore = _port_interrupt_restore,
|
||||
.scheduler_disable = os_scheduler_suspend,
|
||||
.scheduler_enable = os_scheduler_resume,
|
||||
.get_work_q_hdl = _port_get_work_q_hdl,
|
||||
.thread_sleep = _port_thread_sleep,
|
||||
};
|
||||
k_init(&init_struct);
|
||||
|
||||
os_work_q_create(default_os_work_q_hdl,
|
||||
"app-work_q",
|
||||
CONFIG_OS_THREAD_MAIN_STACK_SIZE,
|
||||
CONFIG_OS_THREAD_MAIN_PRIORITY);
|
||||
|
||||
extern void work_app_main(void *arg);
|
||||
static os_work_t _work_hdl_init;
|
||||
os_work_create(&_work_hdl_init, "work-main", work_app_main, NULL, 3);
|
||||
os_work_submit(default_os_work_q_hdl, &_work_hdl_init, 0);
|
||||
}
|
||||
|
||||
static void _init_prev_nvs(void)
|
||||
{
|
||||
esp_err_t err = nvs_flash_init();
|
||||
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
|
||||
{
|
||||
// OTA app partition table has a smaller NVS partition size than the
|
||||
// non-OTA partition table. This size mismatch may cause NVS
|
||||
// initialization to fail. If this happens, we erase NVS partition and
|
||||
// initialize NVS again.
|
||||
ESP_ERROR_CHECK(nvs_flash_erase());
|
||||
err = nvs_flash_init();
|
||||
}
|
||||
ESP_ERROR_CHECK(err);
|
||||
}
|
||||
|
||||
int app_main(void)
|
||||
{
|
||||
/* ota */
|
||||
ota_partition_check();
|
||||
|
||||
/* 初始化平台相关 */
|
||||
_init_prev_nvs();
|
||||
|
||||
/* 初始化 os 抽象层 */
|
||||
_os_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
75
main/ota.c
Normal file
75
main/ota.c
Normal file
@@ -0,0 +1,75 @@
|
||||
#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("");
|
||||
}
|
||||
}
|
||||
3
main/ota.h
Normal file
3
main/ota.h
Normal file
@@ -0,0 +1,3 @@
|
||||
#pragma once
|
||||
|
||||
void ota_partition_check(void);
|
||||
Reference in New Issue
Block a user