修改OTA升级为双线程方式,边收数据边升级

This commit is contained in:
OPTOC
2025-10-20 12:15:17 +08:00
parent c026c49fac
commit 1e15f28f72
4 changed files with 107 additions and 21 deletions

View File

@@ -15,7 +15,7 @@ typedef struct
esp_ota_handle_t ota_handle;
int total_size;
uint16_t ota_data_cnt;
uint8_t ota_buff[20480 + 512];
// uint8_t ota_buff[20480 + 512];
}ota_u_t;
typedef struct

View File

@@ -34,6 +34,8 @@ typedef enum
RESEND_CMD_DATA_ACK = 0x06,
RESEND_CMD_MODE_STATUS = 0x07,
RESEND_CMD_OTA_GET_PARAM = 0x010,
RESEND_CMD_OTA_START = 0x011,
RESEND_CMD_OTA_DATA = 0x012,

View File

@@ -33,6 +33,10 @@ void sertrf_init(void)
//获取飞控代码地址
sertrf.fc_address = parse_hex_or_dec(FC_ADDRESS);
//初始化环形buff
rb_init(&sertrf.data_handle_buffer, DATA_HANDLE_BUFFER_SIZE, sizeof(uint8_t));
//线程启动
sertrf_start();
@@ -44,13 +48,13 @@ void sertrf_start(void)
"embedded_thread",
embedded_thread,
NULL,
4096,
2048,
20);
os_thread_create(&sertrf.pc_thread,
"pc_thread",
pc_thread,
NULL,
4096,
2048,
20);
os_thread_create(&sertrf.app_thread,
"app_thread",
@@ -64,6 +68,12 @@ void sertrf_start(void)
NULL,
2048,
10);
os_thread_create(&sertrf.data_handle_thread,
"data_handle_thread",
data_handle_thread,
NULL,
4096,
10);
}
void sertrf_rf_switch(uint8_t on)
{
@@ -210,6 +220,58 @@ void task_thread(void* arg)
os_thread_sleep(100);
}
}
void data_handle_thread(void* arg)
{
while(true)
{
switch(sertrf.mode_status.task_state)
{
case DATA_HANDLE_OTA_DATA:
{
size_t data_size = rb_size(&sertrf.data_handle_buffer);
if(data_size > 1024)
{
uint8_t data[data_size];
rb_get_bulk(&sertrf.data_handle_buffer, data, data_size);
if(otau_write(&sertrf.otau, data, data_size))
{
SYS_LOG_DBG("stmisp write error");
}
else
{
SYS_LOG_DBG("stmisp write ok");
}
}
}
break;
case DATA_HANDLE_OTA_DATA_END:
{
size_t data_size = rb_size(&sertrf.data_handle_buffer);
if(data_size > 0)
{
uint8_t data[data_size];
rb_get_bulk(&sertrf.data_handle_buffer, data, data_size);
if(otau_write(&sertrf.otau, data, data_size))
{
SYS_LOG_DBG("stmisp write error");
}
else
{
SYS_LOG_DBG("stmisp write ok");
sertrf.mode_status.task_state = DATA_HANDLE_IDLE;
}
}
}
break;
default:
break;
}
os_thread_sleep(1);
}
}
void pc_link_rgb_color(device_t* device)
{
static uint8_t last_connect = 255, last_wifi_mode = 0;
@@ -338,27 +400,24 @@ void resend_user_parse(void *resend_device)
break;
case RESEND_CMD_OTA_DATA:
{
memcpy(sertrf.otau.ota_buff + sertrf.otau.ota_data_cnt, sertrf.resend_device.rx_frame.payload, sertrf.resend_device.rx_frame.len);
sertrf.otau.ota_data_cnt += sertrf.resend_device.rx_frame.len;
// if(!sertrf.resend_device.status.resend_flag)
if(sertrf.otau.ota_data_cnt >= 20480 )
protocol_set_message_status(MESSAGE_OTA);
// 使用环形buff
if(rb_size(&sertrf.data_handle_buffer) + sertrf.resend_device.rx_frame.len <= DATA_HANDLE_BUFFER_SIZE)
{
SYS_LOG_DBG("KUYI_CMD_OTA_DATA %d\n", sertrf.resend_device.rx_frame.seq);
otau_write(&sertrf.otau, sertrf.otau.ota_buff, sertrf.otau.ota_data_cnt);
sertrf.otau.ota_data_cnt = 0;
rb_put_bulk(&sertrf.data_handle_buffer, sertrf.resend_device.rx_frame.payload, sertrf.resend_device.rx_frame.len);
}
else
{
SYS_LOG_DBG("RESEND_CMD_OTA_DATA buffer full");
}
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
}
break;
case RESEND_CMD_OTA_END:
{
if(sertrf.otau.ota_data_cnt > 0)
{
otau_write(&sertrf.otau, sertrf.otau.ota_buff, sertrf.otau.ota_data_cnt);
sertrf.otau.ota_data_cnt = 0;
}
// if(!sertrf.resend_device.status.resend_flag)
sertrf.mode_status.task_state = DATA_HANDLE_OTA_DATA_END;
while (sertrf.mode_status.task_state != DATA_HANDLE_IDLE){
os_thread_sleep(10);}
otau_end(&sertrf.otau);
resend_send_cmd(resend_device, RESEND_CMD_ACK, 0);
os_thread_sleep(2);

View File

@@ -7,17 +7,35 @@
#include "protocol/resend_protl.h"
#include "protocol/stmisp.h"
#include "ota_u.h"
#include "tool.h"
#include "ring_buffer.h"
#include "../../config/app_config.h"
#define FC_ADDRESS "0x08000000"
#define DATA_HANDLE_BUFFER_SIZE 4096
typedef enum
{
DATA_HANDLE_IDLE = 0,
DATA_HANDLE_OTA_DATA,
DATA_HANDLE_OTA_DATA_END,
}data_handle_e;
typedef struct
{
data_handle_e task_state;
}sertrf_mode_status_t;
typedef struct
{
device_t device;
os_thread_t embedded_thread;
os_thread_t pc_thread;
os_thread_t task_thread;
os_thread_t app_thread;
// 任务
os_thread_t embedded_thread; //处理飞控接收
os_thread_t pc_thread; //处理app接收
os_thread_t task_thread; //处理其他任务
os_thread_t app_thread; //处理app任务
os_thread_t data_handle_thread; //数据处理任务
sertrf_mode_status_t mode_status;
// ota
ota_u_t otau;
//自定义协议
@@ -27,6 +45,8 @@ typedef struct
//STMISP协议
stmisp_device_t stmisp_device;
// 环形buff
RingBuffer data_handle_buffer;
uint32_t fc_address;
}sertrf_t;
@@ -80,6 +100,11 @@ void app_thread(void* arg);
* @brief task thread
*/
void task_thread(void* arg);
/**
* @brief data handle thread
*/
void data_handle_thread(void* arg);
/**
* @brief 根据连接状态显示不同的颜色
*