瀏覽代碼

[V2版本大改动:集成UDS下线检测和CAN],测试成功,2021.03.12

CHENJIE-PC\QiXiang_CHENJIE 4 年之前
父節點
當前提交
fedd22ed55
共有 13 個文件被更改,包括 1404 次插入55 次删除
  1. 3 1
      ARMCC/Makefile
  2. 24 0
      inc/CANTask.h
  3. 3 1
      inc/MainTask.h
  4. 12 0
      inc/UDSService.h
  5. 20 3
      inc/app.h
  6. 192 0
      src/CANTask.c
  7. 1 1
      src/GpsTask.c
  8. 304 7
      src/MainTask.c
  9. 1 1
      src/TcpTask.c
  10. 835 0
      src/UDSService.c
  11. 1 1
      src/UartTask.c
  12. 1 0
      src/app.c
  13. 7 40
      src/hal_module_adapter.c

+ 3 - 1
ARMCC/Makefile

@@ -21,6 +21,8 @@ obj-y             += PLAT/project/$(TARGET)/apps/qx_app/src/app.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/MainTask.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/UartTask.o \
 					PLAT/project/$(TARGET)/apps/qx_app/src/TcpTask.o  \
-					PLAT/project/$(TARGET)/apps/qx_app/src/GpsTask.o
+					PLAT/project/$(TARGET)/apps/qx_app/src/GpsTask.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/CANTask.o \
+					PLAT/project/$(TARGET)/apps/qx_app/src/UDSService.o
 
 include $(TOP)/PLAT/tools/scripts/Makefile.rules

+ 24 - 0
inc/CANTask.h

@@ -0,0 +1,24 @@
+
+
+
+extern CAN_Msg_Type CanRxMsg[2];
+extern UINT8	UDSCmd[2];
+extern UINT8	UDSService[2];
+extern UINT8   UDSSubService[2];
+extern UINT8	UDSSubServiceActionCode[2];
+extern UINT8 	UDSSwitch;
+extern UINT8   UDSBattSN[BATT_SN_LEN];
+
+
+typedef enum
+{
+    PROCESS_CAN_STATE_IDLE = 0,
+    PROCESS_CAN_STATE_WORK,
+    PROCESS_CAN_STATE_SLEEP
+}process_CAN;
+
+void CANTaskInit(void* arg);
+static void Can_Receive(void);
+
+
+

+ 3 - 1
inc/MainTask.h

@@ -7,10 +7,12 @@
  *
  ****************************************************************************/
 //全局变量
+#include "app.h"
 extern UINT32 Timer_count;//每100ms加1
 extern volatile bool Sleep_flag;//睡眠标志位
+extern AppNVMDataType AppNVMData;
 /*---------------------------------------------------------------------------*/
