/*
 * Ignition.c
 *
 *  Created on: 2024. 6. 17.
 *      Author: SUPYO
 */
#include "IG_Layer/Ignition.h"
#include "Global_Variable.h"

uint8_t GV_All_ECU_Ready_Flag_2 = 0;
uint8_t GV_ALL_ECU_Off_Flag_2 = 0;
int Operation_Ready = 0;
int Operation_Ready_2 = 0;


/*=================== Ignition On ===================*/
/*
 * Function Name : IG_OnCheck_HV_Status
 * Data Type : void
 * Description : Checklist / 1. -- / 2. HV Released / 3. EPAM Applied / 4. LDC, BMS Fault Check
 * Requirement : 고장 검출 시간 필요, 고장 시 운행 방어 코드 필요.
 */
void IG_OnCheck_HV_Status(void)
{
	if(GV_BMS_Fault_Check == 0 && GV_HV_Apply_Check == 0)	GV_IG_Process_Status = 2;
}

void HV_ON_FUNC(void)
{
	/*Check Fault Status, Contact HV Sys*/
	GV_HV_ON_CMD = 1;

	/*Check Fault Status, Contact LDC Sys*/
	if(GV_HV_Apply_Check == 1)
	{
		GV_LDC_ACT_CMD = 1;
		GV_LDC_TAGT_CMD = 120; // 20240821- 30->40으로 변경, 추후 자율주행 시 180 으로 조정 // 20241010 - 150A 로 조정// 20241024-> 라이다플럭스 인계 120A 로 조정
	}
}

void Key_on_Sub_Func1(void)
{
	if(GV_CE_ON_CMD == 1)
	{
		GV_MCU_Init_Status = 1;
		GV_Steer_Init_Status = 1;
		GV_Brake_Init_Status = 1;
		GV_ACU_Init_Status = 1;
	}
	else
	{
		GV_MCU_Init_Status = 0;
		GV_Steer_Init_Status = 0;
		GV_Brake_Init_Status = 0;
		GV_ACU_Init_Status = 0;
	}
}

/*
 * Function Name : Key_on_Sub_Func1
 * Data Type : void
 * Description : Check the ECU Systems
 * Requirement : *
 */
void Key_on_Sub_Func2(void)
{
	if(GV_BCM_Init_Status == 1 /*&& GV_BMS_Init_Status == 1*/ && GV_MCU_Init_Status == 1 && GV_Steer_Init_Status == 1 && GV_Brake_Init_Status == 1 && GV_ACU_Init_Status == 1 ) //  GV_SOC_STAT_Flag =! 3 �� soc 30 % ���� ������ ��
		GV_All_ECU_Ready_Flag = 1;
	else
		GV_All_ECU_Ready_Flag = 0;

	if(GV_BCM_Init_Status == 0 /*&& GV_BMS_Init_Status == 0 */&& GV_MCU_Init_Status == 0 && GV_Steer_Init_Status == 0 && GV_Brake_Init_Status == 0 && GV_ACU_Init_Status == 0)
		GV_ALL_ECU_Off_Flag = 1;
	else
		GV_ALL_ECU_Off_Flag = 0;
}

/*
 * Function Name : Key_on_Sub_Func3
 * Data Type : void
 * Description : Final Sub Function / Check ECU Init , HV on, LDC on -> Pass
 * Requirement : Operation Ready, EPAM Release, Process Status = Key on Complete 3
 */
void Key_on_Sub_Func3(void)
{
	Operation_Ready = 1;
	GV_EPAM_CMD = 0; //EPAM Release Cmd
	GV_IG_Process_Status = 3;
}

/*
 * Function Name : Key_on_Process_Func
 * Data Type : void
 * Description : ECU Init / Contact HV System / EPAM Release and Process Complete
 * Requirement : *
 */
void Key_on_Process_Func(void)
{
	Key_on_Sub_Func1();
	Key_on_Sub_Func2();
	if(GV_All_ECU_Ready_Flag == 1)	Key_on_Sub_Func3();
}

/*=================== Ignition Off ===================*/
/* Key off 시 고전압 확인
 * Function Name : IG_OffCheck_HV_Status
 * Data Type : void
 * Description : Checklist / 1. -- / 2. HV Released / 3. EPAM Applied / 4. LDC, BMS Fault Check
 * Requirement : *
 */
void IG_OffCheck_HV_Status_2(void)
{
	if(GV_HV_Apply_Check == 1 && GV_EPAM_STA == 1)	GV_IG_Process_Status = 0;
}

void HV_OFF_FUNC(void)
{
	/*Check Fault Status, Contact HV Sys*/
	if(GV_LDC_Apply_Check == 0 && GV_HV_ON_CMD == 1)
	{
		CNT_GV_HV_ON_CMD++;
	}	// 240522 LDC 전원 드랍확인 후 CNT 진행, 충분한 시간 후 HV OFF

	/*Check Fault Status, Contact LDC Sys*/
	// LDC
	GV_LDC_ACT_CMD = 0;

	GV_LDC_TAGT_CMD = 0;



	if (CNT_GV_HV_ON_CMD >= 500) //1000ms
	{
		CNT_GV_HV_ON_CMD = 0;
		GV_HV_ON_CMD = 0;
		GV_Key_Off_Level = 2;
	}
}

void Key_off_Sub_Func1(void)
{
	if(GV_BCM_Init_Status == 1 && GV_MCU_Init_Status == 1 && GV_Steer_Init_Status == 1 && GV_Brake_Init_Status == 1 && GV_ACU_Init_Status == 1)
		GV_All_ECU_Ready_Flag = 1;
	else
		GV_All_ECU_Ready_Flag = 0;

//	if(GV_BCM_Init_Status == 0 && GV_MCU_Init_Status == 0 && GV_Steer_Init_Status == 0 && GV_Brake_Init_Status == 0 && GV_ACU_Init_Status == 0)
//	{
//		GV_ALL_ECU_Off_Flag = 1;
//		GV_Key_Off_Level = 1;
//	}
//	else
//		GV_ALL_ECU_Off_Flag = 0;

	GV_ALL_ECU_Off_Flag = 1;
	GV_Key_Off_Level = 1;
}


void Key_off_Sub_Func2(void)
{
	HV_OFF_FUNC();
}

void Key_off_Sub_Func3(void)
{
	// ldc 드랍
	if(GV_can_rx_ldc_v < 5)
	{
		GV_CE_ON_CMD = 0;
		GV_Key_Off_Level = 3;
	}
	else GV_CE_ON_CMD = 1;
}


void Key_Off_Process_Func(void)
{
	Key_off_Sub_Func1();
	if(GV_Key_Off_Level == 1)		Key_off_Sub_Func2();
	else if(GV_Key_Off_Level == 2) 	Key_off_Sub_Func3();

	if(GV_ALL_ECU_Off_Flag == 1 && GV_Key_Off_Level == 3)
	{
		Operation_Ready = 0;
		GV_IG_Process_Status = 1;
	}
}
