/*********************************************************************************
 * Copyright(C),Ningbo Joyson Preh Car Connect Co.,Ltd.
 * FileName:    Power_Core.c
 * Author:      Zheng shijie
 * Version :    Ver0.1
 * Date:        2020-10-09
 * Description:
 *
 *********************************************************************************/
#include "Config/Include/APP_Config.h"
#include "Abstraction/Include/OS_Interface.h"
#include "Abstraction/OS/Kernel.h"
#include "Abstraction/Driver/GPIO/Gpio_abs.h"
#include "Driver/Wkup/Wakeup.h"
#include "Driver/Interrupt/Interrupt.h"
#include "Power_Os.h"
#include "Power_Config.h"
#include "Power_Ctrl.h"
#include "Driver/GPIO/Gpio_drv.h"
#include "Driver/IIC/LPI2C_I2cDrv.h"
#include "Driver/SPI/LPSPI_SpiDrv.h"

/***********************************global variable*********************************/
uint8_t PowerVocationFlag = NORMAL_FLAG;
uint8_t PowerMode = PM_MODE_IDEL;
uint8_t PowerStandbyOutStage = POWER_STAGE_1;
uint8_t PowerStandbyInStage = POWER_STAGE_4;
uint8_t PowerStandbyStage1SendCnt = POWER_CLEAR;
uint8_t PowerStandbyStage1WaitCnt = POWER_CLEAR;
uint8_t PowerStandbyStage2SendCnt = POWER_CLEAR;
uint8_t PowerStandbyStage2WaitCnt = POWER_CLEAR;
uint8_t	PowerStandbyStage3SendCnt = POWER_CLEAR;
uint8_t PowerStandbyStage3WaitCnt = POWER_CLEAR;
uint8_t PowerStandbyMCU = STANDBY_INIT;
uint8_t PowerStandbyNAD = STANDBY_INIT;
/***********************************tbl define*************************************/
/*Standby config*/
const st_StandbyConfigMap  PowerConfigTbl[] = {
{MSG_ID_SPISEND,	POWER_STAGE_0,EVENT_MCU_POWER_STANDBYOUT,EVENT_MCU_POWER_STANDBYIN,POWER_NULL,POWER_NULL},
{MSG_ID_SPIRECV,	POWER_STAGE_0,EVENT_MCU_POWER_STANDBYOUT,EVENT_MCU_POWER_STANDBYIN,POWER_NULL,POWER_NULL},
{MSG_ID_SPIMANAGER,	POWER_STAGE_1,EVENT_MCU_POWER_STANDBYOUT,EVENT_MCU_POWER_STANDBYIN,POWER_NULL,POWER_NULL},
{MSG_ID_CANCYC,		POWER_STAGE_1,EVENT_MCU_POWER_STANDBYOUT,EVENT_MCU_POWER_STANDBYIN,POWER_NULL,POWER_NULL},
{MSG_ID_CANAPP,		POWER_STAGE_2,EVENT_MCU_POWER_STANDBYOUT,EVENT_MCU_POWER_STANDBYIN,POWER_NULL,POWER_NULL},
{MSG_ID_UPDATE,		POWER_STAGE_2,EVENT_MCU_POWER_STANDBYOUT,EVENT_MCU_POWER_STANDBYIN,POWER_NULL,POWER_NULL},
{MSG_ID_LOG,		POWER_STAGE_3,EVENT_MCU_POWER_STANDBYOUT,EVENT_MCU_POWER_STANDBYIN,POWER_NULL,POWER_NULL},
{MSG_ID_SHARETASK,	POWER_STAGE_3,EVENT_MCU_POWER_STANDBYOUT,EVENT_MCU_POWER_STANDBYIN,POWER_NULL,POWER_NULL},
};
/*Mode config*/
const st_PowerProcess PowerEventTbl[] ={
/*Power off ->cold wakup running*/
{EVENT_MCU_COLD_WAKE_UP,				PM_MODE_IDEL,					PM_MODE_STANDBYOUT_COLD,		Power_Standbyout_Cold},
{EVENT_MCU_POWER_STANDBYOUT_STAGE1_ACK,	PM_MODE_STANDBYOUT_COLD,		PM_MODE_STANDBYOUT_COLD,		Power_Standbyout_Cold_Stage1Wait},
{EVENT_MCU_POWER_STANDBYOUT_STAGE2_ACK,	PM_MODE_STANDBYOUT_COLD,		PM_MODE_STANDBYOUT_COLD,		Power_Standbyout_Cold_Stage2Wait},
{EVENT_MCU_POWER_STANDBYOUT_STAGE3_ACK,	PM_MODE_STANDBYOUT_COLD,		PM_MODE_STANDBYOUT_COLD,		Power_Standbyout_Cold_Stage3Wait},
{EVENT_POWER_NAD_STARTUP_RES,			PM_MODE_STANDBYOUT_COLD,		PM_MODE_STANDBYOUT_COLD,		Power_Standbyout_NAD},
{EVENT_MCU_POWER_STANDBYOUT_OVER,		PM_MODE_STANDBYOUT_COLD,		PM_MODE_RUNNING,				Power_Standbyout_Over},
/*Power off ->warm wakup running*/
{EVENT_MCU_CAN_WAKE_UP,					PM_MODE_IDEL,					PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_Warm},
{EVENT_MCU_IMU_WAKE_UP,					PM_MODE_IDEL,					PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_Warm},
{EVENT_MCU_NAD_WAKE_UP,					PM_MODE_IDEL,					PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_Warm},
{EVENT_MCU_SOS_WAKE_UP,					PM_MODE_IDEL,					PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_Warm},
{EVENT_MCU_BATTERY_WAKE_UP,				PM_MODE_IDEL,					PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_Warm},
{EVENT_MCU_POWER_STANDBYOUT_STAGE1_ACK,	PM_MODE_STANDBYOUT_WARM,		PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_Warm_Stage1Wait},
{EVENT_MCU_POWER_STANDBYOUT_STAGE2_ACK,	PM_MODE_STANDBYOUT_WARM,		PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_Warm_Stage2Wait},
{EVENT_MCU_POWER_STANDBYOUT_STAGE3_ACK,	PM_MODE_STANDBYOUT_WARM,		PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_Warm_Stage3Wait},
{EVENT_POWER_NAD_STARTUP_RES,			PM_MODE_STANDBYOUT_WARM,		PM_MODE_STANDBYOUT_WARM,		Power_Standbyout_NAD},
{EVENT_MCU_POWER_STANDBYOUT_OVER,		PM_MODE_STANDBYOUT_WARM,		PM_MODE_RUNNING,				Power_Standbyout_Over},
/*Running->ECO*/
{EVENT_MCU_POWER_VEHSTATE_CHANGE,		PM_MODE_KEEP,					PM_MODE_KEEP,					Power_CheckEcoMode},
{EVENT_MCU_POWER_DRVMODE_CHANGE,		PM_MODE_KEEP,					PM_MODE_KEEP,					Power_CheckEcoMode},
{EVENT_MCU_POWER_ECO_ENTER, 			PM_MODE_RUNNING,				PM_MODE_ECO_SOC_OFF,			Power_EnterEcoMode},
{EVENT_POWER_ENTER_ECO_MODE_RES,		PM_MODE_ECO_SOC_OFF,			PM_MODE_ECO_SOC_OFF,			Power_SocOff_Set},
{EVENT_MCU_POWER_SOC_SHUTDOWN,			PM_MODE_ECO_SOC_OFF,			PM_MODE_ECO,					Power_EcoMode_Done},
/*ECO->Running*/
{EVENT_MCU_POWER_ECO_EXIT,				PM_MODE_ECO,					PM_MODE_RUNNING,				Power_ExitEcoMode},
{EVENT_POWER_EXIT_ECO_MODE_RES,			PM_MODE_RUNNING,				PM_MODE_RUNNING,				Power_EcoMode_Done},
/*ANY->Battery Running*/
{EVENT_MCU_POWER_BATTERY_ENTER_ISR,		PM_MODE_KEEP,					PM_MODE_BATTERY_SOC_OFF,		Power_Enter_Battery},
{EVENT_POWER_ENTER_BATTERY_MODE_RES,	PM_MODE_BATTERY_SOC_OFF,		PM_MODE_BATTERY_SOC_OFF,		Power_SocOff_Set},
{EVENT_MCU_POWER_SOC_SHUTDOWN,			PM_MODE_BATTERY_SOC_OFF,		PM_MODE_BATTERY,				Power_Battery_Done},
{EVENT_MCU_POWER_ECALL_OFF,				PM_MODE_BATTERY,				PM_MODE_BATTERY,				Power_OffCall_Set},
/*Battery Running->Running*/
{EVENT_MCU_POWER_BATTERY_EXIT_ISR,		PM_MODE_BATTERY,				PM_MODE_RUNNING,				Power_Exit_Battery},
{EVENT_POWER_EXIT_BATTERY_MODE_RES,		PM_MODE_RUNNING,				PM_MODE_RUNNING,				Power_Battery_Done},
/*Running/Eco->Voltage abnormal*/
{EVENT_MCU_POWER_LOW_VOLTAGE,			PM_MODE_RUNNING,				PM_MODE_VOLTAGE_ABNORMAL_WAIT,	Power_Voltage_Abnormal},
{EVENT_MCU_POWER_LOW_VOLTAGE,			PM_MODE_ECO,					PM_MODE_VOLTAGE_ABNORMAL_WAIT,	Power_Voltage_Abnormal},
{EVENT_MCU_POWER_HIGH_VOLTAGE,			PM_MODE_RUNNING,				PM_MODE_VOLTAGE_ABNORMAL_WAIT,	Power_Voltage_Abnormal},
{EVENT_MCU_POWER_HIGH_VOLTAGE,			PM_MODE_ECO,					PM_MODE_VOLTAGE_ABNORMAL_WAIT,	Power_Voltage_Abnormal},
{EVENT_POWER_NAD_SLEEP_PENDING,			PM_MODE_VOLTAGE_ABNORMAL_WAIT,	PM_MODE_VOLTAGE_ABNORMAL_WAIT,	Power_NadSleep_Pending},
{EVENT_POWER_SOC_OFF_NAD_SLEEP_RES,		PM_MODE_VOLTAGE_ABNORMAL_WAIT,	PM_MODE_VOLTAGE_ABNORMAL_WAIT,	Power_NadSleep_SocOff_Set},
{EVENT_MCU_POWER_NAD_SLEEP_SOC_OFF,		PM_MODE_VOLTAGE_ABNORMAL_WAIT,	PM_MODE_VOLTAGE_ABNORMAL,		Power_NadSleep_SocOff_Done},
{EVENT_MCU_POWER_NAD_SOC_TIMEOUT,		PM_MODE_VOLTAGE_ABNORMAL_WAIT,	PM_MODE_VOLTAGE_ABNORMAL,		Power_NadSleep_SocOff_Done},
/*Voltage abnormal->Running/Eco*/
{EVENT_MCU_POWER_NORMAL_VOLAGE,			PM_MODE_VOLTAGE_ABNORMAL,		PM_MODE_RUNNING,				Power_Exit_Voltage_Abnormal},
{EVENT_POWER_NAD_STARTUP_RES,			PM_MODE_RUNNING,				PM_MODE_RUNNING,				Power_Exit_Voltage_Abnormal_Done},
/*x-Running->Listening*/
{EVENT_MCU_READY_TO_SLEEP,				PM_MODE_BATTERY,				PM_MODE_STANDBYIN,				Power_StandbyInStart},
{EVENT_POWER_NAD_SLEEP_TIMER_EXPIRED,	PM_MODE_BATTERY,				PM_MODE_STANDBYIN,				Power_StandbyInStart},
{EVENT_MCU_READY_TO_SLEEP,				PM_MODE_RUNNING,				PM_MODE_STANDBYIN,				Power_StandbyInStart},
{EVENT_MCU_READY_TO_SLEEP,				PM_MODE_VOLTAGE_ABNORMAL,		PM_MODE_STANDBYIN,				Power_StandbyInStart},
{EVENT_POWER_NAD_SLEEP_PENDING,			PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_NadSleep_Pending},
{EVENT_POWER_SOC_OFF_NAD_SLEEP_RES,		PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_NadSleep_SocOff_Set},
{EVENT_POWER_NAD_POWEROFF_PENDING,		PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_NadOff_Pending},
{EVENT_MCU_POWER_NAD_SLEEP_SOC_OFF,		PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_Standbyin_Stage4Wait},
{EVENT_MCU_POWER_NAD_SOC_TIMEOUT,		PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_Standbyin_Stage4Wait},
{EVENT_MCU_POWER_STANDBYIN_STAGE3_ACK,	PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_Standbyin_Stage3Wait},
{EVENT_MCU_POWER_STANDBYIN_STAGE2_ACK,	PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_Standbyin_Stage2Wait},
{EVENT_MCU_POWER_STANDBYIN_STAGE1_ACK,	PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_Standbyin_Stage1Wait},
{EVENT_MCU_POWER_STANDBYIN_OVER,		PM_MODE_STANDBYIN,				PM_MODE_STANDBYIN,				Power_Standbyin_Over},
/*Sleep break*/
{EVENT_MCU_SLEEP_BREAK,					PM_MODE_STANDBYIN,				PM_MODE_STANDBYOUT_WARM,		Power_StandbyIn_Break},
};
/*********************************************************************************
  * Function:	   Power_MainProcess
  * Description:
  * Input:		event
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
 **********************************************************************************/
