Browse Source

串口OTA升级,刷写OTA_flash

CHENJIE-PC\QiXiang_CHENJIE 4 years ago
parent
commit
609e0d44b9
2 changed files with 71 additions and 17 deletions
  1. 5 1
      inc/app.h
  2. 66 16
      src/app.c

+ 5 - 1
inc/app.h

@@ -53,7 +53,11 @@ typedef struct Fota_Type
 {
     bool Fota_update ;
     bool Fota_update_error ;
-    uint16_t Fota_Data_Len ;
+    uint32_t Fota_All_Data_Len ;
+    uint32_t Fota_Current_Addres ;
+    uint8_t Fota_Recv_Data_Len ;
+    uint8_t Fota_Recv_Data[100] ;
+    uint8_t Fota_CRC ;
 
 }Fota_Type;
 

+ 66 - 16
src/app.c

@@ -68,7 +68,7 @@ uint8_t UartSlpHandler          = 0xfe;
 #define PROC_FOTA_TASK_STACK_SIZE           (1024)
 static StaticTask_t             gProcess_Fota_Task_t;
 static UINT8                  gProcess_Fota_TaskStack[PROC_FOTA_TASK_STACK_SIZE];
-Fota_Type Fota_Stuct;
+Fota_Type Fota_Struct;
 //CAN线程定义
 
 //Tcp线程定义
@@ -129,7 +129,7 @@ static void Main_Task(void* arg)
     uint32_t sleep_time_value;
     int32_t inParam = 0xAABBCCDD;
     uint32_t Chrgend_Work_time = 180000;
-    uint32_t Wakeup_Work_time = 1000;
+    uint32_t Wakeup_Work_time = 30000;
     uint32_t Sleep_Time = 20000;
     //上述参数应写入文件里
     slpManSlpState_t State;
@@ -503,24 +503,36 @@ static void Uart_Task(void* arg)
         }
     }
 }
+uint8_t Fota_crc_chk(uint8_t* data,uint8_t length)
+{  
+    uint8_t reg_crc=0x00;
+    while(length--)
+    { 
+        reg_crc ^= *data++;
+    }
+    return reg_crc;
+}
 //Fota线程
 static void Fota_Task(void* arg)
 {
-    Fota_Stuct.Fota_Data_Len=100;
+    Fota_Struct.Fota_Current_Addres = 0x00000000;
+    Fota_Struct.Fota_Recv_Data_Len=100;
     uint8_t Uart_Rece_buffer[110];
-    uint32_t file_address = 0x00000000;
+    uint8_t temp[100];
     uint8_t Uart_Send_buffer[6] = {0x00,0x00,0x00,0x00,0x64,0x01};
-    uint32_t timeout = 0x00000000;
+    uint8_t timeout = 0x00;
+    uint8_t Fota_Crc;
+    osDelay(5000);
+    USARTdrv->Send(Uart_Send_buffer,6);
     while (true)
     {
-        osDelay(1000);
-        USARTdrv->Send(Uart_Send_buffer,6);
+        osDelay(500);
         USARTdrv->Receive(Uart_Rece_buffer,110);
         while((isRecvTimeout == false) && (isRecvComplete == false))
         {
             timeout++;
             osDelay(100);
-            if (timeout>=10)
+            if (timeout>=50)
             {
                 timeout =0;
                 isRecvTimeout = true;
@@ -530,20 +542,58 @@ static void Fota_Task(void* arg)
         if (isRecvComplete == true)
         {
             isRecvComplete == false;
-            file_address = file_address + 0x64;
-            Uart_Send_buffer[0] = file_address>>24;
-            Uart_Send_buffer[1] = file_address>>16;
-            Uart_Send_buffer[2] = file_address>>8;
-            Uart_Send_buffer[3] = file_address;
-            #ifdef USING_PRINTF
-                printf("Uart_Send_buffer:");
+            Fota_Struct.Fota_All_Data_Len = Uart_Rece_buffer[0]<<24|Uart_Rece_buffer[1]<<16|Uart_Rece_buffer[2]<<8|Uart_Rece_buffer[3];
+            Fota_Crc = Fota_crc_chk(&Uart_Rece_buffer[9],Fota_Struct.Fota_Recv_Data_Len);
+            if (Fota_Crc==Uart_Rece_buffer[109])
+            {
+                #ifdef USING_PRINTF
+                    printf("Fota_Crc:%x!\n",Fota_Crc);
+                    printf("Fota_Current_Addres:%x!\n",Fota_Struct.Fota_Current_Addres);
+                #endif
+                BSP_QSPI_Erase_Safe(0x2A0000,100);
+                BSP_QSPI_Write_Safe(&Uart_Rece_buffer[9],0x2A0000,100);
+                BSP_QSPI_Read_Safe(&temp,0x2A0000,100);
+                #ifdef USING_PRINTF
+                    printf("temp:");
+                    for(int i=0;i<100;i++)
+                        printf("%x ",temp[i]);
+                    printf("\n");
+                #endif
+            }
+            if (Fota_Struct.Fota_Current_Addres + Fota_Struct.Fota_Recv_Data_Len >Fota_Struct.Fota_All_Data_Len)
+            {
+                Fota_Struct.Fota_Recv_Data_Len = Fota_Struct.Fota_All_Data_Len-Fota_Struct.Fota_Current_Addres;
+            }
+            else
+            {
+                Fota_Struct.Fota_Recv_Data_Len = 0x64;
+            }
+            if(Fota_Struct.Fota_Recv_Data_Len==0)
+            {
+                #ifdef USING_PRINTF
+                    printf("Done!\n");
+                #endif
+            }
+            #ifdef USING_PRINTF1
+                printf("Uart_Rece_buffer:");
                 for(int i=0;i<110;i++)
-                    printf("%x",Uart_Send_buffer[i]);
+                    printf("%x ",Uart_Rece_buffer[i]);
                 printf("\n");
             #endif
+            Uart_Send_buffer[0] = Fota_Struct.Fota_Current_Addres>>24;
+            Uart_Send_buffer[1] = Fota_Struct.Fota_Current_Addres>>16;
+            Uart_Send_buffer[2] = Fota_Struct.Fota_Current_Addres>>8;
+            Uart_Send_buffer[3] = Fota_Struct.Fota_Current_Addres;
+            Uart_Send_buffer[4] = Fota_Struct.Fota_Recv_Data_Len;
+            USARTdrv->Send(Uart_Send_buffer,6);
+            Fota_Struct.Fota_Current_Addres = Fota_Struct.Fota_Current_Addres + Fota_Struct.Fota_Recv_Data_Len;
+            
+
+
         }
         else
         {
+            USARTdrv->Send(Uart_Send_buffer,6);
             isRecvTimeout == false;
             memset(Uart_Rece_buffer,0,110);
         }