From 0ff91e66071237834a1501c78648729c8c32f67a Mon Sep 17 00:00:00 2001
From: tao_z <tzj0429@163.com>
Date: Wed, 01 Jun 2022 03:18:48 +0800
Subject: [PATCH] 保存bootloader调试到下载flash,以前步骤均ok。

---
 Application/source/uds_modules/uds.c |  357 ++++++++++++++++++++++++++++++++---------------------------
 1 files changed, 192 insertions(+), 165 deletions(-)

diff --git a/Application/source/uds_modules/uds.c b/Application/source/uds_modules/uds.c
index f65d787..153ebff 100644
--- a/Application/source/uds_modules/uds.c
+++ b/Application/source/uds_modules/uds.c
@@ -223,7 +223,7 @@
 static UDS_CONTOL_TYPE_e UDS_L_CommunicationControlType_e = EnableRxAndTx;
 
 static uint8_t UDS_L_SecurityFault_cnt;
-static uint8_t UDS_L_SecurityStartDelay = 1;
+static uint8_t UDS_L_SecurityStartDelay = 0;
 static uint8_t UDS_L_SecurityAccessStep_e = SECURITY_ACCESS_STEP_MAX;
 /*****************************************************************************/
 /*  Local Variables Definition                                               */
@@ -260,6 +260,7 @@
 static uint16_t ResponsePendingTimer_u16;
 static bool UDS_L_TransferDataInProgressing_b = false;
 static bool UDS_L_CRCCheckFail_b = false;
+static uint32_t UDS_L_CRCReceived = 0;
 static UDS_TRANFER_DATA_STATE_Te UDS_L_TransferDataState_e;
 
 static uint8_t expectedSequenceCnt;        /* Block sequence counter             */
@@ -356,6 +357,9 @@
 #define ROUTINE_CONTROL_MIN_LEN (0x04u)
 #define TESTER_PRESENT_MIN_LEN (0x02u)
 #define CONTROL_DTC_SETTING_MIN_LEN (0x02u)
