1、I2C 通信失败,出现 log: “timeout, ipd: 0x00, state: 1”
请检查硬件上拉是否给电。
2、调用 i2c_transfer 返回值为 -6?
返回值为 -6 表示为 NACK 错误,即对方设备无应答响应,这种情况一般为外设的问题,常见的有以下几种情况:
3、当外设对于读时序要求中间是 stop 信号不是 repeat start 信号的时候,该如何处理?
这时需要调用两次 i2c_transfer, I2C read 拆分成两次,修改如下:
static int i2c_read_bytes(struct i2c_client *client, u8 cmd, u8 *data, u8 data_len) {struct i2c_msg msgs[2];
int ret;
u8 *buffer;
buffer = kzalloc(data_len, GFP_KERNEL);
if (!buffer)
return -ENOMEM;
msgs[0].addr = client- >addr;
msgs[0].flags = client- >flags;
msgs[0].len = 1;
msgs[0].buf = &cmd;
ret = i2c_transfer(client- >adapter, msgs, 1);
if (ret < 0) {
dev_err(&client- >adapter- >dev, "i2c read failedn");
kfree(buffer);
return ret;
}
msgs[1].addr = client- >addr;
msgs[1].flags = client- >flags | I2C_M_RD;
msgs[1].len = data_len;
msgs[1].buf = buffer;
ret = i2c_transfer(client- >adapter, &msgs[1], 1);
if (ret < 0)
dev_err(&client- >adapter- >dev, "i2c read failedn");
else
memcpy(data, buffer, data_len);
kfree(buffer);
return ret;
}
全部0条评论
快来发表一下你的评论吧 !