Commit a85cf15e authored by 李秉薇's avatar 李秉薇

feat:打开wifi升级

parent 887ec44c
# Name, Type, SubType, Offset, Size, Flags
# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
nvs, data, nvs, , 0x4000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, , 0x800K,
ota_1, app, ota_1, , 0x800K,
\ No newline at end of file
nvs, data, nvs, , 0x4000,
otadata, data, ota, , 0x2000,
phy_init, data, phy, , 0x1000,
ota_0, app, ota_0, , 0x800K,
ota_1, app, ota_1, , 0x800K,
assert, data, spiffs, , 300K,
\ No newline at end of file
# Embed the server root certificate into the final binary "simple_ota_example.c"
idf_build_get_property(project_dir PROJECT_DIR)
idf_component_register(SRCS "app_BT_User.c" "bt_app_main.c" "bt_app_hf.c" "bt_app_core.c" "gatts_table_creat_demo.c"
idf_component_register(SRCS "mcudata.c" "app_BT_User.c" "bt_app_main.c" "bt_app_hf.c" "bt_app_core.c" "gatts_table_creat_demo.c"
"Protocol_CRC16.c"
"Protocol_Lib.c"
"Protocol_User.c"
"mcudata.c"
"MCU_Core_Protocol.c"
"app_Ble_User.c"
"main_user.c"
......
......@@ -33,6 +33,7 @@
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include "http_server.h"
......@@ -389,12 +390,16 @@ void Prot_Send_Msg_Process(void )
//BAT32A239
UpdateProtocolStructure Update_Frame;
extern const unsigned char _acBAT32A239dat[88016];
Protocol_uint32_t UpgradeStep =0x0;//初始状态
//extern const unsigned char _acBAT32A239dat[88016];
Protocol_uint32_t UpgradeStep =0xFF;//初始状态
uint32_t Step_delayTime =0;
uint32_t Flash_Size =0;
uint32_t temp_Size =0;
uint32_t Write_Counter=0;
uint32_t StartAddr = 0;
uint32_t BackupStartAddr = 0;
uint32_t DataTotalLen = 0;
uint32_t BackupDataTotalLen = 0;
void SetUpgradeStart(void)
{
// uint32_t fills;
......@@ -410,14 +415,25 @@ void SetUpgradeStart(void)
// Flash_Size+=(16-fills);//下载数据需要对齐16字节
// }
UpgradeStep=0;
UpgradeStep=4;
}
void SetUpgradeFlashSize(uint32_t size)
void SetUpgradeFlashSize(uint32_t size,uint32_t len)
{
Flash_Size = size - 16;
DataTotalLen = size - 16;
BackupDataTotalLen = DataTotalLen;
temp_Size=0;
StartAddr = len;
BackupStartAddr = StartAddr;
Write_Counter = 0;
ESP_LOGI("MSI","DataTotalLen : %ld \n",DataTotalLen);
ESP_LOGI("MSI","BackupDataTotalLen : %ld \n",BackupDataTotalLen);
ESP_LOGI("MSI","StartAddr : %ld Erase Chip \n",StartAddr);
ESP_LOGI("MSI","BackupStartAddr : %ld Erase Chip \n",BackupStartAddr);
}
void Cache_data(unsigned char* p,uint32_t len)
{
// memcpy(&_acBAT32A239dat[temp_Size],p,len);
......@@ -432,8 +448,6 @@ void SendCmd_EraseBAT32A239_Chip(void)
memset(Update_Frame.DAT,0,16);
UpdateBAT32A239Protocol_Send(Update_Frame);
}
uint32_t StartAddr = 0;
uint32_t Write_Size=0;
uint32_t SendCmd_WriteBAT32A239_Flash(void)
{
......@@ -444,18 +458,7 @@ uint32_t SendCmd_WriteBAT32A239_Flash(void)
Update_Frame.CMDH = CMD_FLASH_DWNLD;
Update_Frame.CMDL = 0x00;
Update_Frame.LEN = 0;
if(Write_Counter == 0)
{
StartAddr = 0;
StartAddr |= _acBAT32A239dat[8];
StartAddr <<= 8;
StartAddr |= _acBAT32A239dat[9];
StartAddr <<= 8;
StartAddr |= _acBAT32A239dat[10];
StartAddr <<= 8;
StartAddr |= _acBAT32A239dat[11];
//StartAddr += 16;
}//确定下载起始地址
if ((Flash_Size-Write_Counter)>=128)
{
Update_Frame.LEN= 128+8;//0x94;
......@@ -467,7 +470,7 @@ uint32_t SendCmd_WriteBAT32A239_Flash(void)
Write_Size = Flash_Size - Write_Counter;
}
memcpy(&Update_Frame.DAT[4],&_acBAT32A239dat[Write_Counter + 16],Write_Size); //拷贝需要写入的数据到发送结构
memcpy(&Update_Frame.DAT[4],&buf[Write_Counter],Write_Size); //拷贝需要写入的数据到发送结构
//Write_Counter += StartAddr;//转换为下载 FLASH 的实际起始地址
Update_Frame.DAT[0] = (Protocol_uint8_t)(StartAddr >> 24);
......@@ -490,7 +493,7 @@ void SendCmd_CrcCheckBAT32A239_Flash(void)
uint32_t crc=0;
uint32_t start=0x08000000;
uint32_t lenght=Flash_Size;
crc=CalcCrc32(&_acBAT32A239dat[0],lenght);//减掉CRC和前16字节DAT
//crc=CalcCrc32(&_acBAT32A239dat[0],lenght);//减掉CRC和前16字节DAT
Update_Frame.CMDH = CMD_DATA_CRC_CHECK;
Update_Frame.CMDL = 0x00;
Update_Frame.LEN = 0x00;
......@@ -523,6 +526,8 @@ void Upgrade_BAT32A239_Task(void *arg)
void BAT32A239_MCU_Update(void)
{
double percent = 0.0;
const char *partition_label = "assert";
const esp_partition_t *assert_update_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, partition_label);
switch (UpgradeStep)
{
case 0 :
......@@ -533,7 +538,6 @@ void BAT32A239_MCU_Update(void)
break;
case 1 :
Flash_Size =sizeof(_acBAT32A239dat) - 16;
//ESP_LOGI("MSI","Flash_Size : %ld \n",Flash_Size);
Write_Counter = 0;
if (BAT32A239_ACK.CMDH == CMD_FLASH_ERASE)
......@@ -559,13 +563,20 @@ void BAT32A239_MCU_Update(void)
if (Step_delayTime>=2000)
{
ESP_LOGI("MSI","UpgradeStep : %d Erase Chip timeout\n",UpgradeStep);
UpgradeStep = 4; //超时停止
UpgradeStep = 5; //超时停止
}
}
if(BAT32A239_ACK.CMDH==CMD_FLASH_DWNLD)
{
SendCmd_WriteBAT32A239_Flash();//下载shou'zhen
UpgradeStep = 2;
}
break;
case 2:
//ESP_LOGI("MSI","BAT32A239_ACK.CMDH : %d\n",BAT32A239_ACK.CMDH);
if (BAT32A239_ACK.CMDH==CMD_FLASH_DWNLD)
{
//ESP_LOGI("MSI","xxxxxxx");
if (BAT32A239_ACK.dat.download.CR1==0x01)
{
ESP_LOGI("MSI","Download failed, request to re-download the package");
......@@ -576,9 +587,13 @@ void BAT32A239_MCU_Update(void)
BAT32A239_ACK.dat.download.CR1=0xFF;
Write_Counter += Write_Size;
StartAddr += Write_Size;
if (Write_Counter==Flash_Size)//下载完成
//ESP_LOGI("MSI","Flash_Size : %ld verification OK !\n",Flash_Size);
//ESP_LOGI("MSI","Write_Counter : %ld verification OK !\n",Write_Counter);
if (Write_Counter == Flash_Size)//下载完成
{
UpgradeStep=3;
UpgradeStep = 4;
Write_Counter = 0;
Write_Size = 0;
// ESP_LOGI("MSI", " %.2f%%", 100.00);
ESP_LOGI("MSI","\n UpgradeStep : %d download finished \n",UpgradeStep);
}
......@@ -586,6 +601,7 @@ void BAT32A239_MCU_Update(void)
{
// percent = 100.0 - (double)(Write_Counter * 100) / (double)Flash_Size;
// ESP_LOGI("MSI", " %.2f%%", percent);
SendCmd_WriteBAT32A239_Flash();//下载进度
}
}
......@@ -601,7 +617,7 @@ void BAT32A239_MCU_Update(void)
}
if (BAT32A239_ACK.dat.verification.CR1==0x00)
{
UpgradeStep=4;
UpgradeStep=5;
ESP_LOGI("MSI","UpgradeStep : %d verification OK !\n",UpgradeStep);
}
}
......@@ -611,10 +627,42 @@ void BAT32A239_MCU_Update(void)
}
//UpgradeStep=9;
break;
case 4:
if(BackupDataTotalLen != 0)
{
if(DataTotalLen == BackupDataTotalLen)
{
UpgradeStep = 0;
}
else
{
UpgradeStep = 1;
}
if(DataTotalLen > MAX_OTA_BUFF)
{
ESP_LOGI("MSI","~~~~~~~~~~~~~~~~~~~~~~\n");
Flash_Size = MAX_OTA_BUFF;
esp_partition_read(assert_update_partition,StartAddr - BackupStartAddr + 16,buf,Flash_Size);
DataTotalLen -= Flash_Size;
}
else if(DataTotalLen != 0)
{
ESP_LOGI("MSI","!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
Flash_Size = DataTotalLen;
esp_partition_read(assert_update_partition,StartAddr - BackupStartAddr + 16,buf,Flash_Size);
DataTotalLen = 0;
}
else
{
UpgradeStep = 3;
}
}
break;
default:
UpgradeStep=4;
UpgradeStep=5;
break;
}
......
......@@ -160,7 +160,7 @@ void app_main(void)
printf("KL30 INIT OVER~~~~~~\r\n");
xTaskCreatePinnedToCore(Sys_Run_Tasks, "Sys_Run_Tasks", 4096, NULL, 3, Sys_Run_Taskshdl, 1);
// xTaskCreatePinnedToCore(wifi_service_task, "wifi_service_task", 4096, NULL, 5, &wifi_service_hdl, 1);
xTaskCreatePinnedToCore(wifi_service_task, "wifi_service_task", 4096, NULL, 5, &wifi_service_hdl, 1);
xTaskCreatePinnedToCore(Upgrade_BAT32A239_Task, "Upgrade_BAT32A239_Task", 1024 * 4, NULL, 5, NULL, 1);
}
......
#if 0
const unsigned char _acBAT32A239dat[88016] = {
0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x70, 0x00, 0x00, 0x01, 0x57, 0xC0,
0xCB, 0x35, 0xDE, 0x23, 0x00, 0x00, 0x80, 0x00, 0x00, 0x01, 0x47, 0xC0, 0xFF, 0xFF, 0xFF, 0xFF,
......@@ -5500,4 +5501,5 @@
0x30, 0x30, 0x32, 0x02, 0x23, 0xFF, 0x12, 0x01, 0x02, 0x38, 0x07, 0x18, 0x12, 0x64, 0x19, 0x58,
0x1B, 0x28, 0x23, 0x1C, 0x25, 0x80, 0x3E, 0x74, 0x40, 0x44, 0x48, 0x38, 0x4A, 0xFF, 0x70, 0x09,
0x12, 0x14, 0x01, 0x14, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
\ No newline at end of file
};
#endif
\ No newline at end of file
idf_component_register(SRCS "wifi_service.c" "softap.c" "http_server.c"
idf_component_register(SRCS "http_server.c" "wifi_service.c" "softap.c"
INCLUDE_DIRS "." "../../main"
PRIV_REQUIRES nvs_flash esp_netif esp_wifi esp_http_server app_update driver
EMBED_FILES "favicon.ico" "ota_page.html")
......@@ -15,7 +15,7 @@ static const char *TAG = "http_server";
char buf[MAX_OTA_BUFF]; // 接收服务端传来的文件缓存, 必须使用全局变量, 否则会触发看门狗复位, 原理未知
extern void Cache_data(unsigned char* p,uint32_t len);
extern void SetUpgradeFlashSize(uint32_t size);
extern void SetUpgradeFlashSize(uint32_t size,uint32_t len);
extern void SetUpgradeStart(void);//初始状态
static unsigned char softap_ota_start = 0;
......@@ -204,20 +204,45 @@ static esp_err_t app_post_handler(httpd_req_t *req)
// httpd_resp_send_chunk(req, NULL, 0);
// return ESP_OK;
// }
static void MSI_Print_mem16(size_t len,char *buf)
{
for (size_t i = 0; i < len; )
{
ESP_LOGI("MSI","%02xh: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",i,\
buf[i+0],buf[i+1],buf[i+2],buf[i+3],buf[i+4],buf[i+5],buf[i+6],buf[i+7],
buf[i+8],buf[i+9],buf[i+10],buf[i+11],buf[i+12],buf[i+13],buf[i+14],buf[i+15]);
i+=16;
}
}
/*上传文件名是BAT32A239dat.bin的处理函数*/
static esp_err_t nation_post_handler(httpd_req_t *req)
{
int recv_block = 0;
double percent = 0.0;
int ret, remaining = req->content_len;
int total = remaining;
int err_timeout_retry_cnt = 0;
size_t erase_size = 0;
size_t erase_offset = 0;
size_t write_offset = 0;
int Addr = 0;
// if (Sys_Run_hdl != NULL)
esp_err_t err = ESP_OK;
const char *partition_label = "assert";
const esp_partition_t *assert_update_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_ANY, partition_label);
// if (Sys_Run_hdl != NULL)
// {
// vTaskDelete(Sys_Run_hdl);
// Sys_Run_hdl = NULL;
// vTaskDelay(pdMS_TO_TICKS(100));
// }
SetUpgradeFlashSize(remaining); // 设定文件大小
//SetUpgradeFlashSize(remaining); // 设定文件大小
ESP_LOGI(TAG, "---- 国民技术程序文件: 需要接收的数据(byte) = %d", remaining);
while (remaining > 0)
{
......@@ -226,25 +251,98 @@ static esp_err_t nation_post_handler(httpd_req_t *req)
{
if (ret == HTTPD_SOCK_ERR_TIMEOUT)
{
/* Retry receiving if timeout occurred */
continue;
err_timeout_retry_cnt++;
if (err_timeout_retry_cnt >= MAX_RETRY_COUNT)
{
httpd_resp_send_408(req);
httpd_resp_send_chunk(req, NULL, 0);
ESP_LOGE(TAG, "---- 接收数据超时, 退出升级流程, 请保持无线连接稳定后再试.");
return ESP_FAIL;
}
/* else
{
Retry receiving if timeout occurred
continue;
}*/
}
else
{
ESP_LOGI("MSI","total : %d \n",total);
ESP_LOGI("MSI","remaining : %d \n",remaining);
err_timeout_retry_cnt = 0;
}
}
if(total == remaining)//下载长度+地址
{
Addr = 0;
Addr |= buf[8];
Addr <<= 8;
Addr |= buf[9];
Addr <<= 8;
Addr |= buf[10];
Addr <<= 8;
Addr |= buf[11];
SetUpgradeFlashSize(remaining, Addr);
}
while (erase_offset < write_offset + ret)
{
err = esp_partition_erase_range(assert_update_partition, erase_offset, assert_update_partition->erase_size);
if (err == ESP_OK)
{
erase_offset += assert_update_partition->erase_size;
}
else
{
httpd_resp_send_chunk(req, NULL, 0);
//ESP_LOGE(TAG, "---- assert 分区擦除失败(%s), 分区容量 = 0x%x, 偏移地址 = 0x%x, 擦除长度 = 0x%x", esp_err_to_name(err), assert_update_partition->size, erase_offset, assert_update_partition->erase_size);
return ESP_FAIL;
}
}
err = esp_partition_write(assert_update_partition, write_offset, buf, ret);
if (err == ESP_OK)
{
remaining -= ret;
write_offset += ret;
recv_block++;
if ((recv_block % 32) == 0)
{
percent = 100.0 - (double)(remaining * 100) / (double)total;
ESP_LOGI(TAG, "---- 写入数据(%.2f%%), recv_block = %d", percent, recv_block);
}
}
else
{
httpd_resp_send_chunk(req, NULL, 0);
ESP_LOGE(TAG, "---- assert 分区写数据失败(%s)", esp_err_to_name(err));
return ESP_FAIL;
}
remaining -= ret;
Cache_data(&buf,ret);
// ESP_LOGI(TAG, "=========== RECEIVED DATA ==========");
// 数据处理代码
// ESP_LOGI(TAG, "====================================");
// 增加20ms延时, 解决CPU0看门狗超时的问题
vTaskDelay(pdMS_TO_TICKS(20));
}
//SetUpgradeStart(); // 启动升级
ESP_LOGI(TAG, "---- write_offset = %d", write_offset);
if (write_offset == total)
{
ESP_LOGI(TAG, "---- assert 分区更新完成, 1秒后自动重启.");
vTaskDelay(pdMS_TO_TICKS(1000));
}
else
{
httpd_resp_send_chunk(req, NULL, 0);
ESP_LOGE(TAG, "---- assert 分区更新数据部分丢失");
return ESP_FAIL;
}
SetUpgradeStart(); // 启动升级
ESP_LOGI(TAG, "---- 国民技术程序文件: 接收完成");
// End response
httpd_resp_send_chunk(req, NULL, 0);
return ESP_OK;
}
void Nat32G031_ReqReset(void)
{
ESP_LOGI(TAG, "---- NAT32G031程序更新完成, 3秒后自动重启.");
......
......@@ -11,6 +11,8 @@
#define MAX_OTA_BUFF 4096u
#define MAX_RETRY_COUNT 5
extern char buf[MAX_OTA_BUFF];
extern httpd_handle_t http_server;
......@@ -23,8 +25,8 @@ httpd_handle_t http_service(void);
extern void delele_tasks(void);
extern void Catch_data(unsigned char *p, uint32_t len);
extern void SetUpgradeFlashSize(uint32_t size);
extern void SetUpgradeStart(void); // ʼ״̬
extern void SetUpgradeFlashSize(uint32_t size,uint32_t len);
extern void SetUpgradeStart(void); // ��ʼ״̬
void Nat32G031_ReqReset(void);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment