
#include "System_Status_Monitor.h"

#pragma MESSAGE DISABLE C4000
#pragma MESSAGE DISABLE C4001

volatile uint8_t        SysPowerStatus;
volatile uint8_t        SysOperatingStatus;

uint8_t                 SysStatUpdateMode;

SysPwrModeStruct        SysPwrMode;
SysSignalStatusStruct   SysKL15Signal;

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

  SysPowerStatus         = 0x00;
  SysOperatingStatus     = 0x00;

  SysPwrMode.Mode        = SYS_PWR_NORMAL_MODE;
  SysPwrMode.LVPTimer    = 0;
  SysPwrMode.LimitTimer  = 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.LimitTimer  = 0;
    SysPwrMode.NormalTimer = 0;
    SysPwrMode.OVPTimer    = 0;

    SysKL15Signal.Timer    = 0;

    SysStatUpdateMode      = Mode;
  }
}

/******************************************************************************
函数名：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;
  uint8_t   LVPCnt;
  uint8_t   LimitCnt;
  uint8_t   NormalCnt;
  uint8_t   OVPCnt;
  uint16_t  Voltage;
  uint16_t  RocBackup;
  uint16_t  Counter;
  uint8_t   OpStatus;

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

    //正常更新模式下,首先获取电源状态
    //Voltage = 0;                          //获取电源电压
    //Voltage = ANALOG_KL15_VOLTAGE;
    if ((ANALOG_KL30_VOLTAGE_VALID == 0) || (ANALOG_KL15_VOLTAGE_VALID == 0))
      return;                             //必须在电源电压有效的情况下才进行状态更新
    
    //正常更新模式下,首先获取电源状态
    Voltage = 0;                          //获取电源电压
    
    #if SYS_POWERED_BY_KL30_ONLY
      Voltage = ANALOG_KL30_VOLTAGE;
    #else
      if (ANALOG_KL15_VOLTAGE > ANALOG_KL30_VOLTAGE)
        Voltage = ANALOG_KL15_VOLTAGE;
      else
        Voltage = ANALOG_KL30_VOLTAGE;
    #endif
    
    switch (SysPwrMode.Mode)              //电源状态转换
    {
      case SYS_PWR_LVP_MODE     :
        if (NVM_CONFIG_POWER == 1)
        {
          if (Voltage >= 32000)           //进入过压保护阈值
          {
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer++;
            SysPwrMode.OVPTimer++;
          }
          else if (Voltage >= 18000)      //正常工作
          {
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer++;
            SysPwrMode.OVPTimer = 0;
          }
          else if (Voltage >= 16000)      //退出低压阀值
          {
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer = 0;
            SysPwrMode.OVPTimer    = 0;
          }
          else
          {
            SysPwrMode.LimitTimer  = 0;
            SysPwrMode.NormalTimer = 0;
            SysPwrMode.OVPTimer    = 0;
          }
        }
        else
        {
          if (Voltage >= 18000)           //进入过压保护阈值
          {
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer++;
            SysPwrMode.OVPTimer++;
          }
          else if (Voltage >= 9500)       //正常工作
          {
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer++;
            SysPwrMode.OVPTimer = 0;
          }
          else if (Voltage >= 7000)       //退出低压阀值
          {
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer = 0;
            SysPwrMode.OVPTimer    = 0;
          }
          else
          {
            SysPwrMode.LimitTimer  = 0;
            SysPwrMode.NormalTimer = 0;
            SysPwrMode.OVPTimer    = 0;
          }
        }
        if (SysPwrMode.OVPTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_OVP_MODE;
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
        else if (SysPwrMode.NormalTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
        else if (SysPwrMode.LimitTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_LIMIT_MODE;
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
        break;

      case SYS_PWR_LIMIT_MODE   :
        if (NVM_CONFIG_POWER == 1)
        {
          if (Voltage >= 32000)
          {
            SysPwrMode.LVPTimer = 0;
            SysPwrMode.NormalTimer++;
            SysPwrMode.OVPTimer++;
          }
          else if (Voltage >= 18000)
          {
            SysPwrMode.LVPTimer = 0;
            SysPwrMode.NormalTimer++;
            SysPwrMode.OVPTimer = 0;
          }
          else if (Voltage < 15500)       //进入低压保护阈值 
          {
            SysPwrMode.LVPTimer++;
            SysPwrMode.NormalTimer = 0;
            SysPwrMode.OVPTimer    = 0;
          }
          else
          {
            SysPwrMode.LVPTimer    = 0;
            SysPwrMode.NormalTimer = 0;
            SysPwrMode.OVPTimer    = 0;
          }
        }
        else
        {
          if (Voltage >= 18000)
          {
            SysPwrMode.LVPTimer = 0;
            SysPwrMode.NormalTimer++;
            SysPwrMode.OVPTimer++;
          }
          else if (Voltage >= 9500)
          {
            SysPwrMode.LVPTimer = 0;
            SysPwrMode.NormalTimer++;
            SysPwrMode.OVPTimer = 0;
          }
          else if (Voltage < 6500)        //进入低压保护阈值 
          {
            SysPwrMode.LVPTimer++;
            SysPwrMode.NormalTimer = 0;
            SysPwrMode.OVPTimer    = 0;
          }
          else
          {
            SysPwrMode.LVPTimer    = 0;
            SysPwrMode.NormalTimer = 0;
            SysPwrMode.OVPTimer    = 0;
          }
        }
        if (SysPwrMode.OVPTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_OVP_MODE;
          SysPwrMode.LVPTimer    = 0;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
        else if (SysPwrMode.NormalTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
          SysPwrMode.LVPTimer    = 0;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
        else if (SysPwrMode.LVPTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_LVP_MODE;
          SysPwrMode.LVPTimer    = 0;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
        break;

      case SYS_PWR_NORMAL_MODE  :
        if (NVM_CONFIG_POWER == 1)
        {
          if (Voltage < 15500)
          {
            SysPwrMode.LVPTimer++;
            SysPwrMode.LimitTimer++;
            SysPwrMode.OVPTimer = 0;
          }
          else if (Voltage < 17000)
          {
            SysPwrMode.LVPTimer = 0;
            SysPwrMode.LimitTimer++;
            SysPwrMode.OVPTimer = 0;
          }
          else if (Voltage >= 32000)
          {
            SysPwrMode.LVPTimer   = 0;
            SysPwrMode.LimitTimer = 0;
            SysPwrMode.OVPTimer++;
          }
          else
          {
            SysPwrMode.LVPTimer   = 0;
            SysPwrMode.LimitTimer = 0;
            SysPwrMode.OVPTimer   = 0;
          }
        }
        else
        {
          if (Voltage < 6500)
          {
            SysPwrMode.LVPTimer++;
            SysPwrMode.LimitTimer++;
            SysPwrMode.OVPTimer = 0;
          }
          else if (Voltage < 9000)
          {
            SysPwrMode.LVPTimer = 0;
            SysPwrMode.LimitTimer++;
            SysPwrMode.OVPTimer = 0;
          }
          else if (Voltage >= 18000)
          {
            SysPwrMode.LVPTimer   = 0;
            SysPwrMode.LimitTimer = 0;
            SysPwrMode.OVPTimer++;
          }
          else
          {
            SysPwrMode.LVPTimer   = 0;
            SysPwrMode.LimitTimer = 0;
            SysPwrMode.OVPTimer   = 0;
          }
        }
        if (SysPwrMode.LVPTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_LVP_MODE;
          SysPwrMode.LVPTimer   = 0;
          SysPwrMode.LimitTimer = 0;
          SysPwrMode.OVPTimer   = 0;
        }
        else if (SysPwrMode.LimitTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_LIMIT_MODE;
          SysPwrMode.LVPTimer   = 0;
          SysPwrMode.LimitTimer = 0;
          SysPwrMode.OVPTimer   = 0;
        }
        else if (SysPwrMode.OVPTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_OVP_MODE;
          SysPwrMode.LVPTimer   = 0;
          SysPwrMode.LimitTimer = 0;
          SysPwrMode.OVPTimer   = 0;
        }
        break;

      case SYS_PWR_OVP_MODE     :
        if (NVM_CONFIG_POWER == 1)
        {
          if (Voltage < 15500)
          {
            SysPwrMode.LVPTimer++;
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer++;
          }
          else if (Voltage < 17000)
          {
            SysPwrMode.LVPTimer = 0;
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer++;
          }
          else if (Voltage < 32200)
          {
            SysPwrMode.LVPTimer   = 0;
            SysPwrMode.LimitTimer = 0;
            SysPwrMode.NormalTimer++;
          }
          else
          {
            SysPwrMode.LVPTimer    = 0;
            SysPwrMode.LimitTimer  = 0;
            SysPwrMode.NormalTimer = 0;
          }
        }
        else
        {
          if (Voltage < 6500)
          {
            SysPwrMode.LVPTimer++;
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer++;
          }
          else if (Voltage < 9000)
          {
            SysPwrMode.LVPTimer = 0;
            SysPwrMode.LimitTimer++;
            SysPwrMode.NormalTimer++;
          }
          else if (Voltage < 18200)
          {
            SysPwrMode.LVPTimer   = 0;
            SysPwrMode.LimitTimer = 0;
            SysPwrMode.NormalTimer++;
          }
          else
          {
            SysPwrMode.LVPTimer    = 0;
            SysPwrMode.LimitTimer  = 0;
            SysPwrMode.NormalTimer = 0;
          }
        }      
        if (SysPwrMode.LVPTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_LVP_MODE;
          SysPwrMode.LVPTimer    = 0;
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
        }
        else if (SysPwrMode.LimitTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_LIMIT_MODE;
          SysPwrMode.LVPTimer    = 0;
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
        }
        else if (SysPwrMode.NormalTimer >= SYS_PWR_DEBOUNCE_TIME / 20)
        {
          SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
          SysPwrMode.LVPTimer    = 0;
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
        }
        break;

      default :
        SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
        SysPwrMode.LVPTimer    = 0;
        SysPwrMode.LimitTimer  = 0;
        SysPwrMode.NormalTimer = 0;
        SysPwrMode.OVPTimer    = 0;
        break;
    }

    if (SysPwrMode.Mode == SYS_PWR_LVP_MODE)                   //更新电源标志
      SysPowerStatus = SYS_PWR_STATUS_BIT_LVP;      /////////////过压模式没改
    else if (SysPwrMode.Mode == SYS_PWR_LIMIT_MODE) /////////////过压模式没改
      SysPowerStatus = SYS_PWR_STATUS_BIT_LIM;      /////////////过压模式没改
    else if (SysPwrMode.Mode == SYS_PWR_OVP_MODE)   /////////////过压模式没改
     SysPowerStatus = SYS_PWR_STATUS_BIT_OVP;       /////////////过压模式没改
    else 
     SysPowerStatus = SYS_PWR_STATUS_BIT_RUN;

  //更新系统运行状态
  #if SYS_RUNNING_IN_LIMIT_MODE
    if (SYS_PWR_STAT_UNPROTECT)
  #else
    if (SYS_PWR_STAT_NORMAL)
  #endif
    {
      OpStatus = SYS_OPR_STATUS_BIT_OFF;
      //SysOperatingStatus = SYS_OPR_STATUS_BIT_OFF;

      //ACC状态检测
      #if SYS_ACC_MODE_DET_ENABLE
        if (Sys_ACC_Status_Detect())
          OpStatus = SYS_OPR_STATUS_BIT_ACC;
	      //SysOperatingStatus = SYS_OPR_STATUS_BIT_ACC;
      #endif

      //IGN状态检测
      if (SysKL15Signal.Status == SYS_SIG_STAT_ACTIVE)
      {
        if (ANALOG_KL15_VOLTAGE < SYS_OFF_THRESHOLD)
        {
          SysKL15Signal.Timer++;
          if (SysKL15Signal.Timer >= SYS_OPR_DEBOUNCE_TIME / 20)
          {
            SysKL15Signal.Status = SYS_SIG_STAT_INACTIVE;
            SysKL15Signal.Timer  = 0;
          }
        }
        else
          SysKL15Signal.Timer = 0;
      }
      else
      {
        if (ANALOG_KL15_VOLTAGE >= SYS_ON_THRESHOLD)
        {
          SysKL15Signal.Timer++;
          if (SysKL15Signal.Timer >= SYS_OPR_DEBOUNCE_TIME / 20)
          {
            SysKL15Signal.Status = SYS_SIG_STAT_ACTIVE;
            SysKL15Signal.Timer  = 0;
          }
        }
        else
          SysKL15Signal.Timer  = 0;
      }

      if (SysKL15Signal.Status)
	      OpStatus = SYS_OPR_STATUS_BIT_IGN;
      //SysOperatingStatus = SYS_OPR_STATUS_BIT_IGN;

      //START状态检测
      #if SYS_CRANK_MODE_DET_ENABLE
        if (Sys_Crank_Status_Detect())
  	      OpStatus = SYS_OPR_STATUS_BIT_CRK;
        //SysOperatingStatus = SYS_OPR_STATUS_BIT_CRK;
      #endif
      SysOperatingStatus = OpStatus;
    }
    else
      SysOperatingStatus = SYS_OPR_STATUS_BIT_ERR;
  }
  else if (SysStatUpdateMode == SYS_STAT_UPDATE_MODE_WAKEUP) //唤醒更新模式
  {
    //对IGN电源进行5次检测,必须连续5次均高于门限值才认为IGN ON
    for (i = 0; i < 5; i++)
    {
      Analog_KL15_Voltage_Conversion();
      if (ANALOG_KL15_VOLTAGE < SYS_ON_THRESHOLD)
        return;
    }

    //默认电源为正常状态,唤醒后初始化电源状态
    LVPCnt    = 0;
    LimitCnt  = 0;
    NormalCnt = 0;
    OVPCnt    = 0;

    for (;;)
    {
      Voltage = 0;

    #if SYS_POWERED_BY_KL30_ONLY
      Analog_KL30_Voltage_Conversion();
      Voltage = ANALOG_KL30_VOLTAGE;  
    #else
      Analog_KL30_Voltage_Conversion();
      Analog_KL15_Voltage_Conversion();
      if (ANALOG_KL15_VOLTAGE > ANALOG_KL30_VOLTAGE)
        Voltage = ANALOG_KL15_VOLTAGE;
      else
        Voltage = ANALOG_KL30_VOLTAGE;
    #endif
      if (NVM_CONFIG_POWER == 1)
      {  
        if (Voltage < 15500)
        {
          LVPCnt++;
          LimitCnt++;
          NormalCnt = 0;
          OVPCnt    = 0;
        }
        else if (Voltage < 17000)
        {
          LVPCnt    = 0;
          LimitCnt++;
          NormalCnt = 0;
          OVPCnt    = 0;
        }
        else if (Voltage >= 32000)
        {
          LVPCnt    = 0;
          LimitCnt  = 0;
          NormalCnt = 0;
          OVPCnt++;
        }
        else
        {
          LVPCnt    = 0;
          LimitCnt  = 0;
          NormalCnt++;
          OVPCnt    = 0;
        }
      }
      else
      {  
        if (Voltage < 6500)
        {
          LVPCnt++;
          LimitCnt++;
          NormalCnt = 0;
          OVPCnt    = 0;
        }
        else if (Voltage < 9000)
        {
          LVPCnt    = 0;
          LimitCnt++;
          NormalCnt = 0;
          OVPCnt    = 0;
        }
        else if (Voltage >= 18000)
        {
          LVPCnt    = 0;
          LimitCnt  = 0;
          NormalCnt = 0;
          OVPCnt++;
        }
        else
        {
          LVPCnt    = 0;
          LimitCnt  = 0;
          NormalCnt++;
          OVPCnt    = 0;
        }
      }
      
      if (LVPCnt >= 5)
      {
        SysPwrMode.Mode      = SYS_PWR_LVP_MODE;
        SysPowerStatus       = SYS_PWR_STATUS_BIT_LVP;
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_ERR;
        return;
      }

      if (LimitCnt >= 5)
      {
        SysPwrMode.Mode      = SYS_PWR_LIMIT_MODE;
        SysKL15Signal.Status = SYS_SIG_STAT_ACTIVE;
        SysPowerStatus       = SYS_PWR_STATUS_BIT_LIM;
      #if SYS_RUNNING_IN_LIMIT_MODE
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_IGN;
      #else
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_ERR;
      #endif
        return;
      }

      if (NormalCnt >= 5)
      {
        SysPwrMode.Mode      = SYS_PWR_NORMAL_MODE;
        SysKL15Signal.Status = SYS_SIG_STAT_ACTIVE;
        SysPowerStatus       = SYS_PWR_STATUS_BIT_RUN;
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_IGN;
        return;
      }

      if (OVPCnt >= 5)
      {
        SysPwrMode.Mode      = SYS_PWR_OVP_MODE;
        SysPowerStatus       = SYS_PWR_STATUS_BIT_OVP;
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_ERR;
        return;
      }
    }
  }
  else if (SysStatUpdateMode == SYS_STAT_UPDATE_MODE_RECOVERY) //异常保护恢复更新模式
  {
    Voltage = 0;

  #if SYS_POWERED_BY_KL30_ONLY
    Analog_KL30_Voltage_Conversion();
    Voltage = ANALOG_KL30_VOLTAGE;  
  #else
    Analog_KL30_Voltage_Conversion();
    Analog_KL15_Voltage_Conversion();
    if (ANALOG_KL15_VOLTAGE > ANALOG_KL30_VOLTAGE)
      Voltage = ANALOG_KL15_VOLTAGE;
    else
      Voltage = ANALOG_KL30_VOLTAGE;
  #endif

    //异常保护恢复更新模式下只对电源状态进行监控
    if (SysPwrMode.Mode == SYS_PWR_LVP_MODE)
    {
      if (NVM_CONFIG_POWER == 1)
      {  
        if (Voltage >= 32000)
        {
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer++;
          SysPwrMode.OVPTimer++;
        }
        else if (Voltage >= 18000)
        {
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer++;
          SysPwrMode.OVPTimer = 0;
        }
        else if (Voltage >= 16000)
        {
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
        else
        {
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
      }
      else
      {  
        if (Voltage >= 18000)
        {
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer++;
          SysPwrMode.OVPTimer++;
        }
        else if (Voltage >= 9500)
        {
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer++;
          SysPwrMode.OVPTimer = 0;
        }
        else if (Voltage >= 7000)
        {
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
        else
        {
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
          SysPwrMode.OVPTimer    = 0;
        }
      }
      if (SysPwrMode.OVPTimer >= SYS_PWR_DEBOUNCE_TIME / 100)
      {
        SysPwrMode.Mode = SYS_PWR_OVP_MODE;
        SysPwrMode.LimitTimer  = 0;
        SysPwrMode.NormalTimer = 0;
        SysPwrMode.OVPTimer    = 0;
      }
      else if (SysPwrMode.NormalTimer >= SYS_PWR_DEBOUNCE_TIME / 100)
      {
        SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
        SysPwrMode.LimitTimer  = 0;
        SysPwrMode.NormalTimer = 0;
        SysPwrMode.OVPTimer    = 0;
      }
      else if (SysPwrMode.LimitTimer >= SYS_PWR_DEBOUNCE_TIME / 100)
      {
        SysPwrMode.Mode = SYS_PWR_LIMIT_MODE;
        SysPwrMode.LimitTimer  = 0;
        SysPwrMode.NormalTimer = 0;
        SysPwrMode.OVPTimer    = 0;
      }
    }
    else if (SysPwrMode.Mode == SYS_PWR_OVP_MODE)
    {
      if (NVM_CONFIG_POWER == 1)
      {    
        if (Voltage < 15500)
        {
          SysPwrMode.LVPTimer++;
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer++;
        }
        else if (Voltage < 17000)
        {
          SysPwrMode.LVPTimer = 0;
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer++;
        }
        else if (Voltage < 32200)
        {
          SysPwrMode.LVPTimer   = 0;
          SysPwrMode.LimitTimer = 0;
          SysPwrMode.NormalTimer++;
        }
        else
        {
          SysPwrMode.LVPTimer    = 0;
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
        }
      }
      else
      {    
        if (Voltage < 6500)
        {
          SysPwrMode.LVPTimer++;
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer++;
        }
        else if (Voltage < 9000)
        {
          SysPwrMode.LVPTimer = 0;
          SysPwrMode.LimitTimer++;
          SysPwrMode.NormalTimer++;
        }
        else if (Voltage < 18200)
        {
          SysPwrMode.LVPTimer   = 0;
          SysPwrMode.LimitTimer = 0;
          SysPwrMode.NormalTimer++;
        }
        else
        {
          SysPwrMode.LVPTimer    = 0;
          SysPwrMode.LimitTimer  = 0;
          SysPwrMode.NormalTimer = 0;
        }
      }      
      if (SysPwrMode.LVPTimer >= SYS_PWR_DEBOUNCE_TIME / 100)
      {
        SysPwrMode.Mode = SYS_PWR_LVP_MODE;
        SysPwrMode.LVPTimer    = 0;
        SysPwrMode.LimitTimer  = 0;
        SysPwrMode.NormalTimer = 0;
      }
      else if (SysPwrMode.LimitTimer >= SYS_PWR_DEBOUNCE_TIME / 100)
      {
        SysPwrMode.Mode = SYS_PWR_LIMIT_MODE;
        SysPwrMode.LVPTimer    = 0;
        SysPwrMode.LimitTimer  = 0;
        SysPwrMode.NormalTimer = 0;
      }
      else if (SysPwrMode.NormalTimer >= SYS_PWR_DEBOUNCE_TIME / 100)
      {
        SysPwrMode.Mode = SYS_PWR_NORMAL_MODE;
        SysPwrMode.LVPTimer    = 0;
        SysPwrMode.LimitTimer  = 0;
        SysPwrMode.NormalTimer = 0;
      }
    }

    if (SysPwrMode.Mode == SYS_PWR_LVP_MODE)                    //更新电源标志
      SysPowerStatus       = SYS_PWR_STATUS_BIT_LVP;
    else if (SysPwrMode.Mode == SYS_PWR_LIMIT_MODE)
    {
      SysKL15Signal.Status = SYS_SIG_STAT_INACTIVE;
      SysPowerStatus       = SYS_PWR_STATUS_BIT_LIM;
    #if SYS_RUNNING_IN_LIMIT_MODE
      SysOperatingStatus   = SYS_OPR_STATUS_BIT_OFF;
    #else
      SysOperatingStatus   = SYS_OPR_STATUS_BIT_ERR;
    #endif
    }
    else if (SysPwrMode.Mode == SYS_PWR_OVP_MODE)
      SysPowerStatus       = SYS_PWR_STATUS_BIT_OVP;
    else
    {
      SysKL15Signal.Status = SYS_SIG_STAT_INACTIVE;
      SysPowerStatus       = SYS_PWR_STATUS_BIT_RUN;
      SysOperatingStatus   = SYS_OPR_STATUS_BIT_OFF;
    }
  }
  else                                                          //初始化更新模式
  {
    //上电延时150ms,等待电源电压稳定
    RocBackup = API_ROLLING_COUNTER;
    do
    {
      Counter  = API_ROLLING_COUNTER;
      Counter -= RocBackup;
    }
    while (Counter < (150000 / API_INT_CYCLE));

    //默认电源为正常状态,初始化电源状态
    LVPCnt    = 0;
    LimitCnt  = 0;
    NormalCnt = 0;
    OVPCnt    = 0;

    for (;;)
    {
      Voltage = 0;
      
    #if SYS_POWERED_BY_KL30_ONLY
      Analog_KL30_Voltage_Conversion();
      Voltage = ANALOG_KL30_VOLTAGE;
    #else
      Analog_KL15_Voltage_Conversion();
      Analog_KL30_Voltage_Conversion();
      if (ANALOG_KL15_VOLTAGE > ANALOG_KL30_VOLTAGE)
        Voltage = ANALOG_KL15_VOLTAGE;
      else
        Voltage = ANALOG_KL30_VOLTAGE;
    #endif
      if (NVM_CONFIG_POWER == 1)
      { 
        if (Voltage < 15500)
        {
          LVPCnt++;
          LimitCnt++;
          NormalCnt = 0;
          OVPCnt    = 0;
        }
        else if (Voltage < 17000)
        {
          LVPCnt    = 0;
          LimitCnt++;
          NormalCnt = 0;
          OVPCnt    = 0;
        }
        else if (Voltage >= 32000)
        {
          LVPCnt    = 0;
          LimitCnt  = 0;
          NormalCnt = 0;
          OVPCnt++;
        }
        else
        {
          LVPCnt    = 0;
          LimitCnt  = 0;
          NormalCnt++;
          OVPCnt    = 0;
        }
      }
      else
      { 
        if (Voltage < 6500)
        {
          LVPCnt++;
          LimitCnt++;
          NormalCnt = 0;
          OVPCnt    = 0;
        }
        else if (Voltage < 9000)
        {
          LVPCnt    = 0;
          LimitCnt++;
          NormalCnt = 0;
          OVPCnt    = 0;
        }
        else if (Voltage >= 18000)
        {
          LVPCnt    = 0;
          LimitCnt  = 0;
          NormalCnt = 0;
          OVPCnt++;
        }
        else
        {
          LVPCnt    = 0;
          LimitCnt  = 0;
          NormalCnt++;
          OVPCnt    = 0;
        }
      }
      if (LVPCnt >= 5)
      {
        SysPwrMode.Mode      = SYS_PWR_LVP_MODE;
        SysPowerStatus       = SYS_PWR_STATUS_BIT_LVP;
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_ERR;
        return;
      }

      if (LimitCnt >= 5)
      {
        SysPwrMode.Mode      = SYS_PWR_LIMIT_MODE;
        SysKL15Signal.Status = SYS_SIG_STAT_INACTIVE;
        SysPowerStatus       = SYS_PWR_STATUS_BIT_LIM;
      #if SYS_RUNNING_IN_LIMIT_MODE
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_OFF;
      #else
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_ERR;
      #endif
        return;
      }

      if (NormalCnt >= 5)
      {
        SysPwrMode.Mode      = SYS_PWR_NORMAL_MODE;
        SysKL15Signal.Status = SYS_SIG_STAT_INACTIVE;
        SysPowerStatus       = SYS_PWR_STATUS_BIT_RUN;
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_OFF;
        return;
      }

      if (OVPCnt >= 5)
      {
        SysPwrMode.Mode      = SYS_PWR_OVP_MODE;
        SysPowerStatus       = SYS_PWR_STATUS_BIT_OVP;
        SysOperatingStatus   = SYS_OPR_STATUS_BIT_ERR;
        return;
      }
    }
  }
}

/******************************************************************************
函数名：Sys_ACC_Status_Detect
功  能：ACC状态检测
参  数：无
返回值：0 - ACC关闭 1 - ACC开启
******************************************************************************/
uint8_t Sys_ACC_Status_Detect(void)
{
  /* if ((PEPS_PowerModeValidity == 0x02) && (PEPS_PowerMode == 0x01))
    return 1;
  else
    return 0;*/
}
 
/******************************************************************************
函数名：Sys_Crank_Status_Detect
功  能：CRANK状态检测
参  数：无
返回值：0 - CRANK关闭 1 - CRANK开启
******************************************************************************/
uint8_t Sys_Crank_Status_Detect(void)
{
  /* if ((PEPS_PowerModeValidity == 0x02) && (PEPS_PowerMode == 0x04))
    return 1;
  else
    return 0;    */
}
