Browse Source

【更改】更改了错误,但未进行验证

CHENJIE-PC\QiXiang_CHENJIE 4 years ago
parent
commit
04b877c24b
1 changed files with 88 additions and 64 deletions
  1. 88 64
      app.c

+ 88 - 64
app.c

@@ -29,7 +29,7 @@
 
 //define
 // app task static stack and control block
-#define PROC_TASK_STACK_SIZE    (1536)
+#define PROC_TASK_STACK_SIZE    (1024)
 
 
 //uart def
@@ -50,8 +50,7 @@ volatile bool isRecvComplete = false;
 uint8_t process0SlpHandler     = 0xff;
 uint8_t process1SlpHandler     = 0xff;
 uint8_t process2SlpHandler     = 0xff;
-uint8_t deepslpTimerID         = 7;
-
+uint8_t process3SlpHandler     = 0xff;
 uint8_t Can_Rece_buffer[8];
 uint16_t Batt_Cell_Num = 14;//默认数值
 uint16_t Batt_Cell_Num_2 ;//默认数值
@@ -159,26 +158,26 @@ uint8_t* Uart_Receive_func(Uart_Receive_Type Uart_Receive_Msg,uint8_t *Uart_Rece
             break;
         }
     }
-    Rece_Data_Len = *(Uart_Rece_buffer+2);
-
-    // #ifdef USING_PRINTF
-    //     for(int i=0;i<8;i++)
-    //     {
-    //     printf("%x ",Uart_Send_buffer[i]);
-    //     }
-    //     printf("\n");
-    //     for(int i=0;i<Rece_Data_Len+5;i++)
-    //     {
-    //     printf("%x ",*(Uart_Rece_buffer+i));
-    //     }
-    //     printf("\n");
-    // #endif
     if (isRecvComplete == true)
     {
+        Rece_Data_Len = *(Uart_Rece_buffer+2);
         isRecvComplete = false;
         CRC_Rece_buffer =*(Uart_Rece_buffer+Rece_Data_Len+4)<<8|*(Uart_Rece_buffer+Rece_Data_Len+3);
-        CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Rece_Data_Len+3);
-
+        CRC_chk_buffer = crc_chk(Uart_Rece_buffer,Rece_Data_Len+3);  
+        #ifdef USING_PRINTF
+            printf("Uart_Send_buffer:  ");
+            for(int i=0;i<8;i++)
+            {
+            printf("%x ",Uart_Send_buffer[i]);
+            }
+            printf("\n");
+            printf("Uart_Rece_buffer: ");
+            for(int i=0;i<Rece_Data_Len+5;i++)
+            {
+            printf("%x ",*(Uart_Rece_buffer+i));
+            }
+            printf("\n");
+        #endif
         if (CRC_Rece_buffer == CRC_chk_buffer)//满足校验
         {
             return Uart_Rece_buffer+3;
@@ -210,11 +209,11 @@ void USART_callback(uint32_t event)
     }
 }
 
-static void process0AppTask(void* arg)
+static void process0AppTask(void* arg)//任务调度程序
 {
-    UINT8 Can_index = 0;
-    UINT8 Uart_index = 0;
-    UINT8 NB_index  = 0;
+    UINT16 Can_index = 0;
+    UINT16 Uart_index = 0;
+    UINT16 NB_index  = 0;
     uint32_t sleep_index = 0;
     PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
     NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
@@ -227,6 +226,9 @@ static void process0AppTask(void* arg)
     slpManApplyPlatVoteHandle("process0slp",&process0SlpHandler);
     slpManPlatVoteDisableSleep(process0SlpHandler, SLP_SLP2_STATE); 
     slpManSlpState_t slpstate = slpManGetLastSlpState();
+    #ifdef USING_PRINTF
+            printf("slpstate:%d \n",slpstate);
+    #endif
     if((slpstate == SLP_SLP2_STATE) || (slpstate == SLP_HIB_STATE))
     {
         PROC_Task_STATE_SWITCH(PROCESS_STATE_IDLE);
@@ -242,15 +244,18 @@ static void process0AppTask(void* arg)
         {
             case PROCESS_STATE_IDLE:
             {   
+                #ifdef USING_PRINTF
+                        printf("wake up 2s \n");
+                #endif
                 PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_CHECK);
                 NetSocDisplay(LED_SOC_0,LED_TURN_ON);
-                osDelay(100);//delay 100ms
+                osDelay(10);//delay 10ms
                 if(Uart_Rece_BattI!=0x0000)
                 {
                     PROC_Task_STATE_SWITCH(PROCESS_STATE_WORK);
                 }
                 sleep_index++;
-                if(sleep_index>=100)
+                if(sleep_index>=200)
                 {
                     NetSocDisplay(LED_SOC_0,LED_TURN_OFF);
                     sleep_index=0;
@@ -274,7 +279,7 @@ static void process0AppTask(void* arg)
                     PROC_Can_STATE_SWITCH(PROCESS_STATE_WORK);
                     Can_index = 0;
                 }
-                if (NB_index >=1000)//NB 10s 调用一次
+                if (NB_index >=100)//NB 1s 调用一次
                 {
                     PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_CONNECT);
                     NB_index = 0;
@@ -303,7 +308,10 @@ static void process0AppTask(void* arg)
                 PROC_Can_STATE_SWITCH(PROCESS_STATE_SLEEP);
                 slpManPlatVoteEnableSleep(process0SlpHandler,SLP_SLP2_STATE);
                 slpManPlatVoteDisableSleep(process0SlpHandler, SLP_HIB_STATE); 
-                slpManDeepSlpTimerStart(deepslpTimerID, 300000); // create a 10s timer, DeepSleep Timer is always oneshoot
+                #ifdef USING_PRINTF
+                        printf("ready to sleep \n");
+                #endif
+                slpManDeepSlpTimerStart(DEEPSLP_TIMER_ID7,180000); // create a 60s timer, DeepSleep Timer is always oneshoot
                 while(1)                // now app can enter hib, but ps and phy maybe not, so wait here
                 {
                     osDelay(3000);
@@ -326,6 +334,7 @@ static void process1AppTask(void* arg)
     uint8_t *Uart_Reve_Point = NULL;
     Uart_Receive_Type Uart_Receive_Msg;
 	PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
+    slpManApplyPlatVoteHandle("process1slp",&process1SlpHandler);
     Uart_Receive_Msg.Bms_Address = 0x01;
     Uart_Receive_Msg.Bms_Read_Funcode = 0x03;
     uint8_t *Uart_Rece_buffer;
@@ -342,15 +351,15 @@ static void process1AppTask(void* arg)
             }
             case PROCESS_Uart_STATE_CHECK://检查电流数值
             {
-                #ifdef USING_PRINTF
-                        printf("Check_Current!\n ");
-                #endif
                 Uart_Receive_Msg.Reg_Begin_H = 0x00;
                 Uart_Receive_Msg.Reg_Begin_L= 0x02+Batt_Cell_Num;
                 Uart_Receive_Msg.Reg_Num_H = 0x00;
                 Uart_Receive_Msg.Reg_Num_L = 0x01;
                 Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
                 Uart_Rece_BattI = *(Uart_Reve_Point+0)<<8 |*(Uart_Reve_Point+1);
+                #ifdef USING_PRINTF
+                        printf("Check_Current! %x\n ",Uart_Rece_BattI);
+                #endif
                 PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
                 break;
             }
@@ -378,13 +387,14 @@ static void process1AppTask(void* arg)
                         Uart_Receive_Msg.Reg_Num_H = Batt_Cell_Num>>8;
                         Uart_Receive_Msg.Reg_Num_L = Batt_Cell_Num;
                         Uart_Reve_Point = Uart_Receive_func(Uart_Receive_Msg,Uart_Rece_buffer);
-                        // #ifdef USING_PRINTF
-                        //     for (size_t i = 0; i < Batt_Cell_Num_2; i++)
-                        //     {
-                        //         printf("%x ",*(Uart_Reve_Point+i));
-                        //     }
-                        //     printf("\n");
-                        // #endif
+                        #ifdef USING_PRINTF
+                            printf("BattCellV: ");
+                            for (size_t i = 0; i < Batt_Cell_Num_2; i++)
+                            {
+                                printf("%x ",*(Uart_Reve_Point+i));
+                            }
+                            printf("\n");
+                        #endif
                         battbuffer[30] = Batt_Cell_Num;
                         memcpy(&battbuffer[31],Uart_Reve_Point,Batt_Cell_Num_2);
                         break;
@@ -401,13 +411,14 @@ static void process1AppTask(void* arg)
                         {
                             battbuffer[32+Batt_Cell_Num_2+i] = *(Uart_Reve_Point+2*i+1);
                         }
-                        // #ifdef USING_PRINTF
-                        //     for (size_t i = 0; i < Batt_Temp_Num; i++)
-                        //     {
-                        //         printf("%x ",*(Uart_Reve_Point+i));
-                        //     }
-                        //     printf("\n");
-                        // #endif
+                        #ifdef USING_PRINTF
+                            printf("BattCellT: ");
+                            for (size_t i = 0; i < Batt_Temp_Num; i++)
+                            {
+                                printf("%x ",*(Uart_Reve_Point+i));
+                            }
+                            printf("\n");
+                        #endif
                         break;
                     }
                     case 3://读取总电压,目前保护板只有一个电压
@@ -447,12 +458,12 @@ static void process1AppTask(void* arg)
                     }
                     default:
                     {
-                       PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
+                        PROC_Uart_STATE_SWITCH(PROCESS_Uart_STATE_IDLE);
                         break;
                     }
                 }
