소스 검색

2022-01-07 / Eason Yang
Action
1. Synchronize: Synchronize AX-CCS Project to AX-Noodoe.

File
1. AX-Noodoe Project
Action 1

FIRMWARE VERSION: B0.60.XX.XXXX.NX

8009 3 년 전
부모
커밋
48e99bc766

+ 145 - 92
EVSE/Projects/NoodoeAX/Apps/LCM/Module_LcmControl.c

@@ -2,9 +2,9 @@
  * Module_LcmControl.c
  *
  * Created on : 2020-10-20
- * Update on : 2021-12-14
+ * Update on : 2022-01-07
  * Author : Folus Wen, Eason Yang
- * Version : V0.24
+ * Version : V0.26
  *
  */
 
@@ -58,7 +58,6 @@ void setPriceFromWebsite(float monry_rate);
 void setAuthorizationPage(uint8_t gun_index);
 float getPresentFinalCost(uint8_t gun_index);
 void setGunPluginAnimation(uint8_t gun_index);
-void setPresentChargedEnergy(uint8_t gun_index);
 void setPresentChargedgPower(uint8_t gun_index);
 void setPlugAndChargeAnimation(uint8_t gun_index);
 void setPresentChargedDuration(uint8_t gun_index);
@@ -72,6 +71,7 @@ void setDefaultValue(uint8_t gun_index, uint8_t system_mode);
 void setQRcodeContent(char *input, uint8_t length, uint8_t mode);
 void setBillingFromBackend(uint8_t gun_index, uint8_t system_mode);
 void setConnectionAnimation(uint8_t gun_index, uint8_t system_mode);
+void setPresentChargedEnergy(uint8_t gun_index, uint8_t system_mode);
 void setMarqueeControl(uint16_t address, uint8_t gun_index, uint8_t type);
 void setCurrencyAndUnitFromBackend(uint8_t type_price,uint8_t type_currency_unit, uint8_t gun_index);
 
@@ -85,21 +85,22 @@ enum TMR_IDX
 	TMR_IDX_PLUGIN,
 	TMR_IDX_ALARM,
 	TMR_IDX_PRICE,
-	TMR_IDX_5,
+	TMR_IDX_REFRESH_INFO,
 	TMR_IDX_6,
 	TMR_IDX_7,
 	TMR_IDX_8,
 	TMR_IDX_SHOW_AUTH_RESULT
 };
 
-struct timeb					startTime[AC_QUANTITY][10];
+struct timespec					startTime[AC_QUANTITY][TMR_IDX_SHOW_AUTH_RESULT];
 
-#define TIME_ANIMATION_PLUGIN			1000
-#define TIME_ANIMATION_BATTERY			1000
-#define TIME_ANIMATION_CONNECTION		1000
-#define TIME_ANIMATION_ALARM			5000
-#define TIME_REFRESH_TIME				5000
-#define TIME_AUTH_RESULT_TIME			5000
+#define TIME_ANIMATION_PLUGIN			1 // Unit: Second
+#define TIME_ANIMATION_BATTERY			1 // Unit: Second
+#define TIME_ANIMATION_CONNECTION		1 // Unit: Second
+#define TIME_ANIMATION_ALARM			5 // Unit: Second
+#define TIME_REFRESH_TIME				5 // Unit: Second
+#define TIME_AUTH_RESULT_TIME			5 // Unit: Second
+#define TIME_REFRESH_INFO				3 // Unit: Second
 
 //=======================================
 // Declare Variable
@@ -113,7 +114,7 @@ int Uart1Fd;
 //=======================================
 // Record version and date
 //=======================================
-char *FIRMWARE_UPDATE_IMAGE[3] = {"V0.24", "2021-12-14", "REV.02.00"};
+char *FIRMWARE_UPDATE_IMAGE[3] = {"V0.26", "2022-01-07", "REV.02.00"};
 
 //=======================================
 // Common routine
@@ -152,6 +153,25 @@ int StoreLogMsg(const char *fmt, ...)
 	return rc;
 }
 
+void refreshStartTimer(struct timespec *timer)
+{
+	clock_gettime(CLOCK_MONOTONIC, timer);
+}
+
+int getDiffSecNow(struct timespec timer)
+{
+	struct timespec timerNow;
+
+	clock_gettime(CLOCK_MONOTONIC, &timerNow);
+
+	return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
+}
+
+int getDiffSecBetween(struct timespec start, struct timespec end)
+{
+	return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
+}
+
 long long DiffTimebWithNow(struct timeb ST)
 {
 	//return milli-second
@@ -401,6 +421,7 @@ void page_preparing(uint8_t gun_index, uint8_t system_mode)
 		else
 		{
 			setConnectionAnimation(gun_index, system_mode);
+			setPresentChargedEnergy(gun_index, system_mode);
 		}
 	}
 	else
@@ -416,12 +437,15 @@ void page_preparing(uint8_t gun_index, uint8_t system_mode)
 			setPresentConnectionTimeout(gun_index);
 			setConnectionAnimation(gun_index, system_mode);
 			setGunPluginAnimation(gun_index);
+			setPresentChargedEnergy(gun_index, system_mode);
 		}
 	}
 }
 
 void page_charging(uint8_t gun_index, uint8_t system_mode)
 {
+	static char runningCost[256] = {0};
+
 	if((getCurrentPage() != SYSTEM_SCREEN_CHARGING) && (!ShmCharger->isAuthrizing && !ShmCharger->isGetAuthResult))
 	{
 		setCurrentPage(SYSTEM_SCREEN_CHARGING);
@@ -432,37 +456,44 @@ void page_charging(uint8_t gun_index, uint8_t system_mode)
 	{
 		setConnectionAnimation(gun_index, system_mode);
 		setPresentChargedDuration(gun_index);
-		setPresentChargedEnergy(gun_index);
 		setPresentChargedgPower(gun_index);
 
-		if((ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON))
+		if((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_INFO]) >= TIME_REFRESH_INFO) || (strcmp((char*)ShmOCPP16Data->Cost.RunningCost[gun_index].description, runningCost) != 0))
 		{
-			if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
-			{
-				setRunningCost(gun_index, TOTAL_COST);
-			}
-			else
-			{
-				// DISPLAY BY OCPP 2.0.1
-			}
-		}
-		else
-		{
-			if(ShmSysConfigAndInfo->SysConfig.BillingData.isBilling == ON)
+			setPresentChargedEnergy(gun_index, system_mode);
+			memcpy((char*) runningCost,(char*)ShmOCPP16Data->Cost.RunningCost[gun_index].description,ARRAY_SIZE(ShmOCPP16Data->Cost.RunningCost[gun_index].description));
+
+			if((ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON))
 			{
-				setDisplayValue(ICON_CHARGING_COST, APPEAR);
-				setPresentFinalCost(getPresentFinalCost(gun_index));
+				if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
+				{
+					setRunningCost(gun_index, TOTAL_COST);
+				}
+				else
+				{
+					// DISPLAY BY OCPP 2.0.1
+				}
 			}
 			else
 			{
-				// User spend cost
-				setDisplayValue(ICON_CHARGING_COST, DISAPPEAR);
-				setDisplayValue(TEXT_CHARGING_COST, DISAPPEAR);
+				if(ShmSysConfigAndInfo->SysConfig.BillingData.isBilling == ON)
+				{
+					setDisplayValue(ICON_CHARGING_COST, APPEAR);
+					setPresentFinalCost(getPresentFinalCost(gun_index));
+				}
+				else
+				{
+					// User spend cost
+					setDisplayValue(ICON_CHARGING_COST, DISAPPEAR);
+					setDisplayValue(TEXT_CHARGING_COST, DISAPPEAR);
 
-				// User account balance
-				setDisplayValue(ICON_COMPLETE_WALLET, DISAPPEAR);
-				setDisplayValue(TEXT_REMAINING_ACCOUNT_BALANCE, DISAPPEAR);
+					// User account balance
+					setDisplayValue(ICON_COMPLETE_WALLET, DISAPPEAR);
+					setDisplayValue(TEXT_REMAINING_ACCOUNT_BALANCE, DISAPPEAR);
+				}
 			}
+
+			refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_INFO]);
 		}
 	}
 }
@@ -494,7 +525,7 @@ void page_complete(uint8_t gun_index, uint8_t system_mode)
 	}
 
 	setPresentChargedDuration(gun_index);
-	setPresentChargedEnergy(gun_index);
+	setPresentChargedEnergy(gun_index, system_mode);
 
 	if((ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON))
 	{
@@ -543,7 +574,7 @@ void page_terminating(uint8_t gun_index, uint8_t system_mode)
 	else
 	{
 		setPresentChargedDuration(gun_index);
-		setPresentChargedEnergy(gun_index);
+		setPresentChargedEnergy(gun_index, system_mode);
 		setPresentChargedgPower(gun_index);
 
 		if((ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON))
@@ -680,9 +711,9 @@ void page_header(uint8_t gun_index, uint8_t system_mode)
 	else
 	{
 		// EVERY 5 SECONDS TO UPDATE PRICE
-		if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PRICE]) > (TIME_REFRESH_TIME))
+		if(getDiffSecNow(startTime[gun_index][TMR_IDX_PRICE]) > (TIME_REFRESH_TIME))
 		{
-			ftime(&startTime[gun_index][TMR_IDX_PRICE]);
+			refreshStartTimer(&startTime[gun_index][TMR_IDX_PRICE]);
 			setBillingFromBackend(gun_index, system_mode);
 		}
 	}
@@ -1364,7 +1395,7 @@ void setFinalCost(uint8_t gun_index, uint8_t type)
 					lcdRegisterWrite(Uart1Fd, REG_TYPE_RAM, TEXT_REMAINING_ACCOUNT_BALANCE, output_data, ARRAY_SIZE(output_data));
 				}
 				else
