

#include "ccm_drv.h"
#include "lcd_drv.h"
#include "eport_drv.h"
#include "pwm_drv.h"
#include "spi_drv.h"
#include "delay.h"

UINT16 LCD_XSIZE_TFT = 0;
UINT16 LCD_YSIZE_TFT = 0;
UINT16 LCD_VBPD = 0;
UINT16 LCD_VFPD = 0;
UINT16 LCD_VSPW = 0;
UINT16 LCD_HBPD = 0;
UINT16 LCD_HFPD = 0;
UINT16 LCD_HSPW = 0;

UINT8  TFTinfo[50]={0};

UINT8 DMA_rxbuf1[6144]; 

UINT8 m_PngData[MAX_PIXELSIZE];
UINT16 *p16_m_PngData=(UINT16 *)&m_PngData;

//extern u32 Flash_Type;

void LCD_SPI_Port_Init(void)
{
	/*
	SPI_InitTypeDef SPI_InitStruct;
	SPI_InitStruct.SPI_Mode = SPI_MASTERMODE;
	SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;          
	SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
	SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
	SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
	SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
	
	SPI_Init(SPI3,&SPI_InitStruct);
	
	*/
	
	SPI_Init(SPI3,SPI_MASTERMODE);
	
	//PWM[1]/GINT[13]
	PWM_INPUT_OUTPUT_EN;
	PWM_ConfigGpio(PWM_PORT1,GPIO_OUTPUT,PWM_PULLUP_EN); //RST
	PWM_WriteGpioData(PWM_PORT1,Bit_SET);
	//PWM[0]/GINT[12]
	PWM_INPUT_OUTPUT_EN;
	PWM_ConfigGpio(PWM_PORT0,GPIO_OUTPUT,PWM_PULLUP_EN); //CS
	PWM_WriteGpioData(PWM_PORT0,Bit_SET);	
	
	EPORT_ConfigGpio(EPORT_PIN7, GPIO_OUTPUT);  			//RS/A0
	EPORT_WriteGpioData(EPORT_PIN7, Bit_SET);	
	
	//DMA_Init(DMA1_BASE_ADDR);                  
//	m_dma_control->DMA_CONFIG = 1;
	SPI3->SPIDMACR &= (~0x03);	//禁止SPI TX RX的DMA	
}

UINT8 LCD_SPI_ReadWriteByte(UINT8 Send)
{
	 UINT8 ucTemp = 0;
	 while(((SPI3->SPISRHW)&(SPISR_TXFFULL_MASK)));
	 SPI3->SPIDR = Send;
	 while(!(0x80 & SPI3->SPISR));
	 while(((SPI3->SPISRHW) & (SPISR_RXFEMP_MASK)));
	 ucTemp = SPI3->SPIDR;
	 return ucTemp;
}

void LCD_PPI_Port_Init(void)
{
#if LCD_DATABIT == LT268_8DATABIT

	//PWM[1]/GINT[13]               
	PWM_INPUT_OUTPUT_EN;
	PWM_ConfigGpio(PWM_PORT1,GPIO_OUTPUT,PWM_PULLUP_EN); //RST			/*全部设置成输出*/
	PWM_WriteGpioData(PWM_PORT1,Bit_SET);
	
	//PWM[0]/GINT[12]
	PWM_INPUT_OUTPUT_EN;
	PWM_ConfigGpio(PWM_PORT0,GPIO_OUTPUT,PWM_PULLUP_EN); //CS
	PWM_WriteGpioData(PWM_PORT0,Bit_SET);	

	EPORT_ConfigGpio(EPORT_PIN7, GPIO_OUTPUT);  			//RS
	EPORT_WriteGpioData(EPORT_PIN7, Bit_SET);
	
	SPI_ConfigGpio(SPI3,SPI_MOSI, GPIO_OUTPUT);				//RD
	SPI_WriteGpioData(SPI3,SPI_MOSI, Bit_SET);
	
	SPI_ConfigGpio(SPI3,SPI_SCK, GPIO_OUTPUT);				//WR
	SPI_WriteGpioData(SPI3,SPI_SCK, Bit_SET);
	
	EPORT_ConfigGpio(EPORT_PIN24, GPIO_OUTPUT); //DB0
	EPORT_ConfigGpio(EPORT_PIN25, GPIO_OUTPUT); //DB0
	EPORT_ConfigGpio(EPORT_PIN26, GPIO_OUTPUT); //DB0
	EPORT_ConfigGpio(EPORT_PIN27, GPIO_OUTPUT); //DB0
	EPORT_ConfigGpio(EPORT_PIN28, GPIO_OUTPUT); //DB0
	EPORT_ConfigGpio(EPORT_PIN29, GPIO_OUTPUT); //DB0
	EPORT_ConfigGpio(EPORT_PIN30, GPIO_OUTPUT); //DB0
	EPORT_ConfigGpio(EPORT_PIN31, GPIO_OUTPUT); //DB0

	EPORT3->EPDDR  = 0xFF;  //DB0-DB7
	EPORT3->EPPUER = 0xFF;
	EPORT3->EPDR   = 0x00;
#endif
	
#if LCD_DATABIT == LT268_16DATABIT
	
#endif
}

void TFT_CommandWrite(UINT8 cmd)
{
#if LCD_INTERFACE ==	LT268_SPI
		LCD_RS_Command;
		LCD_CS_RESET;
		LCD_SPI_ReadWriteByte(cmd);
		LCD_CS_SET;
#endif

#if LCD_INTERFACE == LT268_PPI
		LCD_RD_SET;
		LCD_RS_Command;
		LCD_CS_RESET;

		LCD_data_pin_L = cmd;
		LCD_WR_RESET;
		LCD_WR_SET;
		LCD_CS_SET;
#endif
}

void TFT_DataWrite(UINT8 data)
{
#if	LCD_INTERFACE == LT268_SPI
		LCD_RS_Data;
		LCD_CS_RESET;
		LCD_SPI_ReadWriteByte(data);
		LCD_CS_SET;
#endif

#if LCD_INTERFACE == LT268_PPI
		LCD_RD_SET;
		LCD_RS_Data;
		LCD_CS_RESET;

		LCD_data_pin_L = data;
		LCD_WR_RESET;
		LCD_WR_SET;
		LCD_CS_SET;
#endif
}

UINT32 TFT_DataReadID(void)
{
#if LCD_INTERFACE == LT268_PPI

	#if LCD_DATABIT == LT268_8DATABIT
	UINT32 Val=0;
	UINT8 dey=1;

	LCD_CS_RESET;
	LCD_RD_SET;	
	LCD_RS_Command;	
	LCD_data_pin_L = 0xD3;	  //reg = 0xD3
	LCD_WR_RESET;	
	LCD_WR_SET;	
		
	LCD_RS_Data;	
	LCD_data_pin_input;
	LCD_data_pin_L =0xFF;	
		
	LCD_RD_RESET;	       //dummy
	DelayMS(dey);
	LCD_RD_SET;	

	LCD_RD_RESET;	
	DelayMS(dey);
	LCD_RD_SET;		
	Val |= ((LCD_data_pin_read)<<24);	

	LCD_RD_RESET;	
	DelayMS(dey);
	LCD_RD_SET;		
	Val |= ((LCD_data_pin_read)<<16);	

	LCD_RD_RESET;	
	DelayMS(dey);
	LCD_RD_SET;		
	Val |= ((LCD_data_pin_read)<<8);	
		
	LCD_RD_RESET;	
	DelayMS(dey);
	LCD_RD_SET;		
	Val |= (LCD_data_pin_read);	

	LCD_CS_SET;	
		
	LCD_data_pin_output;	
	return Val;
	#endif
#endif
}

