From a7c0d42a6590c26d37c17b082aef52925b466569 Mon Sep 17 00:00:00 2001
From: tao_z <tzj0429@163.com>
Date: Sun, 29 May 2022 10:03:04 +0800
Subject: [PATCH] bootloader运行,烧录流程尚未测试

---
 /dev/null                            |   82 -------------
 Application/source/uds_modules/did.c |   43 ++++++
 Cmsis/Startup/startup_XL6600.S       |   82 -------------
 xl6600_flash.ld                      |    2 
 Application/source/main.c            |    3 
 Application/source/uds_modules/uds.c |   84 +++++++------
 Application/source/nvm.c             |    8 
 Application/source/uds_modules/did.h |    4 
 8 files changed, 98 insertions(+), 210 deletions(-)

diff --git a/Application/include/cali.h b/Application/include/cali.h
deleted file mode 100644
index 398c2c0..0000000
--- a/Application/include/cali.h
+++ /dev/null
@@ -1,82 +0,0 @@
-/**
- * @file cali.h
- * @author Ethan.Tao (tzj0429@163.com)
- * @brief
- * @version 0.1
- * @date 2022-05-07
- *
- * @copyright Copyright (c) 2022 �������������㲿�����޹�˾
- *
- */
-
-#ifndef CALI_H
-#define CALI_H
-#include "typedef.h"
-#define GEAR_ALPHA (0)
-#define GEAR_BETA (1)
-#define AUTO_GEAR_USED (GEAR_BETA)
-#define MANUAL_GEAR_USED (GEAR_ALPHA)
-
-#define VA_Z_CALA (36u)          // 4B
-#define VA_M_CALA (40u)          // 4B
-#define VA_MP_CALA (44u)         // 4B
-#define VA_MN_CALA (48u)         // 4B
-#define VA_X2_CALA (52u)         // 4B
-#define VA_X1_CALA (56u)         // 4B
-#define VA_Y1_CALA (60u)         // 4B
-#define VA_Y2_CALA (64u)         // 4B
-#define VA_GearGapAuto (68u)     // 4B
-#define VA_GearGapManu (72u)     // 4B
-#define VA_CALIBRATED_FLAG (76u) // 4B
-
-typedef struct
-{
-    unsigned short alpha;
-    unsigned short beta;
-} Angle_t;
-
-typedef struct cali
-{
-
-    Angle_t Z;
-    Angle_t M;
-    Angle_t MP;
-    Angle_t MN;
-    Angle_t X2;
-    Angle_t X1;
-    Angle_t Y1;
-    Angle_t Y2;
-    Angle_t Auto_delta;
-    Angle_t Manu_delta;
-    uint32_t CalibratedFlag;
-} CaliData_t;
-
-typedef enum DCAL_CalCmd
-{
-    CAL_INIT = 0x0000,
-    CAL_Zero = 0x0001,
-    CAL_M = 0x0002,
-    CAL_MP = 0x0003,
-    CAL_MN = 0x0004,
-    CAL_X2 = 0x0005,
-    CAL_X1 = 0x0006,
-    CAL_Y1 = 0x0007,
-    CAL_Y2 = 0x0008,
-    CAL_GAP = 0x0009,
-    CAL_FINISH = 0x000A
-} CAL_Cmd_e;
-typedef enum DCAL_CalStatus
-{
-    CAL_IDLE = 0,
-    CAL_In_Progress,
-    CAL_Current_Done,
-    Cal_Complete
-} CAL_CalStatus_e;
-
-extern CaliData_t Cali_O_AngleData;
-extern void Cali_Init(void);
-extern void Cali_Task(void *p);
-extern uint8_t Cali_GetDataLoadFlag(void);
-extern uint8_t Cali_SetCalibrationCmd(uint32_t cmd);
-extern uint8_t Cali_GetSaveCalibrationDataFlag(uint8_t cmd);
-#endif
diff --git a/Application/source/main.c b/Application/source/main.c
index 39d2bfb..1e5a2c3 100644
--- a/Application/source/main.c
+++ b/Application/source/main.c
@@ -37,9 +37,10 @@
 	Hal_CANConfig();
 	Hal_CrcInit(); // CRC FOR FLASH
 	UDS_Init();