-				{	
+				{
 					setDisplayValue(ICON_COMPLETE_WALLET, APPEAR);
 
 					strcpy((char*)balance_empty, "-----");
@@ -1416,28 +1447,33 @@ void setRunningCost(uint8_t gun_index, uint8_t type)
 			}
 			else
 			{
-				if((strstr((char*)ShmOCPP16Data->Cost.RunningCost[gun_index].description, "Total Cost:") > 0))
+				if(strlen((char*)ShmOCPP16Data->Cost.RunningCost[gun_index].description) > 0)
 				{
-					// APPEAR COST ICON
-					setDisplayValue(ICON_CHARGING_COST, APPEAR);
+					if((strstr((char*)ShmOCPP16Data->Cost.RunningCost[gun_index].description, "Total Cost:") > 0))
+					{
+						// APPEAR COST ICON
+						setDisplayValue(ICON_CHARGING_COST, APPEAR);
 
-					// APPEAR COST TEXT
-					strcpy((char*)tmp,(char*)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
-					split((char**)splitString, tmp, Symbol);
-					memcpy(input_data, splitString[3], strlen(splitString[3]));
-					length = strlen(input_data);
-					trim_string(input_data,length);
-					memcpy(output_data ,input_data, strlen(input_data));
-					lcdRegisterWrite(Uart1Fd, REG_TYPE_RAM, TEXT_CHARGING_COST, output_data, ARRAY_SIZE(output_data));
-				}
-				else
-				{
-					setDisplayValue(ICON_CHARGING_COST, APPEAR);
+						// APPEAR COST TEXT
+						strcpy((char*)tmp,(char*)ShmOCPP16Data->Cost.RunningCost[gun_index].description);
+						split((char**)splitString, tmp, Symbol);
+						memcpy(input_data, splitString[3], strlen(splitString[3]));
+						length = strlen(input_data);
+						trim_string(input_data,length);
+						memcpy(output_data ,input_data, strlen(input_data));
+						lcdRegisterWrite(Uart1Fd, REG_TYPE_RAM, TEXT_CHARGING_COST, output_data, ARRAY_SIZE(output_data));
+					}
+					else
+					{
+						setDisplayValue(ICON_CHARGING_COST, APPEAR);
 
-					strcpy((char*)cost_empty, "-----");
-					string2ByteArray(cost_empty, output_data);
-					lcdRegisterWrite(Uart1Fd, REG_TYPE_RAM, TEXT_CHARGING_COST, output_data, ARRAY_SIZE(output_data));
+						strcpy((char*)cost_empty, "-----");
+						string2ByteArray(cost_empty, output_data);
+						lcdRegisterWrite(Uart1Fd, REG_TYPE_RAM, TEXT_CHARGING_COST, output_data, ARRAY_SIZE(output_data));
+					}
 				}
+				else
+				{}
 			}
 
 			break;
@@ -1690,9 +1726,9 @@ void setWarningStatus(uint8_t gun_index)
 
 		setAlarmCodeAndIcon();
 	}
-	else if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 4 && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_ALARM]) > (TIME_ANIMATION_ALARM)))
+	else if(ShmSysConfigAndInfo->SysWarningInfo.WarningCount > 4 && (getDiffSecNow(startTime[gun_index][TMR_IDX_ALARM]) > (TIME_ANIMATION_ALARM)))
 	{
-		ftime(&startTime[gun_index][TMR_IDX_ALARM]);
+		refreshStartTimer(&startTime[gun_index][TMR_IDX_ALARM]);
 		if(ShmSysConfigAndInfo->SysWarningInfo.PageIndex == 0)
 		{
 			ShmSysConfigAndInfo->SysWarningInfo.PageIndex = 1;
@@ -1752,30 +1788,30 @@ void setConnectionAnimation(uint8_t gun_index, uint8_t system_mode)
 		case SYS_MODE_IDLE:
 			if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
 			{
-				if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
+				if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
 				{
 					setDisplayValue(ICON_PRECHARGING_ANIMATION, CONNECTION_FLASHING_1);
 					CONNECTION_LEVEL_STATUS = CONNECTION_LEVEL_1;
 				}
-				else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
+				else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
 				{
 					setDisplayValue(ICON_PRECHARGING_ANIMATION, CONNECTION_FLASHING_2);
 					CONNECTION_LEVEL_STATUS =  CONNECTION_LEVEL_0;
-					ftime(&startTime[gun_index][TMR_IDX_CONNECTION]);
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_CONNECTION]);
 				}
 			}
 			else
 			{
-				if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
+				if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
 				{
 					setDisplayValue(ICON_PREPARING_ANIMATION, CONNECTION_FLASHING_1);
 					CONNECTION_LEVEL_STATUS = CONNECTION_LEVEL_1;
 				}
-				else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
+				else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
 				{
 					setDisplayValue(ICON_PREPARING_ANIMATION, CONNECTION_FLASHING_2);
 					CONNECTION_LEVEL_STATUS =  CONNECTION_LEVEL_0;
-					ftime(&startTime[gun_index][TMR_IDX_CONNECTION]);
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_CONNECTION]);
 				}
 			}
 
@@ -1783,45 +1819,45 @@ void setConnectionAnimation(uint8_t gun_index, uint8_t system_mode)
 		case SYS_MODE_PREPARING:
 			if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
 			{
-				if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
+				if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
 				{
 					setDisplayValue(ICON_PRECHARGING_ANIMATION, CONNECTION_FLASHING_1);
 					CONNECTION_LEVEL_STATUS = CONNECTION_LEVEL_1;
 				}
-				else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
+				else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
 				{
 					setDisplayValue(ICON_PRECHARGING_ANIMATION, CONNECTION_FLASHING_2);
 					CONNECTION_LEVEL_STATUS =  CONNECTION_LEVEL_0;
-					ftime(&startTime[gun_index][TMR_IDX_CONNECTION]);
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_CONNECTION]);
 				}
 			}
 			else
 			{
-				if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
+				if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
 				{
 					setDisplayValue(ICON_PREPARING_ANIMATION, CONNECTION_FLASHING_1);
 					CONNECTION_LEVEL_STATUS = CONNECTION_LEVEL_1;
 				}
-				else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
+				else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
 				{
 					setDisplayValue(ICON_PREPARING_ANIMATION, CONNECTION_FLASHING_2);
 					CONNECTION_LEVEL_STATUS =  CONNECTION_LEVEL_0;
-					ftime(&startTime[gun_index][TMR_IDX_CONNECTION]);
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_CONNECTION]);
 				}
 			}
 
 			break;
 		case SYS_MODE_CHARGING:
-			if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
+			if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_0) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION)))
 			{
 				setDisplayValue(ICON_CHARGING_ANIMATION, CONNECTION_FLASHING_1);
 				CONNECTION_LEVEL_STATUS = CONNECTION_LEVEL_1;
 			}
-			else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
+			else if((CONNECTION_LEVEL_STATUS == CONNECTION_LEVEL_1) && (getDiffSecNow(startTime[gun_index][TMR_IDX_CONNECTION]) > (TIME_ANIMATION_CONNECTION*2)))
 			{
 				setDisplayValue(ICON_CHARGING_ANIMATION, CONNECTION_FLASHING_2);
 				CONNECTION_LEVEL_STATUS =  CONNECTION_LEVEL_0;
-				ftime(&startTime[gun_index][TMR_IDX_CONNECTION]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_CONNECTION]);
 			}
 			break;
 		case SYS_MODE_TERMINATING:
@@ -1844,16 +1880,16 @@ void setGunPluginAnimation(uint8_t gun_index)
 	}
 	else
 	{
-		if((GUN_PLUGING_LEVEL_STATUS == GUN_PLUGING_LEVEL_0) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PLUGIN]) > (TIME_ANIMATION_PLUGIN)))
+		if((GUN_PLUGING_LEVEL_STATUS == GUN_PLUGING_LEVEL_0) && (getDiffSecNow(startTime[gun_index][TMR_IDX_PLUGIN]) > (TIME_ANIMATION_PLUGIN)))
 		{
 			setDisplayValue(ICON_PREPARING_PLUG_ARROW, PLUGIN_ARROW_1);
 			GUN_PLUGING_LEVEL_STATUS = GUN_PLUGING_LEVEL_1;
 		}
-		else if((GUN_PLUGING_LEVEL_STATUS == GUN_PLUGING_LEVEL_1) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PLUGIN]) > (TIME_ANIMATION_PLUGIN*2)))
+		else if((GUN_PLUGING_LEVEL_STATUS == GUN_PLUGING_LEVEL_1) && (getDiffSecNow(startTime[gun_index][TMR_IDX_PLUGIN]) > (TIME_ANIMATION_PLUGIN*2)))
 		{
 			setDisplayValue(ICON_PREPARING_PLUG_ARROW, PLUGIN_ARROW_2);
 			GUN_PLUGING_LEVEL_STATUS =  GUN_PLUGING_LEVEL_0;
-			ftime(&startTime[gun_index][TMR_IDX_PLUGIN]);
+			refreshStartTimer(&startTime[gun_index][TMR_IDX_PLUGIN]);
 		}
 	}
 }
@@ -1863,16 +1899,16 @@ void setGunPluginAnimation(uint8_t gun_index)
 //=======================================
 void setPlugAndChargeAnimation(uint8_t gun_index)
 {
-	if((GUN_PLUGING_LEVEL_STATUS == GUN_PLUGING_LEVEL_0) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PLUGIN]) > (TIME_ANIMATION_PLUGIN)))
+	if((GUN_PLUGING_LEVEL_STATUS == GUN_PLUGING_LEVEL_0) && (getDiffSecNow(startTime[gun_index][TMR_IDX_PLUGIN]) > (TIME_ANIMATION_PLUGIN)))
 	{
 		setDisplayValue(ICON_PLUG_AND_CHARGE_ARROW, PLUGIN_ARROW_1);
 		GUN_PLUGING_LEVEL_STATUS = GUN_PLUGING_LEVEL_1;
 	}