void Power_MainProcess(uint16_t event_code)
{
	uint32_t ret = 0xFF;
	uint8_t wakeupsrc = 0x0;
	if(event_code == EVENT_POWER_NAD_STARTUP_RES)
	{
		ret = OS_sndMsg(MSG_ID_SPIMANAGER,EVENT_POWER_WAKEUP_SOURCE,&wakeupsrc,1,OS_WAIT_INFINITE);
	}
	else if(event_code == EVENT_MCU_POWER_BATTERY_ENTER_ISR)
	{
		ret = OS_sndMsg(MSG_ID_BACKUPBATTERY,EVENT_MCU_POWER_BATTERY_ENTER_ISR,NULL,0,OS_WAIT_INFINITE);
	}
	else if(event_code == EVENT_MCU_POWER_BATTERY_EXIT_ISR)
	{
		ret = OS_sndMsg(MSG_ID_BACKUPBATTERY,EVENT_MCU_POWER_BATTERY_EXIT_ISR,NULL,0,OS_WAIT_INFINITE);
	}
	else if(event_code == EVENT_MCU_SOS_WAKE_UP)
	{
		Power_WarmSequenceOn_WithSoc();
		ret = OS_sndMsg(MSG_ID_BACKUPBATTERY,EVENT_MCU_POWER_STANDBYOUT,NULL,0,OS_WAIT_INFINITE);
		OS_startTimer(TIMER_ID_ALL_SLEEP_TEST);
	}
	else if(event_code == EVENT_MCU_COLD_WAKE_UP)
	{
		PowerMode = PM_MODE_IDEL;
		Power_ColdSequenceOn_WithSoc();
		ret = OS_sndMsg(MSG_ID_BACKUPBATTERY,EVENT_MCU_POWER_STANDBYOUT,NULL,0,OS_WAIT_INFINITE);
		//OS_startTimer(TIMER_ID_ALL_SLEEP_TEST);
		//OS_sleep(50);
		//BackupBatteryActive();
#if 0
		//PIN143	PTA9:PWR_5V0_EN
		//GPIO_Port_Lev_Set(PTA,PIN_INDEX_9,PORT_LOW);
		//PIN2		PTE15:PWR_SYS_EN
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_15,PORT_LOW);
		//PIN120	PTA15:PWR_8V2_EN
		GPIO_Port_Lev_Set(PTA,PIN_INDEX_15,PORT_LOW);
		//PIN119	PTA16:PWR_6V8_EN
		GPIO_Port_Lev_Set(PTA,PIN_INDEX_16,PORT_LOW);
		//PIN41		PTB4:7702_5V0_EN
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_4,PORT_LOW);
		//PIN38		PTB21:PWR_V2X_EN
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_21,PORT_LOW);
		//PIN36		PTB18:PWR_3V8_EN
		//GPIO_Port_Lev_Set(PTB,PIN_INDEX_18,PORT_LOW);
		//PIN144	PTA8:PWR_3V3_EN
		GPIO_Port_Lev_Set(PTA,PIN_INDEX_8,PORT_LOW);
		//PIN1		PTE16:PWR_1V8_EN
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_16,PORT_LOW);
		//PIN37		MCU_AG550_ONOFF,KEEP
		//PIN30		MCU_AG550_RESET,KEEP
		//PIN107	PTB11:AudioIO_1V8_EN
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_11,PORT_LOW);
		//PIN108	PTB10:AudioANA_3V3_EN
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_10,PORT_LOW);
		//PIN109	PTB9:AudioD_1V8_EN
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_9,PORT_LOW);
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_11,PORT_LOW);
		/*disable int*/
		INT_SYS_DisableIRQGlobal();
		/* unlock watchdog */
		WDOG->CNT = 0xD928C520;
	    (void)WDOG->CNT;
		/* disable watchdog */
		WDOG->CS &= ~WDOG_CS_EN_MASK;
		INT_SYS_EnableIRQGlobal();
		ret = POWER_SYS_SetMode(0, POWER_MANAGER_POLICY_AGREEMENT);
		/*Re Initialize and configure clocks*/
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_11,PORT_HIGH);
	    CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
	    CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_COLD_WAKE_UP,NULL,0,OS_WAIT_INFINITE);