-#define PROC_MAIN_TASK_STACK_SIZE           (512)
+#define PROC_MAIN_TASK_STACK_SIZE           (1024)
 typedef enum
 {
     PROCESS_STATE_IDLE = 0,

+ 12 - 0
inc/UDSService.h

@@ -0,0 +1,12 @@
+#include "TcpTask.h"
+
+extern CAN_Msg_Type CanRxMsg[2];
+
+extern UINT8   BattSN[BATT_SN_LEN];  //ASCII code
+extern AppNVMDataType AppNVMData;
+extern AppSocketConnectionContext socContext;
+extern UINT8 GpsData[16];
+
+
+extern void appSaveNVMData(void);
+extern void UDS_Service(void);

+ 20 - 3
inc/app.h

@@ -25,10 +25,27 @@ extern "C" {
 #define BATT_SN_LEN           17
 #define	SWVERSION		0xA21
 #define HWVERSION		0xB22
-#define APP_CHARGING_WORK_TIME      30
-#define APP_WAKEUP_WORK_TIME		1
-#define APP_SLEEP_TIME				5
+#define APP_CONFIG_FILE_LATEST_VERSION 1
+#define APP_CONFIG_FILE_NAME  "qxappconfig.nvm"
 //--------------------------------------------------------------------------------
+typedef struct AppNVMDataType
+{	
+    BOOL   appDataModify;		//数据更改标志位
+	UINT8  battSN[BATT_SN_LEN]; //电池SN号码
+   	BOOL   isBattLocked;		//0:not locked;	1:locked
+    UINT8  chargEndWorkTime;   //the unit is min
+    UINT8  wakeupWorkTime;     //the unit is min
+    UINT8  sleepTime;
+    UINT16 HwVersion;
+    UINT16 SwVersion;
+}AppNVMDataType;
+
+typedef struct AppConfigHeader
+{
+    UINT16 fileBodySize; //file body size, not include size of header;
+    UINT8  version;
+    UINT8  checkSum;
+}AppConfigHeader;
 typedef struct _UartAnswerMsg
 {
 	bool UartFlag;

+ 192 - 0
src/CANTask.c

@@ -0,0 +1,192 @@
+#include "bsp.h"
+#include "bsp_custom.h"
+#include "osasys.h"
+#include "ostask.h"
+#include "queue.h"
+#include "ps_event_callback.h"
+#include "cmisim.h"
+#include "cmimm.h"
+#include "cmips.h"
+#include "sockets.h"
+#include "psifevent.h"
+#include "ps_lib_api.h"
+#include "lwip/netdb.h"
+#include <cis_def.h>
+#include "debug_log.h"
+#include "slpman_ec616.h"
+#include "plat_config.h"
+#include "ec_tcpip_api.h"
+#include "hal_module_adapter.h"
+#include "UartTask.h"
+#include "MainTask.h"
+#include "app.h"
+#include "CANTask.h"
+#include "UDSService.h"
+
+CAN_Msg_Type CanRxMsg[2];
+
+
+
+
+/*线程声明*/
+#define 			PROC_CAN_RX_TASK_STACK_SIZE			(1024)
+static StaticTask_t gProcess_Can_Rx_Task_t;
+static UINT8 		gProcess_Can_Rx_TaskStack[PROC_CAN_RX_TASK_STACK_SIZE];
+
+
+static process_CAN	gProcess_CAN_Task = PROCESS_CAN_STATE_IDLE;
+#define 			PROC_CAN_STATE_SWITCH(a)  (gProcess_CAN_Task = a)
+volatile bool 		CAN_Sleep_State = false;
+UINT8 				CanSendSlpHandler	= 0xfd;
+
+
+
+void CANTaskInit(void* arg)
+{
+    osThreadAttr_t task_rx_attr,task_tx_attr;
+    Can_InitType param;
+    param.baudrate = CAN_250Kbps;
+	param.mode = REQOP_NORMAL;	
+	param.packType = STD_PACK;
+	
+    memset(&task_rx_attr,0,sizeof(task_rx_attr));
+    memset(gProcess_Can_Rx_TaskStack, 0, PROC_CAN_RX_TASK_STACK_SIZE);
+    task_rx_attr.name = "Can_Task";
+    task_rx_attr.stack_mem = gProcess_Can_Rx_TaskStack;
+    task_rx_attr.stack_size = PROC_CAN_RX_TASK_STACK_SIZE;
+    task_rx_attr.priority = osPriorityBelowNormal7;
+    task_rx_attr.cb_mem = &gProcess_Can_Rx_Task_t;
+    task_rx_attr.cb_size = sizeof(StaticTask_t);
+	
+    HAL_Can_Init(param);
+    osDelay(1000);
+    osThreadNew(Can_Receive, NULL, &task_rx_attr);
+}
+
+
+static void Can_Receive()
+{    
+	slpManSlpState_t State;
+    uint8_t cnt,temp,udsFlag=0;
+	UINT8 i = 0;
+
+    while(true)
+    {
+		memset(CanRxMsg, 0, sizeof(CanRxMsg));    		
+		switch(gProcess_CAN_Task)
+		{
+			case PROCESS_CAN_STATE_IDLE:
+				CAN_Sleep_State = false;
+				
+				PROC_CAN_STATE_SWITCH(PROCESS_CAN_STATE_WORK);
+				
+				break;				
+			
+			case PROCESS_CAN_STATE_WORK:
+				{
+					
+					HAL_Can_Receive(CanRxMsg);
+					for(i=0; i<2; i++)
+					{								
+						if(CanRxMsg[i].Id == 0x7A0)
+						{	
+							udsFlag = 1;
+							UDSService[i] =  CanRxMsg[i].Data[0];
+							UDSSubService[i] = CanRxMsg[i].Data[1];
+							UDSSubServiceActionCode[i] = CanRxMsg[i].Data[2];
+							if(UDSService[i] == 0x10)
+							{
+								if(UDSSubService[i] == 0x02)
+								{
+									UDSSwitch = 1;
+								}								
+							}
+						}
+					}	
+					if(udsFlag==1)
+					{	
+						UDS_Service();
+						udsFlag = 0;							
+					}
+					else
+					{
+						//osDelay(2000);
+					}
+/*********************************************/
+#if 0
+					UINT32 NTCR2,NTCR3,NTCR4,NTCR5,Vbat = 0;
+					INT32 ret1 = 0;
+					ret1 = AdcSendReq(ADC_REQ_BITMAP_CH1,&NTCR2,01,ADC_GET_RESULT_TIMOUT);
+					//if(ret1==0)
+					{	
+						printf("ret1 = %d\n",ret1);
+						printf("NTCR2 = %d\n",NTCR2);
+
+					}
+					osDelay(1000);
+					ret1 = AdcSendReq(ADC_REQ_BITMAP_CH2,&NTCR3,01,ADC_GET_RESULT_TIMOUT);
+					//if(ret1==0)
+					{	
+					
+						printf("ret1 = %d\n",ret1);
+						printf("NTCR3 = %d\n",NTCR3);
+
+					}
+					osDelay(1000);
+					ret1 = AdcSendReq(ADC_REQ_BITMAP_CH31,&NTCR4,01,ADC_GET_RESULT_TIMOUT);
+					//if(ret1==0)
+					{	
+					
+						printf("ret1 = %d\n",ret1);
+						printf("NTCR4 = %d\n",NTCR4);
+
+					}
+					osDelay(1000);
+					ret1 = AdcSendReq(ADC_REQ_BITMAP_CH30,&NTCR5,01,ADC_GET_RESULT_TIMOUT);
+					//if(ret1==0)
+					{	
+					
+						printf("ret1 = %d\n",ret1);
+						printf("NTCR5 = %d\n",NTCR5);
+
+					}
+					osDelay(1000);
+					ret1 = AdcSendReq(ADC_REQ_BITMAP_CH4,&Vbat,01,ADC_GET_RESULT_TIMOUT);
+					//if(ret1==0)
+					{	
+					
+						printf("ret1 = %d\n",ret1);
+						printf("Vbat = %d\n",Vbat);
+
+					}
+					osDelay(1000);
+
+
+
+
+#endif
+/*********************************************/
+
+
+
+
+					
+				break;
+			}
+	        case PROCESS_CAN_STATE_SLEEP:
+	        {	            
+	            HAL_Can_Sleep();	 
+	            
+	            CAN_Sleep_State = true;
+	            while(true)
+	            {
+	                osDelay(60000/portTICK_PERIOD_MS);
+	            }	            
+	            break;
+	       }
+	   }
+	   osDelay(50);
+	}      
+}
+
+

+ 1 - 1
src/GpsTask.c

@@ -85,7 +85,7 @@ static void GpsTask(void* arg)
                 }
                 if (strcmp(databuffer[1],"V")==0|strcmp(databuffer[2],"V")==0)
                 {
-                    GpsData[0] = 0x00;
+                    memset(GpsData,0x00,sizeof(GpsData));
                 }
                 else if (strcmp(databuffer[2],"A")==0)
                 {

+ 304 - 7
src/MainTask.c

@@ -35,6 +35,7 @@
 UINT32 Timer_count;//每100ms加1
 volatile bool Sleep_flag = false;//睡眠标志位
 
+AppNVMDataType AppNVMData;
 
 //主线程堆栈声明区
 static StaticTask_t           gProcess_Main_Task_t;
@@ -56,6 +57,14 @@ static void appAfterSlp2(void *pdata, slpManLpState state);
 void montior_timer_callback(TimerHandle_t xTimer);
 void work_timer_callback(TimerHandle_t xTimer);
 
+static void setDefaultAppDataValue(void);
+void appLoadConfig(void);
+void appSaveConfig(void);
+static void appGetNVMSavedData(void);
+void appSaveNVMData(void);
+
+
+AppConfigHeader    AppConfigHr;   //4 bytes
 
 
 //主线程任务区
@@ -76,14 +85,32 @@ static void MainTask(void* arg)
     TimerHandle_t montior_timer = NULL;
     TimerHandle_t work_timer = NULL;
     slpManWakeSrc_e Wakeup_source;
-    
-    static UINT8 work_time;
-    static UINT8 Sleep_Time;
-    Sleep_Time = APP_SLEEP_TIME;//睡眠时长 min
-    work_time = APP_WAKEUP_WORK_TIME;//工作时长 min
 
+	appGetNVMSavedData();
+    #ifdef USING_PRINTF
+    UINT8 *pReadAppConfig;
+    UINT32 readCount;
+    //AppConfigHeader    AppConfigHr;
+    OSAFILE file;
+    file = OsaFopen(APP_CONFIG_FILE_NAME,"rb");
+    OsaFseek(file, 0, SEEK_SET);
+    pReadAppConfig  = OsaAllocZeroMemory(AppConfigHr.fileBodySize+4);    
+    readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize+4, 1, file);
+    printf("AppConfigHr.fileBodySize+4 = %d\n",AppConfigHr.fileBodySize+4);
+    printf("readCount = %d\n",readCount);
+    printf("the config data = \n");
+    for (int i ;i<AppConfigHr.fileBodySize+4;i++)
+        printf("%d ",*(pReadAppConfig+i));
+    printf("\n");
+    #endif
+
+
+	
     montior_timer = xTimerCreate("montior_timer", 100 / portTICK_RATE_MS, pdTRUE, NULL, montior_timer_callback);
-    work_timer = xTimerCreate("work_timer", work_time*60*1000 / portTICK_RATE_MS, pdFALSE, NULL, work_timer_callback);
+    work_timer = xTimerCreate("work_timer", AppNVMData.wakeupWorkTime*60*1000 / portTICK_RATE_MS, pdFALSE, NULL, work_timer_callback);
+
+	
+	
     PROC_MAIN_STATE_SWITCH(PROCESS_STATE_IDLE);
     while(true)
     {
@@ -136,7 +163,7 @@ static void MainTask(void* arg)
                 #ifdef USING_PRINTF
                     printf("which slpstate can go now :%d \n",slpstate);
                 #endif
-                slpManDeepSlpTimerStart(deepslpTimerID, Sleep_Time*1000*60);
+                slpManDeepSlpTimerStart(deepslpTimerID, AppNVMData.sleepTime*1000*60);
                 while(1)
                 {
                     osDelay(20*60*1000/portTICK_PERIOD_MS);
@@ -249,4 +276,274 @@ static void appAfterSlp2(void *pdata, slpManLpState state)
 }
 
 
+static void appGetNVMSavedData(void)
+{
+    appLoadConfig();
+
+/*	IH_appChargEndWorkTime = AppNVMData.chargEndWorkTime;
+	IH_appWakeupWorkTime=AppNVMData.wakeupWorkTime;
+	IH_appSleepTime = AppNVMData.sleepTime;
+	IH_isBattLocked = AppNVMData.isBattLocked;
+	MEMCPY(&BattSN, AppNVMData.battSN, BATT_SN_LEN);	
+*/
+}
+
+void appSaveNVMData(void)
+{
+/*
+	AppNVMData.chargEndWorkTime = OH_appChargEndWorkTime;
+    AppNVMData.wakeupWorkTime = OH_appWakeupWorkTime;
+    AppNVMData.sleepTime = OH_appSleepTime ;
+    AppNVMData.isBattLocked = OH_isBattLocked;    
+	MEMCPY( AppNVMData.battSN, &BattSN, BATT_SN_LEN);
+	*/
+	appSaveConfig();
+}
+
+static void appSaveConfig(void)
+{
+    OSAFILE fp = PNULL;
+    UINT32  writeCount = 0;
+    AppConfigHeader    AppConfigHr;   //4 bytes
+
+    /*
+     * open the NVM file
+    */
+    fp = OsaFopen(APP_CONFIG_FILE_NAME, "wb");   //read & write
+    if(OsaFseek(fp, 0, SEEK_SET) != 0)
+	{
+        #ifdef USING_PRINTF
+				printf("Seek file failed [%d] \r\n",__LINE__);
+	    #endif
+	    OsaFclose(fp);
+        return;
+	}
+    if (fp == PNULL)
+    {
+	   #ifdef USING_PRINTF
+			printf(" NVM, can't open/create NVM: 'qxappconfig.nvm', save NVM failed\n");
+		#endif
+        return;
+    }
+
+    /*
+     * write the header
+    */
+    AppConfigHr.fileBodySize   = sizeof(AppNVMData);
+    AppConfigHr.version	= 	APP_CONFIG_FILE_LATEST_VERSION;    
+    AppConfigHr.checkSum       = OsaCalcCrcValue((UINT8 *)&AppNVMData, sizeof(AppNVMData));
+
+    writeCount = OsaFwrite(&AppConfigHr, sizeof(AppConfigHeader), 1, fp);
+    if (writeCount != 1)
+    {
+        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppConfigSave_2, P_ERROR, 0,
+        //           " NVM: 'qxappconfig.nvm', write the file header failed");
+	#ifdef USING_PRINTF
+				printf(" NVM: 'qxappconfig.nvm', write the file header failed\n");
+	#endif
+
+        OsaFclose(fp);
+        return;
+    }
+
+    /*
+     * write the file body
+    */
+    writeCount = OsaFwrite(&AppNVMData, sizeof(AppNVMData), 1, fp);
+    if (writeCount != 1)
+    {
+        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppConfigSave_3, P_ERROR, 0,
+        //           " NVM: 'qxappconfig.nvm', write the file body failed");
+        #ifdef USING_PRINTF
+					printf(" NVM: 'qxappconfig.nvm', write the file body failed\n");
+		#endif
+    }
+    OsaFclose(fp);
+    return;
+}
+
+
+void appLoadConfig(void)
+{
+    OSAFILE fp = PNULL;
+    UINT32  readCount = 0;
+    //AppConfigHeader    AppConfigHr;   //4 bytes
+    UINT8   crcCheck = 0;
+    BOOL    needAdjust = FALSE;
+    void    *pReadAppConfig = (void *)&AppNVMData;
+    /*
+     * open the NVM file
+    */
+    fp = OsaFopen(APP_CONFIG_FILE_NAME, "rb");   //read only
+    
+    if (fp == PNULL)
+    {
+	#ifdef USING_PRINTF
+		printf(" NVM, can't open NVM: 'qxappConfig.nvm', use the defult value\n");
+	#endif
+        setDefaultAppDataValue();
+        appSaveConfig();
+        return;
+    }
+    /*
+     * read the file header
+    */
+    readCount = OsaFread(&AppConfigHr, sizeof(AppConfigHeader), 1, fp);
+    
+    if (readCount != 1)
+    {
+        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_header_e_1, P_ERROR, 1,
+        //            "NVM: 'qxappconfig.nvm', can't read header, return: %d, use the defult value", readCount);
+
+	#ifdef USING_PRINTF
+				printf("NVM: 'qxappconfig.nvm', can't read header, return: %d, use the defult value \n");
+	#endif
+
+        OsaFclose(fp);
+        setDefaultAppDataValue();
+        appSaveConfig();
+
+        return;
+    }
+
+    if (AppConfigHr.version != APP_CONFIG_FILE_LATEST_VERSION)
+    {
+        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_ver_w_1, P_ERROR, 2,
+        //            "NVM: 'qxappconfig.nvm', version: %d not latest: %d, try to adapt",
+        //            AppConfigHr.version, MID_WARE_NVM_FILE_LATEST_VERSION);
+
+		#ifdef USING_PRINTF
+				printf("NVM: 'qxappconfig.nvm', version: %d not latest: %d, try to adapt\n",
+						AppConfigHr.version, MID_WARE_NVM_FILE_LATEST_VERSION);
+		#endif
+        if (AppConfigHr.fileBodySize > 1024)   //in fact this NVM size should limited in 1KB
+        {
+            //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_ver_e_1, P_ERROR, 2,
+            //            "NVM: 'qxappconfig.nvm', version: %d, NVM body size: %d > 1024, not right, use the default value",
+             //           AppConfigHr.version, AppConfigHr.fileBodySize);
+			#ifdef USING_PRINTF
+				printf("NVM: 'qxappconfig.nvm', version: %d, NVM body size: %d > 1024, not right, use the default value",
+                       AppConfigHr.version, AppConfigHr.fileBodySize);
+			#endif
+            OsaFclose(fp);
+            setDefaultAppDataValue();
+            appSaveConfig();
+
+            return;
+        }
+
+        /*
+         * As need to do adaption, can't read the old NVM into "mwNvmConfig", here we allocate a new buffer to store it
+        */
+        pReadAppConfig  = OsaAllocZeroMemory(AppConfigHr.fileBodySize);
+
+        if (pReadAppConfig == PNULL)
+        {
+            //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_buff_e_1, P_ERROR, 2,
+            //            "NVM: 'qxappconfig.nvm', version: %d, can not allo memsize: %d, use the default value",
+            //            AppConfigHr.version, AppConfigHr.fileBodySize);
+			#ifdef USING_PRINTF
+				printf("NVM: 'qxappconfig.nvm', version: %d, can not allo memsize: %d, use the default value",
+                        AppConfigHr.version, AppConfigHr.fileBodySize);
+			#endif
+            OsaFclose(fp);
+            setDefaultAppDataValue();
+            appSaveConfig();
+
+            return;
+        }		
+        needAdjust = TRUE;
+    }
+    else if (AppConfigHr.fileBodySize != sizeof(AppNVMData))  //file version is the same, but NVM file size not right
+    {
+        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_2, P_ERROR, 2,
+        //            "MW NVM: 'midwareconfig.nvm', file body size not right: (%u/%u), use the defult value",
+        //            AppConfigHr.fileBodySize, sizeof(AppNVMData));
+		#ifdef USING_PRINTF
+				printf("MW NVM: 'midwareconfig.nvm', file body size not right: (%u/%u), use the defult value",
+        		            AppConfigHr.fileBodySize, sizeof(AppNVMData));
+		#endif
+		
+        OsaFclose(fp);
+
+        setDefaultAppDataValue();
+        appSaveConfig();
+
+        return;
+    }
+
+    /*
+     * read the file body
+    */
+    
+    readCount = OsaFread(pReadAppConfig, AppConfigHr.fileBodySize, 1, fp);
+    crcCheck = OsaCalcCrcValue((UINT8 *)pReadAppConfig, AppConfigHr.fileBodySize);
+	
+    if (readCount != 1 ||
+        crcCheck != AppConfigHr.checkSum)
+    {
+        //ECOMM_TRACE(UNILOG_PLA_MIDWARE, AppLoadNvmConfig_3, P_ERROR, 2,
+        //            "NVM: 'qxappconfig.nvm', can't read body, or body not right, (%u/%u), use the defult value",
+        //            crcCheck, AppConfigHr.checkSum);
+		#ifdef USING_PRINTF
+			printf(" NVM: 'qxappconfig.nvm', can't read body, or body not right, (%u/%u), use the defult value\n ",
+						crcCheck, AppConfigHr.checkSum);
+		#endif
+        OsaFclose(fp);
+        setDefaultAppDataValue();
+        appSaveConfig();
+
+        if (needAdjust)
+        {
+            OsaFreeMemory(&pReadAppConfig);
+        }
+        return;
+    }
+
+#if 1
+    if (needAdjust)
+    {
+        OsaFclose(fp);
+        setDefaultAppDataValue();
+        appSaveConfig();
+
+        /* free memory */
+        OsaFreeMemory(&pReadAppConfig);
+    }
+#endif
+
+#if 0 
+	int i=0;
+
+	printf("AppConfigHeader:\n");
+	printf("%d,%d,%d\n",AppConfigHr.fileBodySize,AppConfigHr.version,AppConfigHr.checkSum);
+	printf("AppConfigFileBody:\n");
+	printf("%d,%d,%d\n",AppNVMData.chargEndWorkTime,AppNVMData.wakeupWorkTime,AppNVMData.sleepTime);
+	printf("\n");
+#endif
+    return;
+}
+
+
+//if the AppNVMData.version(read form nvm) != APP_CONFIG_FILE_LATEST_VERSION, it will setDefaultAppDataValue()
+//so if you want to modify the default value, please modify the APP_CONFIG_FILE_LATEST_VERSION (in MainTask.h) meanwhile, so that will be store to the nvm file
+static void setDefaultAppDataValue(void)
+{
+    UINT8  i = 0;
+    memset(&AppNVMData, 0x00, sizeof(AppNVMDataType));
+	for(i=0;i<BATT_SN_LEN;i++)
+	{
+		AppNVMData.battSN[i] = 0x30;  //the defult sn is "0000..0000"(17 bytes ASCII)
+	}
+	AppNVMData.isBattLocked = false;
+	AppNVMData.chargEndWorkTime = 30;  //30 min
+	AppNVMData.wakeupWorkTime = 2;	//2min
+	AppNVMData.sleepTime = 5;			//5min
+    AppNVMData.HwVersion = HWVERSION;
+    AppNVMData.SwVersion = SWVERSION;
+    return;
+}
+
+
+
 

+ 1 - 1
src/TcpTask.c

@@ -41,7 +41,7 @@ extern osMutexId_t GpsMutex;
 extern UINT8 GpsData[16];
 CHAR BattSN[BATT_SN_LEN] = "GYTEST00000000003";//SN仍在测试
 //局部变量申请
-static AppSocketConnectionContext socContext = {-1, APP_SOCKET_CONNECTION_CLOSED};
+AppSocketConnectionContext socContext = {-1, APP_SOCKET_CONNECTION_CLOSED};
 static StaticTask_t             gProcess_Tcp_Task_t;
 static UINT8                  gProcess_Tcp_TaskStack[PROC_TCP_TASK_STACK_SIZE];
 static QueueHandle_t            psEventQueueHandle;//状态转换队列句柄

+ 835 - 0
src/UDSService.c

@@ -0,0 +1,835 @@
+#include "bsp.h"
+#include "bsp_custom.h"
+#include "osasys.h"
+#include "ostask.h"
+#include "queue.h"
+#include "ps_event_callback.h"
+#include "cmisim.h"
+#include "cmimm.h"
+#include "cmips.h"
+#include "sockets.h"
+#include "psifevent.h"
+#include "ps_lib_api.h"
+#include "lwip/netdb.h"
+#include <cis_def.h>
+#include "debug_log.h"
+#include "slpman_ec616.h"
+#include "plat_config.h"
+#include "ec_tcpip_api.h"
+#include "hal_module_adapter.h"
+#include "UartTask.h"
+#include "MainTask.h"
+#include "app.h"
+#include "CANTask.h"
+#include "UDSService.h"
+
+
+//
+extern UartReadMsgType UartReadMsg;
+extern osMutexId_t UartMutex;
+
+void UDS_Service(void);
+void UDS_CAN_Update(void);
+BOOL UDSClearFotaDownloadRegion(void);
+BOOL UDSAskforDownLoadData(void);
+extern void appSaveConfigValue(void);
+
+
+UINT8	UDSService[2];
+UINT8	UDSSubService[2];
+UINT8	UDSSubServiceActionCode[2];
+UINT8 	UDSSwitch = 0;
+UINT8   UDSDialogMode = 0;
+UINT8   UDSBattSN[BATT_SN_LEN];
+
+UINT16  updateDifferDataByteLen = 0;
+
+//each package means 100 bytes, max PackageCounter = updateDifferDataByteLen%100 + 1
+UINT16  updateDifferDataPackageCounter = 0;  
+
+//each slice means 4 bytes, max SliceCounter = 25;
+UINT16  updateDifferDataSliceCounter = 0;
+
+
+UINT8   updateDataBuffer[100];
+
+extern UINT8 BattSN[BATT_SN_LEN];
+
+
+void UDS_Service()
+{
+	UINT8 i,j,k = 0;
+	INT8 ret = 0;
+	BOOL boolRet = false;
+	static UINT8 snFlag = 0;
+	static UINT32 sliceCounterFlag = 0;
+	UINT16 tempSliceCounter=0;
+	static UINT8 counter=0;  
+	CAN_Msg_Type UDSAnswer ={0};
+	static UINT8 battInfo[8*14] = {0};
+	
+	//UartReadMsg Uart_Recv_Msg;
+	//UINT8 temp00[94] ={0x00,0x0E,0x00,0x05,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0x7A,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x0E,0x42,0x0D,0xDE,0x0D,0xDE,0x0D,0xDE,0x13,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x21,0x00,0x24,0x00,0x26,0x00,0x22,0x00,0x24,0x00,0x00,0x00,0x00,0x6e,0x78,0x00,0x00,0x00,0x00,0x00,0x5F,0x00,0x62,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x13,0x74,0x0E,0x42,0x0D,0x7A,0x00,0x00};
+	//MEMCPY(battInfo, temp00, 94);
+	
+	//while(true)
+	//{
+		//if(UDSSwitch == 1)
+		{
+			for(i=0; i<2; i++)
+			{
+				if(CanRxMsg[i].Id == 0x7A0)
+					{
+					switch (UDSService[i])
+						{
+							case 0x10:
+								if(UDSSubService[i] == 0x01)
+								{
+									if(UDSDialogMode == 0x01 || UDSDialogMode==0x02)
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x04;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0x00;
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSSwitch = 0;
+										UDSDialogMode = 1;
+									}
+									else
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x7F;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0xFF;
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+								}
+								else if(UDSSubService[i] == 0x02)
+								{
+									UDSAnswer.Id = 0x7A8;
+									UDSAnswer.DLC = 8;
+									UDSAnswer.Data[0] = 0x04;
+									UDSAnswer.Data[1] = 0x78;
+									UDSAnswer.Data[2] = UDSService[i];
+									UDSAnswer.Data[3] = UDSSubService[i];
+									UDSAnswer.Data[4] = 0x00;
+									UDSAnswer.Data[5] = 0x00;
+									UDSAnswer.Data[6] = 0x00;
+									UDSAnswer.Data[7] = 0x00;
+									UDSSwitch = 1;
+									UDSDialogMode = 2;
+									ret = HAL_Can_Transmit(UDSAnswer);
+								}
+								else if(UDSSubService[i] == 0x03)
+								{
+									if(UDSSwitch == 1)
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x04;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0x00;
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+										UDSSwitch = 1;
+										UDSDialogMode = 3;
+			                		}
+			                		else
+			                		{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x7F;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+			                		}
+
+								}
+								else if(UDSSubService[i] == 0x11)   //make NB software reset
+								{
+									if(UDSDialogMode = 2)
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x04;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0x00;
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+										osDelay(100);
+										appSaveNVMData();
+										appSetCFUN(0);
+										osDelay(1000);
+			                			EC_SystemReset();
+			                		}
+			                		else
+			                		{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x7F;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+			                		}
+								}								
+								else
+								{
+									UDSAnswer.Id = 0x7A8;
+									UDSAnswer.DLC = 8;
+									UDSAnswer.Data[0] = 0x05;
+									UDSAnswer.Data[1] = 0x7F;
+									UDSAnswer.Data[2] = UDSService[i];
+									UDSAnswer.Data[3] = UDSSubService[i];
+									UDSAnswer.Data[4] = 0xEE;  //the sub service is not surported
+									UDSAnswer.Data[5] = 0x00;
+									UDSAnswer.Data[6] = 0x00;
+									UDSAnswer.Data[7] = 0x00;
+									ret = HAL_Can_Transmit(UDSAnswer);
+								}
+								break;
+							case 0x22:
+								if(UDSDialogMode == 2)
+								{
+									if(UDSSubService[i] == 0x01)		//check the sw of NB
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x06;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = SWVERSION>>8;
+										UDSAnswer.Data[5] = SWVERSION;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+									else if(UDSSubService[i] == 0x02)  //check the hw of NB
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x06;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = HWVERSION>>8;
+										UDSAnswer.Data[5] = HWVERSION;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+									else if(UDSSubService[i] == 0x03)  //check the SN number
+									{	
+										UDSAnswer.DLC = 8;
+										switch(UDSSubServiceActionCode[i])
+										{
+											case 00:
+												UDSAnswer.Id = 0x20;
+												for(k=0; k<8; k++)
+												{
+													UDSAnswer.Data[k] = AppNVMData.battSN[k+8*0];
+												}
+												ret = HAL_Can_Transmit(UDSAnswer);
+												break;
+												
+											case 01:
+												UDSAnswer.Id = 0x21;
+												for(k=0; k<8; k++)
+												{
+													UDSAnswer.Data[k] = AppNVMData.battSN[k+8*1];
+												}
+												ret = HAL_Can_Transmit(UDSAnswer);
+												break;
+
+											case 02:
+												UDSAnswer.Id = 0x22;
+												UDSAnswer.Data[0] = AppNVMData.battSN[16];;
+												for(k=1; k<8; k++)
+												{
+													UDSAnswer.Data[k] = 0x00;
+												}
+												ret = HAL_Can_Transmit(UDSAnswer);
+												break;
+											default:
+											break;
+											
+										}
+									#ifdef USING_PRINTF
+										printf("ID = %x\nData = ",UDSAnswer.Id);
+										for (int ii = 0; ii < 8; ii++)
+										{
+											printf("%x  ", UDSAnswer.Data[ii]);
+										}
+										printf("\n");
+									#endif
+									}
+									else if(UDSSubService[i] == 0x04)  //check the batt message
+									{
+									/*
+										if(UDSSubService[i] == 0x00)
+										{
+											ret = xQueuePeek(UartReadQueueHandle,&Uart_Recv_Msg,1000);
+										}
+										if(ret)
+										{
+											//the uart_recv_msg.len = 5+(0x21+CellNum+TempNum)*2 = 5+(33+17+7)*2 = 	5+ 114(max);114>112(14*8)	
+											UINT8* p = (UINT8*)Uart_Recv_Msg.dataPtr+3;
+											MEMCPY(battInfo, p, 14*8);																			
+										}
+										*/
+										osStatus_t result = osMutexAcquire(UartMutex, osWaitForever);
+										if(CanRxMsg[i].Data[2] == 0x00)
+										{
+											memcpy(battInfo,UartReadMsg.data,14*8);
+											#ifdef USING_PRINTF
+											printf("battInfo=\n");
+											for(int jj = 0;jj<14*8;jj++)
+												printf("%x ",battInfo[jj]);
+											printf("\n");
+											#endif
+										}
+										osMutexRelease(UartMutex);
+
+										UDSAnswer.Id = 0x30+UDSSubServiceActionCode[i];
+										UDSAnswer.DLC = 8;
+
+										MEMCPY(UDSAnswer.Data, &(battInfo[UDSSubServiceActionCode[i]*8]),8);
+										ret = HAL_Can_Transmit(UDSAnswer);
+										#ifdef USING_PRINTF
+										printf("ID = %x\nData = ",UDSAnswer.Id);
+										for (int ij = 0; ij < 8; ij++)
+										{
+											printf("%x  ", UDSAnswer.Data[ij]);
+										}
+										printf("\n");
+										#endif
+									}
+									else if(UDSSubService[i] == 0x05)  //check the enviroment temp of NB
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x08;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0x00;//gNtcDev.NTCvalue[2+NTC_Channel2];
+										UDSAnswer.Data[5] = 0x00;//gNtcDev.NTCvalue[2+NTC_Channel3];
+										UDSAnswer.Data[6] = 0x00;//gNtcDev.NTCvalue[2+NTC_Channel4];
+										UDSAnswer.Data[7] = 0x00;//gNtcDev.NTCvalue[2+NTC_Channel5];
+										ret = HAL_Can_Transmit(UDSAnswer);
+										
+									}									
+									else if(UDSSubService[i] == 0x06)	//check the tcp link of NB
+									{
+										
+
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x06;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = socContext.status;
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+									else if(UDSSubService[i] == 0x07)  //check the GPS link of NB
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = GpsData[1];  //gps satellite num(uint8), should be modified
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}	
+									else
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x7F;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0xEE;  //the sub service is not surported
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+								}
+								else	//当前会话模式错误
+								{
+									UDSAnswer.Id = 0x7A8;
+									UDSAnswer.DLC = 8;
+									UDSAnswer.Data[0] = 0x05;
+									UDSAnswer.Data[1] = 0x7F;
+									UDSAnswer.Data[2] = UDSService[i];
+									UDSAnswer.Data[3] = UDSSubService[i];
+									UDSAnswer.Data[4] = 0xFF;
+									UDSAnswer.Data[5] = 0x00;
+									UDSAnswer.Data[6] = 0x00;
+									UDSAnswer.Data[7] = 0x00;
+									ret = HAL_Can_Transmit(UDSAnswer);
+								}
+								break;
+							case 0x2E:
+								if(UDSDialogMode == 2)
+								{
+									if(UDSSubService[i] == 0x03)
+									{
+										if(UDSSubServiceActionCode[i] == 0x00 && snFlag == 0x00)
+										{
+											for(j=0; j<5; j++)
+											{
+												UDSBattSN[j+5*0] = CanRxMsg[i].Data[j+3];
+											}	
+											snFlag = snFlag|0x01;
+										}
+										else if(UDSSubServiceActionCode[i] == 0x01 && snFlag == 0x01)
+										{
+											for(j=0; j<5; j++)
+											{
+												UDSBattSN[j+5*1] = CanRxMsg[i].Data[j+3];
+											}
+											snFlag = snFlag|0x02;
+										}
+										else if(UDSSubServiceActionCode[i] == 0x02 && snFlag == 0x03)
+										{
+											for(j=0; j<5; j++)
+											{
+												UDSBattSN[j+5*2] = CanRxMsg[i].Data[j+3];
+											}
+											snFlag = snFlag|0x04;
+										}
+										else if(UDSSubServiceActionCode[i] == 0x03 && snFlag == 0x07)
+										{
+											for(j=0; j<2; j++)
+											{
+												UDSBattSN[j+5*3] = CanRxMsg[i].Data[j+3];
+											}
+											snFlag = snFlag|0x08;
+										}
+
+										if(snFlag == 0x0F)
+										{
+											UDSAnswer.Id = 0x7A8;
+											UDSAnswer.DLC = 8;
+											UDSAnswer.Data[0] = 0x04;
+											UDSAnswer.Data[1] = 0x78;
+											UDSAnswer.Data[2] = UDSService[i];
+											UDSAnswer.Data[3] = UDSSubService[i];
+											UDSAnswer.Data[4] = 0x00;
+											UDSAnswer.Data[5] = 0x00;
+											UDSAnswer.Data[6] = 0x00;
+											UDSAnswer.Data[7] = 0x00;
+											snFlag = 0;
+
+											MEMCPY(AppNVMData.battSN, UDSBattSN, BATT_SN_LEN);											
+										}
+										else
+										{
+											UDSAnswer.Id = 0x7A8;
+											UDSAnswer.DLC = 8;
+											UDSAnswer.Data[0] = 0x05;
+											UDSAnswer.Data[1] = 0x3E;
+											UDSAnswer.Data[2] = UDSService[i];
+											UDSAnswer.Data[3] = UDSSubService[i];
+											UDSAnswer.Data[4] = snFlag;	
+											UDSAnswer.Data[5] = 0x00;
+											UDSAnswer.Data[6] = 0x00;
+											UDSAnswer.Data[7] = 0x00;
+										}
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+									else
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x7F;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+									break;
+								}
+								else if(UDSDialogMode==3)
+								{
+									if(UDSSubService[i] == 0x0F) //程序更新配置信息写入
+									{										
+										updateDifferDataByteLen = (CanRxMsg[i].Data[2]<<8)|(CanRxMsg[i].Data[3]);
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x04;
+										UDSAnswer.Data[1] = 0x78;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0x00;			
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+									else
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x7F;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+								}						
+								
+								else
+								{
+									UDSAnswer.Id = 0x7A8;
+									UDSAnswer.DLC = 8;
+									UDSAnswer.Data[0] = 0x05;
+									UDSAnswer.Data[1] = 0x7F;
+									UDSAnswer.Data[2] = UDSService[i];
+									UDSAnswer.Data[3] = UDSSubService[i];
+									UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
+									UDSAnswer.Data[5] = 0x00;
+									UDSAnswer.Data[6] = 0x00;
+									UDSAnswer.Data[7] = 0x00;
+									ret = HAL_Can_Transmit(UDSAnswer);
+								}
+									break;
+							case 0x31:
+								if(UDSDialogMode == 3)
+								{
+									if(UDSSubService[i] == 0x0F)
+									{
+										if(UDSSubServiceActionCode[i] == 0x01)
+										{
+											//Clear the Flash
+											boolRet = UDSClearFotaDownloadRegion();
+											//if ok
+											
+											UDSAnswer.Id = 0x7A8;
+											UDSAnswer.DLC = 8;
+											UDSAnswer.Data[0] = 0x05;
+											UDSAnswer.Data[1] = 0x78;
+											UDSAnswer.Data[2] = UDSService[i];
+											UDSAnswer.Data[3] = UDSSubService[i];
+											UDSAnswer.Data[4] = (UINT8)boolRet;  // true:01, false: 00			
+											UDSAnswer.Data[5] = 0x00;
+											UDSAnswer.Data[6] = 0x00;
+											UDSAnswer.Data[7] = 0x00;
+											ret = HAL_Can_Transmit(UDSAnswer);						
+										}
+										else
+										{
+											UDSAnswer.Id = 0x7A8;
+											UDSAnswer.DLC = 8;
+											UDSAnswer.Data[0] = 0x06;
+											UDSAnswer.Data[1] = 0x7F;
+											UDSAnswer.Data[2] = UDSService[i];
+											UDSAnswer.Data[3] = UDSSubService[i];
+											UDSAnswer.Data[4] = UDSSubServiceActionCode[i];		
+											UDSAnswer.Data[5] = 0xFF;					//the uds subservice action code is not surpported
+											UDSAnswer.Data[6] = 0x00;
+											UDSAnswer.Data[7] = 0x00;
+											ret = HAL_Can_Transmit(UDSAnswer);			
+										}
+										
+									}
+									else
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x7F;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0xEE;			//the sub service is not surpported
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+								}
+								else
+								{
+									UDSAnswer.Id = 0x7A8;
+									UDSAnswer.DLC = 8;
+									UDSAnswer.Data[0] = 0x05;
+									UDSAnswer.Data[1] = 0x7F;
+									UDSAnswer.Data[2] = UDSService[i];
+									UDSAnswer.Data[3] = UDSSubService[i];
+									UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
+									UDSAnswer.Data[5] = 0x00;
+									UDSAnswer.Data[6] = 0x00;
+									UDSAnswer.Data[7] = 0x00;
+									ret = HAL_Can_Transmit(UDSAnswer);
+
+								}
+								break;
+							case 0x34:  //ask for download update data
+								if(UDSDialogMode == 3)
+								{
+									if(UDSSubService[i] == 0x0F)
+									{
+										//ask for download the update data
+											boolRet = UDSAskforDownLoadData();
+											//if ok
+											
+											UDSAnswer.Id = 0x7A8;
+											UDSAnswer.DLC = 8;
+											UDSAnswer.Data[0] = 0x05;
+											UDSAnswer.Data[1] = 0x78;
+											UDSAnswer.Data[2] = UDSService[i];
+											UDSAnswer.Data[3] = UDSSubService[i];
+											UDSAnswer.Data[4] = (UINT8)boolRet;  // true:01, false: 00			
+											UDSAnswer.Data[5] = 0x00;
+											UDSAnswer.Data[6] = 0x00;
+											UDSAnswer.Data[7] = 0x00;
+											ret = HAL_Can_Transmit(UDSAnswer);
+									}
+									else
+									{
+										UDSAnswer.Id = 0x7A8;
+										UDSAnswer.DLC = 8;
+										UDSAnswer.Data[0] = 0x05;
+										UDSAnswer.Data[1] = 0x7F;
+										UDSAnswer.Data[2] = UDSService[i];
+										UDSAnswer.Data[3] = UDSSubService[i];
+										UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
+										UDSAnswer.Data[5] = 0x00;
+										UDSAnswer.Data[6] = 0x00;
+										UDSAnswer.Data[7] = 0x00;
+										ret = HAL_Can_Transmit(UDSAnswer);
+									}
+								}
+								else
+								{
+									UDSAnswer.Id = 0x7A8;
+									UDSAnswer.DLC = 8;
+									UDSAnswer.Data[0] = 0x05;
+									UDSAnswer.Data[1] = 0x7F;
+									UDSAnswer.Data[2] = UDSService[i];
+									UDSAnswer.Data[3] = UDSSubService[i];
+									UDSAnswer.Data[4] = 0xFF;				//the dialog mode error
+									UDSAnswer.Data[5] = 0x00;
+									UDSAnswer.Data[6] = 0x00;
+									UDSAnswer.Data[7] = 0x00;
+									ret = HAL_Can_Transmit(UDSAnswer);
+
+								}
+								break;
+							case 0x36:  //download the update data
+								if(UDSSubService[i] == 0x0F)
+								{
+									counter++;  //记录报文数量
+									
+									tempSliceCounter =  (CanRxMsg[i].Data[2]<<8)|(CanRxMsg[i].Data[3]);
+									printf("tempSliceCounter = %x\n",tempSliceCounter);
+									// updateDifferDataByteLen
+									if(tempSliceCounter < (updateDifferDataPackageCounter+1)*25 && tempSliceCounter>=updateDifferDataPackageCounter*25)
+									{
+										updateDifferDataSliceCounter = tempSliceCounter % 25;
+										for(j=0;j<4;j++)
+										{
+											updateDataBuffer[updateDifferDataSliceCounter*4+j] = CanRxMsg[i].Data[4+j];
+										}								
+										
+										if(updateDifferDataSliceCounter>0)
+										{
+											sliceCounterFlag = sliceCounterFlag |(0x01<<updateDifferDataSliceCounter);
+										}
+										else
+										{
+											sliceCounterFlag = sliceCounterFlag |0x01;
+										}
+										
+										printf("sliceCounterFlag = %x\n",sliceCounterFlag);
+										
+										
+									}
+
+									if(counter == 25)
+									{
+										counter = 0;
+										
+										if(sliceCounterFlag == 0x1FFFFFF)
+										{
+											
+											UDSAnswer.Id = 0x7A8;
+											UDSAnswer.DLC = 8;
+											UDSAnswer.Data[0] = 0x06;
+											UDSAnswer.Data[1] = 0x78;
+											UDSAnswer.Data[2] = UDSService[i];
+											UDSAnswer.Data[3] = UDSSubService[i];
+											UDSAnswer.Data[4] = tempSliceCounter>>8;				
+											UDSAnswer.Data[5] = tempSliceCounter;
+											UDSAnswer.Data[6] = 0x00;
+											UDSAnswer.Data[7] = 0x00;
+											ret = HAL_Can_Transmit(UDSAnswer);											
+											
+											sliceCounterFlag = 0x0;
+											updateDifferDataPackageCounter++;
+											printf("updateDifferDataPackageCounter = %d\n",updateDifferDataPackageCounter);
+
+											printf("updateDataBuffer\n");
+											for(k=0;k<100;k++)
+											{
+												printf("%x  ",updateDataBuffer[k]);
+											}
+											//BSP_QSPI_Write_Safe(updateDataBuffer,FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,100);
+											printf("startAddr = 0x%x,endAddr = 0x%x\n",
+												FLASH_FOTA_REGION_START+(updateDifferDataPackageCounter-1)*100,FLASH_FOTA_REGION_START+updateDifferDataPackageCounter*100-1);
+											memset(updateDataBuffer,0, 100);
+										}
+										else
+										{
+											UDSAnswer.Id = 0x7A8;
+											UDSAnswer.DLC = 8;
+											UDSAnswer.Data[0] = 0x08;
+											UDSAnswer.Data[1] = 0x7F;
+											UDSAnswer.Data[2] = UDSService[i];
+											UDSAnswer.Data[3] = UDSSubService[i];
+											UDSAnswer.Data[4] = sliceCounterFlag>>24;				
+											UDSAnswer.Data[5] = sliceCounterFlag>>16;
+											UDSAnswer.Data[6] = sliceCounterFlag>>8;
+											UDSAnswer.Data[7] = sliceCounterFlag;
+											ret = HAL_Can_Transmit(UDSAnswer);
+											
+											printf("updateDataBuffer\n");
+											for(k=0;k<100;k++)
+											{
+												printf("%x  ",updateDataBuffer[k]);
+											}
+										}
+									}
+									
+									
+								}
+								else
+								{
+									UDSAnswer.Id = 0x7A8;
+									UDSAnswer.DLC = 8;
+									UDSAnswer.Data[0] = 0x05;
+									UDSAnswer.Data[1] = 0x7F;
+									UDSAnswer.Data[2] = UDSService[i];
+									UDSAnswer.Data[3] = UDSSubService[i];
+									UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
+									UDSAnswer.Data[5] = 0x00;
+									UDSAnswer.Data[6] = 0x00;
+									UDSAnswer.Data[7] = 0x00;
+									ret = HAL_Can_Transmit(UDSAnswer);
+								}
+								break;
+							case 0x37:  // exit the download
+								 if(UDSSubService[i] == 0x0F)
+								 {
+									//
+								 }
+								 else
+								 {
+								 	UDSAnswer.Id = 0x7A8;
+									UDSAnswer.DLC = 8;
+									UDSAnswer.Data[0] = 0x05;
+									UDSAnswer.Data[1] = 0x7F;
+									UDSAnswer.Data[2] = UDSService[i];
+									UDSAnswer.Data[3] = UDSSubService[i];
+									UDSAnswer.Data[4] = 0xEE;			//the sub service is not surported
+									UDSAnswer.Data[5] = 0x00;
+									UDSAnswer.Data[6] = 0x00;
+									UDSAnswer.Data[7] = 0x00;
+									ret = HAL_Can_Transmit(UDSAnswer);
+								}
+								 
+								break;
+							default:
+								UDSAnswer.Id = 0x7A8;
+								UDSAnswer.DLC = 8;
+								UDSAnswer.Data[0] = 0x04;
+								UDSAnswer.Data[1] = 0x7F;
+								UDSAnswer.Data[2] = UDSService[i];	//the serviece is not surpoted
+								UDSAnswer.Data[3] = 0xFF;
+								UDSAnswer.Data[4] = 0x00;				
+								UDSAnswer.Data[5] = 0x00;
+								UDSAnswer.Data[6] = 0x00;
+								UDSAnswer.Data[7] = 0x00;
+								ret = HAL_Can_Transmit(UDSAnswer);
+								break;
+								
+						}
+					}
+				}
+			}
+	//}
+	for(i=0; i<2; i++)
+	{
+		UDSService[i] = 0;
+		UDSSubService[i] = 0;
+	}
+
+}
+
+void UDS_CAN_Update()
+{
+	
+	
+
+
+}
+
+BOOL UDSClearFotaDownloadRegion()
+{
+	
+	return TRUE;
+}
+
+BOOL UDSAskforDownLoadData()
+{
+
+	return TRUE;
+}

+ 1 - 1
src/UartTask.c

@@ -82,7 +82,7 @@ static void UartTask(void* arg)
                 {
                     PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_SLEEP);
                 }