-	else if((GUN_PLUGING_LEVEL_STATUS == GUN_PLUGING_LEVEL_1) && (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PLUGIN]) > (TIME_ANIMATION_PLUGIN*2)))
+	else if((GUN_PLUGING_LEVEL_STATUS == GUN_PLUGING_LEVEL_1) && (getDiffSecNow(startTime[gun_index][TMR_IDX_PLUGIN]) > (TIME_ANIMATION_PLUGIN*2)))
 	{
 		setDisplayValue(ICON_PLUG_AND_CHARGE_ARROW, PLUGIN_ARROW_2);
 		GUN_PLUGING_LEVEL_STATUS =  GUN_PLUGING_LEVEL_0;
-		ftime(&startTime[gun_index][TMR_IDX_PLUGIN]);
+		refreshStartTimer(&startTime[gun_index][TMR_IDX_PLUGIN]);
 	}
 }
 
@@ -1935,7 +1971,7 @@ void setPresentChargedgPower(uint8_t gun_index)
 //=======================================
 // Setting Present Charging [ Energy ]
 //=======================================
-void setPresentChargedEnergy(uint8_t gun_index)
+void setPresentChargedEnergy(uint8_t gun_index, uint8_t system_mode)
 {
 	uint8_t data[16];
 	uint8_t energy[16];
@@ -1943,9 +1979,22 @@ void setPresentChargedEnergy(uint8_t gun_index)
 	memset(data, 0x00, ARRAY_SIZE(data));
 	memset(energy, 0x00, ARRAY_SIZE(energy));
 
-	sprintf((char *)energy, "%.4f kWh", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
-	string2ByteArray(energy, data);
-	lcdRegisterWrite(Uart1Fd, REG_TYPE_RAM, TEXT_CHARGING_ENERGY, data, ARRAY_SIZE(data));
+	switch(system_mode)
+	{
+		case SYS_MODE_PREPARING:
+			sprintf((char *)energy, "%s kWh", "0.0000");
+			string2ByteArray(energy, data);
+			lcdRegisterWrite(Uart1Fd, REG_TYPE_RAM, TEXT_CHARGING_ENERGY, data, ARRAY_SIZE(data));
+			break;
+		case SYS_MODE_CHARGING:
+		case SYS_MODE_TERMINATING:
+		case SYS_MODE_COMPLETE:
+		default:
+			sprintf((char *)energy, "%.3f0 kWh", ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedEnergy);
+			string2ByteArray(energy, data);
+			lcdRegisterWrite(Uart1Fd, REG_TYPE_RAM, TEXT_CHARGING_ENERGY, data, ARRAY_SIZE(data));
+			break;
+	}
 }
 
 //=======================================
@@ -2044,13 +2093,13 @@ void setDefaultValue(uint8_t gun_index, uint8_t system_mode)
 			{
 				setDisplayValue(ICON_PRECHARGING_ANIMATION, CONNECTION_FLASHING_1);
 				CONNECTION_LEVEL_STATUS = CONNECTION_LEVEL_0;
-				ftime(&startTime[gun_index][TMR_IDX_CONNECTION]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_CONNECTION]);
 			}
 			else
 			{
 				setDisplayValue(ICON_PREPARING_PLUG_ARROW, PLUGIN_ARROW_1);
 				GUN_PLUGING_LEVEL_STATUS = GUN_PLUGING_LEVEL_0;
-				ftime(&startTime[gun_index][TMR_IDX_PLUGIN]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_PLUGIN]);
 			}
 			break;
 		case SYS_MODE_AUTHORIZING:
@@ -2060,18 +2109,21 @@ void setDefaultValue(uint8_t gun_index, uint8_t system_mode)
 			{
 				setDisplayValue(ICON_PRECHARGING_ANIMATION, CONNECTION_FLASHING_1);
 				CONNECTION_LEVEL_STATUS = CONNECTION_LEVEL_0;
-				ftime(&startTime[gun_index][TMR_IDX_CONNECTION]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_CONNECTION]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_INFO]);
 			}
 			else
 			{
 				setDisplayValue(ICON_PREPARING_PLUG_ARROW, PLUGIN_ARROW_1);
 				GUN_PLUGING_LEVEL_STATUS = GUN_PLUGING_LEVEL_0;
-				ftime(&startTime[gun_index][TMR_IDX_PLUGIN]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_PLUGIN]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_INFO]);
 			}
 			break;
 		case SYS_MODE_CHARGING:
 			setDisplayValue(ICON_CHARGING_ANIMATION, CONNECTION_FLASHING_1);
-			ftime(&startTime[gun_index][TMR_IDX_CONNECTION]);
+			refreshStartTimer(&startTime[gun_index][TMR_IDX_CONNECTION]);
+			refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_INFO]);
 			CONNECTION_LEVEL_STATUS = CONNECTION_LEVEL_0;
 			break;
 		case SYS_MODE_TERMINATING:
@@ -2587,10 +2639,10 @@ int main(void)
 
 			if(ShmCharger->isAuthrizing || ShmCharger->isGetAuthResult)
 			{
-				if(DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_SHOW_AUTH_RESULT]) >= TIME_AUTH_RESULT_TIME)
+				if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_SHOW_AUTH_RESULT]) >= TIME_AUTH_RESULT_TIME)
 					ShmCharger->isGetAuthResult = FALSE;
 
-				if(DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_SHOW_AUTH_RESULT]) < TIME_AUTH_RESULT_TIME)
+				if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_SHOW_AUTH_RESULT]) < TIME_AUTH_RESULT_TIME)
 				{
 					page_authorizing(ShmCharger->gun_selectd);
 					ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh = YES;
@@ -2603,7 +2655,8 @@ int main(void)
 			}
 			else
 			{
-				ftime(&startTime[ShmCharger->gun_selectd][TMR_IDX_SHOW_AUTH_RESULT]);
+				refreshStartTimer(&startTime[ShmCharger->gun_selectd][TMR_IDX_SHOW_AUTH_RESULT]);
+				ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh = NO;
 
 				switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[ShmCharger->gun_selectd].SystemStatus)
 				{

+ 2 - 2
EVSE/Projects/NoodoeAX/Apps/Makefile

@@ -105,7 +105,7 @@ Module_CSU_Task:
 	@echo "===== Module_CSU_Task ============================================"
 	rm -f main 
 	$(CC) -D $(Project) "-I../../" "-include../../../GPL/json-c-json-c-0.13.1-20180305/release/include/json-c/json.h" "-include../../../Modularization/ocppfiles/sqlite3.h" "-include../../../Modularization/Module_Upgrade.h" "-include../../../Modularization/Module_RFID.h" "-include../../../Modularization/Module_RatedCurrent.h" -O0  -Wall -c -fmessage-length=0 -o main.o "./main.c"
-	$(CC) -o main main.o ${Lib_Module_RFID} ${Lib_Module_Upgrade} ${Lib_SQLite3} ${Lib_Module_RatedCurrent} ${Lib_JSONC}
+	$(CC) -o main main.o ${Lib_Module_RFID} ${Lib_Module_Upgrade} ${Lib_SQLite3} ${Lib_Module_RatedCurrent} ${Lib_JSONC} -lrt
 	rm -f *.o
 	mv -f main ../Images/root		
 	@echo \
@@ -129,7 +129,7 @@ Module_CCS_Task:
 Module_LcmControl_Task:
 	@echo "===== Module_LcmControl_Task ==================================="
 	rm -f Module_LcmControl
-	$(CC) -D $(Project) "-I../../../Modularization/ocppfiles/" "-I./" "-I../../" -O0  -Wall -fmessage-length=0 LCM/lcmComm_dgus.c LCM/Module_LcmControl.c LCM/cbmp.c -o Module_LcmControl
+	$(CC) -D $(Project) "-I../../../Modularization/ocppfiles/" "-I./" "-I../../" -O0  -Wall -fmessage-length=0 LCM/lcmComm_dgus.c LCM/Module_LcmControl.c LCM/cbmp.c -lrt -o Module_LcmControl
 	rm -f *.o	
 	mv -f Module_LcmControl ../Images/root
 

+ 215 - 32
EVSE/Projects/NoodoeAX/Apps/Module_AlarmDetect.c

@@ -49,6 +49,10 @@
 #define HYSTERETIC_OT		10
 #define HYSTERETIC_OC		10
 
+#define ALARM_OC_RETRY_COUNT 4
+
+//#define isDebugPrint
+
 struct{
 	unsigned short int	OV[3];
 	unsigned short int	UV[3];
@@ -63,8 +67,14 @@ struct{
 	unsigned short int	Relay_DrivingFault;
 	unsigned short int	CP_LevelFail;
 	unsigned short int	MCU_SelfTestFail;
+	unsigned short int	OC_Retry[3];
 }Alarm_Counter[2];
 
+struct{
+	uint16_t OC_BegThreshold; 	// OCP過電流門檻值 	單位0.01A	ex: 48 * 110 = 5280 (52.80A)
+	uint16_t OC_EndThreshold;	// OCP過電流門檻值 	單位0.01A	ex: 48 * 110 = 5280 (52.80A)
+}Over_Current[2];
+
 void trim(char *s);
 void substr(char *dest, const char* src, unsigned int start, unsigned int cnt);
 
@@ -74,6 +84,7 @@ struct OCPP16Data				*ShmOCPP16Data;
 struct OCPP20Data				*ShmOCPP20Data;
 struct Charger					*ShmCharger;
 unsigned long					previousAlarmCode[AC_QUANTITY];
+uint32_t OCP_Magnification = 0;		// Magnification
 
 int StoreLogMsg(const char *fmt, ...)
 {
@@ -283,6 +294,36 @@ int qca7000FlashCheck()
 	return result;
 }
 
+void ccsOverCurrentProtection(uint8_t gun_index)
+{	
+	if((ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'E') ||
+	   (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'T') ||
+	   (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'Z'))
+	{
+		Over_Current[gun_index].OC_BegThreshold = (ShmCharger->gun_info[gun_index].targetCurrent * OCP_Magnification) - 100;
+		Over_Current[gun_index].OC_EndThreshold = (ShmCharger->gun_info[gun_index].targetCurrent * OCP_Magnification) - 200;
+	}
+	else
+	{
+		if(ShmCharger->gun_info[gun_index].targetCurrent > 20)
+		{
+			Over_Current[gun_index].OC_BegThreshold = (ShmCharger->gun_info[gun_index].targetCurrent * OCP_Magnification) + 0;
+			Over_Current[gun_index].OC_EndThreshold = (ShmCharger->gun_info[gun_index].targetCurrent * OCP_Magnification) - 200;
+		}
+		else
+		{
+			Over_Current[gun_index].OC_BegThreshold = (ShmCharger->gun_info[gun_index].targetCurrent * 100) + 200;
+			Over_Current[gun_index].OC_EndThreshold = (ShmCharger->gun_info[gun_index].targetCurrent * 100) + 0;
+		}
+	}
+	
+	#ifdef isDebugPrint
+	DEBUG_INFO("Gun-[%d] OC_BegThreshold: %d \n", gun_index, Over_Current[gun_index].OC_BegThreshold);
+	DEBUG_INFO("Gun-[%d] OC_EndThreshold: %d \n", gun_index, Over_Current[gun_index].OC_EndThreshold);
+	#endif
+}
+	
+
 //==========================================
 // Main process
 //==========================================
@@ -301,6 +342,21 @@ int main(void)
 		sleep(5);
 		return FAIL;
 	}
+	
+	if((ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'E') ||
+	   (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'T') ||
+	   (ShmSysConfigAndInfo->SysConfig.ModelName[3] == 'Z'))
+	{
+		// Europe
+		OCP_Magnification = 125;
+	}
+	else
+	{
+		// Other
+		OCP_Magnification = 110;
+	}
+	
+	DEBUG_INFO("OCP_Magnification: %d/100 => %.2f \n", OCP_Magnification,(float)(OCP_Magnification/100.0));
 
 	for(;;)
 	{
@@ -477,69 +533,196 @@ int main(void)
 						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L3_UNDER_VOLTAGE;
 						DEBUG_INFO("ALARM_L3_UNDER_VOLTAGE : recover \n");
 					}
-				}
-							
+				}			
 			}
 
 			//=====================================
 			// Over current detection
 			//=====================================
