/* ----------------------------------------------------
* the functional interface to the i2c busses.
* ----------------------------------------------------
*/
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)
/**
* i2c_transfer - execute a single or combined I2C message
* @adap: Handle to I2C bus
* @msgs: One or more messages to execute before STOP is issued to
* terminate the operation; each message begins with a START.
* @num: Number of messages to be executed.
*
* Returns negative errno, else the number of messages executed.
*
* Note that there is no requirement that each message be sent to
* the same slave address, although that is the most common model.
*/
//exzample
s32 gtp_i2c_read(struct i2c_client *client, u8 *buf, s32 len)
{
struct i2c_msg msgs[2];
s32 ret=-1;
s32 retries = 0;
GTP_DEBUG_FUNC();
msgs[0].flags=!I2C_M_RD;
msgs[0].addr=client->addr;
msgs[0].len=GTP_ADDR_LENGTH;//发送数据的长度(单位byte)
//寄存器地址在buf中,GTP_ADDR_LENGTH = 1,则buf[0]为地址。GTP_ADDR_LENGTH =2 ,则buf[1]、buf[0]为地址。
msgs[0].buf=&buf[0];
msgs[1].flags=I2C_M_RD;
msgs[1].addr=client->addr;
msgs[1].len=len-GTP_ADDR_LENGTH;
msgs[1].buf=&buf[GTP_ADDR_LENGTH];
while(retries<5)
{
ret=i2c_transfer(client->adapter,msgs, 2);
if (!(ret <= 0))break;
retries++;
}
return ret;
}
s32 gtp_i2c_write(struct i2c_client *client,u8 *data,s32 len)
{
struct i2c_msg msg;
s32 ret=-1;
s32 retries = 0;
GTP_DEBUG_FUNC();
msg.flags=!I2C_M_RD;
msg.addr=client->addr;
msg.len=len;
msg.buf=data; //其中包含地址与数据。
while(retries<5)
{
ret=i2c_transfer(client->adapter,&msg, 1);
if (ret == 1)break;
retries++;
}
return ret;
}
//end
int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
/**
* i2c_master_send - issue a single I2C message in master transmit mode
* @client: Handle to slave device
* @buf: Data that will be written to the slave
* @count: How many bytes to write, must be less than 64k since msg.len is u16
*
* Returns negative errno, or else the number of bytes written.
*/
int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
/**
* i2c_master_recv - issue a single I2C message in master receive mode
* @client: Handle to slave device
* @buf: Where to store data read from slave
* @count: How many bytes to read, must be less than 64k since msg.len is u16
*
* Returns negative errno, or else the number of bytes read.
*/
/*************************************************************************/
前面三个最重要的一点是可以处理i2c设备寄存器地址大于8位的情况,后面的函数寄存器(command)地址都是u8,对于大于8位的无法处理,就得用前面三个函数了,各函数的实例在后续我会慢慢补全的。
/*************************************************************************/
s32 i2c_smbus_read_byte(const struct i2c_client *client)
/**
* i2c_smbus_read_byte - SMBus "receive byte" protocol
* @client: Handle to slave device
*
* This executes the SMBus "receive byte" protocol, returning negative errno
* else the byte received from the device.
*/
s32 i2c_smbus_write_byte(const struct i2c_client *client, u8 value)
/**
* i2c_smbus_write_byte - SMBus "send byte" protocol
* @client: Handle to slave device
* @value: Byte to be sent
*
* This executes the SMBus "send byte" protocol, returning negative errno
* else zero on success.
*/
//往设备内部寄存器写数。
int result;
u8 reg = xxx; //寄存器地址
u8 value = xxx; //写往寄存器的值
// write the addr
result = i2c_smbus_write_byte( client_p, reg );
if ( result = < 0 )
{
TR0("FAILED: i2c_smbus_write_byte reg:0x%x, result:%d\n", reg, result);
return (result);
}
result = i2c_smbus_write_byte( client_p, value );
if ( result = < 0 )
{
TR0("FAILED: i2c_smbus_write_byte_data value:0x%x, result:%d\n", value, result);
return (result);
}
return 0;
//end
s32 i2c_smbus_read_byte_data(const struct i2c_client *client, u8 command)
/**
* i2c_smbus_read_byte_data - SMBus "read byte" protocol
* @client: Handle to slave device
* @command: Byte interpreted by slave
*
* This executes the SMBus "read byte" protocol, returning negative errno
* else a data byte received from the device.
*/
s32 i2c_smbus_write_byte_data(const struct i2c_client *client, u8 command, u8 value)
/**
* i2c_smbus_write_byte_data - SMBus "write byte" protocol
* @client: Handle to slave device
* @command: Byte interpreted by slave
* @value: Byte being written
*
* This executes the SMBus "write byte" protocol, returning negative errno
* else zero on success.
*/
s32 i2c_smbus_read_word_data(const struct i2c_client *client, u8 command)
/**
* i2c_smbus_read_word_data - SMBus "read word" protocol
* @client: Handle to slave device
* @command: Byte interpreted by slave
*
* This executes the SMBus "read word" protocol, returning negative errno
* else a 16-bit unsigned "word" received from the device.
*/
s32 i2c_smbus_write_word_data(const struct i2c_client *client, u8 command, u16 value)
/**
* i2c_smbus_write_word_data - SMBus "write word" protocol
* @client: Handle to slave device
* @command: Byte interpreted by slave
* @value: 16-bit "word" being written
*
* This executes the SMBus "write word" protocol, returning negative errno
* else zero on success.
*/
s32 i2c_smbus_read_block_data(const struct i2c_client *client, u8 command,u8 *values)
/**
* i2c_smbus_read_block_data - SMBus "block read" protocol
* @client: Handle to slave device
* @command: Byte interpreted by slave
* @values: Byte array into which data will be read; big enough to hold
* the data returned by the slave. SMBus allows at most 32 bytes.
*
* This executes the SMBus "block read" protocol, returning negative errno
* else the number of data bytes in the slave's response.
*
* Note that using this function requires that the client's adapter support
* the I2C_FUNC_SMBUS_READ_BLOCK_DATA functionality. Not all adapter drivers
* support this; its emulation through I2C messaging relies on a specific
* mechanism (I2C_M_RECV_LEN) which may not be implemented.
*/
s32 i2c_smbus_write_block_data(const struct i2c_client *client, u8 command,
u8 length, const u8 *values)
/**
* i2c_smbus_write_block_data - SMBus "block write" protocol
* @client: Handle to slave device
* @command: Byte interpreted by slave
* @length: Size of data block; SMBus allows at most 32 bytes
* @values: Byte array which will be written.
*
* This executes the SMBus "block write" protocol, returning negative errno
* else zero on success.
*/
s32 i2c_smbus_read_i2c_block_data(const struct i2c_client *client, u8 command,
u8 length, u8 *values)
s32 i2c_smbus_write_i2c_block_data(const struct i2c_client *client, u8 command,
u8 length, const u8 *values)
s32 i2c_smbus_xfer(struct i2c_adapter *adapter, u16 addr, unsigned short flags,
char read_write, u8 command, int protocol,
union i2c_smbus_data *data)
/**
* i2c_smbus_xfer - execute SMBus protocol operations
* @adapter: Handle to I2C bus
* @addr: Address of SMBus slave on that bus
* @flags: I2C_CLIENT_* flags (usually zero or I2C_CLIENT_PEC)
* @read_write: I2C_SMBUS_READ or I2C_SMBUS_WRITE
* @command: Byte interpreted by slave, for protocols which use such bytes
* @protocol: SMBus protocol operation to execute, such as I2C_SMBUS_PROC_CALL
* @data: Data to be read or written
*
* This executes an SMBus protocol operation, and returns a negative
* errno code else zero on success.
*/