+#define REQUEST_DOWNLOAD_MIN_LEN (0xB)
+#define TRANSFER_DATA_MIN_LEN (0XC)
+#define REQUEST_TRANSFER_EXIT_MIN_LEN (0X01)
 
 typedef struct
 {
@@ -423,153 +427,170 @@
     uint8_t sid;
     sid = msg_buf[0];
 
-    if (msg_dlc < 2)
-    {
-        result = false;
-    }
-    else
-    {
-        subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
+    subfunction = UDS_GET_SUB_FUNCTION(msg_buf[1]);
 
-        switch (sid)
+    switch (sid)
+    {
+    case SESSION_CONTROL:
+    {
+        if (msg_dlc == SESSION_CONTROL_MIN_LEN)
         {
-        case SESSION_CONTROL:
-        {
-            if (msg_dlc == SESSION_CONTROL_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
+            result = true;
         }
-        break;
-        case ECU_RESET:
+        else
         {
-            if (msg_dlc == ECU_RESET_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
-        }
-        break;
-        case CLEAR_DIAGNOSTIC_INFORMATION:
-        {
-            if (msg_dlc == CLEAR_DIAGNOSTIC_INFORMATION_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
-        }
-        break;
-        case READ_DTC_INFORMATION:
-        {
-            if ((subfunction == REPORT_DTC_NUMBER_BY_STATUS_MASK && msg_dlc != (READ_DTC_INFORMATION_MIN_LEN + 1)) ||
-                (subfunction == REPORT_DTC_BY_STATUS_MASK && msg_dlc != (READ_DTC_INFORMATION_MIN_LEN + 1)) ||
-                (subfunction == REPORT_SUPPORTED_DTC && msg_dlc != READ_DTC_INFORMATION_MIN_LEN))
-            {
-                result = false;
-            }
-            else
-            {
-                result = true;
-            }
-        }
-        break;
-        case READ_DATA_BY_IDENTIFIER:
-        {
-            if (msg_dlc == READ_DATA_BY_IDENTIFIER_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
-        }
-        break;
-        case SECURITY_ACCESS:
-        {
-            if ((subfunction == UDS_REQUEST_SEED && msg_dlc != SECURITY_ACCESS_MIN_LEN) ||
-                (subfunction == UDS_SEND_KEY && msg_dlc != (SECURITY_ACCESS_MIN_LEN + KEY_SIZE)))
-            {
-                result = false;
-            }
-            else
-            {
-                result = true;
-            }
-        }
-        break;
-        case COMMUNICATION_CONTROL:
-        {
-            if (msg_dlc == COMMUNICATION_CONTROL_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
-        }
-        break;
-        case WRITE_DATA_BY_IDENTIFIER:
-        {
-            if (msg_dlc >= WRITE_DATA_BY_IDENTIFIER_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
-        }
-        break;
-        case ROUTINE_CONTROL:
-            if (msg_dlc >= ROUTINE_CONTROL_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
-            break;
-        case TESTER_PRESENT:
-        {
-            if (msg_dlc == TESTER_PRESENT_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
-        }
-        break;
-        case CONTROL_DTC_SETTING:
-        {
-            if (msg_dlc == CONTROL_DTC_SETTING_MIN_LEN)
-            {
-                result = true;
-            }
-            else
-            {
-                result = false;
-            }
-        }
-        break;
-        default:
             result = false;
-            break;
         }
+    }
+    break;
+    case ECU_RESET:
+    {
+        if (msg_dlc == ECU_RESET_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+    }
+    break;
+    case CLEAR_DIAGNOSTIC_INFORMATION:
+    {
+        if (msg_dlc == CLEAR_DIAGNOSTIC_INFORMATION_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+    }
+    break;
+    case READ_DTC_INFORMATION:
+    {
+        if ((subfunction == REPORT_DTC_NUMBER_BY_STATUS_MASK && msg_dlc != (READ_DTC_INFORMATION_MIN_LEN + 1)) ||
+            (subfunction == REPORT_DTC_BY_STATUS_MASK && msg_dlc != (READ_DTC_INFORMATION_MIN_LEN + 1)) ||
+            (subfunction == REPORT_SUPPORTED_DTC && msg_dlc != READ_DTC_INFORMATION_MIN_LEN))
+        {
+            result = false;
+        }
+        else
+        {
+            result = true;
+        }
+    }
+    break;
+    case READ_DATA_BY_IDENTIFIER:
+    {
+        if (msg_dlc == READ_DATA_BY_IDENTIFIER_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+    }
+    break;
+    case SECURITY_ACCESS:
+    {
+        if ((subfunction == UDS_REQUEST_SEED && msg_dlc != SECURITY_ACCESS_MIN_LEN) ||
+            (subfunction == UDS_SEND_KEY && msg_dlc != (SECURITY_ACCESS_MIN_LEN + KEY_SIZE)))
+        {
+            result = false;
+        }
+        else
+        {
+            result = true;
+        }
+    }
+    break;
+    case COMMUNICATION_CONTROL:
+    {
+        if (msg_dlc == COMMUNICATION_CONTROL_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+    }
+    break;
+    case WRITE_DATA_BY_IDENTIFIER:
+    {
+        if (msg_dlc >= WRITE_DATA_BY_IDENTIFIER_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+    }
+    break;
+    case ROUTINE_CONTROL:
+        if (msg_dlc >= ROUTINE_CONTROL_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+        break;
+    case TESTER_PRESENT:
+    {
+        if (msg_dlc == TESTER_PRESENT_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+    }
+    break;
+    case CONTROL_DTC_SETTING:
+    {
+        if (msg_dlc == CONTROL_DTC_SETTING_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+    }
+    break;
+    case REQUEST_DOWNLOAD:
+        if (msg_dlc == REQUEST_DOWNLOAD_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+        break;
+    case TRANSFER_DATA:
+        if (msg_dlc >= TRANSFER_DATA_MIN_LEN)
+        {
+            result = true;
+        }
+        else
+        {
+            result = false;
+        }
+        break;
+    case REQUEST_TRANSFER_EXIT:
+        result = true;
+
+        break;
+    default:
+        result = false;
+        break;
     }
     return result;
 }
@@ -700,7 +721,7 @@
     TesterPresentTimer_u16 = 0;
     UDS_NS_SessionTimeout_u16 = 5000;
     UDS_L_SecurityFault_cnt = 0;
-    UDS_L_SecurityStartDelay = 1;
+    UDS_L_SecurityStartDelay = 0;
     UDS_L_Diag_session_e = DEFAULT;
     UDS_O_SecurityAccessState_e = SECURITY_ACCESS_LOCKED;
     UDS_L_Current_SID = 0;
@@ -759,11 +780,20 @@
 static void UDS_RxTask_1ms(void)
 {
     static TP_PDU_t msg_rx_temp = {0};
-
+    CAN_PDU_t msg_can_temp = {0};
     uint8_t msg_num = MCAN_GetReceiveMessageCounter(MCAN);
     while (msg_num--)
     {
-        MCAN_ReceiveData(MCAN, (MCAN_MsgTypeDef *)(&msg_rx_temp));
+        MCAN_ReceiveData(MCAN, (MCAN_MsgTypeDef *)(&msg_can_temp));
+
+        msg_rx_temp.arbId = msg_can_temp.MCAN_ID;
+        msg_rx_temp.dlc = msg_can_temp.MCAN_DLC;
+
+        for (TP_uint8_t i = 0; i < msg_can_temp.MCAN_DLC; i++)
+        {
+            msg_rx_temp.frame[i] = msg_can_temp.MCAN_Data[i];
+        }
+
         if (msg_rx_temp.arbId == UDS_PHY_REQ_RID)
         {
             UDS_RxMessage(&msg_rx_temp);
@@ -1031,12 +1061,12 @@
 
             if (CDT_HASEXPIRED(TesterPresentTimer_u16) != false)
             {
-                UDS_SwitchtoDefaultSession(true);
+                UDS_SwitchtoDefaultSession(false);
             }
         }
         else
         {
-            UDS_SwitchtoDefaultSession(true);
+            UDS_SwitchtoDefaultSession(false);
         }
     }
     else
@@ -1772,7 +1802,7 @@
             {
                 if (transferType == DOWNLOAD_FLASH)
                 {
-                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_CODEFLASH);
+                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_CODEFLASH, UDS_L_CRCReceived);
                     if (UDS_L_CRCCheckFail_b == false)
                     {
                         NVM_ReInitFlashDriveRam(); // CLEAR FLASH DRIVE IN RAM
@@ -1780,29 +1810,26 @@
                 }
                 else
                 {
-                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_RAM);
+                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_RAM, UDS_L_CRCReceived);
                     if (UDS_L_CRCCheckFail_b == true)
                     {
                         NVM_ReInitFlashDriveRam(); // CLEAR FLASH DRIVE IN RAM
                     }
                 }
-                if (UDS_L_CRCCheckFail_b == false)
-                {
-                    UDS_L_tx_msg_buffer[0] = GET_RESPONSE_SERVICE_ID(ROUTINE_CONTROL);
-                    UDS_L_tx_msg_buffer[1] = 0x01;
-                    UDS_L_tx_msg_buffer[2] = 0xf0;
-                    UDS_L_tx_msg_buffer[3] = 0x01;
-                    UDS_L_tx_msg_buffer[4] = (uint8_t)UDS_L_CRCCheckFail_b;
-                    /* Specify the send payload size */
-                    UDS_L_tx_msg_size = 0x05;
+                UDS_L_tx_msg_buffer[0] = GET_RESPONSE_SERVICE_ID(ROUTINE_CONTROL);
+                UDS_L_tx_msg_buffer[1] = 0x01;
+                UDS_L_tx_msg_buffer[2] = 0xf0;
+                UDS_L_tx_msg_buffer[3] = 0x01;
+                UDS_L_tx_msg_buffer[4] = (uint8_t)UDS_L_CRCCheckFail_b;
+                /* Specify the send payload size */
+                UDS_L_tx_msg_size = 0x05;
 
-                    /* Compose a response message and send a positive */
-                    /* response with the request data */
-                    UDS_RespTxMessage();
+                /* Compose a response message and send a positive */
+                /* response with the request data */
+                UDS_RespTxMessage();
 
-                    UDS_L_ResponsePendingRequest_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
-                    UDS_L_ResponsePendingRelease_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
-                }
+                UDS_L_ResponsePendingRequest_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
+                UDS_L_ResponsePendingRelease_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
             }
         }
     }
@@ -1973,7 +2000,7 @@
 		FBL_UDS_L_ResponsePendingRelease_e &=(~CHECK_PROGRAMMING_INTERGRITY_PENDING); 
 	}
 #endif
-
+    UDS_L_CRCReceived = (uint32_t)payload[4] << 24 | (uint32_t)payload[5] << 16 | (uint32_t)payload[6] << 8 | (uint32_t)payload[7];
     UDS_L_ResponsePendingRequest_e |= CHECK_PROGRAMMING_INTERGRITY_PENDING;
     CDT_RESET(ResponsePendingTimer_u16, P2SERVER_PENDING_TIMEROUT);
 }

--
Gitblit v1.8.0