📄 lcd_drv.c
字号:
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
//=======================
#include "../UCOS-II/includes.h" /* uC/OS interface */
//=======================
#include "2440addr.h"
#include "lcd_drv.h"
//TFT 640480
//unsigned int __LCDFrameBuffer[VIDEO_VISIBLE_ROWS][LCD_XSIZE_TFT_640480];
volatile unsigned char *__LCDFrameBuffer;
static OS_EVENT *Sem_LcdDraw;
#define WRDATA (1)
#define POLLACK (2)
#define RDDATA (3)
#define SETRDADDR (4)
typedef unsigned char U8;
typedef unsigned int U32;
/*#define IIC_INTPEND 0x10*/
#define IICBUFSIZE 0x20
static U8 _iicData[IICBUFSIZE];
static int _iicDataCount;
static int _iicStatus;
static int _iicMode;
static int _iicPt;
static void IicPoll(void);
static void Run_IicPoll(void);
void Uart0_Printf(char *fmt,...);
static void Delay(int sec)
{
int dl;
while(sec != -1){
sec--;
for (dl=4900;dl>0;dl--);
}
return;
}
//**************[ iic_wr ]*****************************************
static void iic_wr(U32 slvAddr,U32 addr,U8 data)
{
_iicMode = WRDATA;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicData[1] = data;
_iicDataCount = 2;
rIICDS = slvAddr; //0xa0
//Master Tx mode, Start(Write), IIC-bus data output enable
//Bus arbitration sucessful, Address as slave status flag Cleared,
//Address zero status flag cleared, Last received bit is 0
rIICSTAT = 0xf0;
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1)
Run_IicPoll();
_iicMode = POLLACK;
while(1)
{
rIICDS = slvAddr;
_iicStatus = 0x100; //To check if _iicStatus is changed
rIICSTAT = 0xf0; //Master Tx, Start, Output Enable, Sucessful, Cleared, Cleared, 0
rIICCON = 0xaf; //Resumes IIC operation.
while(_iicStatus==0x100)
Run_IicPoll();
if(!(_iicStatus & 0x1))
break; //When ACK is received
}
rIICSTAT = 0xd0; //Master Tx condition, Stop(Write), Output Enable
rIICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Write is completed.
}
//************************[ iic_rd ]********************************
static U8 iic_rd(U32 slvAddr,U32 addr)
{
_iicMode = SETRDADDR;
_iicPt = 0;
_iicData[0] = (U8)addr;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xf0; //MasTx,Start
//Clearing the pending bit isn't needed because the pending bit has been cleared.
while(_iicDataCount!=-1)
Run_IicPoll();
_iicMode = RDDATA;
_iicPt = 0;
_iicDataCount = 1;
rIICDS = slvAddr;
rIICSTAT = 0xb0; //Master Rx,Start
rIICCON = 0xaf; //Resumes IIC operation.
while(_iicDataCount!=-1)
Run_IicPoll();
return _iicData[1];
}
//**********************[ Run_IicPoll ]*********************************
static void Run_IicPoll(void)
{
if(rIICCON & 0x10) //Tx/Rx Interrupt Enable
IicPoll();
}
//**********************[IicPoll ]**************************************
static void IicPoll(void)
{
U32 iicSt,i;
iicSt = rIICSTAT;
if(iicSt & 0x8){} //When bus arbitration is failed.
if(iicSt & 0x4){} //When a slave address is matched with IICADD
if(iicSt & 0x2){} //When a slave address is 0000000b
if(iicSt & 0x1){} //When ACK isn't received
switch(_iicMode)
{
case POLLACK:
_iicStatus = iicSt;
break;
case RDDATA:
if((_iicDataCount--)==0)
{
_iicData[_iicPt++] = rIICDS;
rIICSTAT = 0x90; //Stop MasRx condition
rIICCON = 0xaf; //Resumes IIC operation.
Delay(1); //Wait until stop condtion is in effect.
//Too long time...
//The pending bit will not be set after issuing stop condition.
break;
}
_iicData[_iicPt++] = rIICDS;
//The last data has to be read with no ack.
if((_iicDataCount)==0)
rIICCON = 0x2f; //Resumes IIC operation with NOACK.
else
rIICCON = 0xaf; //Resumes IIC operation with ACK
break;
case WRDATA:
if((_iicDataCount--)==0)
{
rIICSTAT = 0xd0; //stop MasTx condition
rIICCON = 0xaf; //resumes IIC operation.
Delay(1); //wait until stop condtion is in effect.
//The pending bit will not be set after issuing stop condition.
break;
}
rIICDS = _iicData[_iicPt++]; //_iicData[0] has dummy.
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rIICCON = 0xaf; //resumes IIC operation.
break;
case SETRDADDR:
// Uart_Printf("[S%d]",_iicDataCount);
if((_iicDataCount--)==0)
{
break; //IIC operation is stopped because of IICCON[4]
}
rIICDS = _iicData[_iicPt++];
for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
rIICCON = 0xaf; //resumes IIC operation.
break;
default:
break;
}
}
static int VGADAC_init(void)
{
//int var0,var1;
unsigned int lcdcon;
lcdcon = rLCDCON1;
rLCDCON1 = lcdcon & ~1;
rGPEUP |= 0xc000; //Pull-up disable
rGPECON = (rGPECON&~0xf0000000)|0xa0000000; //GPE15:IICSDA , GPE14:IICSCL
//Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
//IICCON = 0xaf;
rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);
rIICADD = 0x10; //2410 slave address = [7:1]
rIICSTAT = 0x10; //IIC bus data output enable(Rx/Tx)
//Uart0_Printf("VGA set1!\n");
iic_wr(0xec,4,0x20);
//Uart0_Printf("VGA set2!\n");
iic_wr(0xec,7,0x8c);
//Uart0_Printf("VGA set3!\n");
iic_wr(0xec,14,0x1b);
//Uart0_Printf("VGA set4!\n");
iic_wr(0xec,13,0x03);
//Uart0_Printf("VGA ve!\n");
if (iic_rd(0xec,4) != 0x20) return -1;
if (iic_rd(0xec,7) != 0x8c) return -1;
if (iic_rd(0xec,14) != 0x1b) return -1;
rLCDCON1 = lcdcon;
return 0;
}
void Lcd_Init()
{
//int ClkVal;
__LCDFrameBuffer = (unsigned char*)_LCD_FBADDRESS;//+0x800000;
if (VGADAC_init()!=0)
{
Uart0_Printf("VGA Initial error!\n");
while(1);
//return;
}
//Uart0_Printf("VGA Initial ok!\n");
rGPCUP=0xffffffff; // Disable Pull-up register
rGPCCON=0xaaaaaaaa; //Initialize VD[7:0],LCDVF[2:0],VM,VFRAME,VLINE,VCLK,LEND
rGPDUP=0xffffffff; // Disable Pull-up register
rGPDCON=0xaaaaaaaa; //Initialize VD[23:8]
// 时钟25MHz MVAL值 TFT 24BPP
//rLCDCON1=(CLKVAL_TFT_640480<<8)|(MVAL_USED<<7)|(3<<5)|(13<<1)|0;
// 时钟25MHz MVAL值 TFT 16BPP
rLCDCON1=(CLKVAL_TFT_640480<<8)|(MVAL_USED<<7)|(3<<5)|(12<<1)|0;
rLCDCON2=(VBPD_640480<<24)|(LINEVAL_TFT_640480<<14)|(VFPD_640480<<6)|(VSPW_640480);
rLCDCON3=(HBPD_640480<<19)|(HOZVAL_TFT_640480<<8)|(HFPD_640480);
rLCDCON4=(MVAL<<8)|(HSPW_640480);
//0x00 XX XX XX 5:5:5:I
// --- R G B
//rLCDCON5=(0<<12)|(0<<11)|(1<<9)|(1<<8)|1; // BPP24:MSB,HSYNC and VSYNC are inverted
//0x00 XX XX XX 5:6:5
// --- R G B
rLCDCON5=(0<<12)|(1<<11)|(1<<9)|(1<<8)|1; // BPP24:MSB,HSYNC and VSYNC are inverted
rLCDSADDR1=( ( (unsigned int)_LCD_FBADDRESS >> 22) << 21 ) | M5D ( (unsigned int)_LCD_FBADDRESS >> 1 );
rLCDSADDR2=M5D( ( (unsigned int)_LCD_FBADDRESS + (VIDEO_VISIBLE_COLS*VIDEO_VISIBLE_ROWS*VIDEO_PIXEL_SIZE ) ) >> 1 );
rLCDSADDR3=(0<<11)|(VIDEO_VISIBLE_COLS*2);
//rLPCSEL&=(~7); // Disable LPC3600
rTCONSEL&=(~0x07); // Disable LPC3600
rTPAL=0; // Disable Temp Palette
//
// 为了避免屏幕抖动,先不打开液晶控制器
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -