/** ##########################################################################
**     Filename  : WheelHeat.c
**     Project   :
**     Module    :
**     Processor :
**     Version   : 1.0
**     Compiler  :
**     Date/Time :
**     Abstract  :
**     Contents  :
**     Note      :
**     (c) Copyright dmdz Co.,Ltd
**  --------------------------------------------------------------------------
**               R E V I S I O N   H I S T O R Y
**  --------------------------------------------------------------------------
**  Date        Ver      Author             Description

**  -20231107-  V01-- --Linboyi---            --初版--
** #########################################################################*/




/*****************************************************************************
* I N C L U D E   F I L E S
*****************************************************************************/
#include "Cpu.h"
#include "common_types.h"
#include "common_memory.h"
#include "WheelHeat.h"
#include "Com_Cfg.h"
#include "adc.h"
#include "pwm_manage.h"
#include "Com.h"
#include "Com_Cfg.h"
#include "can_app.h"
#include "system_voltage_manage.h"
#include "system_voltage_manage_cfg.h"
#include "DEM_Cfg.h"
#include "Demm.h"
/*****************************************************************************
* D E F I N E S / M A C R O S11
*****************************************************************************/

#define WHEEL_HEAT_STATE_ON     1u
#define WHEEL_HEAT_STATE_OFF    0u
#define WHEEL_HEAT_STATE_HOLD   2u
#define WHEEL_HEAT_STATE_HARD_FALUT 3u
#define WHEEL_HEAT_STATE_SLOW_DOWN  4u


#define WHEEL_HEAT_TIME_30MIN   (1800000ul / 10ul)
#define WHEEL_HEAT_GEAR_OFF_CLOSE_DELAY_5S  (5000ul / 10ul)
#define WHEEL_HEAT_OPEN_SHORT_DETECT_2S  (2000ul / 10ul)
#define WHEEL_HEAT_OPEN_SHORT_DETECT_10S  (10000ul / 10ul)

#define TEMP_SENSOR_OPEN_ADC_RESULT  4900        
#define TEMP_SENSOR_SHORT_ADC_RESULT 100

#define WHEEL_HEAT_WIRE_ERROR_3S  (3000ul / 10ul)
#define WHEEL_HEAT_WIRE_ERROR_2S  (2950ul / 10ul)

#define WHEEL_HEAT_SHORT_ADC_RESULT 640
#define WHEEL_HEAT_OPEN_ADC_RESULT  (uint8_t)50
const uint16_t HEAT_LEVEL_NTC_TAB[WHEEL_HEAT_NTCLV_NUM] = 
{
   WHEEL_HEAT_NTC_LV3,WHEEL_HEAT_NTC_LV1,WHEEL_HEAT_NTC_LV2,WHEEL_HEAT_NTC_LV3
}; 
/*****************************************************************************
* T Y P E   D E F I N I T I O N S
*****************************************************************************/
typedef struct 
{
    uint8_t ucEvent;
    //uint32_t ulRunTimer;
    uint8_t ucEcuControlState;
    uint8_t ucDcmControlState;
    uint8_t ucIsControlByDcm;
    //uint8_t ucErrorTimer;
    uint16_t usOffGearDelay;
    uint8_t  ucGearEvent;
    uint16_t  ucTempSensorFalutTimer;
    uint16_t  u16OverCurrentFalutTimer;
    uint8_t  u16OverCurrentFlag;
    uint32_t ulForceOutput;
    uint16_t  usSlowDownTimer;
    uint16_t  usSlowDownDuty;
    uint16_t  usSlowDownTimerStep;
    uint8_t usDigNtcState;
    uint8_t usDigOverCurrent;
	uint8_t ucHeatGear;
    uint8_t ucLimitAcc;
    uint16_t ucHeatDigaTimeOpen;
    //uint16_t ucHeatWireOpenTimeOpen;
    uint16_t ucHeatDigaTime;
    uint16_t ucHeatWireOpenTime;
	uint16_t ucHeatWireShortTime;
	uint8_t ucHeatWireOpenFlag;
	uint8_t ucHeatWireShortFlag;
	uint8_t ucHeatPowerOnFlag;
    uint8_t ucHeatOnFlag;
}WheelHeat_Struct_Type;


/**************************************1***************************************
* G L O B A L   V A R I A B L E S
* only configuration table allowed here,variables are not allowed!
*****************************************************************************/
static WheelHeat_Struct_Type WheelHeat_Struct;
/*****************************************************************************
* C O N S T A N T S
*****************************************************************************/

/*****************************************************************************
* F U N C T I O N   P R O T O T Y P E
*****************************************************************************/
static uint8_t WheelHeat_GetEvent(void);
static void WheelHeat_Handle(void);
static void WheelHeat_RunTime(void);
static uint8_t WheelHeat_GetGearEvevt(void);
static void WheelHeat_OffGearDelay(void);
static void WheelHeat_EventProcess(void);
static void WheelHeat_CanTxProcess(void);
static void WheelHeat_Diag(void);
/*****************************************************************************
|Prototype			: WheelHeat_Init
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_Init(void)
{
    common_memory_fill((uint8_t *)(&WheelHeat_Struct.ucEvent), 0u, sizeof(WheelHeat_Struct));
    WheelHeat_SetHeating_Gear(HEAT_LV3);
	WheelHeat_Struct.ucHeatPowerOnFlag =HEAT_DIAG_ENABLE;
}




/*****************************************************************************
|Prototype			: WheelHeat_SetEvent
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_SetEvent(uint8_t Event)
{
    (void)System_DisableIrqGlobal();
    WheelHeat_Struct.ucEvent = Event;
    (void)System_EnableIrqGlobal();
}

/*****************************************************************************
|Prototype			: WheelHeat_Set_Heat_Limit_ACC
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_Set_Heat_Limit_ACC(uint8_t Evevt)
{
    WheelHeat_Struct.ucLimitAcc = Evevt;
}
/*****************************************************************************
|Prototype			: WheelHeat_GetEvent
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
static uint8_t WheelHeat_GetEvent(void)
{
    uint8_t Event;
    (void)System_DisableIrqGlobal();
    Event = WheelHeat_Struct.ucEvent;
    if(COM_RXIPDUMETER_1_DIR_4BF_TIMEOUTFLAG ==HEAT_DIAG_ENABLE)
    {
        WheelHeat_Set_Heat_Limit_ACC(Heat_Limit_ACC_DISABLE);
    }   
    if((WheelHeat_Struct.ucLimitAcc == Heat_Limit_ACC_Enbale) &&(WheelHeat_Struct.ucEcuControlState == WHEEL_HEAT_STATE_OFF))
    {
        Event = WHEEL_HEAT_EVENT_IDLE;
    }
    WheelHeat_Struct.ucEvent = WHEEL_HEAT_EVENT_IDLE;
    (void)System_EnableIrqGlobal();

    return (Event);


}

/*****************************************************************************
|Prototype			: WheelHeat_Handle
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
static void WheelHeat_Handle(void)
{
    uint16_t NTC_AdcResult;
    uint8_t SystemVoltageMod;
    uint8_t State;

    NTC_AdcResult = adc_get_voltage_value(1);
    SystemVoltageMod = system_voltage_manage_mode_get(FUNC_SYSTEM_VOLTAGE_MANAGE_ID);

    if(WheelHeat_Struct.ucIsControlByDcm > 0u)
    {
        State = WheelHeat_Struct.ucDcmControlState;
    }
    else
    {
        if((SystemVoltageMod == SYSTEM_VOLTAGE_MODE_LOW) || (SystemVoltageMod == SYSTEM_VOLTAGE_MODE_HIGH))
        {
            State = WHEEL_HEAT_STATE_OFF;
        }
        else
        {
            State = WheelHeat_Struct.ucEcuControlState;
        }
        
    }

    switch(State)
    {
        case WHEEL_HEAT_STATE_OFF:
			if(WheelHeat_Struct.ucHeatPowerOnFlag ==HEAT_DIAG_ENABLE)
            {
                WheelHeat_Wire_Short_Check_PowerOn();
            }
            else
            {
            	pwm_set_duty(PWM_BASE_ID_GPTM0, (uint8_t)1, (uint16_t)0);
			}
            break;
        case WHEEL_HEAT_STATE_ON:
            if(WheelHeat_Struct.ulForceOutput > 0)
            {
                pwm_set_duty(PWM_BASE_ID_GPTM0, (uint8_t)1, (uint16_t)1000);
                WheelHeat_Struct.ulForceOutput --;
                if(NTC_AdcResult <= 3081)
                {
                   WheelHeat_Struct.ulForceOutput = 0;
                   WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_HOLD;
                   WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_HOLD; 
                }
                if(WheelHeat_Struct.ulForceOutput == 0)
                {
                    WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_SLOW_DOWN;
                    WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_SLOW_DOWN;                    
                }
            }
            else
            {
                WheelHeat_HeatingGear_STATE_ON();//档位加热功能调用
            }
            break;
        case WHEEL_HEAT_STATE_HOLD:
            WheelHeat_HeatingGear_STATE_HOLD();//档位加热功能调用
            break;
        case WHEEL_HEAT_STATE_SLOW_DOWN:
            WheelHeat_Struct.usSlowDownTimer ++;
            if(WheelHeat_Struct.usSlowDownTimer >= WheelHeat_Struct.usSlowDownTimerStep)
            {
                WheelHeat_Struct.usSlowDownTimer = 0;
                if(WheelHeat_Struct.usSlowDownDuty  > (uint16_t)600)
                {
                    WheelHeat_Struct.usSlowDownDuty -= (uint16_t)1;
                    pwm_set_duty(PWM_BASE_ID_GPTM0, (uint8_t)1, WheelHeat_Struct.usSlowDownDuty);
                }
                else
                {
                    WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_HOLD;
                    WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_HOLD;                    
                }
            }
            if(NTC_AdcResult <= 3081u)
            {
               WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_HOLD;
               WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_HOLD; 
            }
            break;
        case WHEEL_HEAT_STATE_HARD_FALUT:
			if(WheelHeat_Struct.ucHeatPowerOnFlag ==HEAT_DIAG_ENABLE)
            {
			     WheelHeat_Wire_Short_Check_PowerOn();
            }
            else
            {
           		 pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 0);
			}
            break;
        default:
            while(1);

    }
}


/*****************************************************************************
|Prototype			: WheelHeat_RunTime
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
static void WheelHeat_RunTime(void)
{
    #if 0 //取消30分钟加热超时

    if(WheelHeat_Struct.ucEcuControlState > WHEEL_HEAT_STATE_OFF)
    {
        if(WheelHeat_Struct.ulRunTimer < 0xfffffffful)
        {
            WheelHeat_Struct.ulRunTimer ++;
        }
        if(WheelHeat_Struct.ulRunTimer >= WHEEL_HEAT_TIME_30MIN)
        {
            WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
        }
    }
    else
    {
        WheelHeat_Struct.ulRunTimer = 0;
    }
    #endif
}


/*****************************************************************************
|Prototype			: WheelHeat_SetGearEvevt
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_SetGearEvevt(uint8_t Evevt)
{
    WheelHeat_Struct.ucGearEvent = Evevt;
}

/*****************************************************************************
|Prototype			: WheelHeat_SetHeating_Gear
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_SetHeating_Gear(uint8_t Evevt)
{
    WheelHeat_Struct.ucHeatGear = Evevt;
}

/*****************************************************************************
|Prototype			: WheelHeat_SetHeating_Gear
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_HeatingGear_STATE_ON(void) //维持在客户需求温度的加热功率 
{
    u8 heat_gear;
    uint16_t NTC_AdcResult;
    NTC_AdcResult = adc_get_voltage_value(1);
    if(WheelHeat_Struct.ucHeatGear == 0u)
    {
        WheelHeat_Struct.ucHeatGear= HEAT_LV3;
    }
    heat_gear = WheelHeat_Struct.ucHeatGear;
    switch(heat_gear)
    {
        case HEAT_LV1:
                    if(NTC_AdcResult <= 3690u)
                    {
                        pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 400u);
                        WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_HOLD;
                        WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_HOLD;
                    }
                    else
                    {
                        pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 1000);
                    }
                    break;
        case HEAT_LV2:
                      if(NTC_AdcResult <= 3400u)
                    {
                        pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 400);
                        WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_HOLD;
                        WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_HOLD;
                    }
                    else
                    {
                         pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 1000);
                    }
                    break;
        case HEAT_LV3:
                      if(NTC_AdcResult <= 3350u)
                    {
                        pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 500);
                        WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_HOLD;
                        WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_HOLD;
                    }
                    else
                    {
                         pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 1000);
                    }
                    break;
                    default:
                    break;    
    }      
}

/*****************************************************************************
|Prototype			: WheelHeat_SetHeating_Gear
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_HeatingGear_STATE_HOLD(void)
{
    u8 heat_gear;
    uint16_t NTC_AdcResult;
    NTC_AdcResult = adc_get_voltage_value(1);
    if(WheelHeat_Struct.ucHeatGear == 0u)
    {
        WheelHeat_Struct.ucHeatGear= HEAT_LV3;
    }
    heat_gear = WheelHeat_Struct.ucHeatGear;
    switch(heat_gear)
    {
        case HEAT_LV1:
                if(NTC_AdcResult >= 3600u)
                {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 1000);
                    WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_ON;
                    WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_ON;
                }
               else if(NTC_AdcResult < 3510u)
               {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 0);
               }
               else
               {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 400);
               }
                    break;
        case HEAT_LV2:
             if(NTC_AdcResult >= 3450u)
                {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 1000);
                    WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_ON;
                    WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_ON;
                }
               if(NTC_AdcResult < 3300u)
               {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 0);
               }
               else
               {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 400);
               }
                    break;
        case HEAT_LV3:
                if(NTC_AdcResult >= 3280u)
                {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 1000);
                    WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_ON;
                    WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_ON;
                }
               if(NTC_AdcResult < 3200u)
               {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 0);
               }
               else
               {
                    pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 500);
               }
                    break;
         default:
         break;    
    }      
}

/*****************************************************************************
|Prototype			: WheelHeat_GetGearEvevt
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
static uint8_t WheelHeat_GetGearEvevt(void)
{
    uint8_t Event;

    Event = WheelHeat_Struct.ucGearEvent;
    WheelHeat_Struct.ucGearEvent = POWER_GEAR_EVENT_NULL;

    return (Event);
}


/*****************************************************************************
|Prototype			: WheelHeat_OffGearDelay
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
static void WheelHeat_OffGearDelay(void)
{
    uint8_t Event;

    Event = WheelHeat_GetGearEvevt();
    if(WheelHeat_Struct.usOffGearDelay)
    {
        WheelHeat_Struct.usOffGearDelay --;
        if(WheelHeat_Struct.usOffGearDelay == 0u)
        {
            WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
        }
    }
    else
    {
        WheelHeat_Struct.usOffGearDelay = 0;
        if(WheelHeat_Struct.ucEcuControlState > WHEEL_HEAT_STATE_OFF)
        {
            if(Event == POWER_GEAR_EVENT_OFF)
            {
                WheelHeat_Struct.usOffGearDelay = WHEEL_HEAT_GEAR_OFF_CLOSE_DELAY_5S;
            }
        }
    }
}

/*****************************************************************************
|Prototype			: WheelHeat_ControlForDcm
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_ControlForDcm(uint8_t Open)
{
    WheelHeat_Struct.ucIsControlByDcm = 1u;
    if(Open)
    {
        if(WheelHeat_Struct.ucDcmControlState == WHEEL_HEAT_STATE_OFF)
        {
            WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_ON;
        }
        WheelHeat_Struct.ucTempSensorFalutTimer = 0;
        WheelHeat_Struct.u16OverCurrentFalutTimer = 0;
    }
    else
    {   
        WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_OFF;
    }
}


/*****************************************************************************
|Prototype			: WheelHeat_ReturnControlForEcu
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_ReturnControlForEcu(void)
{
    WheelHeat_Struct.ucIsControlByDcm = 0u;
}


/*****************************************************************************
|Prototype			: WheelHeat_EventProcess
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
static void WheelHeat_EventProcess(void)
{
    uint8_t Event;
    uint8_t PowerGear;
    uint16_t NTC_AdcResult;

    Event = WheelHeat_GetEvent();
    NTC_AdcResult = adc_get_voltage_value(1);
    can_app_receive_signal(COM_RXSIGBCM1_POWER_GEAR,&PowerGear);

    if(WHEEL_HEAT_EVENT_OPEN == Event)
    {
        if(PowerGear == POWER_GEAR_ON || PowerGear == POWER_GEAR_ACC)
        {
            if(WheelHeat_Struct.ucEcuControlState == WHEEL_HEAT_STATE_OFF)
            {
                WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_ON;
                 WheelHeat_Struct.ucHeatPowerOnFlag = HEAT_DIAG_DISABLE;
                if(NTC_AdcResult >= TEMP_POSITIVE_0C && NTC_AdcResult < TEMP_NEGATIVE_5C)
                {
                    WheelHeat_Struct.ulForceOutput = WHEEL_HEAT_FORCE_OUTPUT_4MIN;
                    WheelHeat_Struct.usSlowDownTimerStep = 600 / 10;
                    WheelHeat_Struct.usSlowDownTimer = 0;
                    WheelHeat_Struct.usSlowDownDuty = 1000;
                }
                else if(NTC_AdcResult >= TEMP_NEGATIVE_5C && NTC_AdcResult < TEMP_NEGATIVE_10C)
                {
                    WheelHeat_Struct.ulForceOutput = WHEEL_HEAT_FORCE_OUTPUT_5MIN;
                    WheelHeat_Struct.usSlowDownTimerStep = 600 / 10;
                    WheelHeat_Struct.usSlowDownTimer = 0;
                    WheelHeat_Struct.usSlowDownDuty = 1000;
                }
                else if(NTC_AdcResult > TEMP_NEGATIVE_10C && NTC_AdcResult < TEMP_NEGATIVE_15C)
                {
                    WheelHeat_Struct.ulForceOutput = WHEEL_HEAT_FORCE_OUTPUT_6MIN;
                    WheelHeat_Struct.usSlowDownTimerStep = 800 / 10;
                    WheelHeat_Struct.usSlowDownTimer = 0;
                    WheelHeat_Struct.usSlowDownDuty = 1000;
                }
                else if(NTC_AdcResult >= TEMP_NEGATIVE_15C && NTC_AdcResult < TEMP_NEGATIVE_25C)
                {
                    WheelHeat_Struct.ulForceOutput = WHEEL_HEAT_FORCE_OUTPUT_10MIN;
                    WheelHeat_Struct.usSlowDownTimerStep = 1000 / 10;
                    WheelHeat_Struct.usSlowDownTimer = 0;
                    WheelHeat_Struct.usSlowDownDuty = 1000;
                }
                else if(NTC_AdcResult >= TEMP_NEGATIVE_25C)
                {
                    WheelHeat_Struct.ulForceOutput = WHEEL_HEAT_FORCE_OUTPUT_8MIN;
                    WheelHeat_Struct.usSlowDownTimerStep = 1200 / 10;
                    WheelHeat_Struct.usSlowDownTimer = 0;
                    WheelHeat_Struct.usSlowDownDuty = 1000;
                }
                else
                {
                    WheelHeat_Struct.ulForceOutput = 0;
                }
            }
            //WheelHeat_Struct.ulRunTimer = 0;
            WheelHeat_Struct.ucTempSensorFalutTimer = 0;
            WheelHeat_Struct.u16OverCurrentFalutTimer = 0;
        }
    }
    else if(WHEEL_HEAT_EVENT_CLOSE == Event)
    {
        if (WheelHeat_Struct.ucEcuControlState > WHEEL_HEAT_STATE_OFF)
        {
            WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
        }
    }
    else
    {

    }
}

/*****************************************************************************
|Prototype			: WheelHeat_CanTxProcess
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
static void WheelHeat_CanTxProcess(void)
{
    uint8_t Output;
    uint8_t  State;

    if(WheelHeat_Struct.ucIsControlByDcm > 0u)
    {
        State = WheelHeat_Struct.ucDcmControlState;
    }
    else
    {
        State = WheelHeat_Struct.ucEcuControlState;
    }
    if(State > WHEEL_HEAT_STATE_OFF)
    {
        Output = 0x02;
    }
    else
    {
        Output = 0x01;
    }
    if(State == WHEEL_HEAT_STATE_HARD_FALUT)
    {
        Output = 0x01;
    }
    Com_SendSignal(COM_TX_SIGSWS_C_SWH_STATE, &Output);
    Output = WheelHeat_Struct.ucHeatGear;
    Com_SendSignal(COM_TX_SIGSWS_C_HEAT_GEAR, &Output);
}


/*****************************************************************************
|Prototype			: WheelHeat_Diag
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
static void WheelHeat_Diag(void)
{
    uint16_t NTC_AdcResult;
    uint16_t WheelHeatFeedbackAdcResult;
    uint8_t  State;

    if(WheelHeat_Struct.ucIsControlByDcm > 0u)
    {
        State = WheelHeat_Struct.ucDcmControlState;
    }
    else
    {
        State = WheelHeat_Struct.ucEcuControlState;
    }

    WheelHeatFeedbackAdcResult = adc_get_voltage_value(ADC_LOGIC_CHANNEL_SNS);
    NTC_AdcResult = adc_get_voltage_value(1);
   
        if(NTC_AdcResult >= TEMP_SENSOR_OPEN_ADC_RESULT)
        {
            WheelHeat_Struct.ucTempSensorFalutTimer ++;
            if(WheelHeat_Struct.ucTempSensorFalutTimer >= WHEEL_HEAT_OPEN_SHORT_DETECT_2S)
            {
                WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
                dem_set_test_result(DTC_SWH_NTC_OPEN_INDEX,DTC_TEST_STATE_FAILED);
            }
        }
        else if(NTC_AdcResult <= TEMP_SENSOR_SHORT_ADC_RESULT)
        {
            WheelHeat_Struct.ucTempSensorFalutTimer ++;
            if(WheelHeat_Struct.ucTempSensorFalutTimer >= WHEEL_HEAT_OPEN_SHORT_DETECT_2S)
            {
                WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
                dem_set_test_result(DTC_SWH_NTC_SHORT_INDEX,DTC_TEST_STATE_FAILED);
                
            }
        }
        else
        {
            WheelHeat_Struct.ucTempSensorFalutTimer = 0;
            dem_set_test_result(DTC_SWH_NTC_OPEN_INDEX,DTC_TEST_STATE_PASSED);
            dem_set_test_result(DTC_SWH_NTC_SHORT_INDEX,DTC_TEST_STATE_PASSED);
        }
   
      
    
    
    if (State > WHEEL_HEAT_STATE_OFF)
    {
        if(WheelHeatFeedbackAdcResult >= WHEEL_HEAT_SHORT_ADC_RESULT)
        {
            WheelHeat_Struct.u16OverCurrentFalutTimer ++;
            if(WheelHeat_Struct.u16OverCurrentFalutTimer >= WHEEL_HEAT_OPEN_SHORT_DETECT_10S)
            {
                WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.u16OverCurrentFlag = HEAT_DIAG_ENABLE;
                dem_set_test_result(DTC_HEAT_OVERCURRENT_INDEX,DTC_TEST_STATE_FAILED);
                WheelHeat_Struct.u16OverCurrentFalutTimer=0;

            }
        }
        else 
        {       
            if(WheelHeat_Struct.u16OverCurrentFlag == HEAT_DIAG_ENABLE)
            {
                dem_set_test_result(DTC_HEAT_OVERCURRENT_INDEX,DTC_TEST_STATE_PASSED);
            }
            WheelHeat_Struct.u16OverCurrentFlag = HEAT_DIAG_DISABLE;
            WheelHeat_Struct.u16OverCurrentFalutTimer = 0;
        }
    }
    else
    {
        if(WheelHeat_Struct.u16OverCurrentFlag == HEAT_DIAG_ENABLE)
        {
            dem_set_test_result(DTC_HEAT_OVERCURRENT_INDEX,DTC_TEST_STATE_PASSED);
        }
        WheelHeat_Struct.u16OverCurrentFalutTimer = 0;
        WheelHeat_Struct.u16OverCurrentFlag = HEAT_DIAG_DISABLE;
    }

    if((State == WHEEL_HEAT_STATE_OFF ) && (WheelHeat_Struct.ucHeatPowerOnFlag ==HEAT_DIAG_DISABLE))
	{
		if(WheelHeatFeedbackAdcResult >= HEAT_CURRENT)
		{
			WheelHeat_Struct.ucHeatWireShortTime++;
			if(WheelHeat_Struct.ucHeatWireShortTime>=WHEEL_HEAT_WIRE_ERROR_3S)
			{
				WheelHeat_Struct.ucHeatWireShortFlag = HEAT_DIAG_ENABLE;
				dem_set_test_result(DTC_SWH_STRIP_SHORT_INDEX,DTC_TEST_STATE_FAILED);
				WheelHeat_Struct.ucHeatWireShortTime=0;
                WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
			}
		}
		if(WheelHeatFeedbackAdcResult<= HEAT_CURRENT)
		{
			if(WheelHeat_Struct.ucHeatWireShortFlag == HEAT_DIAG_ENABLE)
			{
				dem_set_test_result(DTC_SWH_STRIP_SHORT_INDEX,DTC_TEST_STATE_PASSED);
				WheelHeat_Struct.ucHeatWireShortFlag = HEAT_DIAG_DISABLE;
			}
			WheelHeat_Struct.ucHeatWireShortTime = 0;
		}
	}
	if(State > WHEEL_HEAT_STATE_OFF)
	{
		if(WheelHeat_Struct.ucHeatWireOpenFlag == HEAT_DIAG_ENABLE)
		{
			if(WheelHeatFeedbackAdcResult>= HEAT_CURRENT)
			{
				dem_set_test_result(DTC_SWH_STRIP_OPEN_INDEX,DTC_TEST_STATE_PASSED);
				WheelHeat_Struct.ucHeatWireOpenFlag = HEAT_DIAG_DISABLE;
			}
		}
	}
    
}

void WheelHeat_Wire_Short_Check_PowerOn(void) //开机自检
{
    uint8_t PowerGear;
    uint16_t WheelHeatFeedbackAdcResult;
    can_app_receive_signal(COM_RXSIGBCM1_POWER_GEAR,&PowerGear);
	WheelHeatFeedbackAdcResult = adc_get_voltage_value(ADC_LOGIC_CHANNEL_SNS);
    if(PowerGear != POWER_GEAR_ON )
    {
        pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 0);
        WheelHeat_Struct.ucHeatDigaTime=0;
    }
    else
    {
        pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 300);
        WheelHeat_Struct.ucHeatWireOpenTime++;
        if(WheelHeatFeedbackAdcResult<= HEAT_CURRENT)
	    {
            WheelHeat_Struct.ucHeatDigaTime++;
            if(WheelHeat_Struct.ucHeatDigaTime>WHEEL_HEAT_WIRE_ERROR_2S)
            {
                dem_set_test_result(DTC_SWH_STRIP_OPEN_INDEX,DTC_TEST_STATE_FAILED);
	    	    WheelHeat_Struct.ucHeatWireOpenFlag = HEAT_DIAG_ENABLE;
                WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.ucHeatDigaTime=0;
            }	
	    }
	    if(WheelHeat_Struct.ucHeatWireOpenTime >= WHEEL_HEAT_WIRE_ERROR_3S)
	    {
            pwm_set_duty(PWM_BASE_ID_GPTM0, 1, 0);
		    WheelHeat_Struct.ucHeatPowerOnFlag = HEAT_DIAG_DISABLE;
		    WheelHeat_Struct.ucHeatWireOpenTime = 0;
            WheelHeat_Struct.ucHeatDigaTime=0;
	    }
    }
}

void WheelHeat_Wire_Short_Check_Open(void) //打开加热检测
{
    uint16_t NTC_AdcResult;
    uint8_t heat_gear;
    uint16_t WheelHeatFeedbackAdcResult;
    WheelHeatFeedbackAdcResult = adc_get_voltage_value(ADC_LOGIC_CHANNEL_SNS);
     NTC_AdcResult = adc_get_voltage_value(1);
	heat_gear = WheelHeat_Struct.ucHeatGear;
    if((WheelHeat_Struct.ucEcuControlState > WHEEL_HEAT_STATE_OFF) && (NTC_AdcResult>HEAT_LEVEL_NTC_TAB[heat_gear]))
    {
        if(WheelHeatFeedbackAdcResult<= HEAT_CURRENT)
        {
            WheelHeat_Struct.ucHeatDigaTimeOpen++;
            if(WheelHeat_Struct.ucHeatDigaTimeOpen>WHEEL_HEAT_WIRE_ERROR_2S)
            {
                dem_set_test_result(DTC_SWH_STRIP_OPEN_INDEX,DTC_TEST_STATE_FAILED);
	     	    WheelHeat_Struct.ucHeatWireOpenFlag = HEAT_DIAG_ENABLE;
                WheelHeat_Struct.ucDcmControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.ucEcuControlState = WHEEL_HEAT_STATE_OFF;
                WheelHeat_Struct.ucHeatDigaTimeOpen=0;
          }	
        }
        else
        {
            WheelHeat_Struct.ucHeatDigaTimeOpen=0;
        }
    }
    else
    {
        WheelHeat_Struct.ucHeatDigaTimeOpen=0;
    }
}
/*****************************************************************************
|Prototype			: WheelHeat_GetNtcState
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
uint8_t WheelHeat_GetNtcState(void)
{
	return WheelHeat_Struct.usDigNtcState;
}

/*****************************************************************************
|Prototype			: WheelHeat_GetHeatOverCurrent
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
uint8_t WheelHeat_GetHeatOverCurrent(void)
{
	return  WheelHeat_Struct.usDigOverCurrent;
}


/*****************************************************************************
|Prototype			: WheelHeat_MainFunction
|Called by			:
|Preconditions		:
|Input parameters	:
|Output parameters	:
|Return value		:
|Description		:
*****************************************************************************/
void WheelHeat_MainFunction(void)
{ 
    WheelHeat_EventProcess();
    WheelHeat_Handle();
   // WheelHeat_RunTime();
    WheelHeat_OffGearDelay();
    WheelHeat_Diag();
    WheelHeat_CanTxProcess();
    WheelHeat_Wire_Short_Check_Open();
}










