i2c驱动中的传输函数

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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值