#include "serial_at_cmd_app.h"
|
#include "global_param.h"
|
#include "MCUFlash.h"
|
#include <string.h>
|
#include <stdio.h>
|
//#include "dw_app.h"
|
#include "AppConfig.h"
|
//#include "SPIFlash.h"
|
#define CMD_READ 1
|
#define CMD_WRITE 2
|
#define CMD_REPLY 3
|
#define EUART_RX_BUF_SIZE 100
|
|
uint16_t Checksum_u16(uint8_t* pdata, uint32_t len)
|
{
|
uint16_t sum = 0;
|
uint32_t i;
|
for(i=0; i<len; i++)
|
sum += pdata[i];
|
sum = ~sum;
|
return sum;
|
}
|
typedef enum{ UsartReceiveWaitHead0,
|
UsartReceiveWaitHead1,
|
UsartReceiveWaitMsgType,
|
UsartReceiveWaitLength,
|
UsartReceiveWaitCMD,
|
UsartReceiveWaitIndex,
|
UsartReceiveWaitDataLen,
|
UsartReceiveWaitData,
|
UsartReceiveWaitChecksum
|
}UsartRecvPackState;
|
uint8_t mUsartReceivePack[100] = {0};
|
|
void SendComMap(uint8_t data_length, uint8_t index)
|
{
|
static uint8_t send_frame[EUART_RX_BUF_SIZE];
|
uint16_t checksum = 0;
|
send_frame[0] = 0x55;
|
send_frame[1] = 0xAA;
|
send_frame[2] = 0x03;
|
send_frame[3] = data_length+5;
|
send_frame[4] = CMD_REPLY;
|
send_frame[5] = index;
|
send_frame[6] = data_length;
|
memcpy(&send_frame[7], &g_com_map[index], data_length);
|
for(int i = 0; i<(data_length+5); i++)
|
{
|
checksum += send_frame[2+i];
|
}
|
checksum = Checksum_u16(&send_frame[2],5+data_length);
|
memcpy(&send_frame[7+data_length],&checksum,2);
|
|
HAL_UART_Transmit(&huart1, send_frame, data_length+9, 1000);
|
//UART_PushFrame(send_frame, data_length+9);
|
}
|
void UpdateProcess(uint8_t index)
|
{
|
if(index == CNT_REBOOT*2)
|
{
|
g_com_map[CNT_REBOOT]=0;
|
g_com_map[0] = 0;
|
save_com_map_to_flash();
|
// ʹÓÃÄÚ²¿ flash ±£´æºóÖØÆô
|
printf("URTRestart\r\n");
|
HAL_NVIC_SystemReset();
|
}
|
|
if(index == CNT_UPDATE*2)
|
{
|
/* ÒÔǰʹÓÃÍⲿ SPIFlash ´æ´¢ IAP flag£¬ÏÖÒÑͳһΪʹÓà g_com_map ²¢±£´æÔÚ
|
* APP_CONFIG_CTRL_MAP£¨ÄÚ²¿ Flash£©¡£Ö±½ÓÉèÖà CNT_UPDATE ²¢±£´æ¼´¿É¡£
|
*/
|
g_com_map[CNT_UPDATE] = 1;
|
save_com_map_to_flash();
|
HAL_Delay(10);
|
printf("IAP flag set in internal flash, rebooting...\r\n");
|
HAL_NVIC_SystemReset();
|
}
|
}
|
void UsartParseDataHandler(uint8_t data)
|
{
|
static UsartRecvPackState usart_receive_state = UsartReceiveWaitHead0;
|
uint16_t checksum = 0;
|
static uint8_t pack_datalen = 0,pack_length = 0,pack_index = 0,pack_msgtype = 0,pack_cmd = CMD_READ;
|
static uint8_t index = 0;
|
|
|
|
if(usart_receive_state == UsartReceiveWaitChecksum) { //ÈôÊÕµ½Ð£ÑéºÍ°ü
|
checksum = 0;
|
for(int i = 0; i<pack_length-5; i++) {
|
checksum += mUsartReceivePack[i];
|
}
|
checksum += pack_cmd;
|
checksum += pack_length;
|
checksum += pack_index;
|
checksum += pack_datalen;
|
checksum += pack_msgtype;
|
if(((data + checksum)&0xff) == 0xFF) //УÑéͨ¹ý
|
{
|
switch(pack_cmd)
|
{
|
case CMD_WRITE:
|
//´ÓmUsartReceivePackÖжÁÈ¡pack_length³¤¶ÈµÄ×Ö½Ú£¬·Åµ½È«¾Ö±äÁ¿ÖÐ
|
memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
|
//·µ»ØÒ»¸öerror״̬
|
if(pack_index<0x60||pack_index>=0x64)
|
{
|
save_com_map_to_flash();
|
g_com_map[CNT_RESTART] = 1;
|
}
|
/* Èç¹ûÉÏλдÈë CNT_UPDATE ×ֶΣ¬Ö±½Ó±£´æµ½ÄÚ²¿ flash ²¢¸´Î»½øÈë IAP */
|
if(g_com_map[CNT_UPDATE] == 1)
|
{
|
save_com_map_to_flash();
|
HAL_Delay(10);
|
HAL_NVIC_SystemReset();
|
}
|
break;
|
case CMD_READ:
|
//read°üÖÐdata×Ö½Ú£¬¼´mUsartReceivePack[0]±íʾÊý¾Ý³¤¶È£»
|
//´Óg_com_data½á¹¹ÌåÖеĵÚindexλÖöÁÈ¡³¤¶ÈΪmUsartReceivePack[0]µÄ×Ö½Ú£¬·¢ËͳöÀ´
|
SendComMap(pack_datalen,pack_index>>1);
|
break;
|
default:
|
break;
|
}
|
}
|
usart_receive_state = UsartReceiveWaitHead0;
|
pack_index = 0;
|
pack_length = 0;
|
index=0;
|
} else if((usart_receive_state == UsartReceiveWaitData) ) { //Èô¹ûÊÕµ½µÄÊÇÕý³£Í¨Ñ¶°ü
|
mUsartReceivePack[index] = data;
|
index++;
|
if(index == pack_length-5) { //Èç¹ûÊÕµ½µÄindexÓ볤¶ÈÏàµÈ
|
usart_receive_state = UsartReceiveWaitChecksum;
|
}
|
} else if(usart_receive_state == UsartReceiveWaitDataLen) { //ÊÕµ½Ö¸ÁîÀàÐÍ×Ö½Ú
|
pack_datalen = data;
|
usart_receive_state = UsartReceiveWaitData;
|
}else if(usart_receive_state == UsartReceiveWaitIndex) { //ÊÕµ½Ö¸ÁîÀàÐÍ×Ö½Ú
|
pack_index = data;
|
usart_receive_state = UsartReceiveWaitDataLen;
|
} else if(usart_receive_state == UsartReceiveWaitCMD) { //ÊÕµ½Ö¸ÁîÀàÐÍ×Ö½Ú
|
pack_cmd = data;
|
usart_receive_state = UsartReceiveWaitIndex;
|
} else if(usart_receive_state == UsartReceiveWaitLength) { //ÊÕµ½³¤¶È×Ö½Ú
|
|
pack_length = data;
|
pack_index = 0;
|
usart_receive_state = UsartReceiveWaitCMD;
|
|
} else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) { //ÊÕµ½µÚÒ»¸ö°üÍ·
|
usart_receive_state = UsartReceiveWaitHead1;
|
} else if((usart_receive_state == UsartReceiveWaitHead1) && (data == 0xAA)) { //ÊÕµ½µÚ¶þ¸ö°üÍ·
|
usart_receive_state = UsartReceiveWaitMsgType;
|
}else if ((usart_receive_state == UsartReceiveWaitMsgType) && (data == 0x3)) {
|
usart_receive_state = UsartReceiveWaitLength;
|
pack_msgtype = data;
|
}
|
else {
|
usart_receive_state = UsartReceiveWaitHead0;
|
pack_index = 0;
|
pack_length = 0;
|
}
|
|
}
|