/**************************************************************
The initial and control for 320℅240 16Bpp TFT LCD----3.5渡旳
**************************************************************/
#include <string.h>
#include "def.h"
#include "2440addr.h"
#include "2440slib.h"
#include "2440slib.h"
#include "LCD_TX06D18.h"
#include "data.h"
//------------------------------------------------------------------------
#define BLACK 0x0000
#define WHITE 0xFFFF
#define R 0xF800
#define G 0x07E0
#define B 0x001F
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
//------------------------------------------------------------------------
#define MVAL (13)
#define MVAL_USED (0) //0=each frame 1=rate by MVAL
#define INVVDEN (1) //0=normal 1=inverted
#define BSWP (0) //Byte swap control
#define HWSWP (1) //Half word swap control
#define M5D(n) ((n) & 0x1fffff) // To get lower 21bits
//TFT 240320
#define LCD_XSIZE_TFT_240320 (320)
#define LCD_YSIZE_TFT_240320 (240)
//TFT 240320
#define SCR_XSIZE_TFT_240320 (LCD_XSIZE_TFT_240320)
#define SCR_YSIZE_TFT_240320 (LCD_YSIZE_TFT_240320)
//#define SCR_XSIZE_TFT_240320 (240)
//#define SCR_YSIZE_TFT_240320 (320)
//TFT240320
#define HOZVAL_TFT_240320 (LCD_XSIZE_TFT_240320-1)
#define LINEVAL_TFT_240320 (LCD_YSIZE_TFT_240320-1)
//Timing parameter for LCD
#define VBPD_240320 (2) //晶眻肮祭陓瘍腔綴潛
#define VFPD_240320 (2) //晶眻肮祭陓瘍腔潛
#define VSPW_240320 (3) //晶眻肮祭陓瘍腔闕遵
#define HBPD_240320 (8) //阨肮祭陓瘍腔綴潛
#define HFPD_240320 (8) //阨肮祭陓瘍腔潛
#define HSPW_240320 (6) //阨肮祭陓瘍腔闕遵
#define CLKVAL_TFT_240320 (5)
//FCLK=180MHz,HCLK=90MHz,VCLK=6.5MHz
#define RST_HIGH() (rGPEDAT |= 0x2000)
#define RST_LOW() (rGPEDAT &= ~0x2000)
#define CSB_HIGH() (rGPEDAT |= 0x0800)
#define CSB_LOW() (rGPEDAT &= ~0x0800)
#define SDA_HIGH() (rGPEDAT |= 0x1000)
#define SDA_LOW() (rGPEDAT &= ~0x1000)
#define SCK_HIGH() (rGPGDAT |= 0x0004)
#define SCK_LOW() (rGPGDAT &= ~0x0004)
static void Lcd_Init(void);
static void Lcd_EnvidOnOff(int onoff);
static void lcd_eio35_init(void);
static void LCD_WriteDat(int dat);
static void LCD_WriteCmd(int cmd);
//static void Lcd_Lpc3600Enable(void);
//static void Lcd_PowerEnable(int invpwren,int pwren);
//static void MoveViewPort(void);
//static void Lcd_MoveViewPort(int vx,int vy);
static void PutPixel(U32 x,U32 y,U32 c);
//static void Glib_Rectangle(int x1,int y1,int x2,int y2,int color);
static void Glib_FilledRectangle(int x1,int y1,int x2,int y2,int color);
static void Glib_Line(int x1,int y1,int x2,int y2,int color);
static void Lcd_ClearScr(U16 c);
static void Paint_Bmp(int x0,int y0,int h,int l,const unsigned char bmp[]);
//extern unsigned char girl13_240_320[]; //遵240ㄛ詢320
extern unsigned char xyx_240_320[]; //遵240ㄛ詢320
volatile static unsigned short LCD_BUFER[SCR_YSIZE_TFT_240320][SCR_XSIZE_TFT_240320];
/**************************************************************
320℅240 16Bpp TFT LCD杅擂睿諷秶傷諳場宎趙
**************************************************************/
static void Lcd_Port_Init(void)
{
rGPCUP = 0xffffffff; // Disable Pull-up register
// rGPCUP = 0x0; // enable Pull-up register
rGPCCON = 0xaaaa56a9; //Initialize VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND
rGPDUP = 0xffffffff ; // Disable Pull-up register
// rGPDUP = 0x0 ; // enable Pull-up register
rGPDCON=0xaaaaaaaa; //Initialize VD[15:8]
//------------------------------------------------
// lcd_ISP_PORT_init
//------------------------------------------------
rGPECON = (rGPECON & 0xf57fffff) | 0x05400000; //GPE11-13 output -- bruce
rGPEUP = 0xffff; // The pull up function is disabled GPE[15:0]
rGPEDAT = 0; //GPE11-13 output 0
rGPGCON = (rGPGCON & 0xffffffdf) | 0x00000010; //GPG2 output
rGPGUP = 0xffff; // The pull up function is disabled GPG[15:0]
rGPGDAT = 0; //GPG2 output0
}
/**************************************************************
320℅240 16Bpp TFT LCD髡夔耀輸場宎趙
**************************************************************/
static void Lcd_Init(void)
{
rLCDCON1=(CLKVAL_TFT_240320<<8)|(MVAL_USED<<7)|(3<<5)|(12<<1)|0;
// TFT LCD panel,12bpp TFT,ENVID=off
rLCDCON2=(VBPD_240320<<24)|(LINEVAL_TFT_240320<<14)|(VFPD_240320<<6)|(VSPW_240320);
rLCDCON3=(HBPD_240320<<19)|(HOZVAL_TFT_240320<<8)|(HFPD_240320);
rLCDCON4=(MVAL<<8)|(HSPW_240320);
rLCDCON5=(1<<11)|(1<<10)|(1<<9)|(1<<8)|(0<<6)|(BSWP<<1)|(HWSWP); //FRM5:6:5,HSYNC and VSYNC are /not/ inverted
rLCDSADDR1=(((U32)LCD_BUFER>>22)<<21)|M5D((U32)LCD_BUFER>>1);
rLCDSADDR2=M5D( ((U32)LCD_BUFER+(SCR_XSIZE_TFT_240320*LCD_YSIZE_TFT_240320*2))>>1 );
rLCDSADDR3=(((SCR_XSIZE_TFT_240320-LCD_XSIZE_TFT_240320)/1)<<11)|(LCD_XSIZE_TFT_240320/1);
rLCDINTMSK|=(3); // MASK LCD Sub Interrupt
// rLPCSEL&=(~7); // Disable LPC3600
rTPAL=0; // Disable Temp Palette
}
/**************************************************************
LCD弝睿諷秶陓瘍怀堤麼氪礿砦ㄛ1羲弝怀堤
**************************************************************/
static void Lcd_EnvidOnOff(int onoff)
{
if(onoff==1)
rLCDCON1|=1; // ENVID=ON
else
rLCDCON1 =rLCDCON1 & 0x3fffe; // ENVID Off
}
/**************************************************************
LPC3600 is a timing control logic unit
**************************************************************/
/*
static void Lcd_Lpc3600Enable(void)
{
rLPCSEL&=~(7);
rLPCSEL|=(7); // 240320,Enable LPC3600
}
*/
/**************************************************************
320℅240 8Bpp TFT LCD 萇埭諷秶竘褐妏夔
**************************************************************/
/*
static void Lcd_PowerEnable(int invpwren,int pwren)
{
//GPG4 is setted as LCD_PWREN
rGPGUP = rGPGUP|(1<<4); // Pull-up disable
rGPGCON = rGPGCON|(3<<8); //GPG4=LCD_PWREN
//Enable LCD POWER ENABLE Function
rLCDCON5 = rLCDCON5&(~(1<<3))|(pwren<<3); // PWREN
rLCDCON5=rLCDCON5&(~(1<<5))|(invpwren<<5); // INVPWREN
}
*/
/**************************************************************
320℅240 8Bpp TFT LCD 晇伎場宎趙
**************************************************************/
/*
static void Lcd_Palette_Init(void)
{
unsigned char cdata, p_red, p_green, p_blue;
U32 *palette;
//#define PALETTE 0x4d000400 //Palette start address
palette=(U32 *)PALETTE;
*palette++=0; //black
for(cdata=1;cdata<255;cdata++)
{
p_red=(cdata & 0xe0);
p_green=(cdata & 0x1c);
p_blue=(cdata & 0x03);
*palette++=((U32)((p_red<<8)|(p_green<<6)|(p_blue<<3)));
}
*palette=0xffff; //white
}
*/
/**************************************************************
320℅240 16Bpp TFT LCD痄雄夤舷敦諳
**************************************************************/
static void Lcd_MoveViewPort(int vx,int vy)
{
U32 addr;
SET_IF();
#if (LCD_XSIZE_TFT_240320<32)
while((rLCDCON1>>18)<=1); // if x<32
#else
while((rLCDCON1>>18)==0); // if x>32
#endif
addr=(U32)LCD_BUFER+(vx*2)+vy*(SCR_XSIZE_TFT_240320*2);
rLCDSADDR1= ( (addr>>22)<<21 ) | M5D(addr>>1 );
rLCDSADDR2= M5D(((addr+(SCR_XSIZE_TFT_240320*LCD_YSIZE_TFT_240320*2))>>1));
CLR_IF();
}
/**************************************************************
320℅240 16Bpp TFT LCD痄雄夤舷敦諳
**************************************************************/
static void MoveViewPort(void)
{
int vx=0,vy=0,vd=1;
printf("\n*Move the LCD view windos:\n");
printf(" press 8 is up\n");
printf(" press 2 is down\n");
printf(" press 4 is left\n");
printf(" press 6 is right\n");
printf(" press Enter to exit!\n");
while(1)
{
switch(getkey())
{
case '8':
if(vy>=vd)vy-=vd;
break;
case '4':
if(vx>=vd)vx-=vd;
break;
case '6':
if(vx<=(SCR_XSIZE_TFT_240320-LCD_XSIZE_TFT_240320-vd))vx+=vd;
break;
case '2':
if(vy<=(SCR_YSIZE_TFT_240320-LCD_YSIZE_TFT_240320-vd))vy+=vd;
break;
case '\r':
return;
default:
break;
}
//printf("vx=%3d,vy=%3d\n",vx,vy);
Lcd_MoveViewPort(vx,vy);
}
}
/**************************************************************
320℅240 16Bpp TFT LCD等跺砓匼腔珆尨杅擂怀堤
**************************************************************/
static void PutPixel(U32 x,U32 y,U32 c)
{
if ( (x < SCR_XSIZE_TFT_240320) && (y < SCR_YSIZE_TFT_240320) )
LCD_BUFER[(y)][(x)] = c;
}
/**************************************************************
320℅240 16Bpp TFT LCD沓喃杻隅晇伎等啋麼
**************************************************************/
static void Lcd_ClearScr(U16 c)
{
unsigned int x,y ;
for( y = 0 ; y < SCR_YSIZE_TFT_240320 ; y++ )
{
for( x = 0 ; x < SCR_XSIZE_TFT_240320 ; x++ )
{
LCD_BUFER[y][x] = c;
}
}
}
/**************************************************************
LCD躉珆尨晶眻楹蛌
// LCD display is flipped vertically
// But, think the algorithm by mathematics point.
// 3I2
// 4 I 1
// --+-- <-8 octants mathematical cordinate
// 5 I 8
// 6I7
**************************************************************/
static void Glib_Line(int x1,int y1,int x2,int y2,int color)
{
int dx,dy,e;
dx=x2-x1;
dy=y2-y1;
if(dx>=0)
{
if(dy >= 0) // dy>=0
{
if(dx>=dy) // 1/8 octant
{
e=dy-dx/2;
while(x1<=x2)
{
PutPixel(x1,y1,color);
if(e>0){y1+=1;e-=dx;}
x1+=1;
e+=dy;
}
}
else // 2/8 octant
{
e=dx-dy/2;
while(y1<=y2)
{
PutPixel(x1,y1,color);
if(e>0){x1+=1;e-=dy;}
y1+=1;
e+=dx;
}
}
}
else // dy<0
{
dy=-dy; // dy=abs(dy)
if(dx>=dy) // 8/8 octant
{
e=dy-dx/2;
while(x1<=x2)
{
PutPixel(x1,y1,color);
if(e>0){y1-=1;e-=dx;}
x1+=1;
e+=dy;
}
}
else // 7/8 octant
{
e=dx-dy/2;
while(y1>=y2)
{
PutPixel(x1,y1,color);
if(e>0){x1+=1;e-=dy;}
y1-=1;
e+=dx;
}
}
}
}
else //dx<0
{
dx=-dx; //dx=abs(dx)
if(dy >= 0) // dy>=0
{
if(dx>=dy) // 4/8 octant
{
e=dy-dx/2;
while(x1>=x2)
{
PutPixel(x1,y1,color);
if(e>0){y1+=1;e-=dx;}
x1-=1;
e+=dy;
}
}
else // 3/8 octant
{
e=dx-dy/2;
while(y1<=y2)
{
PutPixel(x1,y1,color);
if(e>0){x1-=1;e-=dy;}
y1+=1;
e+=dx;
}
}
}
else // dy<0
{
dy=-dy; // dy=abs(dy)
if(dx>=dy) // 5/8 octant
{
e=dy-dx/2;
while(x1>=x2)
{
PutPixel(x1,y1,color);
if(e>0){y1-=1;e-=dx;}
x1-=1;
e+=dy;
}
}
else // 6/8 octant
{
e=dx-dy/2;
while(y1>=y2)
{
PutPixel(x1,y1,color);
if(e>0){x1-=1;e-=dy;}
y1-=1;
e+=dx;
}
}
}
}
}
/**************************************************************
婓LCD躉奻賒珨跺撻倛
**************************************************************/
/*
static void Glib_Rectangle(int x1,int y1,int x2,int y2,int color)
{
Glib_Line(x1,y1,x2,y1,color);
Glib_Line(x2,y1,x2,y2,color);
Glib_Line(x1,y2,x2,y2,color);
Glib_Line(x1,y1,x1,y2,color);
}
*/
/**************************************************************
婓LCD躉奻蚚晇伎沓喃珨跺撻倛
**************************************************************/
static void Glib_FilledRectangle(int x1,int y1,int x2,int y2,int color)
{
int i;
for(i=y1;i<=y2;i++)
Glib_Line(x1,i,x2,i,color);
}
/**************************************************************
婓LCD躉奻硌隅釴梓萸賒珨跺硌隅湮苤腔芞
**************************************************************/
static void Paint_Bmp(int x0,int y0,int h,int l,const unsigned char bmp[])
{
int x,y;
U32 c;
int p = 0;
//掛芞珆尨岆淏腔珆尨源宒
for( y = 0 ; y < l ; y++ )
{
for( x = 0 ; x < h ; x++ )
{
c = bmp[p+1] | (bmp[p]<<8) ;
if ( ( (x0+x) < SCR_XSIZE_TFT_240320) && ( (y0+y) < SCR_YSIZE_TFT_240320) )
LCD_BUFER[y0+y][x0+x] = c ;
p = p + 2 ;
}
}
/*掛芞珆尨岆給徹懂腔ㄛ岆唅蛌賸180僅腔珆尨源宒
for( y = l ; y > 0 ; y-- )
{
for( x = h ; x > 0 ; x-- )
{
c = bmp[p+1] | (bmp[p]<<8) ;
if ( ( (x0+x) < SCR_XSIZE_TFT_240320) && ( (y0+y) < SCR_YSIZE_TFT_240320) )
LCD_BUFER[y0+y][x0+x] = c ;
p = p + 2 ;
}
}
*/
}
void Lcd_Tft_240X320_Init( void )
{
Lcd_Port_Init();
Lcd_Init();
Lcd_EnvidOnOff(1); //turn on vedio
Lcd_ClearScr(0xffff); //fill all screen with some color
#define LCD_BLANK 16
#define C_UP ( LCD_XSIZE_TFT_240320 - LCD_BLANK*2 )
#define C_RIGHT ( LCD_XSIZE_TFT_240320 - LCD_BLANK*2 )
#define V_BLACK ( ( LCD_YSIZE_TFT_240320 - LCD_BLANK*4 ) / 6 )
Glib_FilledRectangle( LCD_BLANK, LCD_BLANK, ( LCD_XSIZE_TFT_240320 - LCD_BLANK ), ( LCD_YSIZE_TFT_240320 - LCD_BLANK ),0x0000); //fill a Rectangle with some color
Glib_FilledRectangle( (LCD_BLANK*2), (LCD_BLANK*2 + V_BLACK*0), (C_RIGHT), (LCD_BLANK*2 + V_BLACK*1),0x001f); //fill a Rectangle with some color
Glib_FilledRectangle( (LCD_BLANK*2), (LCD_BLANK*2 + V_BLACK*1), (C_RIGHT), (LCD_BLANK*2 + V_BLACK*2),0x07e0); //fill a Rectangle with some color
Glib_FilledRectangle( (LCD_BLANK*2), (LCD_BLANK*2 + V_BLACK*2), (C_RIGHT), (LCD_BLANK*2 + V_BLACK*3),0xf800); //fill a Rectangle with some color
Glib_FilledRectangle( (LCD_BLANK*2), (LCD_BLANK*2 + V_BLACK*3), (C_RIGHT), (LCD_BLANK*2 + V_BLACK*4),0xffe0); //fill a Rectangle with some color
Glib_FilledRectangle( (LCD_BLANK*2), (LCD_BLANK*2 + V_BLACK*4), (C_RIGHT), (LCD_BLANK*2 + V_BLACK*5),0xf81f); //fill a Rectangle with some color
Glib_FilledRectangle( (LCD_BLANK*2), (LCD_BLANK*2 + V_BLACK*5), (C_RIGHT), (LCD_BLANK*2 + V_BLACK*6),0x07ff); //fill a Rectangle with some color
}
void DelayK(void)
{
int i;
for(i=0;i<100;i++);
}
void DelayMs( int x)
{
int i,j;
for(x=0;x<1;x++)
{
for(i=0;i<500;i++)
{
for(j=0;j<1000;j++);
}
}
}
void DelayGs(void)
{
int i,j;
for(i=0;i<1000;i++)
{
for(j=0;j<3000;j++);
}
}
//rGPFCON = rGPFCON & (~((3<<4)|(3<<0))) | ((0<<4)|(0<<0)) ; //GPF2,0 set input
//GPFDAT
char KeyEvent(void)
{
if((rGPFDAT&0x04) == 0)
{
DelayK();
if((rGPFDAT&0x04) == 0)
{
// while((IO1PIN&KEY1) == 0);
return (0);
}
else
return (1);
}
else
{
return (1);
}
}
/**************************************************************
**************************************************************/
void Test_Lcd_Tft_240X320( void )
{
#ifdef DEBUG
Uart_Printf("\nTest 240*320 TFT LCD !\n");
#endif
Lcd_Port_Init();
lcd_eio35_init(); //EIO3.5 LCD spi init
Lcd_Init(); //printf("EIO3.5_SPI_INIT OK ");
Lcd_EnvidOnOff(1); //turn on vedio
// printf("Lcd_EnvidOn");
// Lcd_Tft_240X320_Init();
while(1)
{
Paint_Bmp(0,0,LCD_XSIZE_TFT_240320,LCD_YSIZE_TFT_240320, picture1) ;
DelayGs();
while(!KeyEvent());
Paint_Bmp(0,0,LCD_XSIZE_TFT_240320,LCD_YSIZE_TFT_240320, picture5) ;
DelayGs();
while(!KeyEvent());
Paint_Bmp(0,0,LCD_XSIZE_TFT_240320,LCD_YSIZE_TFT_240320, picture3) ;
DelayGs();
while(!KeyEvent());
Paint_Bmp(0,0,LCD_XSIZE_TFT_240320,LCD_YSIZE_TFT_240320, picture4) ; //paint a bmp 320240pic3 in data.h
DelayGs();
while(!KeyEvent());
Lcd_ClearScr(WHITE);
DelayGs();
while(!KeyEvent());
Lcd_ClearScr(BLACK);
DelayGs();
while(!KeyEvent());
Lcd_ClearScr(R);
DelayGs();
while(!KeyEvent());
Lcd_ClearScr(G);
DelayGs();
while(!KeyEvent());
Lcd_ClearScr(B);
DelayGs();
while(!KeyEvent());
}
}
//*************************************************************
static void lcd_eio35_init(void)
{
int i;
RST_HIGH();
for(i=2000;i>0;i--);
RST_LOW();
for(i=20000;i>0;i--);
RST_HIGH();
for(i=2000;i>0;i--);
LCD_WriteCmd(0x0001); LCD_WriteDat(0x733f);
LCD_WriteCmd(0x0002); LCD_WriteDat(0x0200); //LCD-Driving-Waveform Control
LCD_WriteCmd(0x0003); LCD_WriteDat(0x6166);
// LCD_WriteCmd(0x0004); LCD_WriteDat(0x00c7);
LCD_WriteCmd(0x0005); LCD_WriteDat(0xff53);
// LCD_WriteCmd(0x000A); LCD_WriteDat(0x4008);
LCD_WriteCmd(0x000B); LCD_WriteDat(0x0000);
LCD_WriteCmd(0x000D); LCD_WriteDat(0x333A);
LCD_WriteCmd(0x000E); LCD_WriteDat(0x2EC0);
LCD_WriteCmd(0x000F); LCD_WriteDat(0x0000);
LCD_WriteCmd(0x0016); LCD_WriteDat(0x9F80);
LCD_WriteCmd(0x0017); LCD_WriteDat(0x4483);
LCD_WriteCmd(0x001E); LCD_WriteDat(0x006B);
LCD_WriteCmd(0x0030); LCD_WriteDat(0x0507);
LCD_WriteCmd(0x0031); LCD_WriteDat(0x0004);
LCD_WriteCmd(0x0032); LCD_WriteDat(0x0707);
LCD_WriteCmd(0x0033); LCD_WriteDat(0x0000);
LCD_WriteCmd(0x0034); LCD_WriteDat(0x0000);
LCD_WriteCmd(0x0035); LCD_WriteDat(0x0307);
LCD_WriteCmd(0x0036); LCD_WriteDat(0x0700);
LCD_WriteCmd(0x0037); LCD_WriteDat(0x0000);
LCD_WriteCmd(0x003A); LCD_WriteDat(0x140B);
LCD_WriteCmd(0x003B); LCD_WriteDat(0x140B);
}
static void LCD_WriteCmd(int cmd)
{
int i,tmp;
tmp = 0x70;
CSB_LOW();
for(i=8; i>0; i--)
{
if(tmp&0x80)
SDA_HIGH();
else
SDA_LOW();
SCK_LOW();
SCK_HIGH();
tmp<<=1;
}
for(i=16; i>0; i--)
{
if(cmd&0x8000)
SDA_HIGH();
else
SDA_LOW();
SCK_LOW();
SCK_HIGH();
cmd<<=1;
}
CSB_HIGH();
}
static void LCD_WriteDat(int dat)
{
int i,tmp;
tmp = 0x72;
CSB_LOW();
for(i=8; i>0; i--)
{
if(tmp&0x80)
SDA_HIGH();
else
SDA_LOW();
SCK_LOW();
SCK_HIGH();
tmp<<=1;
}
for(i=16; i>0; i--)
{
if(dat&0x8000)
SDA_HIGH();
else
SDA_LOW();
SCK_LOW();
SCK_HIGH();
dat<<=1;
}
CSB_HIGH();
}