/************************************************************************************
* 超越自我,创新未来,自强不息
* House Confidential Strictly Private
*
* Archive: I2C.C
* Revision: 1.0
* Author: GANDY HUANG
* Date: 2007/09/18 10:40:01
*
*************************************************************************************/
// ----------------------------------------------------------------------------------
// >>>>>>>>>>>>>>>>>>>>>>>> COPYRIGHT NOTICE <<<<<<<<<<<<<<<<<<<<<<<<<<
// ----------------------------------------------------------------------------------
// Copyright 2007(c) CCT Design House.
// This is an unpublished work.
// ----------------------------------------------------------------------------------
////////update version content here////////////////////////
///////////////////////////////////////////////////////
#if !defined(__I2C_C__)
#define __I2C_C__
//////////////////////////////////////////////////////////////////////
#include "system.h"
#ifdef I2C_DRIVE_MODULE
//#include "common\I2c.h" //INSIDE INCLUDE "system.h" !!!!!!
#define SET_SDA SDA=1
#define CLR_SDA SDA=0
#define SET_SCL SCL=1
#define CLR_SCL SCL=0
#if 0
#define SDIO_DIR_IN SET_SDA
#define SDIO_LOW CLR_SDA
#define SDIO_HIGH SET_SDA
#define READ_SDIO SDA
#define SCLK_LOW CLR_SCL
#define SCLK_HIGH SET_SCL
unsigned char fgOperationI2c(T_OPERA_MODE operation, unsigned char *data, unsigned char numBytes);
#endif
/////////////////////////////////////////////////////////////////////////////////
void vI2cTest(void)
{
if (unBoolState.stBoolState.bI2CIsBusy == 0)
unBoolState.stBoolState.bI2CIsBusy = 1;
else
return ;
vI2cStart( );
fgI2cSendData(0X55,NO_NEED_ACK);//0101 0101 //P1.6
vI2cStop( );
unBoolState.stBoolState.bI2CIsBusy = 0;
}
////////////////////////////////////////////////////////////////////////////////////
void vI2cInit(void)
{
SET_SCL; //
SET_SDA; //
}
////////////////////////////////////////////////////////////
void vI2cStart(void)
{
//UINT16 bBusDelayTemp = wWAIT_TIME;
SET_SDA; // make sure SDA released
vDelay5Us();
SET_SCL; // make sure SCL released
vDelay5Us();
CLR_SDA; // start condition here
vDelay5Us() ;
CLR_SCL; // ready for clocking
vDelay5Us() ;
}
/************************************************************************
Function : void vI2CStop(void)
Description : Stop Routine
timing : SCL ___|^^^^^
SDA xx___|^^^
(P)
|<--- stop condition
Parameter : NONE
Return : NONE
************************************************************************/
void vI2cStop(void)
{
CLR_SDA; // ready for stop condition
vDelay5Us() ;
SET_SCL; // ready for stop condition
vDelay5Us() ;
SET_SDA; // stop condition here
vDelay5Us() ;
}
/************************************************************************
Function : BOOL fgI2cSendData(BYTE bValue)
Description : Send Routine
timing : SCL ___|^|___|^|__~__|^|___|^|__
SDA __/D7 \_/D6 \_~_/D0 \_/ACK\_
Parameter : bValue(8-bit output data)
Return : TRUE : successful with ACK from slave
FALSE : bus (SCL = 0) or ACK failure
************************************************************************/
BOOL8 fgI2cSendData(UINT8 idata bValue, BOOL8 idata fgSendAck)
{
UINT8 bBitMask = 0x80;
// step 1 : 8-bit data transmission
while(bBitMask)
{
if(bBitMask & bValue)
{
SET_SDA;
}
else
{
CLR_SDA;
}
vDelay5Us() ;
SET_SCL; // data clock in
vDelay5Us() ;
CLR_SCL; // ready for next clock in
vDelay5Us() ;
bBitMask = bBitMask >> 1; // MSB first & timing delay
}
// step 2 : slave acknowledge check
SET_SDA; // release SDA for ACK polling
vDelay5Us() ;
SET_SCL; // start ACK polling
bBitMask = (UINT8) wWAIT_TIME; // time out protection
vDelay5Us() ;
if (fgSendAck)
{
while(SDA && bBitMask--)
{
; // wait for ACK, SDA=0 or bitMask=0->jump to this loop
}
CLR_SCL; // end ACK polling
vDelay5Us() ;
if(bBitMask)
return(TRUE); // return TRUE if ACK detected
else
return(FALSE); // return FALSE if time out
}
else
{
CLR_SCL; // end ACK polling
vDelay5Us() ;
return(TRUE);
}
}
/************************************************************************
Function : BOOL fgI2cSendDataMore(BYTE bDevice, BYTE bData_Addr,
BYTE bDataCount, BYTE *prData)
Description : ByteWrite Routine
Parameter :
bData_Addr -> ic eeprom Data Address
bDataCount -> Data Content Cont
*prData -> SEND Data Content Pointer
Return : TRUE : successful with ACK from slave
FALSE : bus (SCL = 0) or ACK failure
************************************************************************/
BOOL8 fgI2cSendDataMore( UINT8 *idata prData, UINT8 idata bDataCount, BOOL8 idata fgSendAck )
{
while(bDataCount)
{
if(!fgI2cSendData(* prData++,fgSendAck)) // Data Content Write ///?????????!!!!!!!!!!!!!!!***********
{
return(FALSE); // Device Address exceeds the range
}
bDataCount--;
}
return(TRUE);
}
//////////////////////////////////
void vI2cReadData(UINT8 *idata prValue, BOOL8 idata fgReadAck)
{
UINT8 bBitMask = 0x80;
*prValue = 0; // reset data buffer
SET_SDA; // make sure SDA released
vDelay5Us();
// step 1 : 8-bit data reception
while(bBitMask)
{
// CLR_SCL; // lee add 2007.12.22??????
// vDelay5Us();
SET_SCL; // data clock out
vDelay5Us();
if(SDA)
{
*prValue = *prValue | bBitMask; // Get all data
} // non-zero bits to buffer
CLR_SCL; // ready for next clock out
vDelay5Us();
bBitMask = bBitMask >> 1; // shift bit mask & clock delay
}
// step 2 : acknowledgement to slave
if(fgReadAck)
{
CLR_SDA; // ACK here for Sequential Read
}
else
{
SET_SDA; // NACK here (for single byte read)
}
vDelay5Us();
SET_SCL; // NACK clock out
vDelay5Us();
CLR_SCL; // ready for next clock out
vDelay5Us();
SET_SDA; // release SDA
vDelay5Us();
}
/************************************************************************
Function : BOOL vI2cReadDataMore(BYTE bDevice, BYTE bData_Addr,
BYTE bDataCount, BYTE *prData)
Description : DataRead Routine
Parameter : bDevice -> Device Address
bData_Addr -> Data Address
bDataCount -> Data Content Cont
*prData -> Data Content Pointer
Return : TRUE : successful with ACK from slave
FALSE : bus (SCL = 0) or ACK failure
******