-			if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L1 == ON)
+			ccsOverCurrentProtection(gun_index);
+			if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L1 == ON) || ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent*100.0) > Over_Current[gun_index].OC_BegThreshold))
 			{
-				if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == OFF)
+				if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC?(Alarm_Counter[gun_index].OC[0] > 60):(Alarm_Counter[gun_index].OC[0] > FILTER_SPEC)))
+				{
+					if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == OFF)
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = ON;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_L1_OVER_CURRENT;
+						if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC?(Alarm_Counter[gun_index].OC_Retry[0]++):(Alarm_Counter[gun_index].OC_Retry[0]==0)));
+						
+						DEBUG_INFO("ALARM_L1_OVER_CURRENT : alarm \n");
+						DEBUG_INFO("Gun-[%d] PresentChargingCurrent: %d \n", gun_index, (uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent*100.0));
+						DEBUG_INFO("Gun-[%d] OC_BegThreshold: %d \n", gun_index, Over_Current[gun_index].OC_BegThreshold);
+						DEBUG_INFO("Gun-[%d] OC_Connter[0]: %d \n", gun_index, Alarm_Counter[gun_index].OC[0]);
+						DEBUG_INFO("Gun-[%d] OC_Retry[0]: %d \n", gun_index, Alarm_Counter[gun_index].OC_Retry[0]);
+					}
+				}
+				else
 				{
-					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = ON;
-					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_L1_OVER_CURRENT;
-					DEBUG_INFO("ALARM_L1_OVER_CURRENT : alarm \n");
+					Alarm_Counter[gun_index].OC[0]++;
 				}
 			}
-			else
+			else if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L1 == OFF) || ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrent*100.0) < Over_Current[gun_index].OC_EndThreshold))
 			{
-				if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == ON)
+				Alarm_Counter[gun_index].OC[0] = 0;
+				if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
+				{
+					if(Alarm_Counter[gun_index].OC_Retry[0] <= ALARM_OC_RETRY_COUNT)
+					{
+						if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == ON)
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = OFF;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L1_OVER_CURRENT;
+							DEBUG_INFO("ALARM_L1_OVER_CURRENT : recover \n");
+						}
+					}
+					else
+					{
+						if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
+						{
+							Alarm_Counter[gun_index].OC_Retry[0] = 0;
+							if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == ON)
+							{
+								ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = OFF;
+								ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L1_OVER_CURRENT;
+								DEBUG_INFO("ALARM_L1_OVER_CURRENT : recover latch \n");
+							}
+						}
+					}
+				}
+				else
 				{
-					ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = OFF;
-					ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L1_OVER_CURRENT;
-					DEBUG_INFO("ALARM_L1_OVER_CURRENT : recover \n");
+					if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP == ON)
+					{
+						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCP = OFF;
+						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L1_OVER_CURRENT;
+						DEBUG_INFO("ALARM_L1_OVER_CURRENT : recover \n");
+					}
 				}
 			}
 
 			if(ShmSysConfigAndInfo->SysConfig.AcPhaseCount == 3)
 			{
-				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L2 == ON)
+				if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L2 == ON) || ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2*100.0) > Over_Current[gun_index].OC_BegThreshold))
 				{
-					if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 == OFF)
+					if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC?(Alarm_Counter[gun_index].OC[1] > 60):(Alarm_Counter[gun_index].OC[1] > FILTER_SPEC)))
 					{
-						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 = ON;
-						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_L2_OVER_CURRENT;
-						DEBUG_INFO("ALARM_L2_OVER_CURRENT : alarm \n");
+						if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 == OFF)
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 = ON;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_L2_OVER_CURRENT;
+							if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC?(Alarm_Counter[gun_index].OC_Retry[1]++):(Alarm_Counter[gun_index].OC_Retry[1]==0)));
+							
+							DEBUG_INFO("ALARM_L2_OVER_CURRENT : alarm \n");
+							DEBUG_INFO("Gun-[%d] PresentChargingCurrentL2: %d \n", gun_index, (uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2*100.0));
+							DEBUG_INFO("Gun-[%d] OC_BegThreshold: %d \n", gun_index, Over_Current[gun_index].OC_BegThreshold);
+							DEBUG_INFO("Gun-[%d] OC_Connter[1]: %d \n", gun_index, Alarm_Counter[gun_index].OC[1]);
+							DEBUG_INFO("Gun-[%d] OC_Retry[1]: %d \n", gun_index, Alarm_Counter[gun_index].OC_Retry[1]);
+						}
 					}
+					else
+					{
+						Alarm_Counter[gun_index].OC[1]++;
+					}
+					
 				}
-				else
+				else if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L2 == OFF) || ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL2*100.0) < Over_Current[gun_index].OC_EndThreshold))
 				{
-					if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 == ON)
+					Alarm_Counter[gun_index].OC[1] = 0;
+					if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
+					{
+						if(Alarm_Counter[gun_index].OC_Retry[1] <= ALARM_OC_RETRY_COUNT)
+						{
+							if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 == ON)
+							{
+								ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 = OFF;
+								ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L2_OVER_CURRENT;
+								DEBUG_INFO("ALARM_L2_OVER_CURRENT : recover \n");
+							}
+						}
+						else
+						{
+							if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
+							{
+								Alarm_Counter[gun_index].OC_Retry[1] = 0;
+								if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 == ON)
+								{
+									ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 = OFF;
+									ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L2_OVER_CURRENT;
+									DEBUG_INFO("ALARM_L2_OVER_CURRENT : recover latch \n");
+								}
+							}
+						}
+					}
+					else
 					{
-						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 = OFF;
-						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L2_OVER_CURRENT;
-						DEBUG_INFO("ALARM_L2_OVER_CURRENT : recover \n");
+						if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 == ON)
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL2 = OFF;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L2_OVER_CURRENT;
+							DEBUG_INFO("ALARM_L2_OVER_CURRENT : recover \n");
+						}
 					}
 				}
 
-				if(ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L3 == ON)
+				if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L3 == ON) || ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3*100.0) > Over_Current[gun_index].OC_BegThreshold))
 				{
-					if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 == OFF)
+					if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC?(Alarm_Counter[gun_index].OC[2] > 60):(Alarm_Counter[gun_index].OC[2] > FILTER_SPEC)))
+					{
+						if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 == OFF)
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 = ON;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_L3_OVER_CURRENT;
+							if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC?(Alarm_Counter[gun_index].OC_Retry[2]++):(Alarm_Counter[gun_index].OC_Retry[2]==0)));
+							
+							DEBUG_INFO("ALARM_L3_OVER_CURRENT : alarm \n");
+							DEBUG_INFO("Gun-[%d] PresentChargingCurrentL3: %d \n", gun_index, (uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3*100.0));
+							DEBUG_INFO("Gun-[%d] OC_BegThreshold: %d \n", gun_index, Over_Current[gun_index].OC_BegThreshold);
+							DEBUG_INFO("Gun-[%d] OC_Connter[2]: %d \n", gun_index, Alarm_Counter[gun_index].OC[2]);
+							DEBUG_INFO("Gun-[%d] OC_Retry[2]: %d \n", gun_index, Alarm_Counter[gun_index].OC_Retry[2]);
+						}
+					}
+					else
 					{
-						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 = ON;
-						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode |= ALARM_L3_OVER_CURRENT;
-						DEBUG_INFO("ALARM_L3_OVER_CURRENT : alarm \n");
+						Alarm_Counter[gun_index].OC[2]++;
 					}
 				}
