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

📄 twowire.c

📁 t112 参考文件
💻 C
字号:
//---------------------------------------------------------------------------
// Terawins Inc. Company Confidential Strictly Private
//
// $Archive: $
// $Revision: 1.9 $
// $Author: jwang $
// $Date: 2002/07/08 22:34:39 $
//
// --------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
// --------------------------------------------------------------------------
// Copyright 2002 (c) Terawins Inc.
// --------------------------------------------------------------------------

#include "reg51.h"
#include "common.h"
#include "System.h"
#include "TW10xReg.h"
#include "TwoWire.h"
#include "Tuner.h"
#define TW_SLOW_SPEED
//----------------------------------------
// S_Condition for TWD protocol
//----------------------------------------
#if defined(TW_SLOW_SPEED)
void StartCondition(void)
{
    uCHAR ix;
    uCHAR cTWtrytime=0;
    EA=0;
    while(++cTWtrytime)
    {
        Set_SDA_High;
        Set_SCL_High;        /* make sure two line is release */
        for(ix = 0; ix < TWD_LONG_TIME; ix++)
        { };  /* Delay 12us */

        Set_SDA_Low;
        for(ix = 0; ix < TWD_LONG_TIME; ix++)
        { };  /* Delay 12us */

        if((SCL_High)&&(SDA_Low))
            break;
    }

    for(ix = 0; ix < TWD_SHORT_TIME; ix++)
    { };  /* Delay 12us */
}

//----------------------------------------
// P_Condition for TWD protocol
//----------------------------------------
void StopCondition(void)
{
    uCHAR ix;
    uCHAR cTWtrytime=0;

    Set_SDA_Low;
    Set_SCL_High;
    for(ix = 0; ix < TWD_SHORT_TIME; ix++)
    { };  /* delay 12us */

    while(SCL_Low && ++cTWtrytime)
    { };

    for(ix = 0; ix < TWD_SHORT_TIME; ix++)
    { };  /* delay 12us */

    Set_SDA_High;
    for(ix = 0; ix < TWD_SHORT_TIME; ix++)
    { };  /* delay 12us */
	EA=1;
}

//--------------------------------------------------
// Send_Byte
//    Send a byte to master with a acknowledge bit
//--------------------------------------------------
uCHAR Send_Byte(uCHAR cData)
{
    uCHAR ix, j, cAcknowledge;
    uCHAR cTWtrytime=0;

    cAcknowledge = 0;

    for(ix = 0; ix < 8; ix++)
    {
        Set_SCL_Low;
        for(j = 0; j < TWD_SHORT_TIME; j++)
        { };

        if(cData&0x80)Set_SDA_High;
		else Set_SDA_Low;
		cData<<=1;

        for(j = 0; j < TWD_SHORT_TIME; j++)
        { };

        Set_SCL_High;
        while(SCL_Low && ++cTWtrytime)
        { };

        for(j = 0; j < TWD_SHORT_TIME; j++)
        { };
    }
    for(j = 0; j < TWD_SHORT_TIME; j++)
    { };

    Set_SCL_Low;
    for(j = 0; j < TWD_SHORT_TIME; j++)
    { };

    Set_SDA_High;                /* release data line for acknowledge */
    for(j = 0; j < TWD_SHORT_TIME; j++)
    { };

    Set_SCL_High;                /* Send a clock for Acknowledge */
    while(SCL_Low)
    { };
    for(j = 0; j < TWD_SHORT_TIME; j++)
    { };

    if(SDA_High) 
       cAcknowledge = 1; /* No Acknowledge */
    Set_SCL_Low;                   /* Finish Acknoledge */
    for(j = 0; j < TWD_SHORT_TIME; j++)
    { };

    return(cAcknowledge);
}


