1

所以我有 2 个 PCA9555(16 通道数字 I/O)芯片连接到通过 i2c 运行 Linux 的小型嵌入式设备。PCA9555 器件具有 7 位地址 0100000 和 0100001。

当我给电路板加电时,我运行:

# modprobe i2c-i801
# modprobe i2c-dev
# i2cdetect -l
i2c-0  smbus    SMBus I801 adapter at 0500    SMBus adapter
# i2cdump -y -r 0
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
... (Same for every line)
70: -- -- -- -- -- -- --
# dmesg|tail
i801_smbus 0000:00:1f.3 Transaction timeout
... (For every '--' in i2cdump, one of these errors outputted)
i801_smbus 0000:00:1f.3 Transaction timeout

于是我尝试编写一个简单的程序,将第一个 PCA9555 芯片上的所有引脚设置为输出高电平:

char *file;
unsigned char buf[3];
int addr, fd;

file = "/dev/i2c-0";
addr = 0x20; /* 0100000 */

if((fd = open(file, O_RDWR)) < 0){
  err(1, "open()");
}

if(ioctl(fd, I2C_SLAVE, addr) < 0){ /* also tried I2C_SLAVE_FORCE */
  err(1, "ioctl()");
}

/* Set all digital I/Os to output */
buf[0] = 6;
buf[1] = 0x00;
buf[2] = 0x00;

if(write(fd, buf, 3) != 3){
  err(1, "write() 1");
}

/* Set all outputs high */
buf[0] = 2;
buf[1] = 0xFF;
buf[2] = 0xFF;

if(write(fd, buf, 3) != 3){
  err(1, "write() 2");
}

return 0;

运行此返回:

# ./i2c
./i2c: write() 1: Operation not supported

所以我真的不明白我做错了什么。假设硬件连接正确,我从这里去哪里?

4

1 回答 1

2

i801 适配器仅支持 SMBUS 传输,Linux i2c 内核不支持对 SMBUS 适配器的任意写入调用。您将需要使用i2c_smbus_write_word_data. 内核文档没有对此进行详细介绍,但这就是它建议使用 SMBUS 命令的原因。

于 2011-03-04T16:48:51.290 回答