

#include "Include.h"
// #include "System_Status_Monitor.h"

#pragma MESSAGE DISABLE C4000
#pragma MESSAGE DISABLE C4001

/******************************************************************************
函数名：
功  能：系统状态初始化
        仅在首次上电时初始化一次
参  数：无
返回值：无
******************************************************************************/
void Sys_Status_Init(void)
{
	SysStatUpdateMode = SYS_STAT_UPDATE_MODE_INIT;

	//SysPowerStatus = 0x00;——
	//SysOperatingStatus = 0x00;

	SysPwrMode.Mode = SYS_PWR_INIT_MODE;
	SysPwrMode.LVPTimer = 0;
	SysPwrMode.NormalTimer = 0;
	SysPwrMode.OVPTimer = 0;

	SysKL15Signal.Status = SYS_SIG_STAT_INACTIVE;
	SysKL15Signal.Timer = 0;
}

/******************************************************************************
函数名：Sys_Status_Update_Mode_Set
功  能：系统状态更新模式设置
参  数：Mode：更新模式 SYS_STAT_UPDATE_MODE_NORMAL   - 正常更新模式
                       SYS_STAT_UPDATE_MODE_WAKEUP   - 休眠后唤醒更新模式
                       SYS_STAT_UPDATE_MODE_RECOVERY - 电压异常保护后恢复模式
                       SYS_STAT_UPDATE_MODE_INIT     - 初始化模式
返回值：无
******************************************************************************/
void Sys_Status_Update_Mode_Set(uint8_t Mode)
{
	if (Mode != SysStatUpdateMode) //当更新模式发生变化时,重置所有定时器
	{
		SysPwrMode.LVPTimer = 0;
		SysPwrMode.NormalTimer = 0;
		SysPwrMode.OVPTimer = 0;

		SysKL15Signal.Timer = 0;

		SysStatUpdateMode = Mode;
	}
}
//电压异常检测，不同模式下都要调用判断
//20ms 调用一次
void Voltage_Abnormal_Detect(INT16U Voltage, INT8U Mode)
{
	INT8U Debounce_Count = 0;
	if (SYS_STAT_UPDATE_MODE_INIT == Mode) //初始化//唤醒等模式下
	{
		Debounce_Count = 5;
	}
	else if (SYS_STAT_UPDATE_MODE_WAKEUP == Mode)
	{
		//唤醒模式下，在睡眠时不检测电压异常
		if (_IGN_Sleep == g_Power_St)
		{
			return;
		}
		else //此时应该是已经IGOFF了，不过没睡眠呢
		{
			Debounce_Count = SYS_PWR_DEBOUNCE_TIME / 20;
		}
	}
	else //正常运行的模式下
	{
		Debounce_Count = SYS_PWR_DEBOUNCE_TIME / 20;
	}

	//外界电压采集无效值时传入   0
	if (Voltage)
	{
		switch (SysPwrMode.Mode) //电源状态转换
		{
			case SYS_PWR_LVP_MODE:
				if (Voltage >= SYS_OVP_ENTER_THRESHOLD)
				{
					SysPwrMode.NormalTimer = 0;
					SysPwrMode.OVPTimer++;
				}
				else if (Voltage >= SYS_LVP_EXIT_THRESHOLD)
				{
					SysPwrMode.NormalTimer++;
					SysPwrMode.OVPTimer = 0;
				}
				else
				{
					SysPwrMode.NormalTimer = 0;
					SysPwrMode.OVPTimer = 0;
				}

				if (SysPwrMode.OVPTimer >= Debounce_Count)
				{
					SysPwrMode.Mode = SYS_PWR_OVP_MODE;
					SysPwrMode.NormalTimer = 0;
					SysPwrMode.OVPTimer = 0;
				}
				else if (SysPwrMode.NormalTimer >= Debounce_Count)
				{
					SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
					SysPwrMode.NormalTimer = 0;
					SysPwrMode.OVPTimer = 0;
				}
				break;
			case SYS_PWR_INIT_MODE:
			case SYS_PWR_NORMAL_MODE:
				if (Voltage < SYS_LVP_ENTER_THRESHOLD)
				{
					SysPwrMode.LVPTimer++;
					SysPwrMode.OVPTimer = 0;
					SysPwrMode.NormalTimer = 0;
				}
				else if (Voltage >= SYS_OVP_ENTER_THRESHOLD)
				{
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.OVPTimer++;
					SysPwrMode.NormalTimer = 0;
				}
				else
				{
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.OVPTimer = 0;
					SysPwrMode.NormalTimer++;
				}

				if (SysPwrMode.LVPTimer >= Debounce_Count)
				{
					SysPwrMode.Mode = SYS_PWR_LVP_MODE;
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.OVPTimer = 0;
					SysPwrMode.NormalTimer = 0;
				}
				else if (SysPwrMode.OVPTimer >= Debounce_Count)
				{
					SysPwrMode.Mode = SYS_PWR_OVP_MODE;
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.OVPTimer = 0;
					SysPwrMode.NormalTimer = 0;
				}
				else if (SysPwrMode.NormalTimer >= Debounce_Count)
				{
					SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.OVPTimer = 0;
					SysPwrMode.NormalTimer = 0;
				}
				break;

			case SYS_PWR_OVP_MODE:
				if (Voltage < SYS_LVP_ENTER_THRESHOLD)
				{
					SysPwrMode.LVPTimer++;
					SysPwrMode.NormalTimer = 0;
				}
				else if (Voltage < SYS_OVP_EXIT_THRESHOLD)
				{
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.NormalTimer++;
				}
				else
				{
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.NormalTimer = 0;
				}

				if (SysPwrMode.LVPTimer >= Debounce_Count)
				{
					SysPwrMode.Mode = SYS_PWR_LVP_MODE;
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.NormalTimer = 0;
				}
				else if (SysPwrMode.NormalTimer >= Debounce_Count)
				{
					SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
					SysPwrMode.LVPTimer = 0;
					SysPwrMode.NormalTimer = 0;
				}
				break;

			default:
				SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
				SysPwrMode.LVPTimer = 0;
				SysPwrMode.NormalTimer = 0;
				SysPwrMode.OVPTimer = 0;
				break;
		}
	}
}
//KL15电监测，检测15电的有无，不考虑高低
//20ms一次暂定
void Voltage_KL15ON_Detect(INT16U Voltage, INT8U Mode)
{
	INT8U Debounce_Count = 0;
	if (SYS_STAT_UPDATE_MODE_INIT == Mode) //初始化
	{
		Debounce_Count = 5;
	}
	else if (SYS_STAT_UPDATE_MODE_WAKEUP == Mode) //唤醒等模式下
	{
		Debounce_Count = 5;
	}
	else //正常运行的模式下
	{
		Debounce_Count = (SYS_OPR_DEBOUNCE_TIME / 20);
	}
	//IGN状态检测
	if (SysKL15Signal.Status == SYS_SIG_STAT_ACTIVE)
	{
		if (Voltage < SYS_OFF_THRESHOLD)
		{
			SysKL15Signal.Timer++;
			if (SysKL15Signal.Timer >= Debounce_Count)
			{
				SysKL15Signal.Status = SYS_SIG_STAT_INACTIVE;
				SysKL15Signal.Timer = 0;
			}
		}
		else
			SysKL15Signal.Timer = 0;
	}
	else
	{
		if (Voltage >= SYS_ON_THRESHOLD)
		{
			SysKL15Signal.Timer++;
			if (SysKL15Signal.Timer >= Debounce_Count)
			{
				SysKL15Signal.Status = SYS_SIG_STAT_ACTIVE;
				SysKL15Signal.Timer = 0;
			}
		}
		else
			SysKL15Signal.Timer = 0;
	}
}
/******************************************************************************
函数名：Sys_Status_Update_Service
功  能：系统状态更新服务
参  数：无
返回值：无
*******************************************************************************
注  意：该服务函数在不同的工作模式下需要按照不同的周期调用
        SYS_STAT_UPDATE_MODE_NORMAL   模式下,每  20ms 调用一次
        SYS_STAT_UPDATE_MODE_WAKEUP   模式下,不严格要求调用时间
        SYS_STAT_UPDATE_MODE_RECOVERY 模式下,每 100ms 调用一次
        SYS_STAT_UPDATE_MODE_INIT     模式下,于初始化阶段调用一次
******************************************************************************/
void Sys_Status_Update_Service(void)
{
	uint8_t i;

	uint16_t Voltage;
	uint16_t RocBackup;
	uint16_t Counter;
	uint8_t OpStatus;

	if (SysStatUpdateMode == SYS_STAT_UPDATE_MODE_NORMAL) //正常更新模式
	{
		if ((ANALOG_KL30_VOLTAGE_VALID == 0) || (ANALOG_KL15_VOLTAGE_VALID == 0))
			return; //必须在电源电压有效的情况下才进行状态更新

		Voltage_Abnormal_Detect(ANALOG_KL30_VOLTAGE, SYS_STAT_UPDATE_MODE_NORMAL);
		Voltage_KL15ON_Detect(ANALOG_KL15_VOLTAGE, SYS_STAT_UPDATE_MODE_NORMAL);
	}
	else if (SysStatUpdateMode == SYS_STAT_UPDATE_MODE_WAKEUP) //唤醒更新模式
	{
	
		Voltage_Abnormal_Detect(ANALOG_KL30_VOLTAGE, SYS_STAT_UPDATE_MODE_WAKEUP);
	
		//对IGN电源进行5次检测,必须连续5次均高于门限值才认为IGN ON
		for (i = 0; i < 5; i++)
		{
			Analog_KL15_Voltage_Conversion();
			Voltage_KL15ON_Detect(ANALOG_KL15_VOLTAGE, SYS_STAT_UPDATE_MODE_WAKEUP);
			if (ANALOG_KL15_VOLTAGE < SYS_ON_THRESHOLD)
				return;
		}
		//连续5次都大于门限值了，认为有15电了
	}
	else //初始化更新模式//SYS_STAT_UPDATE_MODE_INIT
	{
		//上电延时250ms,等待电源电压稳定
		RocBackup = API_ROLLING_COUNTER;
		do
		{
			Counter = API_ROLLING_COUNTER;
			Counter -= RocBackup;
		} while (Counter < (250000 / API_INT_CYCLE));

		//默认电源为正常状态,初始化电源状态

		SysKL15Signal.Timer = 0;
		SysKL15Signal.Status = SYS_SIG_STAT_INACTIVE;

		SysPwrMode.NormalTimer = 0;
		SysPwrMode.LVPTimer = 0;
		SysPwrMode.OVPTimer = 0;
		SysPwrMode.Mode = SYS_PWR_INIT_MODE;

		for (;;)
		{
			Voltage = 0;
			Analog_KL15_Voltage_Conversion();
			Analog_KL30_Voltage_Conversion();

			Voltage_KL15ON_Detect(ANALOG_KL15_VOLTAGE, SYS_STAT_UPDATE_MODE_INIT);

			Voltage_Abnormal_Detect(ANALOG_KL30_VOLTAGE, SYS_STAT_UPDATE_MODE_INIT);
			//度过了初始化状态
			if (SYS_PWR_INIT_MODE != SysPwrMode.Mode)
			{
				break;
			}
		}
	}
}