-                free(Uart_Rece_buffer);
                 Rece_index++;
+                free(Uart_Rece_buffer);
                 break;
             }
             case PROCESS_Uart_STATE_SLEEP:
@@ -462,7 +473,6 @@ static void process1AppTask(void* arg)
                     osDelay(3000);
                 }           
                 //此处休眠
-                break;
             }
         }
     }
@@ -470,6 +480,7 @@ static void process1AppTask(void* arg)
 static void process2AppTask(void* arg)
 {
 	PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
+    slpManApplyPlatVoteHandle("process2slp",&process2SlpHandler);
     uint32_t Can_ID;
     NVIC_EnableIRQ(PadWakeup1_IRQn);
     Can_InitType param;
@@ -522,6 +533,7 @@ static void process2AppTask(void* arg)
                         Can_TxMsg.stdIDH = Can_ID>>3;
                         Can_TxMsg.stdIDL = Can_ID<<5;
                         Can_TxMsg.DLC  = 8;
+                        HAL_Can_Transmit(Can_TxMsg);
                         break;
                     }
                     case 1:
@@ -534,6 +546,7 @@ static void process2AppTask(void* arg)
                         Can_TxMsg.stdIDH = Can_ID>>3;
                         Can_TxMsg.stdIDL = Can_ID<<5;
                         Can_TxMsg.DLC  = 8;
+                        HAL_Can_Transmit(Can_TxMsg);
                         break;
                     }
                     case 2:
@@ -546,6 +559,7 @@ static void process2AppTask(void* arg)
                         Can_TxMsg.stdIDH = Can_ID>>3;
                         Can_TxMsg.stdIDL = Can_ID<<5;
                         Can_TxMsg.DLC  = 8;
+                        HAL_Can_Transmit(Can_TxMsg);
                         break;
                     }
                     case 3:
@@ -562,6 +576,7 @@ static void process2AppTask(void* arg)
                         Can_TxMsg.stdIDH = Can_ID>>3;
                         Can_TxMsg.stdIDL = Can_ID<<5;
                         Can_TxMsg.DLC  = 8;
+                        HAL_Can_Transmit(Can_TxMsg);
                         break;
                     }
                     case 4:
@@ -575,12 +590,14 @@ static void process2AppTask(void* arg)
                         Can_TxMsg.stdIDH = Can_ID>>3;
                         Can_TxMsg.stdIDL = Can_ID<<5;
                         Can_TxMsg.DLC  = 8;
+                        HAL_Can_Transmit(Can_TxMsg);
                         break;
                     }
                     default:
                     {
                        PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
                        Can_Rece_buffer[0]=0xff;
+                       break;
                     }
                 }
                 // #ifdef USING_PRINTF
@@ -591,7 +608,6 @@ static void process2AppTask(void* arg)
                 //     }
                 //     printf("\n");
                 // #endif
-                HAL_Can_Transmit(Can_TxMsg);
                 #ifdef USING_PRINTF
                         printf("CANsend :%x\n ",Can_ID);
                 #endif
@@ -604,7 +620,6 @@ static void process2AppTask(void* arg)
                 {
                     osDelay(3000);
                 }                                     
-                break;
             }
         }
     }
