Joh*_*rye 3 c linux embedded qemu i2c
我试图在写入后从 I2C 总线上的内存位置读取一个值。当我在终端中运行它时,我得到了奇怪的输出。
这是我的程序
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#define I2C_ADAPTER "/dev/i2c-0"
#define I2C_DEVICE 0x00
int main (int argc, char *argv[])
{
int file;
int addr = 0X00; /* XGPIOPS_DATA_LOW_OFFSET */
if((file = open(I2C_ADAPTER, O_RDWR)) < 0) {
printf("Failed to open the bus");
return -1;
}
if(ioctl(file, I2C_SLAVE, addr) < 0) {
printf("Unable to open device as slave %s", strerror(errno));
return -1;
}
char buf[10];
buf[0] = addr;
buf[1] = 0x10;
if(write(file, buf, 2) != 2) {
printf("Failed to write to bus %s.\n\n", strerror(errno));
}
else {
printf("Successful write\n");
printf(buf);
printf("\n\n");
}
if(read(file, buf, 2) != 2) {
printf("Failed to read from the i2c bus. %s\n\n", strerror(errno));
}
else {
printf("Successful read\n");
printf(buf);
printf("\n\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
程序的输出如下所示
写入成功
阅读成功??
在我的终端上,这些块看起来更像是钻石内部的问号。我不确定在 ASCII 中对应的是什么。
为什么我不回读那个 0x10,它是我最初写入的地址字节之后的第二个字节?
基于第一组答案,这里是更新的代码:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#define I2C_ADAPTER "/dev/i2c-0"
#define I2C_DEVICE 0x00
int main (int argc, char *argv[])
{
int file;
long addr, reg_addr;
char *end;
if(argc == 3) {
addr = strtol(argv[1], &end, 16);
printf("Value of addr is: %ld\n", addr);
reg_addr = strtol(argv[2], &end, 16);
printf("Value of reg_addr is: %ld\n", reg_addr);
}
else {
printf("arg failed\n\n.");
addr = 0x00;
}
if((file = open(I2C_ADAPTER, O_RDWR)) < 0) {
printf("Failed to open the bus\n");
return -1;
}
if(ioctl(file, I2C_SLAVE, addr) < 0) {
printf("Unable to open device as slave \n%s\n", strerror(errno));
return -1;
}
char buf[10];
buf[0] = addr;
buf[1] = reg_addr;
buf[2] = 0x10;
if(write(file, buf, 3) != 3) {
printf("Failed to write to bus %s.\n\n", strerror(errno));
}
else {
printf("Successful write\n");
printf(buf);
printf("\n\n");
}
if(read(file, buf, 3) != 3) {
printf("Failed to read from the i2c bus.\n %s\n\n", strerror(errno));
}
else {
printf("Successful read\n");
printf("Buf = [%02X,%02X,%02X]\n", buf[0], buf[1], buf[2]);
printf("\n\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此时,每当我使用 0x00 作为 addr 时,无论 argv[2] 是什么,我都会得到 FF, FF, FF 作为输出。这是设备树文件的适用部分。请注意,这是模拟的,所以我无法探测物理设备。
&i2c0 {
status = "okay";
clock-frequency = <400000>;
pinctrl-names = "default";
i2cswitch@74 {
compatible = "nxp,pca9548";
#address-cells = <1>;
#size-cells = <0>;
reg = <0x74>;
i2c@0 {
#address-cells = <1>;
#size-cells = <0>;
reg = <0>;
si570: clock-generator@5d {
#clock-cells = <0>;
compatible = "silabs,si570";
temperature-stability = <50>;
reg = <0x5d>;
factory-fout = <156250000>;
clock-frequency = <148500000>;
};
};
i2c@2 {
#address-cells = <1>;
#size-cells = <0>;
reg = <2>;
eeprom@54 {
compatible = "at,24c08";
reg = <0x54>;
};
};
i2c@3 {
#address-cells = <1>;
#size-cells = <0>;
reg = <3>;
gpio@21 {
compatible = "ti,tca6416";
reg = <0x21>;
gpio-controller;
#gpio-cells = <2>;
};
};
i2c@4 {
#address-cells = <1>;
#size-cells = <0>;
reg = <4>;
rtc@51 {
compatible = "nxp,pcf8563";
reg = <0x51>;
};
};
i2c@7 {
#address-cells = <1>;
#size-cells = <0>;
reg = <7>;
hwmon@52 {
compatible = "ti,ucd9248";
reg = <52>;
};
hwmon@53 {
compatible = "ti,ucd9248";
reg = <53>;
};
hwmon@54 {
compatible = "ti,ucd9248";
reg = <54>;
};
};
};
};
Run Code Online (Sandbox Code Playgroud)
这是几个示例测试
尝试测试 SiLabs 时钟发生器
root@plnx_arm:~#/usr/bin/i2c-test-mem-location 0x54 0x00
addr 的值为:84
reg_addr 的值为:0
无法作为从设备打开设备
设备或资源繁忙
尝试测试eeprom设备
root@plnx_arm:~#/usr/bin/i2c-test-mem-location 0x5d 0x00
addr 的值为:93
reg_addr 的值为:0
无法作为从设备打开设备
设备或资源繁忙
这是我第三次尝试的程序。考虑到答案中的注释后,我写了这个
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#define I2C_ADAPTER "/dev/i2c-0"
#define DEVICE_ADDRESS 0x54
int main (int argc, char *argv[])
{
int file;
uint8_t reg, value;
char *end;
printf("The device address on the bus: %d", DEVICE_ADDRESS);
if(argc == 3) {
reg = strtol(argv[1], &end, 16);
printf("Value of register address: %d\n", reg);
value = strtol(argv[2], &end, 16);
printf("value to write is: %d\n", value);
}
else {
printf("arg failed\n\n.");
}
if((file = open(I2C_ADAPTER, O_RDWR)) < 0) {
printf("Failed to open the bus\n");
return -1;
}
if(ioctl(file, I2C_SLAVE, DEVICE_ADDRESS) < 0) {
printf("Unable to open device as slave \n%s\n", strerror(errno));
return -1;
}
char buf[10];
buf[0] = reg;
buf[1] = value;
if(write(file, buf, 2) != 2) {
printf("Failed to write to bus %s.\n\n", strerror(errno));
}
else {
printf("Successful write\n");
printf(buf);
printf("\n\n");
}
if(read(file, buf, 2) != 2) {
printf("Failed to read from the i2c bus.\n %s\n\n", strerror(errno));
}
else {
printf("Successful read\n");
printf("Buf = [%02X,%02X,%02X]\n", buf[0], buf[1], buf[2]);
printf("\n\n");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,即使如此,我还是遇到了同样的错误。
root@plnx_arm:~#/usr/bin/i2c-test-mem-location 0x00 0x10
总线上的设备地址:84寄存器地址的值:0
要写入的值为:16
无法作为从设备打开设备
设备或资源繁忙
root@plnx_arm:~#/usr/bin/i2c-test-mem-location 0x30 0x10
总线上的设备地址:84 寄存器地址的值:48
要写入的值为:16
无法作为从设备打开设备
设备或资源繁忙
编辑 2:我认为您可能没有正确设置 I2C 设备地址。您拥有的I2C_ADAPTER( "/dev/i2c-0") 表示设备所在的 I2C 总线。您甚至没有使用您的I2C_DEVICE宏,但这就是您应该传递给您的ioctl调用的内容(例如ioctl(file, I2C_SLAVE, I2C_DEVICE);),它应该是您要访问的设备的 I2C 地址(例如0x5D用于时钟发生器)而不是0x00.
我也认为您的读/写不正确。一旦你指定了总线和设备open(),ioctl()你就不需要再担心这些了。您只需要担心要访问的寄存器(如果您的 I2C 设备使用寄存器)和实际数据。
要写入您的 I2C 设备,假设它使用一个字节寄存器,请写入一个两个字节的缓冲区:第一个是寄存器,第二个是您要写入的值:
bool i2cdev_byte_write(int file, uint8_t reg, uint8_t val)
{
uint8_t bytes[2];
bytes[0] = reg;
bytes[1] = val;
/* Write the register followed by the value */
if (write(file, bytes, 2) != 2)
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
要从您的 I2C 设备读取,假设它使用一个一字节的寄存器,写入一个一字节的缓冲区(寄存器地址),然后读取一个或多个字节的缓冲区(该寄存器和后续寄存器的值):
bool i2cdev_bytes_read(int file, uint8_t reg, unsigned int count, uint8_t *out_buf)
{
if (!out_buf)
return false;
/* Write the register */
if (write(file, ®, 1) != 1)
{
printf("Failed to write register value\n");
return false;
}
/* Read the specified number of bytes */
if (read(file, out_buf, count) != count)
{
printf("Failed to read from the i2c bus\n");
return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
再次注意,上述所有注释都依赖于它是一个使用单字节寄存器地址的 I2C 设备,并且它支持在一次读取多个字节时自动递增寄存器地址。您需要查看 I2C 设备的数据表,以确定需要如何访问它。
编辑:这是printf()新手失败。你不能只是尝试printf一个字节数组。事情不是这样的printf()。
尝试这个:
printf("Buf = [%02X,%02X]\n", buf[0], buf[1]);
Run Code Online (Sandbox Code Playgroud)
此外,正如我在原始回复中所写,您可能需要在读取寄存器内容之前再次写回寄存器地址。