全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > 电子/半导体 > 接口/总线/驱动 >

I2C数据传输的三个主要API

时间:2023-07-22 15:04

人气:

作者:admin

标签: API  数据传输 

导读:I2C Data Transfer I2C 数据传输主要有三个 API int i2c_master_send(const struct i2c_client *client,const char *buf,int count) client:I2C 设备对应的 i2c_client。 buf:要发送的数据。 count:要发送的数据字节数,要...

I2C Data Transfer

I2C 数据传输主要有三个 API

int i2c_master_send(const struct i2c_client *client,const char *buf,int count)

client:I2C 设备对应的 i2c_client。
buf:要发送的数据。
count:要发送的数据字节数,要小于 64KB,以为 i2c_msg 的 len 成员变量是一个 u16(无符号 16 位)类型的数据。
返回值:负值,失败,其他非负值,发送的字节数。

int i2c_master_recv(const struct i2c_client *client,char *buf,int count)
client:I2C 设备对应的 i2c_client。
buf:要接收的数据。
count:要接收的数据字节数,要小于 64KB,以为 i2c_msg 的 len 成员变量是一个 u16(无符号 16 位)类型的数据。
返回值:负值,失败,其他非负值,发送的字节数。

int i2c_transfer(struct i2c_adapter *adap,struct i2c_msg *msgs,int num)

adap:所使用的 I2C 适配器,i2c_client 会保存其对应的 i2c_adapter。
msgs:I2C 要发送的一个或多个消息。
num:消息数量,也就是 msgs 的数量。
返回值:负值,失败,其他非负值,发送的 msgs 数量。

i2c_master_send 和 i2c_master_recv 都是对 i2c_transfer 的封装。因此我们重点研究 i2c_transfer。

图片

其中,adap->algo->master_xfer 由芯片原厂提供。在 MTK 平台,是 mtk_i2c_transfer 函数,不同平台命名不同。

static int mtk_i2c_transfer(struct i2c_adapter *adap,struct i2c_msg msgs[], int num)
{
 int ret;
 int left_num = num;
 struct mtk_i2c *i2c = i2c_get_adapdata(adap);

  //打开时钟
 ret = mtk_i2c_clock_enable(i2c);
 if (ret)
  return ret;

  //初始化硬件
 mtk_i2c_init_hw(i2c);

 i2c- >auto_restart = i2c- >dev_comp- >auto_restart;

 if (i2c- >auto_restart && num == 2) {
  if (!(msgs[0].flags & I2C_M_RD) && (msgs[1].flags & I2C_M_RD) &&
      msgs[0].addr == msgs[1].addr) {
   i2c- >auto_restart = 0;
  }
 }

 if (i2c- >auto_restart && num >= 2 && i2c- >speed_hz > MAX_FS_MODE_SPEED)
  i2c- >ignore_restart_irq = true;
 else
  i2c- >ignore_restart_irq = false;

 while (left_num--) {
  if (!msgs- >buf) {
   dev_dbg(i2c- >dev, "data buffer is NULL.n");
   ret = -EINVAL;
   goto err_exit;
  }

  if (msgs- >flags & I2C_M_RD)
   i2c- >op = I2C_MASTER_RD;
  else
   i2c- >op = I2C_MASTER_WR;

  if (!i2c- >auto_restart) {
   if (num > 1) {
    /* combined two messages into one transaction */
    i2c- >op = I2C_MASTER_WRRD;
    left_num--;
   }
  }

  /* always use DMA mode. */
  ret = mtk_i2c_do_transfer(i2c, msgs, num, left_num);
  if (ret < 0)
   goto err_exit;

  msgs++;
 }
 /* the return value is number of executed messages */
 ret = num;

err_exit:
 mtk_i2c_clock_disable(i2c);
 return ret;
}
温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信