#include "lcd.h"
#include "lt776_gra.h"

#define PWMT_BADDR          (0x4001f000)
#define PWMT_PUE            (*(volatile unsigned int*)(PWMT_BADDR+0x0064))

CLCD_HandleTypeDef hclcd;

void InitLcd(void)
{
	u32 plck_div;
	
	LCD_XSIZE_TFT=LCD_SIZE_X;
	LCD_YSIZE_TFT=LCD_SIZE_Y;

	(*(volatile unsigned char*)(0x40019005)) |= 0x02;
	(*(volatile unsigned char*)(0x40019003)) |= 0x02;
	PWMT_PUE &=~ 0x000f0000;
	
	plck_div = g_core_clk/((LCD_XSIZE_TFT+LCD_HBPD+LCD_HFPD+LCD_HSPW)*(LCD_YSIZE_TFT+LCD_VBPD+LCD_VFPD+LCD_VSPW)*60);
	//printf("plck_div:%d\r\n",plck_div);

	hclcd.instance = CLCD;
//	hclcd.init.enableIE = ENABLE;
	hclcd.init.enableIE = DISABLE;
	hclcd.init.clcd_timeing.width  	= LCD_XSIZE_TFT;
	hclcd.init.clcd_timeing.height 	= LCD_YSIZE_TFT;
	hclcd.init.clcd_timeing.hbp    	= LCD_HBPD;
	hclcd.init.clcd_timeing.hfp    	= LCD_HFPD;
	hclcd.init.clcd_timeing.vbp    	= LCD_VBPD;
	hclcd.init.clcd_timeing.vfp    	= LCD_VFPD;
	hclcd.init.clcd_timeing.hsync  	= LCD_HSPW;
	hclcd.init.clcd_timeing.vsync  	= LCD_VSPW;
	hclcd.init.clcd_timeing.flags  	= (LCD_TIMING_ACTIVE_HIGH_OE | LCD_TIMING_ACTIVE_HIGH_PIXCLK | LCD_TIMING_ACTIVE_LOW_HSYNC | LCD_TIMING_ACTIVE_LOW_VSYNC);
	
	
	if(LCD_XSIZE_TFT==800)
	{
		hclcd.init.clcd_clkdiv = plck_div;
#if	Camera_GC0308
		hclcd.init.clcd_clkdiv = plck_div;
#endif
#if	Camera_OV5640
		hclcd.init.clcd_clkdiv = plck_div+4;
#endif
		hclcd.init.clcd_cfg.control = (LCD_FMT_GAMMA_CORR_DISABLE | LCD_FMT_OUTPUT_BGR | LCD_FMT_INPUT_LITTLE_ENDIAN | LCD_FMT_INPUT_RGB565);		
	}
	else 
	{
		if(LCD_XSIZE_TFT==1024)
		{
			hclcd.init.clcd_clkdiv = plck_div;
#if	Camera_GC0308
			hclcd.init.clcd_clkdiv = plck_div+5;
#endif
#if	Camera_OV5640	
			hclcd.init.clcd_clkdiv = plck_div+9;
#endif				
			hclcd.init.clcd_cfg.control = (LCD_FMT_GAMMA_CORR_DISABLE | LCD_FMT_OUTPUT_RGB | LCD_FMT_INPUT_LITTLE_ENDIAN | LCD_FMT_INPUT_RGB565);		
		}
		else
		{
			hclcd.init.clcd_clkdiv = plck_div;
			hclcd.init.clcd_cfg.control = (LCD_FMT_GAMMA_CORR_DISABLE | LCD_FMT_OUTPUT_BGR | LCD_FMT_INPUT_LITTLE_ENDIAN | LCD_FMT_INPUT_RGB565);		
		}
	}
	
	hclcd.init.clcd_cfg.fb0_addr = Default_BUFF;
	hclcd.init.clcd_cfg.fb1_addr = Default_BUFF+LCD_XSIZE_TFT*LCD_YSIZE_TFT*2;
	
	HAL_CLCD_Init(&hclcd);
	HLA_CLCD_UpuerPannelBaseAddr(&hclcd,Default_BUFF);
	HLA_CLCD_Cmd(&hclcd,ENABLE);
//	HLA_CLCD_Cmd(&hclcd,DISABLE);
}

u8 LCD_frame_flag = 0;
void CLCD_IRQHandler(void)
{
	if(CLCD->LCD_MIS & VCOMP)
	{
		LCD_frame_flag=1;
//		printf("int\r\n");
	}
	CLCD->LCD_ICR |= MBERR|VCOMP|LNBU|FUF;
}

//void InitLcdForCamera(void)
//{
//	(*(volatile unsigned char*)(0x40019005)) |= 0x02;
//	(*(volatile unsigned char*)(0x40019003)) |= 0x02;
//	
//	PWMT_PUE &=~ 0x000f0000;
//	
//	hclcd.instance = CLCD;
//	
//	HAL_CLCD_DeInit(&hclcd);
//	
//	hclcd.init.enableIE = DISABLE;
//	hclcd.init.clcd_timeing.width  	= 640;//800;
//	hclcd.init.clcd_timeing.height 	= 480;
//	hclcd.init.clcd_timeing.hbp    = 45;
//	hclcd.init.clcd_timeing.hfp    = 210;
//	hclcd.init.clcd_timeing.vbp    = 22;
//	hclcd.init.clcd_timeing.vfp    = 22;
//	hclcd.init.clcd_timeing.hsync  = 1;
//	hclcd.init.clcd_timeing.vsync  = 1;
//	hclcd.init.clcd_timeing.flags  = (LCD_TIMING_ACTIVE_HIGH_OE |
//						                        LCD_TIMING_ACTIVE_HIGH_PIXCLK |
//						                        LCD_TIMING_ACTIVE_LOW_HSYNC |
//						                        LCD_TIMING_ACTIVE_LOW_VSYNC);
//	
//	hclcd.init.clcd_clkdiv = 12;
//	
//	hclcd.init.clcd_cfg.control = LCD_FMT_GAMMA_CORR_DISABLE |
//				              	        LCD_FMT_OUTPUT_BGR |
//					                      LCD_FMT_INPUT_LITTLE_ENDIAN |
//					                      LCD_FMT_INPUT_RGB565;
//												
//	
//	hclcd.init.clcd_cfg.fb0_addr = Default_BUFF+800*480*2*10;
//	
//	HAL_CLCD_Init(&hclcd);
//	color_bar_init_rgb565(hclcd.init.clcd_cfg.fb0_addr, hclcd.init.clcd_timeing.width, hclcd.init.clcd_timeing.height);
//	HLA_CLCD_UpuerPannelBaseAddr(&hclcd, hclcd.init.clcd_cfg.fb0_addr);
//	HLA_CLCD_Cmd(&hclcd, ENABLE);
//}