-                else if(Timer_count%50==0)
+                else if(Timer_count%10==0)
                 {
                     #ifdef USING_PRINTF
                         printf("[%d]Uart Timer 5s:%d\n",__LINE__,Timer_count);

+ 1 - 0
src/app.c

@@ -37,6 +37,7 @@ void appInit(void *arg)
     UartTaskInit(arg);
     TcpTaskInit(arg);
     GpsTaskInit();
+	CANTaskInit(arg);
 }
 //主函数入口
 void main_entry(void) 

+ 7 - 40
src/hal_module_adapter.c

@@ -917,14 +917,6 @@ INT8  HAL_Can_Transmit(CAN_Msg_Type Can_TxMsg)
 			CAN_WriteReg(TXB0CTRL,TXREQ);//???������?������??
 			//SPI_CS_High();
 
-			printf("IDsend0 = 0x%x\n",Can_TxMsg.Id);
-			printf("DataSend0=\n");
-			for(i=0;i<8;i++)
-			{
-				printf("0x%x\t",Can_TxMsg.Data[i]);
-			}
-			printf("\n");
-			
 			ret = 0;
 			break;
 		}
@@ -943,13 +935,7 @@ INT8  HAL_Can_Transmit(CAN_Msg_Type Can_TxMsg)
 			CAN_WriteReg(TXB1CTRL,TXREQ);//???������?������??
 			//SPI_CS_High();
 
