This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

ti的I2C编程 关于PCF8563



背景: 购买了开发板里面具有I2C驱动(/dev/i2c-1),但是它的驱动是不开源的。

网络上i2c的对这驱动文件的读写方式,有两种:一种是read/write函数;另外一种是ioctl函数控制。

下面使用参考网络上使用ioctl控制的,但是发现寄存器的值没有被写入,读取出来的值也不对。。

这是什么原因?? 或者ti有什么关于i2c的代码可以参考的。

#include <stdio.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/i2c-dev.h>

#include <linux/i2c.h>

#define CHIP "/dev/i2c-1"
#define CHIP_ADDR 0x51

int main()
{
int fd = open(CHIP, O_RDWR);
if (fd < 0) {
printf("open "CHIP"failed\n");
goto exit;
}
struct i2c_msg msg;
unsigned char rddata;
unsigned char rdaddr[] = {0xA3,0x05};
unsigned char wrbuf[] = {0xA2,0x05,0x02};
// unsigned char testaddr[] = {0x51,0xA2,0x06,0x02};

struct i2c_rdwr_ioctl_data ioctl_data;
struct i2c_msg msgs[2];

//写入数据
msgs[0].addr = CHIP_ADDR;
msgs[0].len = 3; //3
msgs[0].buf = wrbuf;

ioctl_data.nmsgs = 1;
ioctl_data.msgs = &msgs[0];
printf("ioctl write,return :%d\n", ioctl(fd, I2C_RDWR, &ioctl_data));


//开始读取数据
sleep(1);
msgs[0].addr = CHIP_ADDR;
msgs[0].len = 2; //2
msgs[0].buf = rdaddr;

msgs[1].addr = CHIP_ADDR;
msgs[1].flags |= I2C_M_RD;
msgs[1].len = 1;
msgs[1].buf = &rddata;
ioctl_data.nmsgs = 1;
ioctl_data.msgs = msgs;
printf("ioctl write address, return :%d\n", ioctl(fd, I2C_RDWR, &ioctl_data));
ioctl_data.msgs = &msgs[1];
printf("ioctl read, return :%d\n", ioctl(fd, I2C_RDWR, &ioctl_data));
printf("rddata: %x\r\n", rddata);
close:
close(fd);
exit:
return 0;
}