-	did_init();
+
 	NVM_Init();
 	light_init();
+	did_init();
 }
 
 /**
diff --git a/Application/source/nvm.c b/Application/source/nvm.c
index 2c087ad..9be73eb 100644
--- a/Application/source/nvm.c
+++ b/Application/source/nvm.c
@@ -55,8 +55,8 @@
 
 extern int8_t Fbl_CheckPresent()
 {
-    int8_t rtn = 0;
-    uint8_t i = 0;
+    int8_t rtn = 1;
+    uint8_t i = 1;
     uint8_t prensnttemp[8] = {0};
     uint8_t prensntmasktemp[8] = {0};
     FblReadPattern(prensnttemp, APP_PRESENT_ADDR);
@@ -65,7 +65,7 @@
     {
         if ((prensnttemp[i] + prensntmasktemp[i]) != 0xff)
         {
-            rtn = 1;
+            rtn = 0;
             break;
         }
     }
@@ -88,7 +88,7 @@
     if ((retValue1 == EE_OK) && (retValue2 == EE_OK))
     {
         // rtn = OP_STATUS_OK;
-        rtn = FblWriteProgramBlock();
+        rtn = DID_SaveProgamDataToNvm();
     }
     return rtn;
 }
diff --git a/Application/source/uds_modules/did.c b/Application/source/uds_modules/did.c
index 08a918f..e73fcce 100644
--- a/Application/source/uds_modules/did.c
+++ b/Application/source/uds_modules/did.c
@@ -44,16 +44,51 @@
 
 uint16_t DIDTable_Entry_Count_u16 = 0;
 UDS_DIDtableEntry DIDtable[UDS_DID_TABLE_SIZE];
-static uint8_t DID_LoaderWaitTimer = 0;
+// static uint8_t DID_LoaderWaitTimer = 0;
+static uint8_t DID_NeedtoSaveFlag = 0;
 
 void did_init(void)
 {
+    DID_NeedtoSaveFlag = 0;
     UDS_InitDIDMapRW(DID_NS_ProgrammingCounter, VA_PROGRAM_DATA, &NVM_ProgramBLOCK.ProgrammingCounter, 1);
     UDS_InitDIDMapRW(DID_NS_ProAtpCounter, VA_PROGRAM_DATA, &NVM_ProgramBLOCK.ProgrammingAttempCounter, 1);
     UDS_InitDIDMapRW(DID_NS_ReadFingerPrint, VA_PROGRAM_DATA, &NVM_ProgramBLOCK.FingerPrint_W, 9);
     UDS_InitDIDMapRW(DID_NS_RepairShopCodeOrTester, VA_PROGRAM_DATA, &NVM_ProgramBLOCK.RepairShopCodeOrTesterSerialNumber, 16);
     UDS_InitDIDMapRW(DID_NS_ECUProgtammingDate, VA_PROGRAM_DATA, &NVM_ProgramBLOCK.UDS_NS_ProgrammingDate.ProgrammingDate_YY_H, 4);
     UDS_InitDIDMapRW(DID_NS_WriteFingerPrint, VA_PROGRAM_DATA, &NVM_ProgramBLOCK.FingerPrint_W, 10);
+
+    FblReadProgramBlock(); // load program data.must after ee_init()
+}
+
+extern void DID_SetWriteSaveFlag(void)
+{
+    DID_NeedtoSaveFlag = 1;
+}
+
+extern int8_t DID_SaveProgamDataToNvm(void)
+{
+    int8_t rtn = OP_STATUS_FAIL;
+
+    if (DID_NeedtoSaveFlag == 1)
+    {
+        rtn = FblWriteProgramBlock();
+    }
+    else
+    {
+        rtn = OP_STATUS_OK;
+    }
+}
+
+extern void DID_UpdateProgramCnt(void)
+{
+    NVM_ProgramBLOCK.ProgrammingCounter += 1;
+    DID_NeedtoSaveFlag = 1;
+}
+
+extern void DID_UpdateAtpProgramCnt(void)
+{
+    NVM_ProgramBLOCK.ProgrammingAttempCounter += 1;
+    DID_NeedtoSaveFlag = 1;
 }
 
 extern int8_t FblReadProgramBlock(void)
@@ -61,13 +96,14 @@
 
     uint32_t *p_dest = (uint32_t *)(&NVM_ProgramBLOCK);
     uint8_t i = 0;
-    int8_t rtn = OP_STATUS_FAIL;
+    int8_t rtn = OP_STATUS_OK;
     EE_Status retValue1 = EE_WRITE_ERROR;
 
     for (i = 0; i < (ProgramBlockLenth >> 2); i++)
     {
         if (EE_OK != EE_ReadVariable32bits(VA_PROGRAM_DATA + i * 4, (p_dest + i)))
         {
+            rtn = OP_STATUS_FAIL;
             break;
         }
     }
@@ -78,13 +114,14 @@
 {
     uint32_t *p_dest = (uint32_t *)(&NVM_ProgramBLOCK);
     uint8_t i = 0;
-    int8_t rtn = OP_STATUS_FAIL;
+    int8_t rtn = OP_STATUS_OK;
     EE_Status retValue1 = EE_WRITE_ERROR;
 
     for (i = 0; i < (ProgramBlockLenth >> 2); i++)
     {
         if (EE_OK != EE_WriteVariable32bits(VA_PROGRAM_DATA + i * 4, *(p_dest + i)))
         {
+            rtn = OP_STATUS_FAIL;
             break;
         }
     }
diff --git a/Application/source/uds_modules/did.h b/Application/source/uds_modules/did.h
index ffe8a3d..6f0b45f 100644
--- a/Application/source/uds_modules/did.h
+++ b/Application/source/uds_modules/did.h
@@ -76,4 +76,8 @@
 void did_init(void);
 extern int8_t FblReadProgramBlock(void);
 extern int8_t FblWriteProgramBlock(void);
+extern void DID_UpdateProgramCnt(void);
+extern void DID_UpdateAtpProgramCnt(void);
+extern int8_t DID_SaveProgamDataToNvm(void);
+extern void DID_SetWriteSaveFlag(void);
 #endif /* DID_H */
