0

I am trying to read and write to the AT24MAC402 EEPROM over i2c on the Cubieboard 2 using Arch Linux. I am using the i2c-dev library and i2c-tools.

Datasheet: http://www.atmel.com/images/atmel-8807-seeprom-at24mac402-602-datasheet.pdf

I can successfully write (kind of...) to a chosen address and sequentially write many bites starting at that address. The issues are:

  1. Cannot re-select another address to write once the first address has been selected.
  2. Cannot point the the EEPROM to the location I wish to read from (by dummy-writing), and therefore have almost no real control over the EEPROM.

Upon looking at the datasheet (for hours on end), it looks as if I don't have as much control over the I2C communications as I may need using the i2c-dev library.. It would be great if I could just write X bits or X bytes directly to the EEPROM.

In short, I would like advice on how I can read and write properly to this EEPROM.

    char buf[10];

    int com_serial;
    int failcount;

    int i2c_init(char filename[40], int addr)
        {
        int file;

        if ((file = open(filename,O_RDWR)) < 0)
                {
                printf("Failed to open the bus.");
                /* ERROR HANDLING; you can check errno to see what went wrong */
                com_serial=0;
                exit(1);
                }

    if (ioctl(file,I2C_SLAVE,addr) < 0)
                {
                printf("Failed to acquire bus access and/or talk to slave.\n");
                /* ERROR HANDLING; you can check errno to see what went wrong */
                com_serial=0;
                exit(1);
                }
        return file;
        }


    int main (int argc, char *argv[]) { 

    char read_buf[16];
    char write_buf[17];
    int i;

    int file;
    file=i2c_init("/dev/i2c-1",0x50); //dev,slavei2caddr
    write_buf[0] = 0x00;
    write_buf[1] = 'H';
    write_buf[2] = 'i';
    write_buf[3] = '!';
    write(file, write_buf, 4);
    //Successfully prints "Hi!" to bytes 0x00 -> 0x02

    //Setting EEPROM to point to address 0xA0 to start reading (arbitrary address with known values: all 0xFF)
    write_buf[0] = 0xA0;    
    write(file, write_buf, 1);

    //Reading 1 byte from EEPROM, even though there is a '2'; 2 bytes would be '3'
    read(file, read_buf, 2);

    for (i=1; i<3; i++){
        printf("%X", read_buf[i]);
    }
    //Prints out from address 0x04 to 0x05 instead of 0xA0 to 0xA1

    printf("\n");

    }
StuartKerr
  • 77
  • 3
  • 9

1 Answers1

0

I did work properly using the functions from the linux/i2c-dev.h.

To test the code I get the output generated by i2cdump and put as input to i2c-stub-from-dump tool, it lets you setup one or more fake I2C chips on the i2c-stub bus based on dumps of the chips you want to emulate.

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>

int i2c_init(const char * i2c_device, const int chip_address)
{
    int file;
    if ((file = open(i2c_device, O_RDWR)) < 0) {
        return -1;
    }
    if (ioctl(file, I2C_SLAVE, chip_address) < 0) {
        close(file);
        return -1;
    }
    return file;
}

int i2c_write(int file, const int data_address, const unsigned char * data, size_t size)
{
    return i2c_smbus_write_i2c_block_data(file, data_address, size, data);
}

void i2c_read(int file, const int data_address, unsigned char * data_vector, size_t size)
{
    unsigned char reg = data_address;
    unsigned int i;
    for(i = 0; i < size; ++i, ++reg) {
        data_vector[i] = i2c_smbus_read_byte_data(file, reg);
    }
}

int main(void) {

    char device[] = "/dev/i2c-6";
    int address = 0x50;
    unsigned char buffer_before[30] = {0};
    unsigned char buffer_after[30] = {0};
    unsigned char data[] = "Hello World!"; 

    int file;
    file = i2c_init(device, address);

    if (file > 0) {
        i2c_read(file, 0x00, buffer_before, sizeof(data)); 
        i2c_write(file, 0x00, data, sizeof(data)); 
        i2c_read(file, 0x00, buffer_after, sizeof(data)); 
        close (file);
    }

    printf("data read before write: %s\n", buffer_before);
    printf("data read after write: %s\n", buffer_after);
    return 0;
}