读取时不会返回I2C写入的缓冲区

时间:2017-10-30 20:20:10

标签: 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;
    }
  

程序的输出如下所示

     

成功写作

     

成功阅读��

在我的终端上,这些街区看起来更像钻石内的问号。我不确定在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;
}

此时,每当我使用0x00作为地址时,无论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>;
            };
        };
    };
};

以下是几个示例测试

尝试测试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;
}

不幸的是,即便如此,我也遇到了同样的错误。

  

root @ plnx_arm:〜#/ usr / bin / i2c-test-mem-location 0x00 0x10

     

总线上的器件地址:寄存器地址的84Value:0

     

要写的值是:16

     

无法将设备作为从属设备

打开      

设备或资源繁忙

     

root @ plnx_arm:〜#/ usr / bin / i2c-test-mem-location 0x30 0x10

     

总线上的器件地址:寄存器地址的84Value:48

     

要写的值是:16

     

无法将设备作为从属设备

打开      

设备或资源繁忙

4 个答案:

答案 0 :(得分:4)

编辑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;
}

要从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, &reg, 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;
}

再次注意,上述所有注释都取决于它是使用单字节寄存器地址的I2C器件,并且它支持在一次读取多个字节时自动递增寄存器地址。您需要检查I2C设备的数据表,以确切地确定它是如何被访问的。

编辑:这是printf()新手失败。你不能只尝试printf一个字节数组。这不是printf()的工作方式。

试试这个:

printf("Buf = [%02X,%02X]\n", buf[0], buf[1]);

另外,正如我在原始回复中所写,您可能需要在读取寄存器内容之前再次写入寄存器地址。

答案 1 :(得分:2)

i2c协议要求您指定设备地址(即0x00)和寄存器地址。然后,您可以将值(在您的情况下,0x10)写入该寄存器地址。试试这个:

char buf[10];

buf[0] = addr;
buf[1] = [REGISTER ADDRESS];
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("Addr: %02x Subaddr: %02x Value: %02x\n\n", buf[0], buf[1], buf[2]);
}

完成此写入后,您应该能够阅读:

if(read(file, buf, 1) != 1) {
    printf("Failed to read from the i2c bus. %s\n\n", strerror(errno));
}
else {
    printf("Successful read\n");
     printf("Value: %02x\n\n", buf[0]);
}

答案 2 :(得分:1)

根据设备树,需要提供给IOCtl的地址是i2cswitch mux地址。该地址为0x74,可在设备树中看到。打开i2c-0设备文件对应于设备树中的i2c0条目,该条目是多路复用器的父级。写入EEPROM时,缓冲区中的第一个字节应该是设备地址,如@AndrewCottrell所述。该地址是0x54。第二个字节应该是您要写入的数据

#define I2C_ADAPTER                 "/dev/i2c-0"
#define I2C_SWITHC_MUX_ADDRESS      0x74
#define DEVICE_ADDRESS              0x54

...

file = open(I2C_ADAPTER, O_RDWR); /* Check for error */
ioctl(file, I2C_SLAVE_FORCE, I2C_SWITHC_MUX_ADDRESS); /* Check for error */

uint8_t reg, value;

reg = DEVICE_ADDRESS;

buf[0] = reg;
buf[1] = value;

write(file, buf, 2); /* Check for error */
read(file, buf, 1); /* Check for error */
/* buf[0] should be value*/

答案 3 :(得分:0)

我在从 NTAG 5 升压组件的内存读取数据时也遇到问题,我在寻找此问题的原因时发现了此线程。实际上,对于我在组件上进行的每次 I2C 读取,我都会收到“FF”值。我没有在此线程中找到答案,但也许您在 this thread 中回答了我的问题。