zhyinch
2021-11-15 0382082159710bfd226723dfd54cae09b2668423
Ô´Âë/ºËÐİå/Src/application/serial_at_cmd_app.c
@@ -5,13 +5,14 @@
#include "dw_app.h"
#include "RTC.h"
#define MSG_RW       3
#define MSG_WTAG  6
#define MSG_RW                3
#define MSG_USERDATA       0x10
#define MSG_WTAG           6
#define CMD_READ   1
#define CMD_WRITE   2
#define CMD_REPLY 3
#define EUART_RX_BUF_SIZE 100
#define EUART_RX_BUF_SIZE 200
typedef enum{   UsartReceiveWaitHead0, 
            UsartReceiveWaitHead1,
            UsartReceiveWaitMsgType,
@@ -20,7 +21,9 @@
            UsartReceiveWaitIndex,
            UsartReceiveWaitDataLen,
            UsartReceiveWaitData,
            UsartReceiveWaitChecksum
            UsartReceiveWaitChecksum0,
            UsartReceiveWaitChecksum1
}UsartRecvPackState;
uint8_t mUsartReceivePack[100] = {0};
@@ -34,11 +37,12 @@
   send_frame[3] = 8;
   send_frame[4] = pack_msgtype;
   send_frame[5] = pack_length;
OUT485_ENABLE;
   checksum = Checksum_u16(&send_frame[2],8);
   memcpy(&send_frame[10],&checksum,2);
   
   UART_PushFrame(send_frame, 12);
   USART_puts(send_frame, 12);
    OUT485_DISABLE;
}
void SendComMap(uint8_t data_length, uint8_t index)
{
@@ -60,85 +64,121 @@
   
   UART_PushFrame(send_frame, data_length+9);   
}
u8 remotetag_para[30];
u8 remotetag_para[30],remotetag_paralen,userdata_len,userdata[50];
u8 remotesend_state=0;
u16 rec_checksum,userdata_targetid;
uint16_t configremotetagID;
u32 getuserdata_tick;
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;
   uint8_t i;
   
   if(usart_receive_state == UsartReceiveWaitChecksum) {         //若收到校验和包
   if(usart_receive_state == UsartReceiveWaitChecksum1) {         //若收到校验和包
      checksum = 0;
      for(int i = 0; i<pack_length-5; i++) {
      for(int i = 0; i<pack_length-2; i++) {
         checksum += mUsartReceivePack[i];
      }
      checksum += pack_cmd;
      checksum += pack_length;
      checksum += pack_index;
      checksum += pack_datalen;
      checksum += pack_msgtype;
      if(((data + checksum)&0xff) == 0xFF)            //校验通过
      checksum += pack_length;
      checksum = ~checksum;
      rec_checksum = rec_checksum|(data<<8);
      if(rec_checksum == checksum)            //校验通过
      {
         switch(pack_msgtype)
         {
            case MSG_RW:
                   pack_cmd = mUsartReceivePack[0];
                   pack_index = mUsartReceivePack[1];
                   pack_datalen = mUsartReceivePack[2];
               switch(pack_cmd)
               {
                  case CMD_WRITE:
                     //从mUsartReceivePack中读取pack_length长度的字节,放到全局变量中
                     memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);
                     memcpy((uint8_t*)&g_com_map + pack_index, &mUsartReceivePack[3], pack_datalen);
                         //   memcpy((uint8_t*)&g_com_map2 + pack_index, &mUsartReceivePack[3], pack_datalen);
                     //返回一个error状态
                     //SendComMap(pack_datalen,pack_index);
                  SendConfigConfirm(pack_msgtype,pack_length);
                            if(pack_index==CNT_RESTART*2)
                            {
                                SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
                            }
                            if(pack_index==CNT_UPDATE*2)
                            {
                                uint32_t result = 0;
                                u16 tmp = 0xAAAA;
                                __disable_irq();
                                result = FLASH_Prepare(0x8004A38, 2);
                                if(result)
                                    result = FLASH_Write(0x8004A38, (const uint8_t*)&tmp, 2);
                                __enable_irq();
                                printf("进入升级模式\r\n");
                               //delay_ms(100);
                                SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
                            }
                     save_com_map_to_flash();
                     SendConfigConfirm(pack_msgtype,pack_length);
                     Program_Init();
                     delay_ms(100);
                     NVIC_SystemReset();
                     break;
                  case CMD_READ:
                     //read包中data字节,即mUsartReceivePack[0]表示数据长度;
                  //从g_com_data结构体中的第index位置读取长度为mUsartReceivePack[0]的字节,发送出来
                     SendComMap(pack_datalen,pack_index);      
                     break;
                  default:
                     break;
               }
               break;
               case MSG_WTAG:
               remotesend_state++;
               if(remotesend_state>14)
                  remotesend_state=1;
               memcpy(remotetag_para,mUsartReceivePack,REMOTEPARA_LEN);
            case MSG_WTAG:    //修改标签参数
               remotesend_state=1;
               memcpy(&configremotetagID,&mUsartReceivePack[0],2);
               //configremotetagID = pack_cmd|pack_index<<8;
               //remotetag_para[0] = mUsartReceivePack[2];
               remotetag_paralen = mUsartReceivePack[4]+3;
               memcpy(&remotetag_para[1],&mUsartReceivePack[3],remotetag_paralen);
               break;
            case MSG_USERDATA:
               userdata_len = mUsartReceivePack[2];
            memcpy(&userdata_targetid,mUsartReceivePack,2);
            if(userdata_len==USERDATA_LEN)
            {
               memcpy(userdata,&mUsartReceivePack[0],USERDATA_LEN+3);
               getuserdata_tick = time32_incr;
               SendConfigConfirm(pack_msgtype,pack_length);
            }
               break;
      }
      }
   }
      usart_receive_state = UsartReceiveWaitHead0;
      pack_index = 0;
      pack_length = 0;
      index=0;
   } else if((usart_receive_state == UsartReceiveWaitData) ) {   //若果收到的是正常通讯包
   }else if(usart_receive_state==UsartReceiveWaitChecksum0)
   {
      rec_checksum = data;
      usart_receive_state = UsartReceiveWaitChecksum1;
   }
   else if(usart_receive_state == UsartReceiveWaitData)  {   //若果收到的是正常通讯包
      mUsartReceivePack[index] = data;
      index++;
      if(index == pack_length-5) {      //如果收到的index与长度相等
         usart_receive_state = UsartReceiveWaitChecksum;
      if(index == pack_length-2) {      //如果收到的index与长度相等
         usart_receive_state = UsartReceiveWaitChecksum0;
      }
   } 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;
         usart_receive_state = UsartReceiveWaitData;
      
   } else if((usart_receive_state == UsartReceiveWaitHead0) && (data == 0x55)) {   //收到第一个包头
      usart_receive_state = UsartReceiveWaitHead1;