Search code examples
linuxi2ceeprom

I2C EEPROM : can write but read only 0xFF


I am currently working on an I2C communication between an i.MX6 (Android BSP) and an 24C08WP EEPROM.

I'm running on the i.MX6 a binary previously compiled under an NDK under Linux.

I detect the NTAG 5 component connected to the I2C bus (address 0x50) of the i.MX6 thanks to an i2cdetect tool.

With the following code, I can perform write operation, which I can check using an Arduino board and I2C read operation.

However, when I perform read operations in user space under the i.MX6, I only get the 0xFF value.

Here is my code:

#include <stdio.h>
#include <stdlib.h>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include <linux/i2c-dev.h>

#include "board.h"
#include "debug_tools.h"
#include "boardselection.h"

int main(void) {
    int file;
    int adapter_nr = 1; /* probably dynamically determined */
    char filename[20];

    snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
    file = open(filename, O_RDWR);
    if (file < 0) {
        /* ERROR HANDLING; you can check errno to see what went wrong */
        exit(1);
    }

    int addr = 0x50; /* The I2C address */

    if (ioctl(file, I2C_SLAVE, addr) < 0) {
        /* ERROR HANDLING; you can check errno to see what went wrong */
        exit(1);
    }

    uint8_t reg = 0x00;

    uint8_t data_w[4] = {0x00, 0x00, 0x00, 0x00};

    data_w[0] = reg;
    data_w[1] = 0x01;
    data_w[2] = 0x02;
    data_w[3] = 0x03;
    
    /* Write the register */
    if (write(file, data_w, 4) != 4)
    {
        perror("Failed to write to the i2c bus");
        exit(1);
    }

    usleep(2000000);

    uint8_t data_r[4] = {0x00, 0x00, 0x00, 0x00};

    if (read(file, data_r, 3) != 3) {
        /* ERROR HANDLING: i2c transaction failed */
        perror("Failed to read register value");
        exit(1);
    }

    /* data_r[0] contains the read byte */
    printf("%X %X %X\n", data_r[0], data_r[1], data_r[2]);

    return 0;
}

Can you help me?

This thread describes almost the same problem about receiving 0xFF value.


Solution

  • As @Andrew Cottrell said in this thread: "To read from your I2C device, assuming it uses a one-byte register, write a buffer of one byte (the register address) then read a buffer of one or more bytes (the value at that register and subsequent registers)."

    So the right code is the following:

    #include <stdio.h>
    #include <stdlib.h>
    
    #include <errno.h>
    #include <fcntl.h>
    #include <unistd.h>
    
    #include <linux/i2c-dev.h>
    
    #include "board.h"
    #include "debug_tools.h"
    #include "boardselection.h"
    
    int main(void) {
        int file;
        int adapter_nr = 1; /* probably dynamically determined */
        char filename[20];
    
        snprintf(filename, 19, "/dev/i2c-%d", adapter_nr);
        file = open(filename, O_RDWR);
        if (file < 0) {
            /* ERROR HANDLING; you can check errno to see what went wrong */
            exit(1);
        }
    
        int addr = 0x50; /* The I2C address */
    
        if (ioctl(file, I2C_SLAVE, addr) < 0) {
            /* ERROR HANDLING; you can check errno to see what went wrong */
            exit(1);
        }
    
        uint8_t reg = 0x00;
    
        uint8_t data_w[4] = {0x00, 0x00, 0x00, 0x00};
    
        data_w[0] = reg;
        data_w[1] = 0x44;
    
        /* Write the register */
        if (write(file, data_w, 2) != 2)
        {
            perror("Failed to write to the i2c bus");
            exit(1);
        }
    
        usleep(1000000);
    
        uint8_t data_r[4] = {0x00, 0x00, 0x00, 0x00};
    
        if (write(file, &reg, 1) != 1)
        {
            perror("Failed to write to the i2c bus");
            exit(1);
        }
    
        if (read(file, data_r, 1) != 1) {
            /* ERROR HANDLING: i2c transaction failed */
            perror("Failed to read register value");
            exit(1);
        }
    
        /* data_r[0] contains the read byte: 0x44 */
        printf("%02X\n", data_r[0]);
    
        return 0;
    }
    

    N.B.: If you do not use usleep() to wait between two write operations, the second write operation might fail.