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

fix:增加串口发送

parent 12945184
......@@ -21,6 +21,8 @@ static ProtocolSetData ProtocolSetData_Cbk;
static Protocol_uint8_t *mDataBufPtr = Protocol_NULL;
static Protocol_uint16_t mDataBufLen = 0;
static Protocol_uint32_t DataBufMaxLen = 0;
BAT32A239_ACK_Structure BAT32A239_ACK;
//#define DEBUG_PRO_DATA 0
/**
* @brief 初始化函数
......@@ -203,7 +205,7 @@ Protocol_uint32_t Protocol_Parse(const Protocol_uint8_t *pData, Protocol_uint32_
#endif
}
}
pData += frameLen;
remainLen -= frameLen;
}
......@@ -224,16 +226,16 @@ Protocol_uint32_t Protocol_Parse(const Protocol_uint8_t *pData, Protocol_uint32_
*
* @since 1.0.0
*/
Protocol_uint32_t Protocol_Send(const Protocol_uint16_t cmdID, const Protocol_uint8_t *pData, Protocol_uint8_t len)
{
int i = 0;
Protocol_uint16_t checksum = 0;
Protocol_uint8_t dataBuf [ 256 ];
Protocol_uint32_t frameLen;
if ( len + DATA_PACKAGE_MIN_LEN > 256 )
{
// printf("sendProtocol data is too len !!!\n");
// printf("sendProtocol data is too len !!!\n");
return 0;
}
......@@ -241,7 +243,7 @@ Protocol_uint32_t Protocol_Send(const Protocol_uint16_t cmdID, const Protocol_ui
dataBuf [ 1 ] = CMD_HEAD2; // 同步帧头 Sync frame header
dataBuf [ 2 ] = len + 4;
dataBuf [ 3 ] = len; // 命令字节 Command byte
dataBuf [ 3 ] = 0; // 命令字节 Command byte
dataBuf [ 4 ] = ( Protocol_uint8_t )cmdID;
......@@ -254,7 +256,6 @@ Protocol_uint32_t Protocol_Send(const Protocol_uint16_t cmdID, const Protocol_ui
frameLen++;
}
// 校验码 Checksum
checksum = getCheckSum(dataBuf + 2, frameLen - 2);
dataBuf [ frameLen ] = (checksum >> 8) & 0x00FF;
......@@ -262,7 +263,73 @@ Protocol_uint32_t Protocol_Send(const Protocol_uint16_t cmdID, const Protocol_ui
dataBuf [ frameLen ] = checksum & 0x00FF;
frameLen++;
if ( UARTSend_Cbk != Protocol_NULL )
{
return UARTSend_Cbk(dataBuf, frameLen);
}
else
{
return 0;
}
}
Protocol_uint32_t CalcCrc32(Protocol_uint8_t buf[], int Len);
/**
* 根据协议格式进行拼接
*/
/**
* @brief 串口协议数据拼接,如初始化发送函数,调用此函数后,数据已通过串口发送
* @param[in] cmdID 命令字
* @param[in] pData 协议数据内容(不包括协议头、长度、帧序号、命令字、校验和,从数据域算起)
* @param[in] len 数据域长度
*
* @return 已发送的数据长度
*
* @since 1.0.0
*/
Protocol_uint32_t UpdateBAT32A239Protocol_Send(UpdateProtocolStructure SendPd)
{
int i = 0;
Protocol_uint16_t checksum = 0;
Protocol_uint8_t dataBuf [ 256 ];
Protocol_uint16_t checksumXor = 0;
Protocol_uint32_t frameLen = 0;
Protocol_uint32_t DAT_CRC;
if ( SendPd.LEN > 256 )
{
// printf("sendProtocol data is too len !!!\n");
return 0;
}
frameLen += 7;
dataBuf [ 0 ] = 0xAA;
dataBuf [ 1 ] = 0x55; // 同步帧头 Sync frame header
dataBuf [ 2 ] = SendPd.CMDH; //CMDH
dataBuf [ 3 ] = SendPd.CMDL; // CMDL
dataBuf [ 4 ] = frameLen;
dataBuf [ 5 ] = frameLen >> 8; // 命令字节 Command byte
if(SendPd.CMDH == CMD_FLASH_DWNLD)
{
if(SendPd.LEN > 0)
{
memcpy(&dataBuf[6],&SendPd.DAT[0],SendPd.LEN);
DAT_CRC=CalcCrc32(&SendPd.DAT[16], SendPd.LEN );//计算Address + Data的CRC 32
dataBuf[6+SendPd.LEN-4]=(Protocol_uint8_t)(DAT_CRC);
dataBuf[7+SendPd.LEN-4]=(Protocol_uint8_t)(DAT_CRC>>8);
dataBuf[8+SendPd.LEN-4]=(Protocol_uint8_t)(DAT_CRC>>16);
dataBuf[9+SendPd.LEN-4]=(Protocol_uint8_t)(DAT_CRC>>24);
}
}
for(i = 0 ;i < (frameLen - 1); i++)
{
checksumXor^=dataBuf[i];
}
dataBuf[frameLen - 1]=(Protocol_uint8_t)checksumXor;
if ( UARTSend_Cbk != Protocol_NULL )
{
return UARTSend_Cbk(dataBuf, frameLen);
......
......@@ -39,6 +39,60 @@ typedef struct
Protocol_uint8_t Data [ 256 ]; /**< 有效数据内容 DATA0-DATAN*/
} Protocol_Data_t;
// #define UPGRADE_BAT32A239
// #ifdef UPGRADE_BAT32A239
#define CMD_FLASH_ERASE 0x30//擦除 FLASH
#define CMD_FLASH_DWNLD 0x31//下载用户程序到 FLASH
#define CMD_DATA_CRC_CHECK 0x32//CRC 校验下载用户程
typedef struct
{
Protocol_uint8_t CMDH; //宏定义所包含的
Protocol_uint8_t CMDL;
Protocol_uint16_t LEN; //不关心发送的端格式
Protocol_uint8_t DAT[160];//补充的数据直接补充好再传到下层
}UpdateProtocolStructure;//上层协议数据结构
typedef struct
{
Protocol_uint8_t CMDH ;
Protocol_uint8_t CMDL ;
Protocol_uint8_t LEN[2];
union
{
Protocol_uint8_t DAT[128];
struct
{
Protocol_uint8_t CR1 :8;
Protocol_uint8_t CR2 :8;
}Erase;
struct
{
Protocol_uint8_t CR1 :8;
Protocol_uint8_t CR2 :8;
}download;
struct
{
Protocol_uint8_t CR1 :8;
Protocol_uint8_t CR2 :8;
}verification;
struct
{
Protocol_uint8_t CR1 :8;
Protocol_uint8_t CR2 :8;
}reset;
struct
{
Protocol_uint8_t CR1 :8;
Protocol_uint8_t CR2 :8;
}appgo;
}dat;
}BAT32A239_ACK_Structure;//下层应答协议
extern BAT32A239_ACK_Structure BAT32A239_ACK;
typedef Protocol_uint8_t (*UARTOpen)(void);
typedef Protocol_uint32_t (*UARTSend)(const Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
typedef Protocol_uint32_t (*UARTRead)(Protocol_uint8_t *pData, Protocol_uint32_t u32Len);
......@@ -63,5 +117,6 @@ void Protocol_Init(Protocol_uint8_t *pMemSpace, Protocol_uint32_t M
void Protocol_Service(void);
Protocol_uint32_t Protocol_Parse(const Protocol_uint8_t *pData, Protocol_uint32_t len);
Protocol_uint32_t Protocol_Send(const Protocol_uint16_t cmdID, const Protocol_uint8_t *pData, Protocol_uint8_t len);
Protocol_uint32_t UpdateBAT32A239Protocol_Send(UpdateProtocolStructure SendPd);
#endif
#include <stdint.h>
#include "Protocol_User.h"
#include "Protocol_Lib.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
......@@ -384,6 +385,15 @@ void Prot_Send_Msg_Process(void )
}
}
//BAT32A239
UpdateProtocolStructure Update_Frame;
unsigned char _acBAT32A239dat[0x1000];
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;
void SetUpgradeStart(void)
{
// uint32_t fills;
......@@ -395,7 +405,7 @@ void SetUpgradeStart(void)
// fills=temp_Size%16;
// if (fills!=0)
// {
// memset(&_acN32G031dat[temp_Size],0xFF,16-fills);
// memset(&_acBAT32A239dat[temp_Size],0xFF,16-fills);
// Flash_Size+=(16-fills);//下载数据需要对齐16字节
// }
......@@ -404,11 +414,224 @@ void SetUpgradeStart(void)
void SetUpgradeFlashSize(uint32_t size)
{
// Flash_Size=size;
// temp_Size=0;
Flash_Size = size - 16;
temp_Size=0;
}
void Cache_data(unsigned char* p,uint32_t len)
{
// memcpy(&_acN32G031dat[temp_Size],p,len);
// temp_Size+=len;
memcpy(&_acBAT32A239dat[temp_Size],p,len);
temp_Size+=len;
}
void SendCmd_EraseBAT32A239_Chip(void)
{
Update_Frame.CMDH = CMD_FLASH_ERASE;
Update_Frame.CMDL = 0x00;
Update_Frame.LEN = 0x00;
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)
{
uint32_t Offset=0;
if (Flash_Size>Write_Counter)
{
Update_Frame.CMDH = CMD_FLASH_DWNLD;
Update_Frame.CMDL = 0x00;
Update_Frame.LEN = 0;
if(Write_Counter == 0)
{
StartAddr = 0;
StartAddr |= _acBAT32A239dat[4];
StartAddr <<= 8;
StartAddr |= _acBAT32A239dat[5];
StartAddr <<= 8;
StartAddr |= _acBAT32A239dat[6];
StartAddr <<= 8;
StartAddr |= _acBAT32A239dat[7];
StartAddr += 16;
}//确定下载起始地址
if ((Flash_Size-Write_Counter)>=128)
{
Update_Frame.LEN= 128+4;//0x94;
Write_Size=128;
}
else
{
Update_Frame.LEN = (Flash_Size - Write_Counter) + 4;//剩余数量
Write_Size = Flash_Size - Write_Counter;
}
Write_Counter += Write_Size; //写入指针增加
memcpy(&Update_Frame.DAT[4],&_acBAT32A239dat[Offset],Write_Size); //拷贝需要写入的数据到发送结构
Offset += StartAddr;//转换为下载 FLASH 的实际起始地址
Update_Frame.DAT[0] = (Protocol_uint8_t)(Offset >> 24);
Update_Frame.DAT[1] = (Protocol_uint8_t)(Offset >> 16);
Update_Frame.DAT[2] = (Protocol_uint8_t)(Offset >> 8);
Update_Frame.DAT[3] = (Protocol_uint8_t)(Offset >> 0);
UpdateBAT32A239Protocol_Send(Update_Frame);//发送
}
return Write_Counter;
}
/*
下载成功后,长度的变化;
//写入指针增加
*/
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
Update_Frame.CMDH = CMD_DATA_CRC_CHECK;
Update_Frame.CMDL = 0x00;
Update_Frame.LEN = 0x00;
UpdateBAT32A239Protocol_Send(Update_Frame);
}
void BAT32A239_MCU_Update(void)
{
double percent = 0.0;
switch (UpgradeStep)
{
case 0 :
SendCmd_EraseBAT32A239_Chip();//发送擦除指令
UpgradeStep=6;
Step_delayTime=0;
ESP_LOGI("MSI","UpgradeStep : %d Erase Chip \n",UpgradeStep);
break;
case 1 :
//Flash_Size =sizeof(_acBAT32A239dat);
Write_Counter = 0;
if (BAT32A239_ACK.CMDH == CMD_FLASH_ERASE)
{
if (BAT32A239_ACK.dat.Erase.CR1 == 0)//没判断长度,只判断了擦除状态
{
UpgradeStep = 2;
ESP_LOGI("MSI","UpgradeStep : %d Erase Chip finshed\n",UpgradeStep);
ESP_LOGI("MSI","downloading... \n");
SendCmd_WriteBAT32A239_Flash();//下载shou'zhen
}
else
{
UpgradeStep = 0;
ESP_LOGI("MSI","UpgradeStep : %d Erase Chip finshed\n",UpgradeStep);
ESP_LOGI("MSI","Erasing failed, request erasing again\n");
}
}
else
{
Step_delayTime++;
if (Step_delayTime>=10)
{
ESP_LOGI("MSI","UpgradeStep : %d Erase Chip timeout\n",UpgradeStep);
//UpgradeStep=7; //超时停止
}
}
break;
case 2:
if (BAT32A239_ACK.CMDH==CMD_FLASH_DWNLD)
{
if (BAT32A239_ACK.dat.download.CR1==0x01)
{
ESP_LOGI("MSI","Download failed, request to re-download the package");
SendCmd_WriteBAT32A239_Flash();//下载进度
}
if (BAT32A239_ACK.dat.download.CR1==0x00)
{
BAT32A239_ACK.dat.download.CR1=0xFF;
Write_Counter += Write_Size;
if (Write_Counter==Flash_Size)//下载完成
{
UpgradeStep=3;
// ESP_LOGI("MSI", " %.2f%%", 100.00);
ESP_LOGI("MSI","\n UpgradeStep : %d download finished \n",UpgradeStep);
}
else
{
// percent = 100.0 - (double)(Write_Counter * 100) / (double)Flash_Size;
// ESP_LOGI("MSI", " %.2f%%", percent);
SendCmd_WriteBAT32A239_Flash();//下载进度
}
}
}
break;
case 3:
if (BAT32A239_ACK.CMDH==CMD_DATA_CRC_CHECK)
{
if (BAT32A239_ACK.dat.verification.CR1==0x01)
{
ESP_LOGI("MSI","Err : %2x \n",BAT32A239_ACK.dat.download.CR2);
}
if (BAT32A239_ACK.dat.verification.CR1==0x00)
{
UpgradeStep=4;
ESP_LOGI("MSI","UpgradeStep : %d verification OK !\n",UpgradeStep);
}
}
else
{
SendCmd_CrcCheckBAT32A239_Flash();//校验flash
}
//UpgradeStep=9;
break;
default:
UpgradeStep=4;
break;
}
}
Protocol_uint32_t CalcCrc32(Protocol_uint8_t buf[], int Len)
{
int i,j;
int n = 0;
Protocol_uint8_t LE32buf[4];
Protocol_uint32_t crc = 0xFFFFFFFF; // Initial value
for (n = 0; n < Len; n+=4)
{
LE32buf[0]=buf[n+3];//[n+0];//
LE32buf[1]=buf[n+2];//[n+1];//
LE32buf[2]=buf[n+1];//[n+2];//
LE32buf[3]=buf[n+0];//[n+3];//
for(j=0;j<4;j++)
{
crc ^= (Protocol_uint32_t)LE32buf[j] << 24;
for (i = 0; i < 8; ++i)
{
if (crc & 0x80000000)
{
crc = (crc << 1) ^ 0x04C11DB7; // 0xEDB88320= reverse 0x04C11DB7
}
else
{
crc = (crc << 1);
}
}
}
}
return crc;
}
\ No newline at end of file
......@@ -26,5 +26,6 @@ extern Protocol_User_Ctrl_Struct Prot_User;
void Protocol_User_Ctrl_Init(void );
void Prot_Send_Msg_Process(void );
Protocol_uint32_t CalcCrc32(Protocol_uint8_t buf[], int Len);
#endif
This diff is collapsed.
......@@ -205,7 +205,7 @@ static esp_err_t storage_post_handler(httpd_req_t *req)
return ESP_OK;
}
/*上传文件名是N32G031dat.bin的处理函数*/
/*上传文件名是BAT32A239dat.bin的处理函数*/
static esp_err_t nation_post_handler(httpd_req_t *req)
{
int ret, remaining = req->content_len;
......@@ -262,15 +262,15 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
{
return app_post_handler(req);
}
else if (strcmp(uri_post, "/upload/mmap_assert.bin") == 0)
{
return assert_post_handler(req);
}
else if (strcmp(uri_post, "/upload/storage.bin") == 0)
{
return storage_post_handler(req);
}
else if (strcmp(uri_post, "/upload/N32G031dat.bin") == 0)
// else if (strcmp(uri_post, "/upload/mmap_assert.bin") == 0)
// {
// return assert_post_handler(req);
// }
// else if (strcmp(uri_post, "/upload/storage.bin") == 0)
//{
// return storage_post_handler(req);
//}
else if (strcmp(uri_post, "/upload/BAT32G139.bin") == 0)
{
return nation_post_handler(req);
}
......
......@@ -13,8 +13,6 @@
<label for="selectPartition">目标分区:</label>
<select id="selectPartition" name="selectPartition">
<option selected value="RT200T-2.bin">Application</option>
<option value="mmap_assert.bin">Pictures</option>
<option value="storage.bin">Fonts</option>
<option value="BAT32G139.bin">Nation</option>
</select>
<br><br>
......
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