void LCD_Init(u8 type,u16 pixelX,u16 pixelY)
{
	LCD_XSIZE_TFT = pixelX;
	LCD_YSIZE_TFT = pixelY;

#if LCD_INTERFACE	==	LT268_SPI
	LCD_SPI_Port_Init();
#else 
	LCD_PPI_Port_Init();
#endif
	
	LCD_RST_SET;              //RST
	DelayMS(10);
	LCD_RST_RESET;
	DelayMS(10);
	LCD_RST_SET;
	DelayMS(120); //Delay 120ms

/*屏的初始化代码部分，Horizontal_screen部分为设置横竖屏，此处需根据实际情况调整*/
	TFT_CommandWrite(0x11);
	DelayMS(150);

	TFT_CommandWrite(0xf0);
	TFT_DataWrite(0xc3);

	TFT_CommandWrite(0xf0);
	TFT_DataWrite(0x96);

	TFT_CommandWrite(0x36);

#if LCD_ORIENTATION == LANDSCAPE
	TFT_DataWrite(0xA0);	   			 //无标签 0=0x00 1=0x60  2=0xC0 3=0xA0 	   
#else								   						// 标签 : 0=0x48 1=0x08  2=0x08 3=0xE8 
	TFT_DataWrite(0x00);	
#endif
	
	TFT_CommandWrite(0x3A);
	TFT_DataWrite(0x55);

	TFT_CommandWrite(0xb4);
	TFT_DataWrite(0x01);

	TFT_CommandWrite(0xb7);
	TFT_DataWrite(0xc6);

	TFT_CommandWrite(0xe8);
	TFT_DataWrite(0x40);
	TFT_DataWrite(0x8a);
	TFT_DataWrite(0x00);
	TFT_DataWrite(0x00);
	TFT_DataWrite(0x29);
	TFT_DataWrite(0x19);
	TFT_DataWrite(0xa5);
	TFT_DataWrite(0x33);

	TFT_CommandWrite(0xc1);
	TFT_DataWrite(0x06);

	TFT_CommandWrite(0xc2);
	TFT_DataWrite(0xa7);

	TFT_CommandWrite(0xc5);
	TFT_DataWrite(0x18);
	
	TFT_CommandWrite(0xc6);               //Frame Rate Control in Normal Mode
	TFT_DataWrite(0x0f);
//	TFT_CommandWrite(0xc6);
//	TFT_DataWrite(0x01);
//	TFT_CommandWrite(0x35);                 //TE
//	TFT_DataWrite(0x00);
//	TFT_CommandWrite(0x44);                 //TE line
//	TFT_DataWrite(0x00);
//	TFT_DataWrite(0x40);

	TFT_CommandWrite(0xe0);
	TFT_DataWrite(0xf0);
	TFT_DataWrite(0x09);
	TFT_DataWrite(0x0b);
	TFT_DataWrite(0x06);
	TFT_DataWrite(0x04);
	TFT_DataWrite(0x15);
	TFT_DataWrite(0x2f);
	TFT_DataWrite(0x54);
	TFT_DataWrite(0x42);
	TFT_DataWrite(0x3c);
	TFT_DataWrite(0x17);
	TFT_DataWrite(0x14);
	TFT_DataWrite(0x18);
	TFT_DataWrite(0x1b);

	TFT_CommandWrite(0xe1);
	TFT_DataWrite(0xf0);
	TFT_DataWrite(0x09);
	TFT_DataWrite(0x0b);
	TFT_DataWrite(0x06);
	TFT_DataWrite(0x04);
	TFT_DataWrite(0x03);
	TFT_DataWrite(0x2d);
	TFT_DataWrite(0x43);
	TFT_DataWrite(0x42);
	TFT_DataWrite(0x3b);
	TFT_DataWrite(0x16);
	TFT_DataWrite(0x14);
	TFT_DataWrite(0x17);
	TFT_DataWrite(0x1b);

	TFT_CommandWrite(0xf0);
	TFT_DataWrite(0x3c);

	TFT_CommandWrite(0xf0);
	TFT_DataWrite(0x69);

//	TFT_CommandWrite(0x21);//如果黑白颜色反掉了，就把此行屏蔽掉

	DelayMS(150);
	TFT_CommandWrite(0x29);

	TFT_CommandWrite(0x2c);
}

//------------------------设定窗口----------------------------------------------------------
#define XSoff	 	0       		//X轴： XSoff = 起始偏移  ，XSoff = 终点偏移
#define XEoff 		0
#define YSoff	    0				//Y轴： YSoff = 起始偏移  ，YEoff = 终点偏移
#define YEoff 		0

void LT268_TFT_SetWindows(UINT16 XStar,UINT16 yStar,UINT16 Width,UINT16 High)
{
	UINT16 XS,XE,YS,YE;
	XS=XStar+XSoff;  	XE=XStar+XEoff+Width-1;
	YS=yStar+YSoff;  	YE=yStar+YEoff+High-1;
	
	/*-适用驱动IC : ST7789、ILI9341、、、等 -*/
	TFT_CommandWrite(0x2A);
	TFT_DataWrite(XS>>8);
	TFT_DataWrite(XS);
	TFT_DataWrite(XE>>8);
	TFT_DataWrite(XE);

	TFT_CommandWrite(0x2B);
	TFT_DataWrite(YS>>8);
	TFT_DataWrite(YS);
	TFT_DataWrite(YE>>8);
	TFT_DataWrite(YE);

	TFT_CommandWrite(0x2c);


}

//---------------------TFT Sleep Mode----------------------
void ST7789VPanelSleepInMode(void)
{
	TFT_CommandWrite(0x28);
	TFT_CommandWrite(0x10);
	DelayMS(120);
}

//---------------------TFT Sleep Out----------------------
void ST7789VPanelSleepOutMode(void)
{
	TFT_CommandWrite(0x11);
	DelayMS(120);
	TFT_CommandWrite(0x29);
}
//---------------------TFT display on/off ----------------------
void LT268_Display_ON(void)
{
	TFT_CommandWrite(0x29);
}
void LT268_Display_OFF(void)
{
	TFT_CommandWrite(0x28);
}

//--------------------LCD_PIN_TEST--------------------------------
void LCD_PIN_TEST(void)
{
	while(1)
	{
#if	LCD_INTERFACE == LT268_PPI
		LCD_RST_SET;
		LCD_CS_SET;
		LCD_WR_SET;
		LCD_RD_SET;
		LCD_RS_Data;
		LCD_data_pin_L = 0xff;
		DelayMS(1000);

		LCD_RST_RESET;
		LCD_CS_RESET;
		LCD_WR_RESET;
		LCD_RD_RESET;
		LCD_RS_Command;
		LCD_data_pin_L = 0x00;
		DelayMS(1000);
#endif

	}
}
