gpt4 book ai didi

c - Raspberry pi 上的 I2C_SLAVE 和 I2C_RDWR 返回值错误

转载 作者:行者123 更新时间:2023-11-30 16:11:01 24 4
gpt4 key购买 nike

我正在使用 Raspberry pi 与芯片进行 I2C 通信。

I2C 在 i2c-1 上运行,我想要通信的芯片是 mux0,其地址已指定。我使用了错误的地址来查看从 I2C_SLAVE 和 I2C_RDWR 获得的值。

我想检查我是否正在打开正确的巴士。所以我使用 if 语句,它对我来说工作得很好。`

int file;
file = open(i2c_adapt, O_RDWR);
if (file == -1)
{
perror("Check I2C Bus");
exit(1);
}

`

然后我看到有两个函数可以用来定义芯片地址。 I2C_SLAVE 和 I2C_RDWR。

我想检查芯片地址是否正确,但它们都返回相同的值。

我打印了他们的返回值来检查。

`

printf("\nThe file value when Mux0 is %d", open("/dev/i2c-1", O_RDWR));
printf("\nThe file value of random bus is %d\n", open("/dev/i2c-0", O_RDWR));
printf("\nThe i2c_Slave value when Mux0 is %d", ioctl(file, I2C_SLAVE, mux0));
printf("\nThe i2c_Slave value of random address is %d\n\n", ioctl(file, I2C_SLAVE, 0x60));
printf("\nThe I2C_RDWR value when Mux0 is %d", ioctl(file, I2C_RDWR, mux0));
printf("\nThe i2c_RDWR value of random address is %d\n\n", ioctl(file, I2C_RDWR, 0x60));


当我运行这些命令时,我得到以下值:

Mux0为4时的文件值随机总线文件值为-1

所以我可以使用 if 语句来查看我是否使用了错误的总线

Mux0为0时的i2c_Slave值i2c_Slave值随机地址为0

Mux0为-1时的i2c_RDRW值随机地址的i2c_RDRW值为-1 enter image description here

有人可以告诉我为什么错误的芯片也能得到相同的值吗?

最佳答案

I2C_SLAVE ioctl 不会检查从地址是否存在。它为打开的文件设置默认的 I2C 从机地址,即设置 read()write() 使用的 I2C 从机地址。如果具有指定从属地址的设备绑定(bind)到驱动程序,ioctl 将返回 -1 并将 errno 设置为 EBUSY

您使用的I2C_RDWR ioctl 的第三个参数完全错误。查看文档:I2C Device Interface 。第三个参数指向 struct i2c_rdwr_ioctl_data ,其中包含指向 struct i2c_msg 数组的指针和数组的长度。每个struct i2c_msg 包含一个从地址、一些标志、一个缓冲区长度和一个缓冲区指针。您可以在 i2ctransfer.c 中查看如何使用它的示例。来自i2c-tools git 存储库。

关于c - Raspberry pi 上的 I2C_SLAVE 和 I2C_RDWR 返回值错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58748070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com