-				else
+				else if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.bits.OCP_L3 == OFF) || ((uint16_t)(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargingCurrentL3*100.0) < Over_Current[gun_index].OC_EndThreshold))
 				{
-					if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 == ON)
+					Alarm_Counter[gun_index].OC[2] = 0;
+					if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
 					{
-						ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 = OFF;
-						ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L3_OVER_CURRENT;
-						DEBUG_INFO("ALARM_L3_OVER_CURRENT : recover \n");
+						if(Alarm_Counter[gun_index].OC_Retry[2] <= ALARM_OC_RETRY_COUNT)
+						{
+							if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 == ON)
+							{
+								ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 = OFF;
+								ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L3_OVER_CURRENT;
+								DEBUG_INFO("ALARM_L3_OVER_CURRENT : recover \n");
+							}
+						}
+						else
+						{
+							if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A)
+							{
+								Alarm_Counter[gun_index].OC_Retry[2] = 0;
+								if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 == ON)
+								{
+									ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 = OFF;
+									ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L3_OVER_CURRENT;
+									DEBUG_INFO("ALARM_L3_OVER_CURRENT : recover latch \n");
+								}
+							}
+						}
+					}
+					else
+					{
+						if(ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 == ON)
+						{
+							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.SystemAcOutputOCPL3 = OFF;
+							ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode &= ~ALARM_L3_OVER_CURRENT;
+							DEBUG_INFO("ALARM_L3_OVER_CURRENT : recover \n");
+						}
 					}
 				}
 			}

+ 241 - 150
EVSE/Projects/NoodoeAX/Apps/main.c

@@ -19,21 +19,22 @@
 //==========================
 // Timeout constant define
 //==========================
-#define TIMEOUT_SPEC_HANDSHAKING				180000
-#define TIMEOUT_SPEC_AUTH						15000
-#define TIMEOUT_SPEC_HANDSHAKING_LED			185000
-#define TIMEOUT_SPEC_LOGPPRINTOUT				30000
-#define TIMEOUT_SPEC_PROFILE_PREPARE			60000
-#define TIMEOUT_SPEC_BS_HLC_HANDSHAKE			60000
-#define TIMEOUT_SPEC_EV_READY					30000
-#define TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET	10000
-#define TIMEOUT_SPEC_CCS_HANDSHAKE				120000
-#define TIMEOUT_SPEC_PWN_CHANGE					5000
-#define TIMEOUT_SPEC_POWERSAVING_LCD			120000
-#define TIMEOUT_SPEC_POWERSAVING_RFID			120000
-#define TIMEOUT_SPEC_POWERSAVING_METER			120000
-#define TIMEOUT_SPEC_POWERSAVING_LED_STATUS		120000
-#define TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION	15000
+#define TIMEOUT_SPEC_HANDSHAKING				180
+#define TIMEOUT_SPEC_AUTH						15
+#define TIMEOUT_SPEC_HANDSHAKING_LED			185
+#define TIMEOUT_SPEC_LOGPPRINTOUT				30
+#define TIMEOUT_SPEC_PROFILE_PREPARE			60
+#define TIMEOUT_SPEC_BS_HLC_HANDSHAKE			60
+#define TIMEOUT_SPEC_EV_READY					30
+#define TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET	10
+#define TIMEOUT_SPEC_CCS_HANDSHAKE				120
+#define TIMEOUT_SPEC_PWN_CHANGE					5
+#define TIMEOUT_SPEC_POWERSAVING_LCD			120
+#define TIMEOUT_SPEC_POWERSAVING_RFID			120
+#define TIMEOUT_SPEC_POWERSAVING_METER			120
+#define TIMEOUT_SPEC_POWERSAVING_LED_STATUS		120
+#define TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION	15
+#define TIMEOUT_SPEC_RESET_WIFI_MODULE			180
 
 //==========================
 // GPIO constant define
@@ -90,9 +91,9 @@ struct OCPP16Data				*ShmOCPP16Data;
 struct OCPP20Data				*ShmOCPP20Data;
 struct Charger					*ShmCharger;
 
-struct timeb					startTime[AC_QUANTITY][TMR_IDX_CNT];
-struct timeb					startChargingTime[AC_QUANTITY];
-struct timeb					endChargingTime[AC_QUANTITY];
+struct timespec					startTime[AC_QUANTITY][TMR_IDX_CNT];
+struct timespec 				startChargingTime[AC_QUANTITY];
+struct timespec 				endChargingTime[AC_QUANTITY];
 sqlite3 *localDb;
 
 struct SysConfigData			SysConfigOrg;
@@ -197,6 +198,25 @@ int StoreLogMsg(const char *fmt, ...)
 	return rc;
 }
 
+void refreshStartTimer(struct timespec *timer)
+{
+	clock_gettime(CLOCK_MONOTONIC, timer);
+}
+
+int getDiffSecNow(struct timespec timer)
+{
+	struct timespec timerNow;
+
+	clock_gettime(CLOCK_MONOTONIC, &timerNow);
+
+	return (int)((((unsigned long)(timerNow.tv_sec - timer.tv_sec) * 1000) + ((unsigned long)((timerNow.tv_nsec / 1000000) - (timer.tv_nsec / 1000000))))/1000);
+}
+
+int getDiffSecBetween(struct timespec start, struct timespec end)
+{
+	return (int)((((unsigned long)(end.tv_sec - start.tv_sec) * 1000) + ((unsigned long)((end.tv_nsec / 1000000) - (start.tv_nsec / 1000000))))/1000);
+}
+
 long long DiffTimebWithNow(struct timeb ST)
 {
 	//return milli-second
@@ -476,7 +496,7 @@ uint8_t ocpp_get_connection_status()
 
 uint16_t ocpp_get_connection_timeout()
 {
-	uint16_t result = (TIMEOUT_SPEC_HANDSHAKING/1000);
+	uint16_t result = TIMEOUT_SPEC_HANDSHAKING;
 
 	if(ShmSysConfigAndInfo->SysInfo.OcppRunningVer == OCPP_RUNNING_VERSION_16)
 	{
@@ -2430,26 +2450,29 @@ void InitEthernet()
 				if(cnt_pingDNS_Fail >= 3)
 				{
 					ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaEthernet=ON;
-					if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
+					if(!ShmSysConfigAndInfo->SysInfo.OcppConnStatus)
 					{
-						system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
-						sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
-						system(tmpbuf);
-					}
-					else
-					{
-						system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
-						memset(tmpbuf,0,256);
-						sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
-								ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
-								ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
-						system(tmpbuf);
-						memset(tmpbuf,0,256);
-						sprintf(tmpbuf,"route add default gw %s eth0 &",
-						ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
-						system(tmpbuf);
+						if((ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthDhcpClient==0))
+						{
+							system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
+							sprintf(tmpbuf, "/sbin/udhcpc -i eth0 -x hostname:CSU3_%s -s /root/dhcp_script/eth0.script > /dev/null &", ShmSysConfigAndInfo->SysConfig.SystemId);
+							system(tmpbuf);
+						}
+						else
+						{
+							system("pgrep -f \"udhcpc -i eth0\" | xargs kill");
+							memset(tmpbuf,0,256);
+							sprintf(tmpbuf,"/sbin/ifconfig eth0 %s netmask %s up &",
+									ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthIpAddress,
+									ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthSubmaskAddress);
+							system(tmpbuf);
+							memset(tmpbuf,0,256);
+							sprintf(tmpbuf,"route add default gw %s eth0 &",
+							ShmSysConfigAndInfo->SysConfig.Eth0Interface.EthGatewayAddress);
+							system(tmpbuf);
+						}
+						cnt_pingDNS_Fail = 0;
 					}
-					cnt_pingDNS_Fail = 0;
 				}
 				else
 				{
@@ -2557,6 +2580,60 @@ void InitEthernet()
 				}
 			}
 
+			// Reset wifi function
+			if((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 1))
+			{
+				if((!ShmSysConfigAndInfo->SysInfo.OcppConnStatus) && 
+				   (ShmStatusCodeData->InfoCode.InfoEvents.bits.InternetDisconnectViaWiFi == ON) &&
+				   (ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi == ON))
+				{
+					if((getDiffSecNow(startTime[0][TMR_IDX_RESET_WIFI]) > TIMEOUT_SPEC_RESET_WIFI_MODULE))
+					{
+						DEBUG_INFO("Wifi [Station] mode: Reset wifi power. \n");
+						DEBUG_INFO("The wifi interface may not be found. \n");
+						
+						system("echo 1 > /sys/class/gpio/gpio59/value");
+						sleep(3);
+						system("echo 0 > /sys/class/gpio/gpio59/value");
+
+						refreshStartTimer(&startTime[0][TMR_IDX_RESET_WIFI]);
+					}
+					else
+					{}
+				}
+				else
+				{
+					refreshStartTimer(&startTime[0][TMR_IDX_RESET_WIFI]);
+				}	
+			}
+			else if((ShmSysConfigAndInfo->SysConfig.AthInterface.WifiMode == 2))
+			{
+				if((ShmStatusCodeData->InfoCode.InfoEvents.bits.ApDisconnectViaWiFi == ON))
+				{
+					if((getDiffSecNow(startTime[0][TMR_IDX_RESET_WIFI]) > TIMEOUT_SPEC_RESET_WIFI_MODULE))
+					{
+						DEBUG_INFO("Wifi [Access Point] mode: reset wifi power. \n");
+						DEBUG_INFO("The wifi interface may not be found. \n");
+						
+						system("echo 1 > /sys/class/gpio/gpio59/value");
+						sleep(3);
+						system("echo 0 > /sys/class/gpio/gpio59/value");
+
+						refreshStartTimer(&startTime[0][TMR_IDX_RESET_WIFI]);
+					}
+					else
+					{}
+				}
+				else
+				{
+					refreshStartTimer(&startTime[0][TMR_IDX_RESET_WIFI]);
+				}	
+			}
+			else
+			{
+				refreshStartTimer(&startTime[0][TMR_IDX_RESET_WIFI]);
+			}
+
 			sleep(5);
 		}
 	}
@@ -2882,7 +2959,7 @@ void get_firmware_version(unsigned char gun_index)
 	strcpy((char*)ShmSysConfigAndInfo->SysInfo.CsuPrimFwRev, ShmCharger->gun_info[gun_index].ver.Version_FW);
 
 	// Get CSU root file system version
-	sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "B0.58.00.0000.00");
+	sprintf((char*)ShmSysConfigAndInfo->SysInfo.CsuRootFsFwRev, "B0.60.00.0000.00");
 
 	// Get AC connector type from model name
 	for(uint8_t idx=0;idx<3;idx++)
