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 |  450 ++++++++++++++++++++++++++++++-------------------------
 1 files changed, 246 insertions(+), 204 deletions(-)

diff --git a/Application/source/uds_modules/uds.c b/Application/source/uds_modules/uds.c
index 904fbc6..153ebff 100644
--- a/Application/source/uds_modules/uds.c
+++ b/Application/source/uds_modules/uds.c
@@ -9,6 +9,7 @@
 #include "cantp_user.h"
 #include "xl_mcan.h"
 #include "uds.h"
+#include "did.h"
 #include "fbl_ap.h"
 #include "fbl_def.h"
 #include "nvm.h"
@@ -222,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                                               */
@@ -259,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             */
@@ -308,7 +310,7 @@
 /* Build DID data set */
 static bool UDS_BuildDidSet(uint8_t *payload);
 /* Switch to default session */
-static void UDS_SwitchtoDefaultSession(void);
+static void UDS_SwitchtoDefaultSession(uint8_t resetcmd);
 /*Security access*/
 
 static void UDS_ControlDTCSetting(uint8_t *payload);
@@ -355,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
 {
@@ -376,7 +381,7 @@
     {ECU_RESET,                     UDS_ECURest,                false,  true,  false,    true,  SECURITY_ACCESS_LOCKED},
     {READ_DATA_BY_IDENTIFIER,       UDS_ReadData,               true,  true,  false,    false, SECURITY_ACCESS_LOCKED},
     {SECURITY_ACCESS,               UDS_SecurityAccess,         false, true,  false,   false, SECURITY_ACCESS_LOCKED},
-    {COMMUNICATION_CONTROL,         UDS_CommunicationControl,   false, true,  true,    true,  SECURITY_ACCESS_LOCKED},
+    // {COMMUNICATION_CONTROL,         UDS_CommunicationControl,   false, true,  true,    true,  SECURITY_ACCESS_LOCKED},
     {WRITE_DATA_BY_IDENTIFIER,      UDS_WriteDataByIdentifier,  false, true,  false,   false, SECURITY_ACCESS_UNLOCK_LEVEL_1},
     {ROUTINE_CONTROL,               UDS_RoutineControl,         false, true,  false,   false, SECURITY_ACCESS_UNLOCK_LEVEL_2},
     {TESTER_PRESENT,                UDS_TesterPresent,          true,  true,  true,    true,  SECURITY_ACCESS_LOCKED},
@@ -422,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;
 }
@@ -699,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;
@@ -758,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);
@@ -1030,23 +1061,17 @@
 
             if (CDT_HASEXPIRED(TesterPresentTimer_u16) != false)
             {
-                UDS_SwitchtoDefaultSession();
+                UDS_SwitchtoDefaultSession(false);
             }
         }
         else
         {
-            UDS_SwitchtoDefaultSession();
+            UDS_SwitchtoDefaultSession(false);
         }
     }
     else
     {
         CDT_RESET(TesterPresentTimer_u16, 0);
-#if 0
-      if(CDT_EXPIRED(EntrySleepModeTimer_u16)==true)
-      {
-        //SBC_shutdown();
-      }
-#endif
     }
 }
 
@@ -1204,29 +1229,14 @@
         }
         else
         {
-            if ((DID_u16 & 0xff00u) == 0xF100u)
-            {
-                /* UDS_WriteF1DIDdata(&payload[3], Index_to_be_written); */
-                memcpy(DIDtable[Index_to_be_written].address, &payload[3],
-                       DIDtable[Index_to_be_written].size);
-            }
-            else
-            {
-                /* Flip the data contents in rx buffer */
-                UDS_FlipEndian(&payload[3], DIDtable[Index_to_be_written].size);
-
-                /* not sure this should be done... */
-                /* memory copy the data into DID data location */
-                memcpy(DIDtable[Index_to_be_written].address, &payload[3],
-                       DIDtable[Index_to_be_written].size);
-            }
+            /* UDS_WriteF1DIDdata(&payload[3], Index_to_be_written); */
+            memcpy(DIDtable[Index_to_be_written].address, &payload[3],
+                   DIDtable[Index_to_be_written].size);
 
             /* Write the change to NVM */
             if (DIDtable[Index_to_be_written].nvBlockID != 0xFF)
             {
-                // todo
-                //                CAL_Save_Calibration_Block(
-                //                    DIDtable[Index_to_be_written].nvBlockID);
+                DID_SetWriteSaveFlag();
             }
 
             UDS_L_tx_msg_buffer[0] = GET_RESPONSE_SERVICE_ID(WRITE_DATA_BY_IDENTIFIER);
@@ -1446,6 +1456,7 @@
 /*****************************************************************************/
 static void UDS_SessionControl(uint8_t *payload)
 {
+    uint16_t cnt = 5000u;
     /* Change session based on CAN message request */
     bool UDS_SessionSetOk_b = false;
     uint8_t sub_func = UDS_GET_SUB_FUNCTION(payload[UDS_RQ_BYTE1]);
@@ -1473,6 +1484,7 @@
         {
             /**/
             UDS_L_Diag_session_e = PROGRAMMING;
+            UDS_SessionSetOk_b = true;
             FblSetFblProgramRequestFlag();
         }
         else if ((UDS_L_Diag_session_e == DEFAULT) || (UDS_L_ProgrammingPreconditionCheckOK_b == false)) /*DEFAULT session can not trans to PROGRAMMING*/
@@ -1484,7 +1496,35 @@
         }
         break;
     case DEFAULT:
-        UDS_SwitchtoDefaultSession();
+        if (UDS_L_Diag_session_e == PROGRAMMING)
+        {
+            UDS_L_tx_msg_buffer[0] = GET_RESPONSE_SERVICE_ID(SESSION_CONTROL);
+            UDS_L_tx_msg_buffer[1] = UDS_L_Diag_session_e;
+            UDS_L_tx_msg_buffer[2] = 0;
+            UDS_L_tx_msg_buffer[3] = 0;
+            UDS_L_tx_msg_buffer[4] = 0;
+            UDS_L_tx_msg_buffer[5] = 0;
+
+            /* Specify the send payload size */
+            UDS_L_tx_msg_size = 0x06;
+
+            /* Compose a response message and send a positive */
+            /* response with the request data */
+            UDS_RespTxMessage();
+            while (cnt--)
+            {
+                ; /// wait send command send out and then reset
+            }
+
+            UDS_SwitchtoDefaultSession(true);
+            while (1)
+                ;
+        }
+        else
+        {
+            UDS_SwitchtoDefaultSession(false);
+        }
+
         TesterPresentTimer_u16 = 0;
         break;
     default:
@@ -1508,12 +1548,6 @@
         /* response with the request data */
         UDS_RespTxMessage();
     }
