i2c.h
来自「是一个手机功能的模拟程序」· C头文件 代码 · 共 235 行
H
235 行
//===========================================================================
// I2C.H
//---------------------------------------------------------------------------
// Copyright (c) 2002 Epson Research and Development, Inc.
// All Rights Reserved.
//===========================================================================
// (Tabs set to every 4)
#ifndef __I2C_H__
#define __I2C_H__
#include "datatype.h"
#define I2C_ERR_NONE 0 // No error
#define I2C_ERR_STUCK_BIT 1 // SCL/SDA line stuck low too long
#define I2C_ERR_SLAVE_NACK_ID 2 // Slave NACK'd slave device ID (no such device)
#define I2C_ERR_SLAVE_NACK_DATA 3 // Slave NACK'd a sent data byte (error)
#define I2C_ERR_INVALID_ARGUMENT 4 // Call to i2cInitialize used incorrect arguments
#define I2C_ERR_UNKNOWN_ERROR 5 // Unknown error - MUST BE LAST!!!
#ifdef __cplusplus
extern "C" {
#endif
//---------------------------------------------------------------------------
// FUNCTION: i2cInitialize()
//
// DESCRIPTION:
// This routine initializes the I2C bus operating environment.
// Repeated Start condition is set to TRUE.
// Auto Increment mode is set to TRUE.
// This function now calls the 2 subsequent functions: i2cInitializeLib()
// and i2cInitializeHW() in order to have separate control and yet
// maintain backwards compatibility.
//
// PARAMETERS:
// SizeOf all chip register access (8,16,32).
// Registers and bitmasks to access the I2C bus via GPIO pins.
//
// RETURNS:
// True if successful, False if I2C errors.
//---------------------------------------------------------------------------
Boolean i2cInitialize( int RegSize, UInt32 RegDir, UInt32 RegData, UInt32 SCLDir, UInt32 SCLData, UInt32 SDADir, UInt32 SDAData );
//---------------------------------------------------------------------------
// FUNCTION: i2cInitializeLib()
//
// DESCRIPTION:
// This routine initializes the I2C library.
// Repeated Start condition is set to TRUE.
// Auto Increment mode is set to TRUE.
//
// PARAMETERS:
// SizeOf all chip register access (8,16,32).
// Registers and bitmasks to access the I2C bus via GPIO pins.
//
// RETURNS:
// True if successful, False if I2C errors.
//---------------------------------------------------------------------------
Boolean i2cInitializeLib( void );
//---------------------------------------------------------------------------
// FUNCTION: i2cInitializeHW()
//
// DESCRIPTION:
// This routine initializes the I2C hardware and assumes that the library
// has already been initialized.
//
// PARAMETERS:
// n/a
//
// RETURNS:
// True if successful, False if I2C errors.
//---------------------------------------------------------------------------
Boolean i2cInitializeHW( void );
//---------------------------------------------------------------------------
// FUNCTION: i2cSetSlaveDevice()
//
// DESCRIPTION:
// This function establishes the Save Device ID for I2C functions.
//
// PARAMETERS:
// Slave Device ID.
//
// RETURNS:
// Previous Slave Device ID, zero if nothing previously specified.
//---------------------------------------------------------------------------
UInt8 i2cSetSlaveDevice( UInt8 SlaveDevice );
//---------------------------------------------------------------------------
// FUNCTION: i2cGetLastError()
//
// DESCRIPTION:
// Returns the last error code generated by any FALSE return result.
//
// PARAMETERS:
// n/a
//
// RETURNS:
// Last error code, zero if no previous error.
//---------------------------------------------------------------------------
int i2cGetLastError( void );
//---------------------------------------------------------------------------
// FUNCTION: i2cGetLastErrorText()
//
// DESCRIPTION:
// Returns the last error text generated by any FALSE return result.
//
// PARAMETERS:
// n/a
//
// RETURNS:
// Last error text, or the text "No error" if no previous error.
//---------------------------------------------------------------------------
const char * i2cGetLastErrorText( void );
//---------------------------------------------------------------------------
// FUNCTION: i2cSetAutoIncrement()
//
// DESCRIPTION:
// Enables or disables Auto Increment mode for sub-addressing writing.
// This function modifies the behavior of function i2cWriteBytes(). When
// the Auto Increment flag is set, the i2cWriteBytes() function writes
// the data to the slave device as a contiguous sequence of bytes. When
// the Auto Increment flag is clear, the i2cWriteBytes() function reads
// the first data byte (as the starting sub-address), and then it calls
// i2cWrite2Bytes() for each subsequent data byte, auto-incrementing the
// the sub-address for each data byte. It is the caller's responsibility
// to set this flag for each slave device, when using i2cWriteBytes().
//
// PARAMETERS:
// Flag TRUE or FALSE to enable/disable Auto Increment mode.
//
// RETURNS:
// State of the Auto Increment flag BEFORE being called (last state).
//---------------------------------------------------------------------------
Boolean i2cSetAutoIncrement( Boolean fAutoIncrement );
//---------------------------------------------------------------------------
// FUNCTION: i2cSetRepeatedStart()
//
// DESCRIPTION:
// Enables or disables Repeated Start condition for sub-addressing reading.
// This function modifies the behavior of function i2cReadBytes(). When
// the Repeated Start flag is set, I2C data is read like this:
// I2C_START, SLAVE_ADDRESS_WRITE, SUB_ADDRESS,
// REPEATED_I2C_START, SLAVE_ADDRESS_READ, (read data...), I2C_STOP
// When the Repeated Start flag is clear, I2C data is read like this:
// I2C_START, SLAVE_ADDRESS_WRITE, SUB_ADDRESS, I2C_STOP
// I2C_START, SLAVE_ADDRESS_READ, (read data...), I2C_STOP
// It is the caller's responsibility to set this flag for each slave device.
//
// PARAMETERS:
// Flag TRUE or FALSE to enable/disable Repeated Start condition.
//
// RETURNS:
// State of the Repeated Start flag BEFORE being called (last state).
//---------------------------------------------------------------------------
Boolean i2cSetRepeatedStart( Boolean fRepeatedStart );
//---------------------------------------------------------------------------
// FUNCTION: i2cWriteBytes()
//
// DESCRIPTION:
// This function writes BYTE data to the I2C bus.
// If the Auto Increment flag is clear, this function assumes the first
// byte of data is the starting sub-address. If the Auto Increment flag
// is set, this function makes no assumptions on the data stream.
//
// PARAMETERS:
// Number of bytes and pointer to byte data array.
//
// RETURNS:
// TRUE if successful, FALSE if error occurred.
//---------------------------------------------------------------------------
Boolean i2cWriteBytes( const UInt8 *pBytes, int nBytes );
//---------------------------------------------------------------------------
// FUNCTION: i2cWrite2Bytes()
//
// DESCRIPTION:
// This function writes 2 BYTEs of data to the I2C bus.
//
// PARAMETERS:
// Data byte 1 (typically sub-address), data byte 2 (typically data).
//
// RETURNS:
// TRUE if successful, FALSE if error occurred.
//---------------------------------------------------------------------------
Boolean i2cWrite2Bytes( UInt8 Byte1, UInt8 Byte2 );
//---------------------------------------------------------------------------
// FUNCTION: i2cReadBytes()
//
// DESCRIPTION:
// This function reads BYTE data from the I2C bus.
//
// PARAMETERS:
// Number of bytes and pointer to byte data array.
// Optional (if non-NULL) starting subaddress.
//
// RETURNS:
// TRUE if successful, FALSE if error occurred.
//---------------------------------------------------------------------------
Boolean i2cReadBytes( const UInt8 *pSubAddress, UInt8 *pBytes, int nBytes );
#ifdef __cplusplus
}
#endif
#endif //__I2C_H__
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?