添加ota功能函数
This commit is contained in:
@@ -31,10 +31,12 @@ list(APPEND srcs "utils/sb_aes.c")
|
|||||||
list(APPEND srcs "drivers/sertrf/sertrf.c")
|
list(APPEND srcs "drivers/sertrf/sertrf.c")
|
||||||
list(APPEND srcs "drivers/sertrf/device.c")
|
list(APPEND srcs "drivers/sertrf/device.c")
|
||||||
list(APPEND srcs "drivers/sertrf/key.c")
|
list(APPEND srcs "drivers/sertrf/key.c")
|
||||||
|
list(APPEND srcs "drivers/sertrf/ota_u.c")
|
||||||
list(APPEND srcs "drivers/sertrf/protocol/MSP.c")
|
list(APPEND srcs "drivers/sertrf/protocol/MSP.c")
|
||||||
list(APPEND srcs "drivers/sertrf/protocol/p_protocol.c")
|
list(APPEND srcs "drivers/sertrf/protocol/p_protocol.c")
|
||||||
list(APPEND srcs "drivers/sertrf/protocol/mavlink_control.c")
|
list(APPEND srcs "drivers/sertrf/protocol/mavlink_control.c")
|
||||||
|
|
||||||
|
|
||||||
list(APPEND srcs "drivers/led_strip/led_strip.c")
|
list(APPEND srcs "drivers/led_strip/led_strip.c")
|
||||||
list(APPEND srcs "drivers/led_strip/led_strip_encoder.c")
|
list(APPEND srcs "drivers/led_strip/led_strip_encoder.c")
|
||||||
|
|
||||||
|
|||||||
79
app/drivers/sertrf/ota_u.c
Normal file
79
app/drivers/sertrf/ota_u.c
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
#include "ota_u.h"
|
||||||
|
|
||||||
|
void switch_to_ota(const char *target_label)
|
||||||
|
{
|
||||||
|
// 通过标签获取分区
|
||||||
|
const esp_partition_t *target_partition =
|
||||||
|
esp_partition_find_first(ESP_PARTITION_TYPE_APP,
|
||||||
|
ESP_PARTITION_SUBTYPE_ANY,
|
||||||
|
target_label);
|
||||||
|
if (!target_partition) {
|
||||||
|
SYS_LOG_ERR("Partition '%s' not found!", target_label);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SYS_LOG_INF("Switching to %s at 0x%x", target_partition->label, target_partition->address);
|
||||||
|
|
||||||
|
// 设置下次启动分区
|
||||||
|
esp_err_t err = esp_ota_set_boot_partition(target_partition);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
SYS_LOG_ERR("Failed to set boot partition: %s", esp_err_to_name(err));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SYS_LOG_INF( "Switch successful, rebooting...");
|
||||||
|
esp_restart();
|
||||||
|
}
|
||||||
|
// 获取ota信息
|
||||||
|
void get_partition_status(ota_u_t* otau)
|
||||||
|
{
|
||||||
|
// 获取当前运行分区
|
||||||
|
otau->running = esp_ota_get_running_partition();
|
||||||
|
if (!otau->running) {
|
||||||
|
SYS_LOG_ERR("Failed to get running partition");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
//赋值ota分区
|
||||||
|
if(strcmp(otau->running->label, OTA_TAG_0) == 0){
|
||||||
|
otau->update = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
|
||||||
|
ESP_PARTITION_SUBTYPE_ANY, OTA_TAG_1);
|
||||||
|
}else{
|
||||||
|
otau->update = esp_partition_find_first(ESP_PARTITION_TYPE_APP,
|
||||||
|
ESP_PARTITION_SUBTYPE_ANY, OTA_TAG_0);
|
||||||
|
}
|
||||||
|
SYS_LOG_INF("Running partition: %s at 0x%x", otau->running->label, otau->running->address);
|
||||||
|
}
|
||||||
|
void otau_init(ota_u_t* otau)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (esp_ota_begin(otau->update, OTA_SIZE_UNKNOWN, &otau->ota_handle) != ESP_OK) {
|
||||||
|
SYS_LOG_ERR("esp_ota_begin failed!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
SYS_LOG_INF("Starting OTA update...");
|
||||||
|
}
|
||||||
|
uint8_t otau_write(ota_u_t* otau, void* data, uint32_t size)
|
||||||
|
{
|
||||||
|
esp_err_t err = esp_ota_write(otau->ota_handle, data, size);
|
||||||
|
if (err != ESP_OK) {
|
||||||
|
SYS_LOG_ERR( "esp_ota_write failed! (%s)", esp_err_to_name(err));
|
||||||
|
esp_ota_abort(otau->ota_handle);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
otau->total_size += size;
|
||||||
|
SYS_LOG_INF( "Received: %d bytes, Total: %d bytes", size, otau->total_size);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
uint8_t otau_end(ota_u_t* otau)
|
||||||
|
{
|
||||||
|
if (esp_ota_end(otau->ota_handle) != ESP_OK) {
|
||||||
|
SYS_LOG_ERR("esp_ota_end failed!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (esp_ota_set_boot_partition(otau->update) != ESP_OK) {
|
||||||
|
SYS_LOG_ERR("esp_ota_set_boot_partition failed!");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
SYS_LOG_INF("OTA update successful! Rebooting...");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
40
app/drivers/sertrf/ota_u.h
Normal file
40
app/drivers/sertrf/ota_u.h
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "esp_ota_ops.h"
|
||||||
|
#include "esp_system.h"
|
||||||
|
#include "esp_log.h"
|
||||||
|
#include "sys_log.h"
|
||||||
|
|
||||||
|
#define OTA_TAG_0 "ota_0"
|
||||||
|
#define OTA_TAG_1 "ota_1"
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
const esp_partition_t *running;
|
||||||
|
const esp_partition_t *update;
|
||||||
|
esp_ota_handle_t ota_handle;
|
||||||
|
int total_size;
|
||||||
|
}ota_u_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief OTA初始化
|
||||||
|
*
|
||||||
|
* @param otau
|
||||||
|
*/
|
||||||
|
void otau_init(ota_u_t* otau);
|
||||||
|
/**
|
||||||
|
* @brief OTA写入
|
||||||
|
*
|
||||||
|
* @param otau
|
||||||
|
* @param data
|
||||||
|
* @param size
|
||||||
|
* @return uint8_t
|
||||||
|
*/
|
||||||
|
uint8_t otau_write(ota_u_t* otau, void* data, uint32_t size);
|
||||||
|
/**
|
||||||
|
* @brief OTA结束
|
||||||
|
*
|
||||||
|
* @param otau
|
||||||
|
* @return uint8_t
|
||||||
|
*/
|
||||||
|
uint8_t otau_end(ota_u_t* otau);
|
||||||
@@ -5,12 +5,15 @@
|
|||||||
#include "key.h"
|
#include "key.h"
|
||||||
#include "protocol/p_protocol.h"
|
#include "protocol/p_protocol.h"
|
||||||
#include "protocol/kuyi_protl.h"
|
#include "protocol/kuyi_protl.h"
|
||||||
|
#include "ota_u.h"
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
device_t device;
|
device_t device;
|
||||||
os_thread_t embedded_thread;
|
os_thread_t embedded_thread;
|
||||||
os_thread_t pc_thread;
|
os_thread_t pc_thread;
|
||||||
os_thread_t task_thread;
|
os_thread_t task_thread;
|
||||||
|
|
||||||
|
ota_u_t otau;
|
||||||
}sertrf_t;
|
}sertrf_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -37,6 +40,11 @@ void sertrf_status(void);
|
|||||||
* @brief embedded thread
|
* @brief embedded thread
|
||||||
*/
|
*/
|
||||||
void embedded_thread(void* arg);
|
void embedded_thread(void* arg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief pc thread
|
||||||
|
*/
|
||||||
|
void pc_thread(void* arg);
|
||||||
/**
|
/**
|
||||||
* @brief task thread
|
* @brief task thread
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user