uCHAR Send_Byte1(uCHAR cData)
{
    uCHAR ix, j, cAcknowledge;
    uCHAR cTWtrytime=0;

    cAcknowledge = 0;

    for(ix = 0; ix < 8; ix++)
    {
 

          if(cData&0x80)Set_SDA_High;
		else Set_SDA_Low;
		cData<<=1;
     Set_SCL_High;   
        for(j = 0; j < TWD_SHORT_TIME; j++)
        { };

       	Set_SCL_Low;
  

        for(j = 0; j < TWD_SHORT_TIME; j++)
        { };
    }
 

 
    return(1);
}
//--------------------------------------------------
// Read_Byte
//    Read a byte from master with a acknowledge bit
//--------------------------------------------------
uCHAR Read_Byte(uCHAR cNum)
{
    uCHAR ix, j;
    uCHAR cRetval=0;

    for(ix=0;ix<8;ix++){
          Set_SCL_High;
          while(SCL_Low){};
          for(j=0;j<TWD_SHORT_TIME;j++){};
          cRetval = (SDA_High)? (cRetval|(1<<(7-ix))):cRetval ;  /* MSB First */
	  Set_SCL_Low;
     }

     if(cNum==1)
     	Set_SDA_High;
     else
		Set_SDA_Low;
     //SDA = (cNum==1) 1:0;
     Set_SCL_High;
     while(SCL_Low){};
     for(j = 0; j < TWD_SHORT_TIME; j++)
     { };

    Set_SCL_Low;

    Set_SDA_High;
    for(j = 0; j < TWD_SHORT_TIME; j++)
    { };

    return cRetval;
}

#else
// for fast Two Wire Bus Speed

void StartCondition(void)
{
//    uCHAR ix;
    uCHAR cTWtrytime=0;

    while(++cTWtrytime)
    {
        Set_SDA_High;
        Set_SCL_High;        /* make sure two line is release */

        Set_SDA_Low;

        if((SCL_High)&&(SDA_Low))
            break;
    }
}

//----------------------------------------
// P_Condition for TWD protocol
//----------------------------------------
void StopCondition(void)
{
//    uCHAR ix;
    uCHAR cTWtrytime=0;

    Set_SDA_Low;
    Set_SCL_High;

    Set_SDA_High;
}

//--------------------------------------------------
// Send_Byte
//    Send a byte to master with a acknowledge bit
//--------------------------------------------------
uCHAR Send_Byte(uCHAR cData)
{
    uCHAR ix;
    uCHAR cAcknowledge;
    uCHAR cTWtrytime=0;

    cAcknowledge = 0;

    for(ix = 0; ix < 8; ix++)
    {
        Set_SCL_Low;
        //        (cData&(1<<(7-ix)))?(Set_SDA_High):(Set_SDA_Low);  /* MSB First */
        if(cData&0x80)Set_SDA_High;
		else Set_SDA_Low;
		cData<<=1;


        Set_SCL_High;
    }

    Set_SCL_Low;
    Set_SDA_High;                /* release data line for acknowledge */
    Set_SCL_High;                /* Send a clock for Acknowledge */

    if(SDA_High) cAcknowledge = 1; /* No Acknowledge */
    Set_SCL_Low;                   /* Finish Acknoledge */

    return(cAcknowledge);
}




//--------------------------------------------------
// Read_Byte
//    Read a byte from master with a acknowledge bit
//--------------------------------------------------
uCHAR Read_Byte(void)
{
    uCHAR ix, j;
    uCHAR cRetval;
    uCHAR cTWtrytime=0;

//    cRetval = 0;
    Set_SDA_High;

    for(ix = 0; ix < 8; ix++)
    {
        Set_SCL_Low;
        for(j = 0; j < TWD_SHORT_TIME; j++)
        { };

        Set_SCL_High;
        while(SCL_Low)
        { };

        for(j = 0; j < TWD_SHORT_TIME; j++)
        { };

		cRetval<<=1;
		if(SDA_High) cRetval++;
	}

    for(j = 0; j < TWD_SHORT_TIME; j++)
    { };

    Set_SCL_Low;
    for(j = 0; j < TWD_SHORT_TIME; j++)
    { };

    return cRetval;
}


#endif

//--------------------------------------------------
// Write Byte to Device
//--------------------------------------------------

//--------------------------------------------------
// Read Byte to Device
//--------------------------------------------------
uCHAR I2CReadByte(uCHAR cDevAddr, uCHAR cReg)
{
    uCHAR cResult = 0;


    /* write reg offset */
    StartCondition();
    if(Send_Byte(cDevAddr))
	return 0;  // Write address
    if(Send_Byte(cReg))
	return 0;

    /* read data */
    StartCondition();
    if (Send_Byte(cDevAddr | 0x01)) // Read address
	return 0;
    cResult = Read_Byte(1);

    StopCondition();
    return cResult;
}