@@ -3952,13 +4029,6 @@ void checkTask()
 		DEBUG_INFO("Module_PowerSharing not running, restart it.\n");
 		system ("/root/Module_PowerSharing &");
 	}
-
-	if((system("pidof -s Module_InitUpgrade > /dev/null") != 0) &&
-		ShmSysConfigAndInfo->SysConfig.isReqFirstUpgrade)
-	{
-		DEBUG_INFO("Module_InitUpgrade not running, restart it.\n");
-		system ("/root/Module_InitUpgrade &");
-	}
 }
 
 void checkConnectionTimeout()
@@ -3966,19 +4036,19 @@ void checkConnectionTimeout()
 	if((system("pidof -s OcppBackend > /dev/null") != 0) && (system("pidof -s OcppBackend20 > /dev/null") != 0))
 	{
 		ShmCharger->timeoutSpec.Present_Timeout_Spec = TIMEOUT_SPEC_HANDSHAKING;
-		//DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING/1000);
+		//DEBUG_INFO("Handshaking timeout specification follow by initial setting : %d s \n", TIMEOUT_SPEC_HANDSHAKING);
 	}
 	else
 	{
 		ShmCharger->timeoutSpec.Setting_Timeout_Spec = ocpp_get_connection_timeout();
-		if((ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
+		if((ShmCharger->timeoutSpec.Setting_Timeout_Spec) < TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
 		{
-			ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10000);
+			ShmCharger->timeoutSpec.Present_Timeout_Spec = (TIMEOUT_SPEC_BS_HLC_HANDSHAKE+10);
 			//DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Fail. Value can't be zero or less than zero.\n.");
 		}
 		else
 		{
-			ShmCharger->timeoutSpec.Present_Timeout_Spec = (ShmCharger->timeoutSpec.Setting_Timeout_Spec*1000);
+			ShmCharger->timeoutSpec.Present_Timeout_Spec = ShmCharger->timeoutSpec.Setting_Timeout_Spec;
 			//DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : Pass...\n.");
 		}
 		//DEBUG_INFO("Handshaking timeout specification follow by OCPP Configuration : %s s \n.",ShmOCPP16Data->ConfigurationTable.CoreProfile[ConnectionTimeOut].ItemData);
@@ -4161,7 +4231,7 @@ void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
 		   (((strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidFrom)>0) && (strlen((char*)ShmOCPP16Data->SmartChargingProfile[gun_index].ValidTo)>0)) ? isProfileValid(gun_index) : ON))
 		{
 			// Debug information
-			if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT)
+			if(getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT)
 			{
 				DEBUG_INFO("===============================================================\n");
 				DEBUG_INFO("Profile ID found: %d\n", ShmOCPP16Data->SmartChargingProfile[gun_index].ChargingProfileId);
@@ -4211,7 +4281,7 @@ void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
 		   (((strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validFrom)>0) && (strlen((char*)ShmOCPP20Data->SmartChargingProfile[gun_index].validTo)>0)) ? isProfileValid(gun_index) : ON))
 		{
 			// Debug information
-			if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT)
+			if(getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT)
 			{
 				DEBUG_INFO("===============================================================\n");
 				DEBUG_INFO("Profile ID found: %d\n", ShmOCPP20Data->SmartChargingProfile[gun_index].id);
@@ -4314,12 +4384,12 @@ void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
 				{
 					if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
 					{
-						if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
+						if(getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
 						{
 							ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmCharger->gun_info[gun_index].primaryMcuState.rating_current)?ShmCharger->gun_info[gun_index].primaryMcuState.rating_current:ShmCharger->gun_info[gun_index].targetCurrent);
 							ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
 							ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
-							ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
+							refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
 						}
 					}
 					else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
@@ -4336,12 +4406,12 @@ void checkChargingProfileLimit(uint8_t gun_index, uint8_t system_mode)
 				{
 					if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_BS)
 					{
-						if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
+						if(getDiffSecNow(startTime[gun_index][TMR_IDX_PWN_CHANGE]) > TIMEOUT_SPEC_PWN_CHANGE)
 						{
 							ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((ShmCharger->gun_info[gun_index].targetCurrent > ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent)?ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent:ShmCharger->gun_info[gun_index].targetCurrent);
 							ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current = ((1 <= ShmCharger->gun_info[gun_index].targetCurrent) && (ShmCharger->gun_info[gun_index].targetCurrent <= 5)? 6:((ShmCharger->gun_info[gun_index].targetCurrent == 0)? 100:ShmCharger->gun_info[gun_index].targetCurrent));
 							ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty = YES;
-							ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
+							refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
 						}
 					}
 					else if(ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC)
@@ -4562,6 +4632,34 @@ void checkRemoteUpgradeStatus()
 	}
 }
 
