⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vgadriver_ok.c

📁 使用Linux ARM GCC编译器来编译
💻 C
字号:
#include "2440addr.h"
#include "def.h"
#include "2440iic.h"
#include "../include/command.h"
#include "lcd.h"

#define NULL 0
void printk(const char* , ...);

typedef unsigned int* u_pt;
#define GPEUP (*(u_pt)0x54000048)
#define GPECON (*(unsigned int*)0x54000040)
#define BANKCON6 (*(unsigned int*)0x4800001c)
#define BANKCON7 (*(unsigned int*)0x48000020)
#define REFRESH (*(unsigned int*)0x48000024)
#define MRSRB6 (*(unsigned int*)0x4800002c)
#define MRSRB7 (*(unsigned int*)0x48000030)

/*#define IICCON (*(unsigned int*)0x54000000)
#define IICADD (*(unsigned int*)0x54000008)
#define IICSTAT (*(unsigned int*)0x54000004)

#define IIC_INTPEND 0x10*/

//#define DELAYTIME 1000
//#define TIMEOUT 0x200
//#define Delay(x) udelay (x)
typedef unsigned long ulong;
#define DELAYTIME 1000
#define TIMEOUT (15625*10)
#define Delay(x) udelay (x)
ulong get_timer(ulong base);

char iic_data;

void udelay (int);

static U8 _iicData[IICBUFSIZE];
static volatile int _iicDataCount;
static volatile int _iicStatus;
static volatile int _iicMode;
static int _iicPt;
unsigned short int * const __LCDFrameBuffer = (unsigned short int*)0x30500000;

void Lcd_Init()
{
    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]   
    
#if 0
    //         时钟25MHz               MVAL值       TFT    24BPP
    rLCDCON1=(CLKVAL_TFT_640480<<8)|(MVAL_USED<<7)|(3<<5)|(13<<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
    // ---  R  G  B
    rLCDCON5=(0<<12)|(1<<9)|(1<<8); // BPP24:MSB,HSYNC and VSYNC are inverted

    
    rLCDSADDR1=( ( (unsigned int)__LCDFrameBuffer >> 22) << 21 ) | M5D ( (unsigned int)__LCDFrameBuffer >> 1 );
    rLCDSADDR2=M5D( ( (unsigned int)__LCDFrameBuffer + (LCD_XSIZE_TFT_640480*LCD_YSIZE_TFT_640480*2 ) ) >> 1 );

    rLCDSADDR3=(0<<11)|(LCD_DISPLAY_XSIZE_TFT_640480*1);

    rLPCSEL&=(~7); // Disable LPC3600
    rTPAL=0; // Disable Temp Palette
#endif

    //         时钟25MHz               MVAL值       TFT    16BPP
    rLCDCON1= LCD_DISPLAY_CLKVAL|MVAL_USED|MODE_TFT|BPP_16|LCD_DIS;
    rLCDCON2=(LCD_DISPLAY_VBPD<<24)|(LCD_DISPLAY_LINEVAL<<14)|(LCD_DISPLAY_VFPD<<6)|(LCD_DISPLAY_VSPW);
    rLCDCON3=(LCD_DISPLAY_HBPD<<19)|(LCD_DISPLAY_HOZVAL<<8)|(LCD_DISPLAY_HFPD);
    rLCDCON4=(MVAL<<8)|(LCD_DISPLAY_HSPW);

    //0x00 XX XX XX
    // ---  R  G  B
    rLCDCON5=(0<<12)|(1<<11)|(1<<9)|(1<<8)|1; // BPP24:LSB,5:6:5 Format,HSYNC and VSYNC are inverted,half-word swap

    
    rLCDSADDR1=( ( (unsigned int)__LCDFrameBuffer >> 22) << 21 ) | M5D ( (unsigned int)__LCDFrameBuffer >> 1 );
    rLCDSADDR2=M5D( ( (unsigned int)__LCDFrameBuffer + (LCD_DISPLAY_XSIZE*LCD_DISPLAY_YSIZE*2 ) ) >> 1 );

    rLCDSADDR3=(0<<11)|(LCD_DISPLAY_XSIZE*1);

    rTCONSEL &= ~((1<<4)|7); // Disable LPC3600 & LCC3600
    rTPAL=0; // Disable Temp Palette
    
    //
    // 为了避免屏幕抖动,先不打开液晶控制器
    //

  rLCDCON1 = rLCDCON1 | LCD_EN;
}

intnand_read_ll(unsigned char *buf, unsigned long start_addr, int size);