#endif
	}
	else if(event_code == EVENT_MCU_SOS_WAKE_UP)
	{
		ret = 0xEE;
	}
#if 0
	uint8_t index =0;
	uint8_t tblsize =0;
	tblsize = sizeof(PowerEventTbl)/sizeof(st_PowerProcess);

	for(index = 0;index<tblsize;index++)
	{
		if((event_code == PowerEventTbl[index].EventCode)&&(PM_MODE_KEEP == PowerEventTbl[index].currentmode))
		{
			if(PM_MODE_KEEP != PowerEventTbl[index].currentmode)
			{
				/*Update next power mode*/
				Power_SetMode(PowerEventTbl[index].nextmode);
			}
			/*Function Process*/
			if(PowerEventTbl[index].Process != NULL)
			{
				PowerEventTbl[index].Process();
			}
			break;
		}
		if((event_code == PowerEventTbl[index].EventCode)&&(Power_GetMode() == PowerEventTbl[index].currentmode))
		{
			if(PM_MODE_KEEP != PowerEventTbl[index].currentmode)
			{
				/*Update next power mode*/
				Power_SetMode(PowerEventTbl[index].nextmode);
			}
			/*Function Process*/
			if(PowerEventTbl[index].Process != NULL)
			{
				PowerEventTbl[index].Process();
			}
			break;
		}
	}