+void checkHandshakeCountdown(uint8_t gun_index)
+{
+	switch(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus)
+	{
+		case SYS_MODE_IDLE:
+			
+			if(ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
+				refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
+			else
+				ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
+
+			if(!ShmCharger->isAuthrizing)
+			{
+				if(ocpp_get_isRemoteStartWait())
+					ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT])));
+				else
+					refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
+			}
+			
+			break;
+		case SYS_MODE_PREPARING:
+			
+			ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]));
+			
+			break;
+	}
+}
+
 void checkRfidAuthrize()
 {
 	static uint8_t isCheckdResult = FALSE;
@@ -4641,7 +4739,7 @@ void checkRfidAuthrize()
 			if(!isSnStart)
 			{
 				memcpy(ShmSysConfigAndInfo->SysConfig.UserId, bufferRFID, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
-				ftime(&startTime[0][TMR_IDX_AUTH]);
+				refreshStartTimer(&startTime[0][TMR_IDX_AUTH]);
 				ocpp_set_auth_conf(OFF);
 				ocpp_set_auth_req(ON, "ISO14443");
 				setLedMotion(0,LED_ACTION_AUTHED);
@@ -4654,7 +4752,7 @@ void checkRfidAuthrize()
 	else
 	{
 		// Wait authorize result
-		if(!ocpp_get_auth_conf() && (DiffTimebWithNow(startTime[0][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH))
+		if(!ocpp_get_auth_conf() && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH))
 		{
 			// Authorization timeout process.
 			DEBUG_WARN("Authorize timeout !!!\n");
@@ -4673,7 +4771,7 @@ void checkRfidAuthrize()
 		{
 			if(ocpp_get_auth_conf() ||
 			   (!ocpp_get_connection_status() && ((ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_FREE) || (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_NOCHARGE))) ||
-			   (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (DiffTimebWithNow(startTime[0][TMR_IDX_AUTH]) > 2000))
+			   (!ocpp_get_connection_status() && (ShmSysConfigAndInfo->SysConfig.OfflinePolicy == OFF_POLICY_LOCALLIST) && (getDiffSecNow(startTime[0][TMR_IDX_AUTH]) > 2))
 			  )
 			{
 				if(ocpp_get_auth_result(NO) ||
@@ -4684,7 +4782,7 @@ void checkRfidAuthrize()
 					{
 						DEBUG_INFO("Authorize pass.\n");
 						setSpeaker(ON, SPEAKER_SHORT);
-						ftime(&startTime[0][TMR_IDX_GUN_DETECT]);
+						refreshStartTimer(&startTime[0][TMR_IDX_GUN_DETECT]);
 						ShmCharger->isGetAuthResult = TRUE;
 						isCheckdResult = TRUE;
 
@@ -4696,15 +4794,11 @@ void checkRfidAuthrize()
 						for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
 							ShmCharger->gun_info[gun_index].resultAuthorization = VALIDATED_RFID;
 					}
-					
-					// Handskake timer needs to refresh
-					if(ShmCharger->gun_info[ShmCharger->gun_selectd].isHandshakeTimerRefresh == YES)
-						ftime(&startTime[0][TMR_IDX_GUN_DETECT]);
 
-					// Timer for lcm to shows
-					ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (DiffTimebWithNow(startTime[0][TMR_IDX_GUN_DETECT])/1000));
+					for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
+						checkHandshakeCountdown(gun_index);
 
-					if(DiffTimebWithNow(startTime[0][TMR_IDX_GUN_DETECT]) < (ocpp_get_connection_timeout()*1000))
+					if(getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < (ocpp_get_connection_timeout()))
 					{
 						if(GetCardSerialNumber() != FAIL)
 						{
@@ -4723,7 +4817,7 @@ void checkRfidAuthrize()
 						{
 							for(int gun_index = 0;gun_index<AC_QUANTITY;gun_index++)
 							{
-								if(DiffTimebWithNow(startTime[0][TMR_IDX_GUN_DETECT]) < 3000)
+								if(getDiffSecNow(startTime[0][TMR_IDX_GUN_DETECT]) < 3)
 									setLedMotion(gun_index,LED_ACTION_RFID_PASS);
 								else
 									setLedMotion(gun_index,LED_ACTION_AUTHED);
@@ -4830,7 +4924,7 @@ int main(void)
 		//==============================================
 		// Period check for 10 seconds
 		//==============================================
-		if((DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10000) || (DiffTimebWithNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
+		if((getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) > 10) || (getDiffSecNow(startTime[0][TMR_IDX_CHECK_TASK]) < 0))
 		{
 			//==============================================
 			// Check task processing
@@ -4843,7 +4937,7 @@ int main(void)
 			//==============================================
 			checkConnectionTimeout();
 
-			ftime(&startTime[0][TMR_IDX_CHECK_TASK]);
+			refreshStartTimer(&startTime[0][TMR_IDX_CHECK_TASK]);
 		}
 
 		//==============================================
@@ -4933,7 +5027,7 @@ int main(void)
 			 */
 			if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_A) && (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == OFF))
 			{
-				ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
 			}
 
 			if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_ALARM) ||
@@ -4941,13 +5035,13 @@ int main(void)
 			   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_UPDATE) ||
 			   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_MAINTAIN) ||
 			   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_CHARGING) ||
-			   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING) || 
+			   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_TERMINATING) ||
 			   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_COMPLETE))
 			{
-				ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
-				ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
-				ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
-				ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
+				refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
 			}
 			else
 			{
@@ -4956,37 +5050,37 @@ int main(void)
 				   (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON) ||
 				   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PreviousSystemStatus != ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus) ||
 				   (((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_PREPARING) || (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_RESERVATION)) &&
-					((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)) && 
-					(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]) < 10000)))
+					((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_B) || (ShmCharger->gun_info[gun_index].primaryMcuState.socket_e.isSocketEPinOn == ON)) &&
+					(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]) < 10)))
 				{
-					ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
-					ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
-					ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
-					ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
 
 					if(((gpio_get_value(GPIO_IN_WAKEUP) == OFF) || (ShmCharger->gun_info[gun_index].GPIO_Input.Button_Mode_Switch == ON)) &&
 					   (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus == SYS_MODE_IDLE) &&
-					   (DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_CHECK_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
+					   (getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_CHECK_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
 					{
 						ShmCharger->gun_info[gun_index].isCheckPowerConsumption = YES;
-						ftime(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
 					}
 				}
 				else
 				{
-					ftime(&startTime[gun_index][TMR_IDX_CHECK_POWER_CONSUMPTION]);
-					if((DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_LCM_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
+					refreshStartTimer(&startTime[gun_index][TMR_IDX_CHECK_POWER_CONSUMPTION]);
+					if((getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_LCM_POWER_CONSUMPTION]) > TIMEOUT_SPEC_CEHCK_POWER_CONSUMPTION))
 					{
 						ShmCharger->gun_info[gun_index].isCheckPowerConsumption = NO;
 					}
 				}
 			}
 
-			if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
+			if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]) > TIMEOUT_SPEC_POWERSAVING_LCD)
 			{
 				if(ShmCharger->isLcdOn == ON)
 				{
-					DEBUG_INFO("LCD into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
+					DEBUG_INFO("LCD into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
 					ShmCharger->isLcdOn = OFF;
 				}
 			}
@@ -4994,16 +5088,16 @@ int main(void)
 			{
 				if(ShmCharger->isLcdOn == OFF)
 				{
-					DEBUG_INFO("LCD exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
+					DEBUG_INFO("LCD exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LCD]));
 					ShmCharger->isLcdOn = ON;
 				}
 			}
 
-			if(DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
+			if(getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]) > TIMEOUT_SPEC_POWERSAVING_RFID)
 			{
 				if(gpio_get_value(GPIO_OUT_RST_RFID) == ON)
 				{
-					DEBUG_INFO("RFID into power saving...%d\n", DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
+					DEBUG_INFO("RFID into power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
 					gpio_set_value(GPIO_OUT_RST_RFID, OFF);
 				}
 			}
@@ -5011,16 +5105,16 @@ int main(void)
 			{
 				if(gpio_get_value(GPIO_OUT_RST_RFID) == OFF)
 				{
-					DEBUG_INFO("RFID exit power saving...%d\n", DiffTimebWithNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
+					DEBUG_INFO("RFID exit power saving...%d\n", getDiffSecNow(startTime[ShmCharger->gun_selectd][TMR_IDX_POWERSAVING_RFID]));
 					gpio_set_value(GPIO_OUT_RST_RFID, ON);
 				}
 			}
 
-			if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
+			if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]) > TIMEOUT_SPEC_POWERSAVING_METER)
 			{
 				if(ShmCharger->gun_info[gun_index].isMeterOn)
 				{
-					DEBUG_INFO("Meter into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
+					DEBUG_INFO("Meter into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
 					ShmCharger->gun_info[gun_index].isMeterOn = OFF;
 				}
 			}
@@ -5028,16 +5122,16 @@ int main(void)
 			{
 				if(!ShmCharger->gun_info[gun_index].isMeterOn)
 				{
-					DEBUG_INFO("Meter exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
+					DEBUG_INFO("Meter exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_METER]));
 					ShmCharger->gun_info[gun_index].isMeterOn = ON;
 				}
 			}
 
-			if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]) > TIMEOUT_SPEC_POWERSAVING_LED_STATUS)
+			if(getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]) > TIMEOUT_SPEC_POWERSAVING_LED_STATUS)
 			{
 				if(ShmCharger->gun_info[gun_index].isSleepOn == NO)
 				{
-					DEBUG_INFO("LED status into power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
+					DEBUG_INFO("LED status into power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
 					ShmCharger->gun_info[gun_index].isSleepOn = YES;
 				}
 			}
@@ -5045,7 +5139,7 @@ int main(void)
 			{
 				if(ShmCharger->gun_info[gun_index].isSleepOn == YES)
 				{
-					DEBUG_INFO("LED status exit power saving...%d\n", DiffTimebWithNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
+					DEBUG_INFO("LED status exit power saving...%d\n", getDiffSecNow(startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]));
 					ShmCharger->gun_info[gun_index].isSleepOn = NO;
 				}
 			}
@@ -5090,7 +5184,7 @@ int main(void)
 			if(ShmSysConfigAndInfo->SysInfo.AcChargingData[0].SystemStatus != SYS_MODE_BOOTING)
 			{
 				// Alarm event check
-				if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0) || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
+				if((ShmCharger->gun_info[gun_index].primaryMcuAlarm.InputAlarmCode>0) || (ShmCharger->gun_info[gun_index].systemAlarmCode.SystemAlarmCode) || ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CCSboardStestFail)
 				{
 					if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus != SYS_MODE_ALARM)
 					{
@@ -5149,12 +5243,12 @@ int main(void)
 							ShmStatusCodeData->AlarmCode.AlarmEvents.bits.CsuInitFailed = ON;
 						}
 
-						ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
-						ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
-						ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
-						ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
-						ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
-						ftime(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_RFID]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_METER]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_STATE_B]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LED_STATUS]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_LCM_POWER_CONSUMPTION]);
 					}
 
 					if(ShmCharger->gun_info[gun_index].mcuFlag.isReadFwVerPass &&
@@ -5279,23 +5373,21 @@ int main(void)
 						if(ShmCharger->isCcsEnable)system("pkill Module_CCS");
 						DB_Check_Record_Buf(localDb, gun_index);
 					}
