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

📄 i2c.c

📁 s1d13716的源码 windiws ce 或8位单片机
💻 C
📖 第 1 页 / 共 2 页
字号:
//===========================================================================
//	I2C.C
//
//	This file implements the I2C support for the S1D13713.
//---------------------------------------------------------------------------
//  Copyright (c) 2002 Epson Research and Development, Inc.
//  All Rights Reserved.
//===========================================================================
// (Tabs set to every 4)

//#ifdef _WIN32
//#pragma warning( disable : 4706 )	// Disable "assignment within conditional expression" warning.
//#endif

#include "halapi.h"
#include "hal_regs.h"
#include "i2c.h"

static UInt8	gSlaveDevice;					// I2C Slave Device ID in use

static int		gLastError = I2C_ERR_NONE;		// I2C last error code
static Boolean	gfRepeatedStart = TRUE;			// I2C Repeated Start condition flag for reads
static Boolean	gfAutoIncrement = TRUE;			// I2C Auto Increment mode flag for writes

static Boolean	PrvStartI2C( UInt8 Data );
static Boolean	PrvStopI2C( void );
static Boolean	PrvWriteI2CByte( UInt8 Data, Boolean LastByte );
static Boolean	PrvReadI2CByte( UInt8 *pData, Boolean ACK );


//---------------------------------------------------------------------------
//  FUNCTION:	i2cInitialize()
//
//  DESCRIPTION:
//		Initializes the I2C operating environment.
//
//  PARAMETERS:
//		For backwards and forward compatibility this function still uses
//		the argument list from the GPIO based I2C initialization routine.
//		For use in the 13713 project the caller should set each of these
//		parameter to 0.
//
//  RETURNS:
//		TRUE	- If the initialization is successuful
//		FALSE	- if there is an error during the intialization.
//				- Call 12cGetLastError() or 12cGetLastErrorText() for
//				  more information on the error.
//
//	NOTES:
//		For backwards and forward compatibility this function still uses
//		the argument list from the GPIO based I2C initialization routine
//		and returns a value.
//---------------------------------------------------------------------------
Boolean i2cInitialize( int RegSize, UInt32 RegDir, UInt32 RegData, UInt32 SCLDir, UInt32 SCLData, UInt32 SDADir, UInt32 SDAData )
{
	if (TRUE == i2cInitializeLib())
		if (TRUE == i2cInitializeHW())
			return TRUE;

	return FALSE;

	// Quiet the compiler
	RegSize;	RegDir;		RegData;	SCLDir;		SCLData;	SDADir;	SDAData;
}

//---------------------------------------------------------------------------
//  FUNCTION:	i2cInitializeLib()
//
//  DESCRIPTION:
//		Initialize I2C library only
//
//	RETURNS:
//		TRUE	- For backwards and forward compatibility this function
//				  still returns a value.
//---------------------------------------------------------------------------
Boolean i2cInitializeLib( void )
{
	gLastError = I2C_ERR_NONE;

	gfRepeatedStart = TRUE;
	gfAutoIncrement = TRUE;

	return TRUE;
}

//---------------------------------------------------------------------------
//  FUNCTION:	i2cInitializeHW()
//
//  DESCRIPTION:
//		Performs any HW intialization that needs to be done for I2C.
//
//	RETURNS:
//		TRUE
//
//	NOTES:
//		Enabling the I2C module will enable clocking to the I2C section
//		which will inrease the power used by the system.
//
//		For backwards and forward compatibility this function still uses
//		the argument list from the GPIO based I2C initialization routine.
//		For use in the 13713 project the caller should set each of these
//		parameter to 0.
//---------------------------------------------------------------------------
Boolean i2cInitializeHW( void )
{
	// Clear any error status that may be set and enable the I2C section.
	halWriteReg8(REG0080_I2CSTATUS, 0xC0);

	// Delay the data read time 
//	halWriteReg8(REG008A_I2CREADACK, 4);

	return TRUE;
}


//---------------------------------------------------------------------------
//  FUNCTION: 	i2cSetSlaveDevice()
//
//  DESCRIPTION:
//		Set Slave device ID which will be used for all subsequent
//		I2C operations.
//
//  PARAMETERS:
//		SlaveDevice		- Device ID for the new slave device to communicate
//						  with
//
//  RETURNS: 
//		The device ID of the previous slave device
//---------------------------------------------------------------------------
UInt8 i2cSetSlaveDevice( UInt8 SlaveDevice )
{
	UInt8 LastID = gSlaveDevice;
	gSlaveDevice = SlaveDevice;

	gLastError = I2C_ERR_NONE;

	return LastID;
}