void Lcd_ClearDisplayBuff(void)
{
    memset(__LCDFrameBuffer, 0x00, LCD_DISPLAY_XSIZE*LCD_DISPLAY_YSIZE*2);
    //memcpy(__LCDFrameBuffer, (unsigned char*)0+0x180000, LCD_DISPLAY_XSIZE*LCD_DISPLAY_YSIZE*2);
    //nand_read_ll((unsigned char *)__LCDFrameBuffer, (unsigned long)0x200000, 0x200000);
}


void iic_wr(U32 slvAddr,U32 addr,U8 data)
{
    ulong timeout;
    _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;      
    //rIICCON  = 0xaf;
      //Clearing the pending bit isn't needed because the pending bit has been cleared.
    //timeout = 0;
    //while(_iicDataCount!=-1) {
    //   Run_IicPoll();
    //   if (timeout++ > TIMEOUT) break;
    //}
    timeout = get_timer(0);
    while(_iicDataCount!=-1) {
       Run_IicPoll();
       if (get_timer(0)-timeout > TIMEOUT) break;
    }

    /*_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();
        timeout = get_timer(0);
        while(_iicStatus==0x100) {
           Run_IicPoll();
           if (get_timer(0)-timeout > TIMEOUT) break;
        }
              
        if(!(_iicStatus & 0x1))
            break;                      //When ACK is received
    }*/
    rIICSTAT = 0xd0;                    //Master Tx condition, Stop(Write), Output Enable
    rIICCON  = 0xaf;                    //Resumes IIC operation.
    Delay(DELAYTIME);                           //Wait until stop condtion is in effect.
      //Write is completed.
}
        
//************************[ iic_rd ]********************************
void iic_rd(U32 slvAddr,U32 addr,U8 *data)
{
    ulong timeout;
    
    _iicMode      = SETRDADDR;
    _iicPt        = 0;
    _iicData[0]   = (U8)addr;
    _iicDataCount = 1;

    rIICDS   = slvAddr;
    rIICSTAT = 0xf0;                    //MasTx,Start  
    //rIICCON  = 0xaf;
      //Clearing the pending bit isn't needed because the pending bit has been cleared.
//    timeout = 0;
//    while(_iicDataCount!=-1) {
//       Run_IicPoll();
//       if (timeout++ > TIMEOUT) break;
//    }
    timeout = get_timer(0);
    while(_iicDataCount!=-1) {
       Run_IicPoll();
       if (get_timer(0)-timeout > TIMEOUT) break;
    }

    _iicMode      = RDDATA;
    _iicPt        = 0;
    _iicDataCount = 1;
    
    rIICDS   = slvAddr;
    rIICSTAT = 0xb0;                    //Master Rx,Start
    rIICCON  = 0xaf;                    //Resumes IIC operation.   

//    timeout = 0;
//    while(_iicDataCount!=-1) {
//       Run_IicPoll();
//       if (timeout++ > TIMEOUT) break;
//    }
    timeout = get_timer(0);
    while(_iicDataCount!=-1) {
       Run_IicPoll();
       if (get_timer(0)-timeout > TIMEOUT) break;
    }

    *data = _iicData[1];
    //rIICSTAT = 0xd0;                    //Master Tx condition, Stop(Write), Output Enable
    //rIICCON  = 0xaf;                    //Resumes IIC operation.
    Delay(DELAYTIME);
}

//**********************[ Run_IicPoll ]*********************************
void Run_IicPoll(void)
{
    if(rIICCON & 0x10) {                  //Tx/Rx Interrupt Enable
       //udelay (100);
       IicPoll();
    }
}       
    
//**********************[IicPoll ]**************************************
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
    //_iicStatus = iicSt;

    //printk(">");
    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.
                //while (rIICSTAT & 0x20);
                
                Delay(10);                       //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.
                //while (rIICSTAT & 0x20);
                
                Delay(10);                       //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<100;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)
            {
                Delay(10);
                break;                  //IIC operation is stopped because of IICCON[4]    
            }
            rIICDS = _iicData[_iicPt++];
            for(i=0;i<100;i++);          //for setup time until rising edge of IICSCL
            rIICCON = 0xaf;             //resumes IIC operation.
            break;

        default:
            break;      
    }
}