-
+					
+					checkHandshakeCountdown(gun_index);
+					
 					// LED status in Idle mode
 					if(!ShmCharger->isAuthrizing)
 					{
 						if(ocpp_get_isRemoteStartWait())
 						{
-							ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (DiffTimebWithNow(startTime[0][TMR_IDX_GUN_DETECT])/1000));
-
 							ShmCharger->gun_info[gun_index].isSleepOn = OFF;
 							setLedMotion(gun_index, LED_ACTION_AUTHED);
-							ftime(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
-							DEBUG_INFO("Remote Start Transaction without connector Id.\n");
+							refreshStartTimer(&startTime[gun_index][TMR_IDX_POWERSAVING_LCD]);
+							//DEBUG_INFO("Remote Start Transaction without connector Id.\n");
 						}
 						else
 						{
-							ftime(&startTime[0][TMR_IDX_GUN_DETECT]);
-
 							if(ShmSysConfigAndInfo->SysInfo.OcppConnStatus == ON)
 							{
 								if(ShmCharger->gun_info[gun_index].rfidReq == OFF)
@@ -5377,7 +5469,7 @@ int main(void)
 				case SYS_MODE_AUTHORIZING:
 					if(isModeChange(gun_index))
 					{
-						ftime(&startTime[gun_index][TMR_IDX_AUTH]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_AUTH]);
 						if(ocpp_isAuthorizeRemoteStart() && (ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartMethod == START_METHOD_BACKEND))
 						{
 							DEBUG_INFO("Remote start request authorize.\n");
@@ -5385,7 +5477,7 @@ int main(void)
 						}
 					}
 
-					if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
+					if(getDiffSecNow(startTime[gun_index][TMR_IDX_AUTH]) > TIMEOUT_SPEC_AUTH)
 					{
 						// Authorization timeout process.
 						setSpeaker(ON,SPEAKER_INTERVAL_3COUNT);
@@ -5473,7 +5565,7 @@ int main(void)
 				case SYS_MODE_PREPARING:
 					if(isModeChange(gun_index))
 					{
-						ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
 						setLedMotion(gun_index,LED_ACTION_AUTHED);
 						ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;												 
 
@@ -5502,7 +5594,7 @@ int main(void)
 								{
 									setRequest(gun_index, ON);
 									setLedMotion(gun_index,LED_ACTION_CONNECTED);
-									ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
+									refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
 									DEBUG_INFO("Set Request On.\n");
 
 									//Let CCS task start to negotiate
@@ -5523,17 +5615,17 @@ int main(void)
 								if((ShmCharger->gun_info[gun_index].mcuFlag.isSetCpPwmDuty == OFF))
 								{
 									//2 secs timeout
-									ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
+									refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
 									ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_CCS;
 								}
 								break;
 							case HANDSHAKE_CCS:
 								//CCS handshake timeout
-								if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
+								if(getDiffSecNow(startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]) > TIMEOUT_SPEC_BS_HLC_HANDSHAKE)
 								{
 									ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission = OFF;
 									ShmCharger->gun_info[gun_index].acCcsInfo.EVSENotification = NOTIFICATION_STOP;
-									DEBUG_INFO("BS/HLC %d secs handshake timeout.\n", (TIMEOUT_SPEC_BS_HLC_HANDSHAKE/1000));
+									DEBUG_INFO("BS/HLC %d secs handshake timeout.\n", TIMEOUT_SPEC_BS_HLC_HANDSHAKE);
 								}
 
 								if((ShmCharger->gun_info[gun_index].acCcsInfo.ChargingPermission == OFF) && (ShmCharger->gun_info[gun_index].acCcsInfo.CpSetPWMDuty != CCS_PWM_DUTY_5))
@@ -5551,7 +5643,7 @@ int main(void)
 									ShmCharger->gun_info[gun_index].chargingMode = CHARGING_MODE_HLC;
 									DEBUG_INFO("Enter HLC Mode charging.\n");
 									ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_HLC_MODE;
-									ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
+									refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
 								}
 
 								break;
@@ -5574,7 +5666,7 @@ int main(void)
 									checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
 
 									ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_SET_MAX_CURRENT;
-									ftime(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
+									refreshStartTimer(&startTime[gun_index][TMR_IDX_BS_HLC_HANDSHAKE]);
 								}
 								break;
 							case HANDSHAKE_SET_MAX_CURRENT:
@@ -5583,15 +5675,15 @@ int main(void)
 									ShmCharger->gun_info[gun_index].ccsHandshakeState = HANDSHAKE_BS_MODE;
 									DEBUG_INFO("Enter BS Mode charging.\n");
 									//for EV READY 30 secs didn't start charging to STATE E
-									ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
+									refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
 								}
 								break;
 							case HANDSHAKE_BS_MODE:
-								ftime(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
+								refreshStartTimer(&startTime[gun_index][TMR_IDX_HANDSHAKING]);
 								checkChargingProfileLimit(gun_index, ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].SystemStatus);
 
 								if((ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PilotState == CP_STATE_C) && ((ShmCharger->gun_info[gun_index].targetCurrent != 0)))
-								{									
+								{
 									setRelay(gun_index,ON);
 								}
 
@@ -5616,7 +5708,7 @@ int main(void)
 									memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
 									ocpp_copy_userid_to_starttransaction(gun_index);
 									ocpp_set_starttransaction_req(gun_index, ON);
-									ftime(&startChargingTime[gun_index]);
+									refreshStartTimer(&startChargingTime[gun_index]);
 
 									setChargerMode(gun_index, SYS_MODE_CHARGING);
 								}
@@ -5719,16 +5811,16 @@ int main(void)
 									ocpp_set_starttransaction_req(gun_index, ON);
 									ShmCharger->gun_info[gun_index].isCCSStartTransation = OFF;
 									setChargerMode(gun_index, SYS_MODE_CHARGING);
-									ftime(&startChargingTime[gun_index]);
-									ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
+									refreshStartTimer(&startChargingTime[gun_index]);
+									refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
 								}
 
 								//120 sec timeout
-								if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
+								if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE)
 								{
 									setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
 
-									if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5000)
+									if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > TIMEOUT_SPEC_CCS_HANDSHAKE+5)
 									{
 										DEBUG_INFO("CCS 120 secs handshake timeout, change to BS Mode...\n");
 										//Cancel CCS task negotiating
@@ -5762,7 +5854,7 @@ int main(void)
 						memcpy((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StartUserId, ShmSysConfigAndInfo->SysConfig.UserId, ARRAY_SIZE(ShmSysConfigAndInfo->SysConfig.UserId));
 						ocpp_copy_userid_to_starttransaction(gun_index);
 						ocpp_set_starttransaction_req(gun_index, ON);
-						ftime(&startChargingTime[gun_index]);
+						refreshStartTimer(&startChargingTime[gun_index]);
 						setChargerMode(gun_index, SYS_MODE_CHARGING);
 					}
 
@@ -5789,14 +5881,13 @@ int main(void)
 					{
 						ShmCharger->gun_info[gun_index].rfidReq = OFF;
 					}
-					
-					ShmCharger->timeoutSpec.Handshake_Timeout = ((ocpp_get_connection_timeout()) - (DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING])/1000));
 
-					if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
+					checkHandshakeCountdown(gun_index);
+					if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > ShmCharger->timeoutSpec.Present_Timeout_Spec)
 					{
 						setLedMotion(gun_index, LED_ACTION_HANDSHAKE_FAIL);
 
-						if(DiffTimebWithNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5000))
+						if(getDiffSecNow(startTime[gun_index][TMR_IDX_HANDSHAKING]) > (ShmCharger->timeoutSpec.Present_Timeout_Spec+5))
 						{
 							DEBUG_INFO("Handshaking timeout...\n");
 							
@@ -5822,9 +5913,9 @@ int main(void)
 					{
 						ShmCharger->gun_info[gun_index].rfidReq = OFF;
 						//ftime(&startChargingTime[gun_index]);
-						ftime(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
-						ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
-						ftime(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_PWN_CHANGE]);
 						ocpp_set_auth_req(OFF);
 						ocpp_reset_smartcharging_profileId(gun_index);
 						ocpp_set_profile_req(gun_index, ON);
@@ -5834,9 +5925,9 @@ int main(void)
 
 					//if time up, clear CCS MSG count
 					if((ShmCharger->gun_info[gun_index].chargingMode == CHARGING_MODE_HLC) &&
-					   (DiffTimebWithNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
+					   (getDiffSecNow(startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]) > TIMEOUT_SPEC_CCS_HEARTBEAT_COUNT_RESET))
 					{
-						ftime(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
+						refreshStartTimer(&startTime[gun_index][TMR_IDX_CCS_HEARTBEAT_COUNT_RESET]);
 						if(ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat > 0)
 						{
 							ShmCharger->gun_info[gun_index].acCcsInfo.CcsHeartBeat = 0;
@@ -5878,20 +5969,20 @@ int main(void)
 							//ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PowerConsumption[2] = (ShmCharger->gun_info[gun_index].powerConsumption[2].power_consumption/10000.0);
 						}
 
-						ftime(&endChargingTime[gun_index]);
-						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
+						refreshStartTimer(&endChargingTime[gun_index]);
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
 						presentChargedEnergyUpdate(gun_index);
 
 						// Response StartTransactionConf
 						ocpp_set_starttransaction_conf(gun_index, OFF);
 
 						// Charging profile preparation
-						if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
+						if((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
 						{
 							if(!ocpp_get_profile_conf(gun_index))
 							{
 								ocpp_set_profile_req(gun_index, ON);
-								ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
+								refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
 							}
 							else
 							{
@@ -6026,14 +6117,14 @@ int main(void)
 						}
 
 						// Debug information
-						if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
+						if((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
 						{
 							DEBUG_INFO("==============================================================\n");
 							DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d \n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
 							DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
 							DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
 							DEBUG_INFO("==============================================================\n");
-							ftime(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
+							refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
 							
 							getDateTimeString((char*)ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].StopDateTime);
 							DB_Update_Record_Buf(localDb, gun_index);
@@ -6055,10 +6146,10 @@ int main(void)
 						ShmCharger->gun_info[gun_index].resultAuthorization = DEFAULT_RFID;
 					}
 
-					ftime(&endChargingTime[gun_index]);
+					refreshStartTimer(&endChargingTime[gun_index]);
 					//if(ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration != 0)
 					//{
-						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = DiffTimeb(startChargingTime[gun_index], endChargingTime[gun_index])/1000;
+						ShmSysConfigAndInfo->SysInfo.AcChargingData[gun_index].PresentChargedDuration = getDiffSecBetween(startChargingTime[gun_index], endChargingTime[gun_index]);
 					//}
 
 					// End authorize pass
@@ -6109,12 +6200,12 @@ int main(void)
 						}
 
 						// Charging profile preparation
-						if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
+						if((getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) > TIMEOUT_SPEC_PROFILE_PREPARE) || (getDiffSecNow(startTime[gun_index][TMR_IDX_PROFILE_PREPARE]) < 0))
 						{
 							if(!ocpp_get_profile_conf(gun_index))
 							{
 								ocpp_set_profile_req(gun_index, ON);
-								ftime(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
+								refreshStartTimer(&startTime[gun_index][TMR_IDX_PROFILE_PREPARE]);
 							}
 							else
 							{
@@ -6128,14 +6219,14 @@ int main(void)
 						setRelay(gun_index, OFF);
 
 						// Debug information
-						if((DiffTimebWithNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (DiffTimebWithNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
+						if((getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) > TIMEOUT_SPEC_LOGPPRINTOUT) || (getDiffSecNow(startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]) < 0))
 						{
 							DEBUG_INFO("=============================================================\n");
 							DEBUG_INFO("ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent: %d\n", ShmSysConfigAndInfo->SysConfig.MaxChargingCurrent);
 							DEBUG_INFO("ShmCharger->gun_info[%d].primaryMcuCp_Pwn_Duty.max_current: %d\n", gun_index, ShmCharger->gun_info[gun_index].primaryMcuCp_Pwn_Duty.max_current);
 							DEBUG_INFO("ShmCharger->gun_info[%d].targetCurrent: %d\n", gun_index, ShmCharger->gun_info[gun_index].targetCurrent);
 							DEBUG_INFO("=============================================================\n");
-							ftime(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
+							refreshStartTimer(&startTime[gun_index][TMR_IDX_REFRESH_CHARGING_INFO]);
 						}
 
 						if(!ocpp_get_auth_result(YES, gun_index))

+ 1 - 1
EVSE/Projects/NoodoeAX/Apps/main.h

@@ -233,7 +233,7 @@ enum TIMER_IDX
 	TMR_IDX_POWERSAVING_LED_STATUS,
 	TMR_IDX_CHECK_POWER_CONSUMPTION,
 	TMR_IDX_LCM_POWER_CONSUMPTION,
-	TMR_IDX_15,
+	TMR_IDX_RESET_WIFI,
 	TMR_IDX_16,
 	TMR_IDX_17,
 	TMR_IDX_18,