i2c驱动中的传输函数

编程入门 行业动态 更新时间:2024-10-07 22:29:24

i2c驱动中的传输<a href=https://www.elefans.com/category/jswz/34/1771370.html style=函数"/>

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

更多推荐

i2c驱动中的传输函数

本文发布于:2024-02-13 15:00:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1759206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   i2c

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!