www.pudn.com > S3C2440_uCos-II.rar > lcd_drv.c


#include  
#include  
#include  
#include  
#include  
 
//======================= 
#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 
     
    // 
    // 为了避免屏幕抖动,先不打开液晶控制器 
    // 
    Sem_LcdDraw = OSSemCreate(1);                   /* 创建1个信号量,初值为1   */ 
  rLCDCON1 = rLCDCON1 | 1; 
} 
 
void Lcd_Switch(int cmd) 
{ 
    if (cmd) 
    { 
        rLCDCON1 |= 1; 
    } 
    else 
    { 
        rLCDCON1 &= (~((unsigned int)1)); 
    } 
} 
 
void Lcd_White(void) 
{ 
    memset((unsigned char *)__LCDFrameBuffer, 0xff, VIDEO_VISIBLE_COLS*VIDEO_VISIBLE_ROWS*VIDEO_PIXEL_SIZE); 
} 
 
void Lcd_Cls() 
{ 
    memset((unsigned char *)__LCDFrameBuffer,0,VIDEO_VISIBLE_COLS*VIDEO_VISIBLE_ROWS*VIDEO_PIXEL_SIZE); 
} 
 
void Glib_PutPixel(unsigned int x , unsigned int y , unsigned int c) 
{ 
    //unsigned char *fb; 
     
        unsigned char rcolor = (c>>16)&0xff; 
        unsigned char gcolor = (c>>8)&0xff; 
        unsigned char bcolor = c&0xff; 
 
    if( x < VIDEO_VISIBLE_COLS && y < VIDEO_VISIBLE_ROWS ) 
    { 
        *(unsigned short *)(__LCDFrameBuffer + ((y) * VIDEO_VISIBLE_COLS + (x))*VIDEO_PIXEL_SIZE) = //c; 
                              (((rcolor>>3)<<11)|((gcolor>>2)<<5)|(bcolor>>3)); // lcd format 5:6:5 for 2440 
    } 
} 
 
static void Lcd_PutASCII(unsigned int x,unsigned int y,unsigned char ch,unsigned int c,unsigned int bk_c,unsigned int st) 
{ 
    unsigned short int i,j; 
    unsigned char *pZK,mask,buf; 
     
 
    pZK = &__VGA[ch*16]; 
    for( i = 0 ; i < 16 ; i++ ) 
    { 
        mask = 0x80; 
        buf = pZK[i]; 
        for( j = 0 ; j < 8 ; j++ ) 
        { 
            if( buf & mask ) 
            { 
                Glib_PutPixel(x+j,y+i,c); 
            }else 
            { 
                if( !st ) 
                { 
                    Glib_PutPixel(x+j,y+i,bk_c); 
                } 
            } 
             
            mask = mask >> 1; 
        } 
    } 
} 
 
 
void Lcd_PutHZ(unsigned int x,unsigned int y,unsigned short int QW,unsigned int c,unsigned int bk_c,unsigned int st) 
{ 
    /*This Function is EMPTY now, pls contact with FriendlyARM */ 
    return ; 
} 
//---------------------- 
void Lcd_printf(unsigned int x,unsigned int y,unsigned int c,unsigned int bk_c,unsigned int st,char *fmt,...) 
{ 
    char __LCD_Printf_Buf[256]; 
    va_list ap; 
    unsigned char *pStr = (unsigned char *)__LCD_Printf_Buf; 
    unsigned int i = 0; 
 
    va_start(ap,fmt); 
    vsprintf(__LCD_Printf_Buf,fmt,ap); 
    va_end(ap); 
      
    while(*pStr != 0 ) 
    { 
        switch(*pStr) 
        { 
            case '\n' : 
                { 
             
                    break; 
                } 
 
            default: 
                { 
                    if( *pStr > 0xA0 & *(pStr+1) > 0xA0 )  //中文输出 
                    { 
                        Lcd_PutHZ( x , y , (*pStr - 0xA0)*0x0100 + *(pStr+1) - 0xA0 , c , bk_c , st); 
 
                        pStr++; 
                        i++; 
 
                        x += 16; 
                    }else               //英文输出 
                    { 
                        Lcd_PutASCII( x , y , *pStr , c , bk_c , st ); 
 
                        x += 8; 
 
                    } 
 
                    break; 
                } 
        } 
         
        pStr++; 
        i++;         
 
        if( i > 256 ) break; 
    } 
} 
 
 
void Glib_Rectangle(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2,unsigned 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); 
} 
 
 
 
void Glib_FilledRectangle(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2,unsigned int color) 
{ 
    int i; 
 
    for(i=y1;i<=y2;i++) 
    Glib_Line(x1,i,x2,i,color); 
} 
 
 
 
// LCD display is flipped vertically 
// But, think the algorithm by mathematics point. 
//   3I2 
//   4 I 1 
//  --+--   <-8 octants  mathematical cordinate 
//   5 I 8 
//   6I7 
void Glib_Line(unsigned int x1,unsigned int y1,unsigned int x2,unsigned int y2,unsigned 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) 
                { 
                    Glib_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) 
                { 
                    Glib_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) 
                { 
                    Glib_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) 
                { 
                    Glib_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) 
                { 
                    Glib_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) 
                { 
                    Glib_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) 
                { 
                    Glib_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) 
                { 
                    Glib_PutPixel(x1,y1,color); 
                    if(e>0){x1-=1;e-=dy;}    
                    y1-=1; 
                    e+=dx; 
                } 
            } 
        }    
    } 
}