diff --git a/Application/source/uds_modules/uds.c b/Application/source/uds_modules/uds.c
index 904fbc6..7f1040c 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"
@@ -308,7 +309,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);
@@ -376,7 +377,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},
@@ -1030,23 +1031,17 @@
 
             if (CDT_HASEXPIRED(TesterPresentTimer_u16) != false)
             {
-                UDS_SwitchtoDefaultSession();
+                UDS_SwitchtoDefaultSession(true);
             }
         }
         else
         {
-            UDS_SwitchtoDefaultSession();
+            UDS_SwitchtoDefaultSession(true);
         }
     }
     else
     {
         CDT_RESET(TesterPresentTimer_u16, 0);
-#if 0
-      if(CDT_EXPIRED(EntrySleepModeTimer_u16)==true)
-      {
-        //SBC_shutdown();
-      }
-#endif
     }
 }
 
@@ -1204,29 +1199,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 +1426,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 +1454,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 +1466,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 +1518,6 @@
         /* response with the request data */
         UDS_RespTxMessage();
     }
-#ifdef ENABLE_BOOTLOADER
-    if (UDS_L_Diag_session_e == PROGRAMMING)
-    { /*reset es*/
-        CPU_HardReset();
-    }
-#endif
 }
 
 /*****************************************************************************/
@@ -2076,6 +2080,7 @@
         {
         }
     }
+    DID_UpdateAtpProgramCnt();
     /*always from buff 0*/
     currentbuffId = 0;
     UDS_L_TransferDataState_e = TRANFER_START;
@@ -2245,6 +2250,7 @@
     /* Compose a response message and send a positive */
     /* response with the request data */
     UDS_RespTxMessage();
+    DID_UpdateProgramCnt();
 }
 
 /*****************************************************************************/
@@ -2404,10 +2410,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)
diff --git a/Cmsis/Startup/startup_XL6600.S b/Cmsis/Startup/startup_XL6600.S
index fab7396..bb60df2 100644
--- a/Cmsis/Startup/startup_XL6600.S
+++ b/Cmsis/Startup/startup_XL6600.S
@@ -258,88 +258,6 @@
 	.word	Reserved21_IRQHandler
 	.word	BootRAM       /* @0x1E0. This is for boot in RAM mode for
                          xl6600d High Density devices. */
-	
-/******************************************************************************
-*
-* The minimal vector table for a Cortex M3.  Note that the proper constructs
-* must be placed on this to ensure that it ends up at physical address
-* 0x0000.0000.
-*
-******************************************************************************/
- 	.section	.isr_vector_dup,"a",%progbits
- 	.align 4
-	.type	g_pfnVectors_dup, %object
-	.size	g_pfnVectors_dup, .-g_pfnVectors_dup
-
-
-g_pfnVectors_dup:
-	.word	_estack
-	.word	Reset_Handler
-	.word	NMI_Handler
-	.word	HardFault_Handler
-	.word	MemManage_Handler
-	.word	BusFault_Handler
-	.word	UsageFault_Handler
-	.word	0
-	.word	0
-	.word	0
-	.word	0
-	.word	SVC_Handler
-	.word	DebugMon_Handler
-	.word	0
-	.word	PendSV_Handler
-	.word	SysTick_Handler
-	.word	Reserved16_IRQHandler
-	.word	Reserved17_IRQHandler
-	.word	Reserved18_IRQHandler
-	.word	Reserved19_IRQHandler
-	.word	Reserved20_IRQHandler
-	.word	FMC_IRQHandler
-	.word	PMC_IRQHandler
-	.word	DMA_IRQHandler
-	.word	I2C0_IRQHandler
-	.word	I2C1_IRQHandler
-	.word	SPI0_IRQHandler
-	.word	SPI1_IRQHandler
-	.word	UART0_IRQHandler
-	.word	UART1_IRQHandler
-	.word	UART2_IRQHandler
-	.word	ADC0_IRQHandler
-	.word	ACMP0_IRQHandler
-	.word	FTM0_IRQHandler
-	.word	FTM1_IRQHandler
-	.word	FTM2_IRQHandler
-	.word	RTC_IRQHandler
-	.word	ACMP1_IRQHandler
-	.word	PIT_CH0_IRQHandler
-	.word	PIT_CH1_IRQHandler
-	.word	KBI0_IRQHandler
-	.word	KBI1_IRQHandler
-	.word	Reserved42_IRQHandler
-	.word	ICS_IRQHandler
-	.word	WDOG_IRQHandler
-	.word	PWT_IRQHandler
-	.word	MCAN_IRQHandler
-	.word	EXTI0_IRQHandler
-	.word	EXTI1_IRQHandler
-	.word	EXTI2_IRQHandler
-	.word	EXTI3_IRQHandler
-	.word	EXTI4_IRQHandler
-	.word	EXTI5_IRQHandler
-	.word	EXTI6_IRQHandler
-	.word	EXTI7_IRQHandler
-	.word	EXTI8_IRQHandler
-	.word	EXTI9_IRQHandler
-	.word	EXTI10_IRQHandler
-	.word	EXTI11_IRQHandler
-	.word	EXTI12_IRQHandler
-	.word	EXTI13_IRQHandler
-	.word	EXTI14_IRQHandler
-	.word	EXTI15_IRQHandler
-	.word	EWM_IRQHandler
-	.word	Reserved21_IRQHandler
-	.word	BootRAM       /* @0x1E0. This is for boot in RAM mode for
-                         xl6600d High Density devices. */
 GPIOA:
     .word	0x40100000
 GPIOB:
diff --git a/xl6600_flash.ld b/xl6600_flash.ld
index 1d01fb0..072b2d4 100644
--- a/xl6600_flash.ld
+++ b/xl6600_flash.ld
@@ -45,7 +45,7 @@
   DRAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 18K
   RAM_VECT_TBL (xrw) : ORIGIN = 0x20000000,        LENGTH = 0x200
   FALSH_DRIVER_RAM (xrw):ORIGIN = 0x20000200,        LENGTH = 0x100
-  BOOTFLAG (xrw)  : ORIGIN = 0x20004700, LENGTH = 0x100
+  BOOTFLAG (xrw)  : ORIGIN = 0x20000300, LENGTH = 0x100
 
   FLASH_BOOT     (rx) : ORIGIN = 0x0000000, LENGTH = 48K
   FLASH_APP      (rx) : ORIGIN = 0x000C100, LENGTH = 256K -48K -8K-0x100

--
Gitblit v1.8.0