int VGA_init(void)
{
    //int var0,var1;
    //BANKCON6 = BANKCON7 = 0x18005;
    //REFRESH = 0x008e01e9;
    MRSRB6 = MRSRB7 = 0x20;
    
    rGPECON = (rGPECON&~0xf0000000) | 0xa0000000;                //GPE15:IICSDA , GPE14:IICSCL     
    rGPEUP  |= 0xc000;                  //Pull-up disable
    
    //Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
    //IICCON = 0xaf;  /* PCLK/512 */
    rIICCON  = (1<<7) | (0<<6) | (1<<5) | (0xf);
    
    rIICADD  = 0x10;                    //2440 slave address = [7:1]
    rIICSTAT = 0x10;                    //IIC bus data output enable(Rx/Tx)
    //rIICADD = 0x9f;
    //rIICSTAT = 0x9f;
    
    //printk("write iic\n");
    iic_wr(0xec,4,0x20);
    //printk("2.\n");
    iic_wr(0xec,7,0x8c);
    //printk("3.\n");
    iic_wr(0xec,14,0x1b);
    //printk("4.\n");
    iic_wr(0xec,13,0x03);
    
    //printk("read back from iic\n");
    iic_rd(0xec,4,&iic_data);
    if (iic_data != 0x20) return -1;
    
    //printk("6.\n");
    iic_rd(0xec,7,&iic_data);
    if (iic_data != 0x8c) return -1;
    
    //printk("7.\n");
    iic_rd(0xec,14,&iic_data);
    if (iic_data != 0x1b) return -1;
    
    return 0;
}

int TV_init(int mode)
{
    int var0,var1;
    
    var1 = GPECON;
    var0 = GPEUP;
   
    GPEUP = GPEUP | 0xc000;         //IIC 上拉
    GPECON = GPECON | 0xa00000;   //IIC
    rIICCON = 0xaf;
    rIICADD = rIICSTAT = 0x10;
    
    if (mode == 1)
    {
        printk("initial tv mode 640480.\n");
        iic_wr(0xec,0,0x42);
    }
    
    if (mode == 2)
    {
        printk("initial tv mode 640400.\n");
        iic_wr(0xec,0,0x61);
    }

    iic_wr(0xec,4,0x00);
    iic_wr(0xec,7,0x64);
    iic_wr(0xec,8,0x01);
    iic_wr(0xec,10,0x30);
    iic_wr(0xec,11,0x20);
    iic_wr(0xec,9,0x69);
    iic_wr(0xec,14,0x0a);
    iic_wr(0xec,16,0x00);
    iic_wr(0xec,61,0x00);
    iic_wr(0xec,1,0x08);
    
    iic_rd(0xec,9,&iic_data);
    if (iic_data != 0x69)
        return -1;
    
    iic_rd(0xec,14,&iic_data);
    if (iic_data != 0x0a)
        return -1;

    iic_rd(0xec,16,&iic_data);
    if (iic_data != 0x00)
        return -1;

    iic_rd(0xec,61,&iic_data);
    if (iic_data != 0x00)
        return -1;

    iic_rd(0xec,0,&iic_data);
    if (iic_data != 0x42)
        return -1;

    iic_rd(0xec,4,&iic_data);
    if (iic_data != 0x00)
        return -1;

    iic_rd(0xec,7,&iic_data);
    if (iic_data != 0x64)
        return -1;

    iic_rd(0xec,8,&iic_data);
    if (iic_data != 0x01)
        return -1;

    iic_rd(0xec,10,&iic_data);
    if (iic_data != 0x30)
        return -1;

    iic_rd(0xec,11,&iic_data);
    if (iic_data != 0x20)
        return -1;

    GPEUP = var0;
    GPECON = var1;
    return 0;
}

static void
vga(int argc, const char* argv[])
{
    /*if (chech_id()<0)
    {
        printk("error!\n");
        return;
    }*/
    
    Lcd_ClearDisplayBuff();
    Lcd_Init();
    
    if (VGA_init()!=0)
    {
        printk("vga initialize error!\n");
        return;
    }
    
    printk("vga initialized ok!\n");
}

static void
tv(int argc, const char* argv[])
{
    int ret;
    
    /*if (chech_id()<0)
    {
        printk("");
        return;
    }*/
    
    ret = 0;
    if (argc==1)
        ret = TV_init(1);
    if (argc<=2) {
        if (!strcmp("640400",argv[1]))
            ret = TV_init(1);
        if (!strcmp("640480",argv[1]))
            ret = TV_init(2);
    }
    
    if (ret) {
        printk("error!\n");
        return;
    }
    
    printk("ok!\n");
}

user_command_t vga_cmd = {	"vga",	vga,	NULL,	"vga \t\t-- init vga output."};

user_command_t tv_cmd = {	"tv",	tv,	NULL,	"tv <mode>\t\t-- init tv output."};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -