/*********************************************************************************
 * Copyright(C),Ningbo Joyson Preh Car Connect Co.,Ltd.
 * FileName:    Power_Manager.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 "Driver/Wkup/Wakeup.h"
#include "Driver/Interrupt/Interrupt.h"
#include "Driver/GPIO/Gpio_drv.h"
#include "Power_Os.h"
#include "Power_Config.h"
#include "Power_Ctrl.h"
#include "Driver/IIC/LPI2C_I2cDrv.h"

/***********************************System os api***********************************/
static OS_TaskParam PowerMgrParam;
static void PowerMgr_task_init(void);
static void PowerMgr_recv_msg(uint16_t event_code, void* data, uint32_t data_size);
/*********************************************************************************
  * Function:	   PowerMgr_task_init
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
 **********************************************************************************/
static void PowerMgr_task_init(void)
{
	uint32_t wakeupmode;
	uint8_t ret;

	OS_createTimer(TIMER_ID_POWER_STANDBYOUT,3000,0,Power_Standbyout_Cold_Stage1Timeout);
	OS_createTimer(TIMER_ID_SOC_OFF,1000,1,Power_SocOff_Check);
	OS_createTimer(TIMER_ID_NAD_SLEEP,1000,1,Power_NadSleep_Check);
	OS_createTimer(TIMER_ID_NAD_SLEEP_PENDING,3000,0,Power_NadSleep_Pendingtimeout);
	OS_createTimer(TIMER_ID_NAD_OFF_PENDING,3000,0,Power_NadOff_Pendingtimeout);
	OS_createTimer(TIMER_ID_ALL_SLEEP_TEST,600000,0,Power_AllSleepTest);

	LPI2C1_init();
	/*Debug SRS:0x800*/
	wakeupmode = GetWakeUpSrc();
	/*bit11:1 Reset caised bu Debug*/
	/*bit7:1 Reset caused by POR*/
	/*bit6:1 Reset caused by external reset pin*/
	/*bit5:1 Reset caused by watchdog timeout*/
	/*bit1:1 Low-Voltage Detect Reset or High-Voltage Detect Reset*/
	if((wakeupmode&0x8E2) != 0)
	{
		ret = OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_COLD_WAKE_UP,NULL,0,OS_WAIT_INFINITE);
	}
	else
	{
		ret = OS_sndMsg(MSG_ID_POWERMGR,EVENT_MCU_COLD_WAKE_UP,NULL,0,OS_WAIT_INFINITE);
	}

}
/*********************************************************************************
  * Function:	   PowerMgr_recv_msg
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
 **********************************************************************************/
static void PowerMgr_recv_msg(uint16_t event_code, void* data, uint32_t data_size)
{
	Power_MainProcess(event_code);
}
/*********************************************************************************
  * Function:	   PowerMgr_getTaskParam
  * Description:
  * Input:		-
  * Output: 	 -
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-10-09  Zhengshijie	  New
  *
 **********************************************************************************/
void* PowerMgr_getTaskParam()
{
	PowerMgrParam.queue_id = MSG_ID_POWERMGR;
	PowerMgrParam.wait_time = OS_WAIT_INFINITE;
	PowerMgrParam.task_init = PowerMgr_task_init;//task has been created
	PowerMgrParam.recv_msg = PowerMgr_recv_msg;
    //PowerMgrParam.time_out = PowerMgr_recv_time_out;//need not
    //PowerMgrParam.loop = PowerMgr_loop;//need not
    return &PowerMgrParam;
}
/*********************************************************************************
  * Function:	   GetHardWareVersion
  * Description:
  * Input:		-
  * Output: 	 hard version
  * Return: 	 void
  *********************************************************************************
  * No.   Date			  Author		  Modification
  *--------------------------------------------------------------------------------
  * 01	  2020-11-12  Zhengshijie	  New
  *
 **********************************************************************************/
uint8_t GetHardWareVersion(void)
{
	uint8_t Vervar0 = 0;
	uint8_t Vervar1 = 0;
	uint8_t Vercfg0 = 0;
	uint8_t Vercfg1 = 0;
	uint8_t Vercfg2 = 0;
	uint8_t HardWareVersion = 0x1F;/*xxx1 1111*/
	/*Read HardWare Version Port*/
	Vervar0 = GPIO_INPUT_Port_Lev_Read(PTA,PIN_INDEX_0);/*var0:PTA0*/
	Vervar1 = GPIO_INPUT_Port_Lev_Read(PTA,PIN_INDEX_1);/*var1:PTA1*/
	Vercfg0 = GPIO_INPUT_Port_Lev_Read(PTD,PIN_INDEX_28);/*cfg0:PTD28*/
	Vercfg1 = GPIO_INPUT_Port_Lev_Read(PTD,PIN_INDEX_29);/*cfg1:PTD29*/
	Vercfg2 = GPIO_INPUT_Port_Lev_Read(PTD,PIN_INDEX_30);/*cfg2:PTD30*/
	/*Version Convert*/
	HardWareVersion = (Vervar1<<4)+(Vervar0<<3)+(Vercfg2<<2)+(Vercfg1<<1)+Vercfg0;
	HardWareVersion = HardWareVersion&0x1F;
	return HardWareVersion;
}