uCHAR I2CWriteByte(uCHAR cDevAddr, uCHAR cReg, uCHAR cData)
{


    /* start condition */
    StartCondition();
  cDevAddr &= 0xFE;   // Force WRITE address
    /* send device ID and write data */
    if(!Send_Byte(cDevAddr))
    {
        if(!Send_Byte(cReg))
        {
            if(!Send_Byte(cData))
            {
                StopCondition();
                return(0);      /* success */
            }
        }
    }
    StopCondition();
    return(1);
}

uCHAR I2CWriteByte1(uCHAR cDevAddr, uCHAR cReg, uCHAR cData)
{
 Set_SCL_Low;
 Set_SDA_Low;
Send_Byte1(cDevAddr);
Send_Byte1(cReg);
Send_Byte1(cData);
  Set_SCL_Low;
 Set_SDA_Low;
    return(1);
}

//--------------------------------------------------
// 041901, Added for TWD Burst Write
// 1. PT8655X_Wr_Burst_A    (Start + Slave_ID + Address)
// 2. PT8655X_Wr_Burst_D    (Data)
// 3. PT8655X_Wr_Burst_P    (stop)
//--------------------------------------------------
uCHAR twdWr_Burst_A(uCHAR cReg)
{
    StartCondition();
    if(!Send_Byte(TW101))
    {
        if(!Send_Byte(cReg))
        {
            return(0);               //
        }
    }
    return(1);                  //
}

#ifdef TW100K
void twdWr_Burst_D(uCHAR cData)
{
    uCHAR ix;

    for(ix = 0; ix < 8; ix++)
    {
        Set_SCL_Low;
        if(cData&0x80)Set_SDA_High;
		else Set_SDA_Low;
		cData<<=1;
        Set_SCL_High;
    }
    Set_SCL_Low;
    Set_SDA_High;                /* release data line for acknowledge */
    Set_SCL_High;                /* Send a clock for Acknowledge */
//    if(SDA_High) cAcknowledge = 1; /* No Acknowledge */
    Set_SCL_Low;                   /* Finish Acknoledge */

//    return(cAcknowledge);

}

#else
void twdWr_Burst_D(uCHAR cData)
{
    uCHAR ix;
//uCHAR cAcknowledge;
//    uCHAR cTWtrytime=0;
//    cAcknowledge = 0;

    for(ix = 0; ix < 8; ix++)
    {
        Set_SCL2Low;
        if(cData&0x80)Set_SDA2High;
		else Set_SDA2Low;
		cData<<=1;
        Set_SCL2High;
    }
    Set_SCL2Low;
    Set_SDA2High;                /* release data line for acknowledge */
    Set_SCL2High;                /* Send a clock for Acknowledge */
//    if(SDA_High) cAcknowledge = 1; /* No Acknowledge */
    Set_SCL2Low;                   /* Finish Acknoledge */

//    return(cAcknowledge);

}
#endif

void twdWr_Burst_P(void)
{
    StopCondition();
}

//twdWriteTable() is used for burst write for
//any I2C standar device
#if VIDEO_AVAILABLE
#ifdef VIDEO_SAA7114
#define EOT	-1
BOOL twdWriteTable(uCHAR cDevAddr, char *pString)
{
	uCHAR  cReg=0;
  uCHAR  cNum=0;
  while(*pString != EOT)
  {
		cReg = *pString++;
		cNum = *pString++;
    	if(!I2CWriteBytes(cDevAddr, cReg, cNum, pString))
			break;
		else
			pString += cNum;
	}
}
#endif
#endif
//--------------------------------------------------
// Write Multiple Bytes to Device
//--------------------------------------------------


