/*!
 * @文件名 logic_mode_bist.c
 *
 * @功能描述
 *	通过检测948的LVDS Lock信号,控制屏幕是否进入BIST模式
 */
#include "logic_public.h"

#define PERIOD_CHECK_LVDS_LOCK		10	//检测LVDS信号的周期
task_t s_bist_check_task;				//BIST模式检测
extern void logic_power_up_step2(void); //跨模块调用,违背了原则^-^

/*FUNCTION**********************************************************************
 *
 * Function Name : check_lvds_floating
 * Description   : 通过检测LVDS Lock信号,进入BIST模式
 *
 *END**************************************************************************/
static void check_lvds_floating(void *arg, uint32_t event)
{
	(void)arg;
	(void)event;
	static bool bVoltageNormal = true;
	static uint8_t style = 0;
	static bool bBistMode = false;
	static bool bFirst = true;
	static bool bLockStatusOld = false;
	static int iCheckDurCnt = 0, iDlyCnt = 0,iPeriodCnt = 0;
	if(event > 0)
	{//事件解析
		if((event & EN_NOTIFY_SYS_POWER_DOWN) == EN_NOTIFY_SYS_POWER_DOWN)
		{
			bFirst = true;
			logWrite("bist mode rx power down nofity\r\n");
		}
		if((event & EN_NOTIFY_VOLTAGE_LOW) == EN_NOTIFY_VOLTAGE_LOW)
		{
			bVoltageNormal =  false;
			logWrite("bist mode rx EN_NOTIFY_VOLTAGE_LOW\r\n");
		}
		if((event & EN_NOTIFY_VOLTAGE_HIGH) == EN_NOTIFY_VOLTAGE_HIGH)
		{
			bVoltageNormal =  false;
			logWrite("bist mode rx EN_NOTIFY_VOLTAGE_HIGH\r\n");
		}
		if((event & EN_NOTIFY_VOLTAGE_NORMAL) == EN_NOTIFY_VOLTAGE_NORMAL)
		{
			bVoltageNormal = true;
			logWrite("bist mode rx EN_NOTIFY_VOLTAGE_NORMAL\r\n");
			if(bBistMode)
			{
				logic_power_up_step2();
				fml_max25512_set_bklight_on(true);
				fml_gpio_write(OUT_TFT_BIST,true);
			}
		}
	}
	if(iCheckDurCnt < 6000)
	{//前6秒不检测该信号丢失
		iCheckDurCnt+=PERIOD_CHECK_LVDS_LOCK;
		bLockStatusOld = !fml_gpio_read(IN_LVDS_UNLOCK);
	}
	else
	{//读取LVDS Lock信号
		bool bLockStatus = fml_gpio_read(IN_LVDS_UNLOCK);
		bool bWakeUpStatus = fml_gpio_read(IN_WAKEUP);
		if(bWakeUpStatus == false || bVoltageNormal == false)
			return;

		if((event & EN_NOTIFY_SYS_POWER_UP) == EN_NOTIFY_SYS_POWER_UP)
		{
			if(bBistMode)
			{
				logWrite("bist mode rx power up nofity\r\n");
				iPeriodCnt = 0;
				logic_power_up_step2();
				fml_max25512_set_bklight_on(true);	//开背光
				fml_gpio_write(OUT_TFT_BIST,true);
			}
		}

		if(bLockStatusOld != bLockStatus)
		{//5次消抖
			iDlyCnt = 5;
			bLockStatusOld = bLockStatus;
		}
		if(iDlyCnt > 0)
		{
			iDlyCnt--;
			if(0 == iDlyCnt)
			{
				style = 0;
				iPeriodCnt = 0;
				if(bLockStatus)
				{//检测到LVDS Lock信号生效
					bFirst = false;
					bBistMode = false;
					logWrite("exit bist\r\n");
					fml_max25512_set_bklight_on(true);	//开背光
				}
				else
				{//检测到LVDS Lock信号丢失
					bBistMode = true;
					logWrite("enter bist\r\n");
					if(bFirst)
					{//首次上电时,需要进行如下系列供电
						bFirst = false;
						iPeriodCnt = 0;
						logic_power_up_step2();
					}
					fml_max25512_set_bklight_on(true);	//开背光
					fml_tft_BIST_clear();//写寄存器清除BIST模式设置
				}
				fml_gpio_write(OUT_TFT_BIST,!bLockStatus);//设置Bist模式输出引脚
			}
		}

		if(bLockStatus == false  && bBistMode)
		{
			iPeriodCnt+=PERIOD_CHECK_LVDS_LOCK;
			if(iPeriodCnt > 5000)
			{//每隔5秒切换一次bist模式
				//logWrite("iPeriodCnt=%d\r\n",iPeriodCnt);
				iPeriodCnt = 0;
				fml_tft_BIST_clear();
				fml_tft_write_BIST_mode(style);
				style++;
				if(style == 24)//切换到第24个模式再从0开始
					style=0;
			}
		}
	}
}

/*FUNCTION**********************************************************************
 *
 * Function Name : logic_bist_init
 * Description   : 初始化BIST模式检测任务
 *
 *END**************************************************************************/
void logic_bist_init(void)
{
	cola_timer_create(&s_bist_check_task, check_lvds_floating, NULL);
	cola_timer_start(&s_bist_check_task, EN_COLA_TIMER_ALWAYS, PERIOD_CHECK_LVDS_LOCK);
}