-#ifdef ENABLE_BOOTLOADER
-    if (UDS_L_Diag_session_e == PROGRAMMING)
-    { /*reset es*/
-        CPU_HardReset();
-    }
-#endif
 }
 
 /*****************************************************************************/
@@ -1768,33 +1802,34 @@
             {
                 if (transferType == DOWNLOAD_FLASH)
                 {
-                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_CODEFLASH);
-                }
-                else
-                {
-                    UDS_L_CRCCheckFail_b = !MEM_PartitionCRCOK((uint8_t)MEM_PARTITION_RAM);
+                    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
                     }
                 }
-                if (UDS_L_CRCCheckFail_b == false)
+                else
                 {
-                    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();
-
-                    UDS_L_ResponsePendingRequest_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
-                    UDS_L_ResponsePendingRelease_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
+                    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
+                    }
                 }
+                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();
+
+                UDS_L_ResponsePendingRequest_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
+                UDS_L_ResponsePendingRelease_e &= (~CHECK_PROGRAMMING_INTERGRITY_PENDING);
             }
         }
     }
@@ -1910,7 +1945,7 @@
         UDS_L_tx_msg_buffer[1] = 0x01;
         UDS_L_tx_msg_buffer[2] = 0xff;
         UDS_L_tx_msg_buffer[3] = 0x00;
-        UDS_L_tx_msg_buffer[4] = (uint8_t) true;
+        UDS_L_tx_msg_buffer[4] = 0; // 0:ok 1��error
         /* Specify the send payload size */
         UDS_L_tx_msg_size = 5;
         UDS_RespTxMessage();
@@ -1965,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);
 }
@@ -2076,6 +2111,7 @@
         {
         }
     }
+    DID_UpdateAtpProgramCnt();
     /*always from buff 0*/
     currentbuffId = 0;
     UDS_L_TransferDataState_e = TRANFER_START;
@@ -2179,6 +2215,7 @@
     operation_write.current_sequenc = expectedSequenceCnt;
     operation_write.prev_seqence = currentSequenceCnt;
 
+    UDS_SendNegResp(NRC_SERVICE_BUSY);              // first send busy
     write_result = NEM_WriteData(&operation_write); // todo write all data at this function
     if (write_result == OP_STATUS_OK)
     {
@@ -2245,6 +2282,7 @@
     /* Compose a response message and send a positive */
     /* response with the request data */
     UDS_RespTxMessage();
+    DID_UpdateProgramCnt();
 }
 
 /*****************************************************************************/
@@ -2404,10 +2442,14 @@
 /* (1)      Date: 10/30/2014  Fogbugz: case#: 2283                           */
 /*          Initial release by Tom Tao                                      */
 /*****************************************************************************/
-static void UDS_SwitchtoDefaultSession(void)
+static void UDS_SwitchtoDefaultSession(uint8_t restcmd)
 {
     UDS_L_Diag_session_e = DEFAULT;
     UDS_L_ProgrammingPreconditionCheckOK_b = false;
+    if (restcmd == true)
+    {
+        CPU_HardReset();
+    }
 }
 
 static void UDS_StringCopy(char *p_src, char *p_dest, uint8_t max_size)

--
Gitblit v1.8.0