📄 twowire.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 "TW803Reg.h"
#include "TwoWire.h"
#include "SRC_ctrl.h"
#include "tuner.h"
//----------------------------------------
// S_Condition for TWD protocol
//----------------------------------------
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);
}
//--------------------------------------------------
// 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;
}
//--------------------------------------------------
// 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);
}
//--------------------------------------------------
// 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(TW803_P0))
{
if(!Send_Byte(cReg))
{
return(0); //
}
}
return(1); //
}
void twdWr_Burst_D(uCHAR cData)
{
uCHAR ix;
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 */
Set_SCL2Low; /* Finish Acknoledge */
}
void twdWr_Burst_P(void)
{
StopCondition();
}
//twdWriteTable() is used for burst write for
//any I2C standar device
//--------------------------------------------------
// Write Multiple Bytes to Device
//--------------------------------------------------
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 */
}
//--------------------------------------------------
// 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--);
}
}
// OSD config register Write
void OSDCfgWr(uCHAR index,uCHAR dat)
{
I2CWriteByte(TW803_P0,OSD_CFG_INDEX,index);
I2CWriteByte(TW803_P0,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))
{
//ruby 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 + -