#endif
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage1
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage1(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;

	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);
	/*Hardware Seqence On with Soc*/
	Power_ColdSequenceOn_WithSoc();
	/*Hardware Seqence On without Soc*/
	//Power_ColdSequenceOn_NoSoc();

	#if 0
	/*Stage 1 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyOutStage == POWER_STAGE_1)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyOutEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage1SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	OS_startTimer(TIMER_ID_POWER_STANDBYOUT);
	#endif
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage1
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage1Wait(void)
{
	PowerStandbyStage1WaitCnt++;
	if(PowerStandbyStage1WaitCnt == PowerStandbyStage1SendCnt)
	{
		/*Stage1 standbyout over,stop stage1 timeout timer*/
		OS_stopTimer(TIMER_ID_POWER_STANDBYOUT);
		/*Next Stage*/
		PowerStandbyOutStage = POWER_STAGE_2;
		Power_Standbyout_Cold();
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage1Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage1Timeout(void)
{
	/*Stage1 timeout,Don't wait anymore*/
	PowerStandbyOutStage = POWER_STAGE_2;
	Power_Standbyout_Cold();
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage2
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage2(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;
	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);

	/*Stage 2 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyOutStage == POWER_STAGE_2)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyOutEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage2SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	OS_resetTimer(TIMER_ID_POWER_STANDBYOUT);
	OS_changeTimer(TIMER_ID_POWER_STANDBYOUT,3000,Power_Standbyout_Cold_Stage2Timeout);
	OS_startTimer(TIMER_ID_POWER_STANDBYOUT);
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage2Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage2Wait(void)
{
	PowerStandbyStage2WaitCnt++;
	if(PowerStandbyStage2WaitCnt == PowerStandbyStage2SendCnt)
	{
		/*Stage2 standbyout over,stop stage2 timeout timer*/
		OS_stopTimer(TIMER_ID_POWER_STANDBYOUT);
		/*Next Stage*/
		PowerStandbyOutStage = POWER_STAGE_3;
		Power_Standbyout_Cold();
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage2Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage2Timeout(void)
{
	/*Stage2 timeout,Don't wait anymore*/
	PowerStandbyOutStage = POWER_STAGE_3;
	Power_Standbyout_Cold();
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage3
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage3(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;
	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);

	/*Stage 3 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyOutStage == POWER_STAGE_3)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyOutEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage3SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	/*Wait NAD StartUp Response*/
	OS_resetTimer(TIMER_ID_POWER_STANDBYOUT);
	OS_changeTimer(TIMER_ID_POWER_STANDBYOUT,3000,Power_Standbyout_Cold_Stage3Timeout);
	OS_startTimer(TIMER_ID_POWER_STANDBYOUT);
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage3Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage3Wait(void)
{
	PowerStandbyStage3WaitCnt++;
	if(PowerStandbyStage3WaitCnt == PowerStandbyStage3SendCnt)
	{
		/*Stage3 standbyout over,stop stage3 timeout timer*/
		OS_stopTimer(TIMER_ID_POWER_STANDBYOUT);
		/*Next Stage*/
		PowerStandbyOutStage = POWER_STAGE_4;
		Power_Standbyout_Cold();
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage3Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage3Timeout(void)
{
	/*Stage3 timeout,Don't wait anymore*/
	PowerStandbyOutStage = POWER_STAGE_4;
	Power_Standbyout_Cold();
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage4
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage4(void)
{
	/*Wait NAD StartUp Response*/
	OS_resetTimer(TIMER_ID_POWER_STANDBYOUT);
	OS_changeTimer(TIMER_ID_POWER_STANDBYOUT,100000,Power_Standbyout_Cold_Stage4Timeout);
	OS_startTimer(TIMER_ID_POWER_STANDBYOUT);
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold_Stage4Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold_Stage4Timeout(void)
{
	/*Stage3 timeout,Ready Reboot NAD&SOC*/
	PowerStandbyNAD = STANDBYOUT_TIMEOUT;
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Cold
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Cold(void)
{
	switch(PowerStandbyOutStage)
	{
		case POWER_STAGE_1:
			Power_Standbyout_Cold_Stage1();
			break;
		case POWER_STAGE_2:
			Power_Standbyout_Cold_Stage2();
			break;
		case POWER_STAGE_3:
			Power_Standbyout_Cold_Stage3();
			break;
		case POWER_STAGE_4:
			Power_Standbyout_Cold_Stage4();
			break;
		default:
			break;
	}
}

/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage1
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage1(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;

	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);
	/*Hardware Seqence On with Soc*/
	Power_WarmSequenceOn_WithSoc();
	/*Hardware Seqence On without Soc*/
	//Power_ColdSequenceOn_NoSoc();

	/*Stage 1 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyOutStage == POWER_STAGE_1)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyOutEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage1SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	OS_startTimer(TIMER_ID_POWER_STANDBYOUT);
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage1Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage1Wait(void)
{
	PowerStandbyStage1WaitCnt++;
	if(PowerStandbyStage1WaitCnt == PowerStandbyStage1SendCnt)
	{
		/*Stage1 standbyout over,stop stage1 timeout timer*/
		OS_stopTimer(TIMER_ID_POWER_STANDBYOUT);
		/*Next Stage*/
		PowerStandbyOutStage = POWER_STAGE_2;
		Power_Standbyout_Warm();
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage1Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage1Timeout(void)
{
	/*Stage1 timeout,Don't wait anymore*/
	PowerStandbyOutStage = POWER_STAGE_2;
	Power_Standbyout_Warm();
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage2
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage2(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;
	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);

	/*Stage 2 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyOutStage == POWER_STAGE_2)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyOutEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage2SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	OS_resetTimer(TIMER_ID_POWER_STANDBYOUT);
	OS_changeTimer(TIMER_ID_POWER_STANDBYOUT,3000,Power_Standbyout_Warm_Stage2Timeout);
	OS_startTimer(TIMER_ID_POWER_STANDBYOUT);
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage2Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage2Wait(void)
{
	PowerStandbyStage2WaitCnt++;
	if(PowerStandbyStage2WaitCnt == PowerStandbyStage2SendCnt)
	{
		/*Stage2 standbyout over,stop stage2 timeout timer*/
		OS_stopTimer(TIMER_ID_POWER_STANDBYOUT);
		/*Next Stage*/
		PowerStandbyOutStage = POWER_STAGE_3;
		Power_Standbyout_Warm();
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage2Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage2Timeout(void)
{
	/*Stage2 timeout,Don't wait anymore*/
	PowerStandbyOutStage = POWER_STAGE_3;
	Power_Standbyout_Warm();
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage3
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage3(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;
	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);

	/*Stage 3 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyOutStage == POWER_STAGE_3)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyOutEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage3SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	/*Wait NAD StartUp Response*/
	OS_resetTimer(TIMER_ID_POWER_STANDBYOUT);
	OS_changeTimer(TIMER_ID_POWER_STANDBYOUT,3000,Power_Standbyout_Warm_Stage3Timeout);
	OS_startTimer(TIMER_ID_POWER_STANDBYOUT);
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage3Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage3Wait(void)
{
	PowerStandbyStage3WaitCnt++;
	if(PowerStandbyStage3WaitCnt == PowerStandbyStage3SendCnt)
	{
		/*Stage3 standbyout over,stop stage3 timeout timer*/
		OS_stopTimer(TIMER_ID_POWER_STANDBYOUT);
		/*Next Stage*/
		PowerStandbyOutStage = POWER_STAGE_4;
		Power_Standbyout_Warm();
		if(PowerStandbyNAD == STANDBYOUT_OVER)
		{
			/*SA Standbyout all over*/
			OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_STANDBYOUT_OVER,NULL,0,OS_WAIT_INFINITE);
		}
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage3Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage3Timeout(void)
{
	/*Stage3 timeout,Ready Reboot NAD&SOC*/
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm_Stage4
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm_Stage4(void)
{
	;
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Warm
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Warm(void)
{
	switch(PowerStandbyOutStage)
	{
		case POWER_STAGE_1:
			Power_Standbyout_Warm_Stage1();
			break;
		case POWER_STAGE_2:
			Power_Standbyout_Warm_Stage2();
			break;
		case POWER_STAGE_3:
			Power_Standbyout_Warm_Stage3();
			break;
		case POWER_STAGE_4:
			Power_Standbyout_Warm_Stage4();
			break;
		default:
			break;
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyout_NAD
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_NAD(void)
{
	PowerStandbyNAD = STANDBYOUT_OVER;
	if(PowerStandbyMCU == STANDBYOUT_OVER)
	{
		/*SA Standbyout all over*/
		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_STANDBYOUT_OVER,NULL,0,OS_WAIT_INFINITE);

	}
}
/*********************************************************************************
  * Function:	   Power_Standbyout_Over
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyout_Over(void)
{
	PowerStandbyStage1SendCnt = POWER_CLEAR;
	PowerStandbyStage1WaitCnt = POWER_CLEAR;
	PowerStandbyStage2SendCnt = POWER_CLEAR;
	PowerStandbyStage2WaitCnt = POWER_CLEAR;
	PowerStandbyStage3SendCnt = POWER_CLEAR;
	PowerStandbyStage3WaitCnt = POWER_CLEAR;
	//After Running Over,ReCheck next condition
	/*Check Main Voltage*/
	/*Check ECO Mode*/
	/*Check BackupBattery*/
}

/*********************************************************************************
  * Function:	   Power_CheckEcoMode
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_CheckEcoMode(void)
{
	uint8_t power_vehstate;
	uint8_t power_drvmode;
	static uint8_t LastEcoMode = ECO_MODE_EXIT;
	static uint8_t NewEcoMode = ECO_MODE_EXIT;

	power_vehstate = 0;//get data from canapp
	power_drvmode = 0;//get data from canapp

	//if(((power_vehstate == DRV_PRESENT)||(power_vehstate == DRVING))&&(power_drvmode == ECO))
	{
		NewEcoMode = ECO_MODE_ENTER;
	}
	//else
	{
		NewEcoMode = ECO_MODE_EXIT;
	}

	if((LastEcoMode == ECO_MODE_EXIT)&&(NewEcoMode == ECO_MODE_ENTER))
	{
		//ECO MODE IN
		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_ECO_ENTER,NULL,0,OS_WAIT_INFINITE);
	}
	else if((LastEcoMode == ECO_MODE_ENTER)&&(NewEcoMode == ECO_MODE_EXIT))
	{
		//ECO MODE OUT
		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_ECO_EXIT,NULL,0,OS_WAIT_INFINITE);
	}
	else
	{
		;//keep
	}
	/*update last eco mode after mode check*/
	LastEcoMode = NewEcoMode;
}
/*********************************************************************************
  * Function:	   Power_EnterEcoMode
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_EnterEcoMode(void)
{
	/*Send Message to NAD*/
	//EVENT_POWER_ENTER_ECO_MODE_REQ
}
/*********************************************************************************
  * Function:	   Power_SocOff_Set
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_SocOff_Set(void)
{
	OS_resetTimer(TIMER_ID_SOC_OFF);
	OS_startTimer(TIMER_ID_SOC_OFF);
}
/*********************************************************************************
  * Function:	   Power_SocOff_Check
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_SocOff_Check(void)
{
	uint8_t SocPort;
	static uint8_t SocPortCheckCnt = 0;

	/*Max Check time is 1000ms*10cnt*/
	SocPortCheckCnt++;
	/*PTE13: High(SOC Standby),Low(SOC Working)*/
	SocPort = GPIO_INPUT_Port_Lev_Read(PTE,PIN_INDEX_13);
	/*Soc has been shutdown or timeout*/
	if((SocPort == PORT_HIGH)||(SocPortCheckCnt >=10))
	{
		OS_stopTimer(TIMER_ID_SOC_OFF);
		//PIN2 PTE15:PWR_SYS_EN,SOC Power off
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_15,PORT_LOW);
		/*SOC Power off*/
		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_SOC_SHUTDOWN,NULL,0,OS_WAIT_INFINITE);
		SocPortCheckCnt = 0;
	}
	else
	{
		;/*print SocPort status*/
	}
}
/*********************************************************************************
  * Function:	   Power_ExitEcoMode
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_ExitEcoMode(void)
{
	uint8_t SocPort;

	/*Send Message to NAD*/
	//EVENT_POWER_EXIT_ECO_MODE_REQ
	OS_stopTimer(TIMER_ID_SOC_OFF);
	/*PTE13: High(SOC Standby),Low(SOC Working)*/
	SocPort = GPIO_INPUT_Port_Lev_Read(PTE,PIN_INDEX_13);
	/*Check Soc Port,if not working,Soc Power ON*/
	if(SocPort != PORT_LOW)
	{
		//PIN2 PTE15:PWR_SYS_EN,SOC Power off
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_15,PORT_HIGH);
	}
}
/*********************************************************************************
  * Function:	   Power_EcoMode_Done
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_EcoMode_Done(void)
{
	/*ReCheck ECO Signal again*/
	Power_CheckEcoMode();
}
/*********************************************************************************
  * Function:	   Power_Enter_Battery
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Enter_Battery(void)
{
	/*Send Message to NAD*/
	//EVENT_POWER_ENTER_BATTERY_MODE_REQ
	/*Send Message to MCU Task*/
}
/*********************************************************************************
  * Function:	   Power_OffCall_Set
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_OffCall_Set(void)
{
	//CAN IC OFF

	//EVENT_MCU_READY_TO_SLEEP
}
/*********************************************************************************
  * Function:	   Power_Exit_Battery
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Exit_Battery(void)
{
	uint8_t SocPort;
	/*Send Message to NAD*/
	//EVENT_POWER_EXIT_BATTERY_MODE_REQ
	/*PTE13: High(SOC Standby),Low(SOC Working)*/
	SocPort = GPIO_INPUT_Port_Lev_Read(PTE,PIN_INDEX_13);
	/*Check Soc Port,if not working,Soc Power ON*/
	if(SocPort != PORT_LOW)
	{
		//PIN2 PTE15:PWR_SYS_EN,SOC Power on
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_15,PORT_HIGH);
	}
}
/*********************************************************************************
  * Function:	   Power_Battery_Done
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Battery_Done(void)
{
	/*Check Battery status again*/
	/*Check Main Battery and ECO Status*/
}

/*********************************************************************************
  * Function:	   Power_Voltage_Abnormal
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Voltage_Abnormal(void)
{
	/*Send Message to NAD,with buffer*/
	//EVENT_POWER_SOC_OFF_NAD_SLEEP_REQ   option:0x00 all off
	OS_startTimer(TIMER_ID_NAD_SLEEP_PENDING);
}
/*********************************************************************************
  * Function:	   Power_NadSleep_Pending
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_NadSleep_Pending(void)
{
	OS_resetTimer(TIMER_ID_NAD_SLEEP_PENDING);
	OS_startTimer(TIMER_ID_NAD_SLEEP_PENDING);
}
/*********************************************************************************
  * Function:	   Power_NadSleep_Pendingtimeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_NadSleep_Pendingtimeout(void)
{
	//PIN23 PTE12:MCU_AG550_WAKE Port Low
	GPIO_Port_Lev_Set(PTE,PIN_INDEX_12,PORT_LOW);
	OS_resetTimer(TIMER_ID_NAD_SLEEP);
	OS_startTimer(TIMER_ID_NAD_SLEEP);
}
/*********************************************************************************
  * Function:	   Power_NadSleep_SocOff_Set
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_NadSleep_SocOff_Set(void)
{
	/*REQ NAD Sleep*/
	//PIN23 PTE12:MCU_AG550_WAKE Port Low
	GPIO_Port_Lev_Set(PTE,PIN_INDEX_12,PORT_LOW);
	/*Wait NAD Sleep,Start Timer*/
	OS_resetTimer(TIMER_ID_NAD_SLEEP);
	OS_startTimer(TIMER_ID_NAD_SLEEP);
}
/*********************************************************************************
  * Function:	   Power_NadSleep_Check
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_NadSleep_Check(void)
{
	uint8_t SocPort;
	uint8_t NadPort;
	static uint8_t CheckCnt = 0;

	/*Max Check time is 1000ms*10cnt*/
	CheckCnt++;
	/*PTE13: High(SOC Standby),Low(SOC Working)*/
	SocPort = GPIO_INPUT_Port_Lev_Read(PTE,PIN_INDEX_13);
	/*PTD17: High(NAD Sleep),Low(NAD Working)*/
	NadPort = GPIO_INPUT_Port_Lev_Read(PTD,PIN_INDEX_17);
	/*Soc has been shutdown and Nad has been sleep*/
	if((SocPort == PORT_HIGH)&&(NadPort == PORT_HIGH))
	{
		OS_stopTimer(TIMER_ID_NAD_SLEEP);
		//PIN2 PTE15:PWR_SYS_EN,SOC Power off
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_15,PORT_LOW);
		/*SOC Power off*/
		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_NAD_SLEEP_SOC_OFF,NULL,0,OS_WAIT_INFINITE);
		CheckCnt = 0;
	}
	/*Timeout,Soc Force Power off,Nad Force Power off*/
	if(CheckCnt >=10)
	{
		OS_stopTimer(TIMER_ID_NAD_SLEEP);
		//PIN2 PTE15:PWR_SYS_EN,SOC Power off
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_15,PORT_LOW);
		//PIN36 PTB18:PWR_3V8_EN,NAD Power off
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_18,PORT_LOW);

		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_NAD_SOC_TIMEOUT,NULL,0,OS_WAIT_INFINITE);
		CheckCnt = 0;
	}
}
/*********************************************************************************
  * Function:	   Power_NadSleep_SocOff_Done
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_NadSleep_SocOff_Done(void)
{
	/*ReCheck Current Voltage Mode again*/
	//Get from SensorApp
}
/*********************************************************************************
  * Function:	   Power_Exit_Voltage_Abnormal
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Exit_Voltage_Abnormal(void)
{
	/*Check NAD Power on/off*/
	uint8_t NadStatusPort;

	//PIN2 PTE15:PWR_SYS_EN,SOC Power on
	GPIO_Port_Lev_Set(PTE,PIN_INDEX_15,PORT_HIGH);

	/*PTB5: High(550 OFF),Low(550 ON)*/
	NadStatusPort = GPIO_INPUT_Port_Lev_Read(PTB,PIN_INDEX_5);
	if(NadStatusPort == PORT_HIGH)
	{
		//PIN36 PTB18:PWR_3V8_EN,NAD Power on
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_18,PORT_HIGH);
		//PIN30		PTE9:MCU_AG550_RESET
		GPIO_Port_Lev_Set(PTE,PIN_INDEX_9,PORT_LOW);
		//PIN37		PTB20:MCU_AG550_ON
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_20,PORT_LOW);
		OS_sleep(30);
		//PIN37		PTB20:MCU_AG550_OFF
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_20,PORT_HIGH);
		OS_sleep(500);
		//PIN37		PTB20:MCU_AG550_ON
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_20,PORT_LOW);
	}
	else
	{
		/*NAD Wakeup*/
		//PIN23 PTB12:MCU_AG550_WAKE,High(wakeup),Low(sleep)
		GPIO_Port_Lev_Set(PTB,PIN_INDEX_12,PORT_HIGH);
		/*Ready Check NAD Wakeup Status after 3s*/
	}
}
/*********************************************************************************
  * Function:	   Power_Exit_Voltage_Abnormal_Done
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Exit_Voltage_Abnormal_Done(void)
{
	//EVENT_POWER_WAKEUP_SOURCE
	/*ReCheck Current Voltage Mode again*/
	//Get from SensorApp
}