#if  0
BOOL I2CWriteBytes(uCHAR cDevAddr, uCHAR cReg, uCHAR cNum, char *cData)
{
  /* start condition */
	StartCondition();

  /* send device ID and write data */
  if(!Send_Byte(cDevAddr))
  {
  	if(!Send_Byte(cReg))
		{
	   	while(cNum--)
	   	{
				if(Send_Byte(*cData++))  //If false: release line and return
        	{
        		StopCondition();
           	return(0);      /* unsuccess */
				}
 			}
	   	StopCondition();
    		return(1);
  	}
	}
  StopCondition();
  return(0);                  /* fail */
}
#endif
//--------------------------------------------------
// Read Multiple Bytes to Device
//--------------------------------------------------
BOOL I2CReadBytes(uCHAR cDevAddr, uCHAR cReg, uCHAR *pString, uCHAR cNum)
{
	/* write reg offset */
	StartCondition();
  if(Send_Byte(cDevAddr))
		return 0;  // Write address
  if(Send_Byte(cReg))
		return 0;

	/* read data */
  StartCondition();
  if (Send_Byte(cDevAddr | 0x01)) // Read address
		return 0;
	while(cNum)
  {
  	*pString++ = Read_Byte(cNum);
		cNum--;
	}

 	StopCondition();
  return 1;
}


void twdDelay(uWORD wLoops)
{
	uWORD wTemp;

    while (wLoops--) {
	    wTemp = 1000/6;	// one loop below takes about 11 us
        while (wTemp--);
    }
}
void twdDelay1(uWORD wLoops)
{
	uWORD wTemp;

    while (wLoops--) {
	    wTemp = 1000/6;	// one loop below takes about 11 us
        while (wTemp--);
    }
	/*
	I2CWriteByte(TW101,0x32,0x0f);
    while (wLoops){
	   if(I2CReadByte(TW101,0x32)&0x01){
	   	I2CWriteByte(TW101,0x32,0x0f);
      	wLoops--;
	   }
    }*/
}

// OSD config register Write
void OSDCfgWr(uCHAR index,uCHAR dat)
{
 I2CWriteByte(TW101,OSD_CFG_INDEX,index);
 I2CWriteByte(TW101,OSD_CFG_DATA,dat);
}

#ifdef TV
uCHAR TunerReadByte(uCHAR cDevAddr)  //uCHAR cReg)
{
	uCHAR cResult = 0;
    
#ifdef TWO_TW_BUS
  gbTwBusSel=0; 
#endif
   /* read data */
   StartCondition();
   if (Send_Byte(cDevAddr | 0x01)) // Read address
		return 0;
   cResult = Read_Byte(1);

   StopCondition();
   return cResult;
}
//--------------------------------------------------
// Write Byte to Device 
//--------------------------------------------------

#ifdef PAL
#define STEPCODE	0xC8	//Step:50KHz
#endif
#ifdef NTSC
#define STEPCODE	0xCE	//Step:62.5KHz
#endif

uCHAR TunerWriteByte1(uCHAR cDevAddr, uCHAR cDecH, uCHAR cDecL,uCHAR bData, uCHAR cData)
{
    cDevAddr &= 0xFE;   // Force WRITE address
    
    // start condition 
    StartCondition();

    // send device ID and write data 
   if(!Send_Byte(cDevAddr))
   {
		if(!Send_Byte(cDecH))
		{
			if(!Send_Byte(cDecL))
        	{
				if(!Send_Byte(bData))
				{
           		if(!Send_Byte(cData))
            		{
               		StopCondition();
                		return(0);      // success 
					}
            	}
        	}
		}
    }
    StopCondition();
    return(1);                  // fail 
}

uCHAR TunerWriteByte(uCHAR cDevAddr, uCHAR cDecH, uCHAR cDecL, uCHAR cData)
{
    cDevAddr &= 0xFE;   // Force WRITE address
    
    // start condition 
    StartCondition();

    // send device ID and write data 
   if(!Send_Byte(cDevAddr))
   {
		if(!Send_Byte(cDecH))
		{
			if(!Send_Byte(cDecL))
        	{
				if(!Send_Byte(STEPCODE))
				{
           		if(!Send_Byte(cData))
            		{
               		StopCondition();
                		return(0);      // success 
					}
            	}
        	}
		}
    }
    StopCondition();
    return(1);                  // fail 
}
#endif


⌨️ 快捷键说明

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