/*!
 * @文件名 logic_diagnose.c
 *
 * @功能描述
 *	通过GPIO以及寄存器,来检测max25512,pmic,tft的故障原因
 */
#include "logic_public.h"

extern task_t s_logic_check_wakeup;
extern task_t s_logic_check_lvds;
extern task_t s_bist_check_task;
static task_t s_diagnose_task;

//-----注意: 温度越大,ADC值越小
#define NTC_86_D_ADC		947							//NTC对应86度对应的ADC值,查表为[947]
#define NTC_75_D_ADC		1190						//NTC对应75度对应的ADC值,查表为[1190]
#define NTC_43_D_ADC		2164						//NTC对应43度对应的ADC值,查表为[2164]
#define NTC_39_D_ADC		2303						//NTC对应39度对应的ADC值,查表为[2303]

#define NTC_HIGH_TEMP_IN	NTC_86_D_ADC
#define NTC_HIGH_TEMP_OUT	NTC_75_D_ADC

#define ADC_SAMPLE_CNT		10							//ADC采样次数
static uint16_t g_pAdcTempArray[ADC_SAMPLE_CNT]={0};	//温度AD采集BUF
static uint16_t g_pAdcBattArray[ADC_SAMPLE_CNT]={0};	//电压AD采集BUF
static uint8_t g_iHostPWMValue = 0x00;					//主机发过来的PWM值

ST_DIAGNOSE1_DATA Err_status1 = {};
ST_DIAGNOSE2_DATA Err_status2 = {};
uint8_t	result1,result2;

uint8_t convertBoolStruc1ToUint8(ST_DIAGNOSE1_DATA bs)
{
    uint8_t result = 0;
    if (bs.LOW_VOL_FAIL) result 			|= (1 << 0);
    if (bs.HIGH_VOL_FAIL) result 			|= (1 << 1);
    if (bs.LCD_FAIL) result 				|= (1 << 2);
    if (bs.BACKLIGHT_FAIL) result 			|= (1 << 3);
    if (bs.LCD_TEMPERATURE_FAIL) result 	|= (1 << 4);
    if (bs.LCD_NTC_FAIL) result 			|= (1 << 5);
    if (bs.RESERVER1) result 				|= (1 << 6);
    if (bs.RESERVER2) result 				|= (1 << 7);
    return result;
}

uint8_t convertBoolStruct2ToUint8(ST_DIAGNOSE2_DATA bs)
{
    uint8_t result = 0;
    if (bs.RESERVER0) result 			|= (1 << 0);
    if (bs.RESERVER1) result 			|= (1 << 1);
    if (bs.LOCK_FAIL) result 			|= (1 << 2);
    if (bs.RESERVER3) result 			|= (1 << 3);
    if (bs.RESERVER4) result 			|= (1 << 4);
    if (bs.RESERVER5) result 			|= (1 << 5);
    if (bs.RESERVER6) result 			|= (1 << 6);
    if (bs.RESERVER7) result 			|= (1 << 7);
    return result;
}

/*FUNCTION**********************************************************************
 *
 * Function Name : disgnose_batt_voltage_process
 * Description   : 采集B+电压,处理高低压异常
 *
 *END**************************************************************************/
void disgnose_batt_voltage_process(void)
{
	uint16_t iBattAd,iBattVoltage=0;
	static int i = 0;
	static eNotifyEvent voltageStatusNow = EN_NOTIFY_VOLTAGE_NORMAL;
	static eNotifyEvent voltageStatusOld = EN_NOTIFY_VOLTAGE_NORMAL;
	g_pAdcBattArray[i++] = fml_adc_read_bat_ad();
	if(i==ADC_SAMPLE_CNT)
	{
		i = 0;
		iBattAd	= fml_adc_filter(g_pAdcBattArray,ADC_SAMPLE_CNT);
		iBattVoltage = fml_adc_to_bat_voltage(iBattAd);
		//logWrite("iBattVoltage=%d\r\n",iBattVoltage);
		if(iBattVoltage > VOLTAGE_HIGH_IN)
		{
			Err_status1.HIGH_VOL_FAIL = 1;
			voltageStatusNow = EN_NOTIFY_VOLTAGE_HIGH;
		}
		else if(iBattVoltage < VOLTAGE_LOW_IN)
		{
			Err_status1.LOW_VOL_FAIL = 1;
			voltageStatusNow = EN_NOTIFY_VOLTAGE_LOW;
		}
		else if(iBattVoltage <= VOLTAGE_NORMAL_MAX && iBattVoltage >= VOLTAGE_NORMAL_MIN)
		{
			Err_status1.HIGH_VOL_FAIL = 0;
			Err_status1.LOW_VOL_FAIL = 0;
			voltageStatusNow = EN_NOTIFY_VOLTAGE_NORMAL;
		}
		if(voltageStatusOld != voltageStatusNow)
		{
			voltageStatusOld = voltageStatusNow;
			cola_set_event(&s_bist_check_task,voltageStatusNow);
			cola_set_event(&s_logic_check_wakeup,voltageStatusNow);
		}
	}
}


/*FUNCTION**********************************************************************
 *
 * Function Name : disgnose_ntc_voltage_process
 * Description   : 采集屏NTC电压,处理屏高温异常
 *
 *END**************************************************************************/
void disgnose_ntc_voltage_process(void)
{
	uint16_t iTempAd=0;
	static int i;
	static bool bLCDTempHighNow = false,bLCDTempHighOld = false;
	g_pAdcTempArray[i++] = fml_adc_read_temp_ad();
	if(i==ADC_SAMPLE_CNT)
	{
		i = 0;
		iTempAd	= fml_adc_filter(g_pAdcTempArray,ADC_SAMPLE_CNT);
		if(iTempAd<NTC_HIGH_TEMP_IN)
			bLCDTempHighNow = true;
		else if(iTempAd>NTC_HIGH_TEMP_OUT)
			bLCDTempHighNow = false;
		if(bLCDTempHighOld != bLCDTempHighNow)
		{
			bLCDTempHighOld = bLCDTempHighNow;
			Err_status1.LCD_TEMPERATURE_FAIL = bLCDTempHighNow?1:0;
			fml_pwm_adjust(bLCDTempHighNow);
			logWrite("iTempAd=%d,LCD_TEMPERATURE_FAIL=%d\r\n",iTempAd,Err_status1.LCD_TEMPERATURE_FAIL);
		}
	}
}

/*FUNCTION**********************************************************************
 *
 * Function Name : diagnose_polling_task
 * Description   : 诊断任务处理函数
 *
 *END**************************************************************************/
void diagnose_polling_task(void *arg, uint32_t event)
{
	(void)arg;
	(void)event;
	disgnose_batt_voltage_process();
	disgnose_ntc_voltage_process();
	Err_status1.LCD_FAIL = fml_gpio_read(IN_TFT_ERR);
	Err_status1.BACKLIGHT_FAIL = fml_gpio_read(IN_BL_ERR);
	Err_status2.LOCK_FAIL = fml_gpio_read(IN_LVDS_UNLOCK);
	result1 = convertBoolStruc1ToUint8(Err_status1);
	result2 = convertBoolStruct2ToUint8(Err_status2);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : logic_diagnose_init
 * Description   : 初始化诊断检测任务
 *
 *END**************************************************************************/
void logic_diagnose_init(void)
{
	cola_timer_create(&s_diagnose_task, diagnose_polling_task, NULL);
	cola_timer_start(&s_diagnose_task, EN_COLA_TIMER_ALWAYS, 100);
}