/*********************************************************************************
  * Function:	   Power_StandbyInStart
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_StandbyInStart(void)
{
	PowerStandbyInStage = POWER_STAGE_4;
	Power_StandbyInProcess();
}
/*********************************************************************************
  * Function:	   Power_StandbyInProcess
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_StandbyInProcess(void)
{
	switch(PowerStandbyInStage)
	{
		case POWER_STAGE_1:
			Power_Standbyin_Stage1();
			break;
		case POWER_STAGE_2:
			Power_Standbyin_Stage2();
			break;
		case POWER_STAGE_3:
			Power_Standbyin_Stage3();
			break;
		case POWER_STAGE_4:
			Power_Standbyin_Stage4();
			break;
		default:
			break;
	}
}
/*********************************************************************************
  * Function:	   Power_StandbyInProcess
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage1(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;
	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);

	/*Stage 3 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyInStage == POWER_STAGE_1)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyInEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage1SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	/*Start Stage3 wait timer*/
	OS_resetTimer(TIMER_ID_POWER_STANDBYIN);
	OS_changeTimer(TIMER_ID_POWER_STANDBYIN,3000,Power_Standbyin_Stage1Timeout);
	OS_startTimer(TIMER_ID_POWER_STANDBYIN);
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage1Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage1Wait(void)
{
	PowerStandbyStage1WaitCnt++;
	if(PowerStandbyStage1WaitCnt == PowerStandbyStage1SendCnt)
	{
		OS_stopTimer(TIMER_ID_POWER_STANDBYIN);
		/*Standbyin Over*/
		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_STANDBYIN_OVER,NULL,0,OS_WAIT_INFINITE);

	}
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage1Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage1Timeout(void)
{
	OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_POWER_STANDBYIN_OVER,NULL,0,OS_WAIT_INFINITE);
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage2
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage2(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;
	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);

	/*Stage 3 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyInStage == POWER_STAGE_2)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyInEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage2SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	/*Start Stage3 wait timer*/
	OS_resetTimer(TIMER_ID_POWER_STANDBYIN);
	OS_changeTimer(TIMER_ID_POWER_STANDBYIN,3000,Power_Standbyin_Stage2Timeout);
	OS_startTimer(TIMER_ID_POWER_STANDBYIN);
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage2Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage2Wait(void)
{
	PowerStandbyStage2WaitCnt++;
	if(PowerStandbyStage2WaitCnt == PowerStandbyStage2SendCnt)
	{
		OS_stopTimer(TIMER_ID_POWER_STANDBYIN);
		/*Next Stage*/
		PowerStandbyInStage = POWER_STAGE_1;
		Power_StandbyInProcess();
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage2Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage2Timeout(void)
{
	/*Next Stage*/
	PowerStandbyInStage = POWER_STAGE_1;
	Power_StandbyInProcess();
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage3
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage3(void)
{
	uint8_t index = 0;
	uint8_t tblsize =0;
	uint32_t ret;
	tblsize = sizeof(PowerConfigTbl)/sizeof(st_StandbyConfigMap);

	/*Stage 3 Standbyout*/
	for(index = 0;index<tblsize;index++)
	{
		if(PowerStandbyInStage == POWER_STAGE_3)
		{
			/*Send Standbyout Event to Other Module*/
			ret = OS_sndMsg(PowerConfigTbl[index].ID,PowerConfigTbl[index].StandbyInEvent,NULL,0,OS_WAIT_INFINITE);
			/*Event Send Success,Send cnt add*/
			if(ret == 0x00)
			{
				PowerStandbyStage3SendCnt++;
			}
		}
		else
		{
			break;
		}
	}
	/*Start Stage3 wait timer*/
	OS_resetTimer(TIMER_ID_POWER_STANDBYIN);
	OS_changeTimer(TIMER_ID_POWER_STANDBYIN,3000,Power_Standbyin_Stage3Timeout);
	OS_startTimer(TIMER_ID_POWER_STANDBYIN);
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage3Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage3Wait(void)
{
	PowerStandbyStage3WaitCnt++;
	if(PowerStandbyStage3WaitCnt == PowerStandbyStage3SendCnt)
	{
		OS_stopTimer(TIMER_ID_POWER_STANDBYIN);
		/*Next Stage*/
		PowerStandbyInStage = POWER_STAGE_2;
		Power_StandbyInProcess();
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage3Timeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage3Timeout(void)
{
	/*Next Stage*/
	PowerStandbyInStage = POWER_STAGE_2;
	Power_StandbyInProcess();
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage4
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage4(void)
{
	/*Check Vocation listen or not,Read Flag*/
	if(PowerVocationFlag == 1)
	{
		/*Send Request to NAD,NAD Off,Soc Off*/
		//EVENT_POWER_SOC_OFF_NAD_OFF_REQ
		/*Start NAD OFF Pending Timer*/
		OS_resetTimer(TIMER_ID_NAD_OFF_PENDING);
		OS_startTimer(TIMER_ID_NAD_OFF_PENDING);
	}
	else
	{
		/*Send Request to NAD,NAD Sleep,Soc Off*/
		if(Power_GetMode() == PM_MODE_BATTERY)
		{
			//EVENT_MCU_POWER_NAD_SLEEP_SOC_OFF option:01
		}
		else
		{
			//EVENT_MCU_POWER_NAD_SLEEP_SOC_OFF option:02
		}
		OS_resetTimer(TIMER_ID_NAD_SLEEP_PENDING);
		OS_startTimer(TIMER_ID_NAD_SLEEP_PENDING);
	}
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Stage4Wait
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Stage4Wait(void)
{
	PowerStandbyInStage = POWER_STAGE_3;
	Power_StandbyInProcess();
}
/*********************************************************************************
  * Function:	   Power_NadOff_Pending
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_NadOff_Pending(void)
{
	;
}
/*********************************************************************************
  * Function:	   Power_NadOff_Pendingtimeout
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_NadOff_Pendingtimeout(void)
{
	;
}
/*********************************************************************************
  * Function:	   Power_Standbyin_Over
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_Standbyin_Over(void)
{
	uint8_t CanInhStatus;
	uint8_t SosStatus;
	uint8_t Ag550Status;

	/*Check CAN INH,ECALL,NAD Again*/
	CanInhStatus = GPIO_INPUT_Port_Lev_Read(PTB,PIN_INDEX_16);
	SosStatus = GPIO_INPUT_Port_Lev_Read(PTC,PIN_INDEX_9);
	Ag550Status = GPIO_INPUT_Port_Lev_Read(PTD,PIN_INDEX_14);
	/*Check Gpio Status,Ready to Sleep if all condition meet*/
	if((CanInhStatus != PORT_LOW)||(SosStatus != PORT_HIGH)||(Ag550Status != PORT_HIGH))
	{
		OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_SLEEP_BREAK,NULL,0,OS_WAIT_INFINITE);
	}
	else
	{
		/*all port standbyin*/
		/*wake up int enable*/
		/*stop mode enter*/
	}
}
/*********************************************************************************
  * Function:	   Power_StandbyIn_Break
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
**********************************************************************************/
void Power_StandbyIn_Break(void)
{
	uint8_t Batteryport;
	uint8_t Nadport;

	/*High:idel,Low:battery working*/
	Batteryport = GPIO_INPUT_Port_Lev_Read(PTA,PIN_INDEX_14);
	/*High:Sleep,Low:550 working*/
	Nadport = GPIO_INPUT_Port_Lev_Read(PTD,PIN_INDEX_17);

	/*Check voltage supply,Main battery working*/
	if(Batteryport == PORT_HIGH)
	{
		/*Check Vocation listen or not,Read Flag*/
		if(PowerVocationFlag == 1)
		{
			/*NAD Power On*/
		}
		else
		{
			/*Check if NAD is Sleep,SOC is Poweroff*/
			if(Nadport == PORT_HIGH)
			{
				/*wake up NAD*/
			}
		}
	}
	/*Backup battery working*/
	else
	{
		/*Check if NAD is Sleep,SOC is Poweroff*/
		if(Nadport == PORT_HIGH)
		{
			/*wake up NAD*/
		}
	}
	/*Check if MCU Task has been send standbyin*/
	if(PowerStandbyInStage == POWER_STAGE_3)
	{
		/*Re-StandbyOut*/
		Power_Standbyout_Warm_Stage3();
	}
	else if(PowerStandbyInStage == POWER_STAGE_2)
	{
		/*Re-StandbyOut*/
		Power_Standbyout_Warm_Stage2();
	}
	else if(PowerStandbyInStage == POWER_STAGE_1)
	{
		/*Re-StandbyOut*/
		Power_Standbyout_Warm_Stage1();
	}
}

/*********************************************************************************
  * Function:	   Power_SetMode
  * Description:
  * Input:		mode
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
 **********************************************************************************/
void Power_SetMode(uint8_t mode)
{
	PowerMode = mode;
}
/*********************************************************************************
  * Function:	   Power_GetMode
  * Description:
  * Input:		-
  * Output: 	 mode
  * Return: 	 uint8_t
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
 **********************************************************************************/
uint8_t Power_GetMode(void)
{
	return PowerMode;
}
/*********************************************************************************
  * Function:	   Backup Battery EMC Support
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 -
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-11-17  Zhengshijie	  New
  *
 **********************************************************************************/
void BackupBatteryActive(void)
{
	uint8_t ret;
	uint8_t RegAddr;
	uint8_t ReadData = 0;
	uint8_t WriteData = 0;

	/*backup battery boost enable*/
	RegAddr = 0x04;
	ret = LPI2C1_rx(0x2A,&RegAddr,0x01,&ReadData,0x01);
	WriteData = ReadData|0x43;
	ret = LPI2C1_tx(0x2A,&RegAddr,0x01,&WriteData,0x01);
	/*backup battery cv mode set ,6V*/
	RegAddr = 0x06;
	WriteData = 0x1E;
	ret = LPI2C1_tx(0x2A,&RegAddr,0x01,&WriteData,0x01);
	/*backup battery ISINK set,discharger 1A*/
	RegAddr = 0x07;
	ReadData = 0;
	WriteData = 0;
	ret = LPI2C1_rx(0x2A,&RegAddr,0x01,&ReadData,0x01);
	WriteData = ReadData|0x07;
	ret = LPI2C1_tx(0x2A,&RegAddr,0x01,&WriteData,0x01);
	/*backup battery cc mode set,50mA*/
	RegAddr = 0x05;
	WriteData = 0x01;
	ret = LPI2C1_tx(0x2A,&RegAddr,0x01,&WriteData,0x01);
	/*backup battery charger start*/
	RegAddr = 0x04;
	ReadData = 0;
	WriteData = 0;
	ret = LPI2C1_rx(0x2A,&RegAddr,0x01,&ReadData,0x01);
	WriteData = ReadData|0x08;
	ret = LPI2C1_tx(0x2A,&RegAddr,0x01,&WriteData,0x01);
	/*PIN133   	PTE25:L charger enable,H charger disable*/
	GPIO_Port_Lev_Set(PTE,PIN_INDEX_25,PORT_LOW);
	/*PIN119	PTA16:H charger enable,L charger disable*/
	GPIO_Port_Lev_Set(PTA,PIN_INDEX_16,PORT_HIGH);
}
/*********************************************************************************
  * Function:	   Backup Battery EMC Support
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 -
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-11-17  Zhengshijie	  New
  *
 **********************************************************************************/
void Power_AllSleepTest(void)
{
	uint8_t ret;
	LPSPI2_Deinit_master();
	//PTC.03(DEBUG_UART_TX,OUTPUT,UART)-->GPIO LOW
	GPIO_Port_Function_Set(PORTC,PIN_INDEX_3,PIN_FUNCTION_ALT1);
	GPIO_Port_Dir_Set(PTC,PIN_INDEX_3,DIR_OUTPUT);
	GPIO_Port_Lev_Set(PTC,PIN_INDEX_3,PORT_LOW);
	//PTD.06(MCU_AG550_INT,OUTPUT,GPIO)-->GPIO LOW
	GPIO_Port_Function_Set(PORTD,PIN_INDEX_6,PIN_FUNCTION_ALT1);
	GPIO_Port_Dir_Set(PTD,PIN_INDEX_6,DIR_OUTPUT);
	GPIO_Port_Lev_Set(PTD,PIN_INDEX_6,PORT_LOW);
	//PTB.13(1043_CAN_TX,OUTPUT,GPIO)-->GPIO LOW
	GPIO_Port_Function_Set(PORTB,PIN_INDEX_13,PIN_FUNCTION_ALT1);
	GPIO_Port_Dir_Set(PTB,PIN_INDEX_13,DIR_OUTPUT);
	GPIO_Port_Lev_Set(PTB,PIN_INDEX_13,PORT_LOW);
	/*NAD Sleep REQ,MCU_AG550_WAKE,GPIO1*/
	GPIO_Port_Lev_Set(PTE,PIN_INDEX_12,PORT_LOW);
	/*MCU SLEEP*/
	//PIN143	PTA9:PWR_5V0_EN
	//GPIO_Port_Lev_Set(PTA,PIN_INDEX_9,PORT_LOW);
	//PIN2		PTE15:PWR_SYS_EN
	GPIO_Port_Lev_Set(PTE,PIN_INDEX_15,PORT_LOW);
	//PIN120	PTA15:PWR_8V2_EN
	GPIO_Port_Lev_Set(PTA,PIN_INDEX_15,PORT_LOW);
	//PIN119	PTA16:PWR_6V8_EN
	GPIO_Port_Lev_Set(PTA,PIN_INDEX_16,PORT_LOW);
	//PIN41		PTB4:7702_5V0_EN
	GPIO_Port_Lev_Set(PTB,PIN_INDEX_4,PORT_LOW);
	//PIN38		PTB21:PWR_V2X_EN
	GPIO_Port_Lev_Set(PTB,PIN_INDEX_21,PORT_LOW);
	//PIN36		PTB18:PWR_3V8_EN
	//GPIO_Port_Lev_Set(PTB,PIN_INDEX_18,PORT_LOW);
	//PIN144	PTA8:PWR_3V3_EN
	GPIO_Port_Lev_Set(PTA,PIN_INDEX_8,PORT_LOW);
	//PIN1		PTE16:PWR_1V8_EN
	GPIO_Port_Lev_Set(PTE,PIN_INDEX_16,PORT_LOW);
	//PIN37		MCU_AG550_ONOFF,KEEP
	//PIN30		MCU_AG550_RESET,KEEP
	//PIN107	PTB11:AudioIO_1V8_EN
	GPIO_Port_Lev_Set(PTB,PIN_INDEX_11,PORT_LOW);
	//PIN108	PTB10:AudioANA_3V3_EN
	GPIO_Port_Lev_Set(PTB,PIN_INDEX_10,PORT_LOW);
	//PIN109	PTB9:AudioD_1V8_EN
	GPIO_Port_Lev_Set(PTB,PIN_INDEX_9,PORT_LOW);
	GPIO_Port_Lev_Set(PTE,PIN_INDEX_11,PORT_LOW);
	//PIN86   	PTC31:ECALL ENABLE
	//GPIO_Port_Lev_Set(PTC,PIN_INDEX_31,PORT_LOW);
	//pin100 ptd4
	GPIO_Port_Lev_Set(PTD,PIN_INDEX_4,PORT_LOW);
	//47 ptd12
	GPIO_Port_Lev_Set(PTD,PIN_INDEX_12,PORT_LOW);
	//81 ptc8
	GPIO_Port_Lev_Set(PTC,PIN_INDEX_8,PORT_LOW);
	Power_AllPortDeinit();
	/*disable int*/
	INT_SYS_DisableIRQGlobal();
	/* unlock watchdog */
	WDOG->CNT = 0xD928C520;
	(void)WDOG->CNT;
	/* disable watchdog */
	WDOG->CS &= ~WDOG_CS_EN_MASK;
	INT_SYS_EnableIRQGlobal();
	ret = POWER_SYS_SetMode(0, POWER_MANAGER_POLICY_AGREEMENT);
	/*Re Initialize and configure clocks*/
	CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
	CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
	/*s32k all port init*/
	GPIO_Abs_Port_Init();
	OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_SOS_WAKE_UP,NULL,0,OS_WAIT_INFINITE);
}