-			printf("IDsend1 = 0x%x\n",Can_TxMsg.Id);
-			printf("DataSend1=\n");
-			for(i=0;i<8;i++)
-			{
-				printf("0x%x\t",Can_TxMsg.Data[i]);
-			}
-			printf("\n");
+			
 			ret = 1;
 			break;
 		}
@@ -968,13 +954,7 @@ INT8  HAL_Can_Transmit(CAN_Msg_Type Can_TxMsg)
 			CAN_WriteReg(TXB2CTRL,TXREQ);//???������?������??
 			//SPI_CS_High();
 
-			printf("IDsend2 = 0x%x\n",Can_TxMsg.Id);
-			printf("DataSend2=\n");
-			for(i=0;i<8;i++)
-			{
-				printf("0x%x\t",Can_TxMsg.Data[i]);
-			}
-			printf("\n");
+			
 			ret = 2;
 			break;
 		}
@@ -1060,13 +1040,7 @@ void  HAL_Can_Receive(CAN_Msg_Type* CanRxMsgBuffer)
 			i++;
 		}	
 
-		printf("ID0 = 0x%x\n",(CanRxMsgBuffer[0]).Id);
-		printf("Data0=\n");
-		for(i=0;i<8;i++)
-		{
-			printf("0x%x\t",(CanRxMsgBuffer[0]).Data[i]);
-		}
-		printf("\n");
+		
 	}
 	if(temp & 0x02)  //RX Buffer 1
 	{
@@ -1094,19 +1068,12 @@ void  HAL_Can_Receive(CAN_Msg_Type* CanRxMsgBuffer)
 		{	
 			CAN_ReadReg(RXB1D0+i,1,&((CanRxMsgBuffer[1]).Data[i]));
 			i++;			
-		}
-
-		printf("ID1 = 0x%x\n",(CanRxMsgBuffer[1]).Id);
-		printf("Data1=\n");
-		for(i=0;i<8;i++)
-		{
-			printf("0x%x\t",(CanRxMsgBuffer[1]).Data[i]);
-		}
-		printf("\n");
+		}		
 	}
 	CAN_WriteReg(CANINTF,0);
-	CAN_ReadReg(CANINTF, 1, &temp);
-	printf("CANINTF = %x\n",temp);
+//	CAN_ReadReg(CANINTF,1,&temp);
+//	printf("CANINTF_1 = 0x%x\n",temp);
+	
 }
 
 /**