i2c驱动中的传输函数

来源:互联网 发布:骚男的辣条淘宝店网址 编辑:程序博客网 时间:2024/04/30 20:45

I2C设备驱动编写中要用到信息传输的函数,有很多种,以下以写消息为例。

一、i2c_smbus_write_byte_data

       这是在内核中拷贝的该函数的实现代码,value为字节数据,将value写如client代表的设备。函数中的i2c_smbus_xfer最终调用了smbus_xfer函数进行消息的写入。每次传送均为一个字节。

/**

 * 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_write_byte_data(struct i2c_client *client, u8 command, u8 value)
{
union i2c_smbus_data data;
data.byte = value;
return
i2c_smbus_xfer(client->adapter,client->addr,client->flags,
                     I2C_SMBUS_WRITE,command,
                     I2C_SMBUS_BYTE_DATA,&data)
;

}

二、i2c_master_send

     该函数每次传送一个消息,消息长度是count个字节,将buf开始的count个字节写入client代表的设备。该函数最终调用i2c_transfer传输一条消息。

/**
 * 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
 *
 * Returns negative errno, or else the number of bytes written.
 */
int i2c_master_send(struct i2c_client *client,const char *buf ,int count)
{
int ret;
struct i2c_adapter *adap=client->adapter;
struct i2c_msg msg;


msg.addr = client->addr;
msg.flags = client->flags & I2C_M_TEN;
msg.len = count;                  //消息的长度
msg.buf = (char *)buf;         //消息的内容


ret =
i2c_transfer(adap, &msg, 1);   //参数1代表一条消息


/* If everything went ok (i.e. 1 msg transmitted), return #bytes
  transmitted, else error code. */
return (ret == 1) ? count : ret;
}

原创粉丝点击