/*
 * System_Check.c
 *
 *  Created on: 2024. 2. 29.
 *      Author: SUPYO
 */

#define __System_Check_C__

#include "System_Check.h"
#include "Global_Variable.h"
#include "Safety_Layer/EMERGENCY/Emergency.h"
#include "Safety_Layer/BMS/CE_ON_CMD.h"
#include "Safety_Layer/BMS/Charging_Status.h"
#include <IG_Layer/Ignition.h>
#include <Operation_Layer/MCU/Motor_Status.h>
#include "Operation_Layer/SW_Version/SW_VER.h"
#include <Operation_Layer/MCU/Vehicle_Speed.h>

/*
 * Variable Name : GV_VCU_Ignition
 * Data Type : uint8
 * Description : Main SW Status
 * Requirement : Connect with GPIO IG SW
 */

/*
 * key off process  : GV_IG_Process_Status = 0
 * key off complete : GV_IG_Process_Status = 1
 * Key on process   : GV_IG_Process_Status = 2
 * Key on complete  : GV_IG_Process_Status = 3
 */
void System_Check_Func(void)
{
	/*-------------------- BMS Fault Check --------------------*/
	if(GV_BMS_FltSta == 0 && GV_BMS_TEMP_FAULT == 0 && GV_BMS_Interlock == 1)	GV_BMS_Fault_Check = 0;
	else																		GV_BMS_Fault_Check = 1;

	/*-------------------- MCU Fault Check --------------------*/
	// 0 = Connected
	if(GV_MCU_FltSta == 0)	GV_MCU_Fault_Check = 0;
	else					GV_MCU_Fault_Check = 1;

	/*-------------------- LDC Fault Check --------------------*/
	if(GV_LDC_FltSta == 0 && GV_LDC_TEMP_FAULT == 0)	GV_LDC_Fault_Check = 0;
	else												GV_LDC_Fault_Check = 1;

	/*-------------------- OBC Fault Check --------------------*/
	if(GV_OBC_Interlock == 1)	GV_OBC_Fault_Check = 0;
	else						GV_OBC_Fault_Check = 1;

	/*------------- BMS High voltage Status Check -------------*/
	if(GV_BMS_HVOn_STA == 0)	GV_HV_Apply_Check = 0;
	else						GV_HV_Apply_Check = 1;

	/*-------------------- LDC Status Check --------------------*/
	if(GV_LDC_STA == 0)	GV_LDC_Apply_Check = 0;
	else				GV_LDC_Apply_Check = 1;


	if(GV_VCU_Ignition == 0 && GV_IG_Process_Status == 1) 		// IG switch = 0 / Key on complete
	{
		GV_CE_ON_CMD = 0;
		GV_Key_Off_Level = 0;
		Soc_Check_befor_IGN();	// check SoC (SoC ≥ 30%)
	}
	else if(GV_VCU_Ignition == 0 && GV_IG_Process_Status == 2) 	// 시동 중 bcm off 될 때
	{
		GV_IG_Process_Status = 0;
	}
	else if(GV_VCU_Ignition == 1 && GV_IG_Process_Status == 1)
	{
		GV_VCU_Ready = 1;
		GV_CE_ON_CMD = 1;
		SET_SW_VER();			// set Software version
		IG_OnCheck_HV_Status();
	}
	else if(GV_VCU_Ignition == 1 && GV_IG_Process_Status == 2)
	{
		Key_on_Process_Func();
	}
	else if(GV_VCU_Ignition == 1 && GV_IG_Process_Status == 3)
	{
		Drive_Mode_Func();

		// 충전 상태 돌입인지 확인
		Check_Charging_Status();

		Motor_Status(); // 모터 기본 상태 세팅
		Est_Vehicle_Speed();
	}
	else if(GV_VCU_Ignition == 0 && GV_IG_Process_Status == 3)
	{
		Read_LDC_Voltage();

		GV_IG_Process_Status = 0;
	}
	else if(GV_VCU_Ignition == 0 && GV_IG_Process_Status == 0)
	{
		GV_VCU_EPAM_Park_Request_to_EPC = 1;	// Lock = 1
		GV_VCU_GearSelStat = 0;					// P = 0
		Key_Off_Process_Func();
	}

}