//---------------------------------------------------------------------------
//  FUNCTION: 	i2cGetLastError()
//
//  DESCRIPTION:
//		Returns the numerice value of the last I2C error.
//
//  RETURNS: 
//		Integer value of the last I2C error.
//---------------------------------------------------------------------------
int i2cGetLastError( void )
{
	return gLastError;
}


//---------------------------------------------------------------------------
//  FUNCTION:	i2cGetLastErrorText()
//
//  DESCRIPTION:
//		Returns a pointer to the text representation of the last I2C error.
//
//  RETURNS: 
//		Pointer to the first character of the ASCIIZ string describing
//		the last I2C error.
//
//	NOTE:
//		It is imperative to maintain this list in conjuction with the list
//		of error values in i2c.h.
//---------------------------------------------------------------------------
const char * i2cGetLastErrorText( void )
{
	static const char * const apszErrors[] =
	{
		"No error",					// I2C_ERR_NONE
		"SCL/SDA line stuck low",	// I2C_ERR_STUCK_BIT
		"NAK device ID",			// I2C_ERR_SLAVE_NACK_ID
		"NAK device data",			// I2C_ERR_SLAVE_NACK_DATA
		"Invalid argument",			// I2C_ERR_INVALID_ARGUMENT
		"Unknown error"				// I2C_ERR_UNKNOWN_ERROR - MUST BE LAST!!!
	};

	if ((gLastError < 0) || (gLastError > I2C_ERR_UNKNOWN_ERROR))
		return apszErrors[I2C_ERR_UNKNOWN_ERROR];

	return apszErrors[gLastError];
}


//---------------------------------------------------------------------------
//  FUNCTION:	i2cSetAutoIncrement()
//
//  DESCRIPTION:
//		Set I2C Auto Increment mode for writes.
//
//  PARAMETERS:
//		fAutoIncrement	- flag containing the new auto-increment value
//						  fAutoIncrement == TRUE,  enables auto-incrment
//						  fAutoIncrement == FALSE, disables auto-incrment
//  RETURNS: 
//		Previous auto-increment state.
//---------------------------------------------------------------------------
Boolean i2cSetAutoIncrement( Boolean fAutoIncrement )
{
	Boolean fLastAI = gfAutoIncrement;
	gfAutoIncrement = fAutoIncrement;

	gLastError = I2C_ERR_NONE;

	return fLastAI;
}


//---------------------------------------------------------------------------
//  FUNCTION:	i2cSetRepeatedStart()
//
//  DESCRIPTION:
//		Set I2C Repeated Start condition flag for reads.
//
//  PARAMETERS:
//		fRepeatedStart	- This flag contols the 
//
//  RETURNS: 
//		Previous state of the repeated start flag
//---------------------------------------------------------------------------
Boolean i2cSetRepeatedStart( Boolean fRepeatedStart )
{
	Boolean fLastRS = gfRepeatedStart;
	gfRepeatedStart = fRepeatedStart;

	gLastError = I2C_ERR_NONE;

	return fLastRS;
}


//---------------------------------------------------------------------------
//  FUNCTION:	i2cWriteBytes()
//
//  DESCRIPTION:
//		Write byte data to the I2C bus.
//
//  PARAMETERS:
//		pBytes	- Pointer to an array of 8-bit values to sent to the I2C bus
//		nBytes	- Number of bytes to send to the I2C bus
//
//  RETURNS: 
//		TRUE	- if the function is successful in writing the byte strings
//				  to the I2C bus.
//		FALSE	- if there is an error during the transfer.
//				- Call 12cGetLastError() or 12cGetLastErrorText() for
//				  more information on the error.
//---------------------------------------------------------------------------
Boolean i2cWriteBytes( const UInt8 *pBytes, int nBytes )
{
	int i;
	Boolean ret = TRUE;

	gLastError = I2C_ERR_NONE;

	if (!gfAutoIncrement && (nBytes > 1))		// Auto Increment clear (and at least 2 data bytes)
	{
		UInt8 SubAddress = *pBytes++;
		for (i = 1; ret && i < nBytes; i++)

⌨️ 快捷键说明

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