所以我有 2 个 PCA9555(16 channel 数字 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
所以我真的不明白我做错了什么。假设硬件接线正确,我该从哪里开始?
最佳答案
i801 适配器仅支持 SMBUS 传输,Linux i2c 内核不支持对 SMBUS 适配器的任意写入调用。您需要使用 i2c_smbus_write_word_data
。 kernel documentation没有对此进行详细说明,但这就是它建议使用 SMBUS 命令的原因。
关于c - 写入()到 i2c 设备 : Operation not permitted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5195991/