@@ -628,6 +643,8 @@ void TcpCallBack(void)
 }
 void Tcp_Data_Assemble(uint8_t datatype)
 {
+    int16_t Batt_current;
+    Batt_current = Uart_Rece_BattI;
     OsaUtcTimeTValue timestracture;
     appGetSystemTimeUtcSync(&timestracture);
     battbuffer[0] = timestracture.UTCtimer1>>16;
@@ -654,20 +671,20 @@ void Tcp_Data_Assemble(uint8_t datatype)
         battbuffer[15] = 0x00;//故障代码高
         battbuffer[16] = 0x00;//故障代码低
         //电流适应性更改,从int转换到uint,加1000的偏移量,100mA的单位
-        if (Uart_Rece_BattI>0x8000)
+        if (Batt_current>0x8000)
         {
-            Uart_Rece_BattI = Uart_Rece_BattI|0x7fff;
-            Uart_Rece_BattI = 0x2710 - Uart_Rece_BattI;
-            Uart_Rece_BattI = Uart_Rece_BattI/10;
+            Batt_current = Batt_current|0x7fff;
+            Batt_current = 0x2710 - Batt_current;
+            Batt_current = Batt_current/10;
         }
         else
         {
-            Uart_Rece_BattI = Uart_Rece_BattI+0x2710;
-            Uart_Rece_BattI = Uart_Rece_BattI/10;
+            Batt_current = Batt_current+0x2710;
+            Batt_current = Batt_current/10;
         }
         
-        battbuffer[17] = Uart_Rece_BattI>>8;
-        battbuffer[18] = Uart_Rece_BattI;
+        battbuffer[17] = Batt_current>>8;
+        battbuffer[18] = Batt_current;
 
         data_index = 32+Batt_Cell_Num_2+Batt_Temp_Num;
         battbuffer[data_index] = 0x00;//电池状态
@@ -684,13 +701,14 @@ void Tcp_Data_Assemble(uint8_t datatype)
 }
 static void process3AppTask(void* arg)
 {
-    CHAR SN[] = "GYTEST00000000001";
+    CHAR SN[] = "GYTEST00000000002";
     CHAR   serverip[] = "47.97.127.222";
     UINT16 serverport = 8712;
     int TcpConnectID = -1;
     int TcpSendID = -1;
     int NB_send_len=59+Batt_Cell_Num_2+Batt_Temp_Num;//设定网络发送最大数值
-	PROC_Can_STATE_SWITCH(PROCESS_STATE_IDLE);
+    slpManApplyPlatVoteHandle("process3slp",&process3SlpHandler);
+	PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_IDLE);
 
     while(1)
     {
@@ -706,7 +724,7 @@ static void process3AppTask(void* arg)
                 {
                     TcpConnectID = tcpipConnectionCreate(1,PNULL,PNULL,serverip,serverport,TcpCallBack);
                 }
-                PROC_NB_STATE_SWITCH(PROCESS_STATE_WORK);
+                PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_WORK);
                 break;
             }
             case PROCESS_NB_STATE_WORK:
@@ -732,6 +750,14 @@ static void process3AppTask(void* arg)
                 *(TcpSendBuffer+23) = data_index;//数据长度
 
                 memcpy(TcpSendBuffer+24,battbuffer,data_index);
+                #ifdef USING_PRINTF
+                    printf("battbuffer:");
+                    for (int i = 0; i < data_index; i++)
+                    {
+                        printf("%x ",battbuffer[i]);
+                    }
+                    printf("\n ");
+                #endif
                 *(TcpSendBuffer+NB_send_len-1) = bcc_chk(TcpSendBuffer,NB_send_len-1);
                 // #ifdef USING_PRINTF
                 //     printf("[%d]sizeof:%d \r\n",__LINE__,sizeof(TcpSendBuffer)-1);
@@ -745,13 +771,11 @@ static void process3AppTask(void* arg)
                     {
                         printf("%x ",*(TcpSendBuffer+i));
                     }
-                #endif
-                #ifdef USING_PRINTF
-                        printf("\n ");
+                    printf("\n ");
                 #endif
                 TcpSendID  = tcpipConnectionSend(TcpConnectID,TcpSendBuffer,NB_send_len,PNULL,PNULL,PNULL);
-                PROC_NB_STATE_SWITCH(PROCESS_STATE_IDLE);
                 free(TcpSendBuffer);
+                PROC_NB_STATE_SWITCH(PROCESS_NB_STATE_IDLE);
                 break;
             }
             case PROCESS_NB_STATE_SLEEP: