0

我正在尝试使用 Arch Linux 在 Cubieboard 2 上通过 i2c 读取和写入 AT24MAC402 EEPROM。我正在使用 i2c-dev 库和 i2c-tools。

数据表:http: //www.atmel.com/images/atmel-8807-seeprom-at24mac402-602-datasheet.pdf

我可以成功地写入(有点……)到一个选定的地址,并从该地址开始顺序写入许多位。问题是:

  1. 一旦选择了第一个地址,就无法重新选择要写入的另一个地址。
  2. 无法将 EEPROM 指向我希望读取的位置(通过虚拟写入),因此几乎无法真正控制 EEPROM。

查看数据表(连续数小时)后,我似乎没有使用 i2c-dev 库对 I2C 通信进行尽可能多的控制。如果我能写 X 就好了位或 X 字节直接写入 EEPROM。

简而言之,我想就如何正确读取和写入此 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");

    }
4

1 回答 1

0

我确实使用linux/i2c-dev.h中的功能正常工作。

为了测试代码,我得到了 i2cdump 生成的输出并将其作为 i2c-stub-from-dump 工具的输入,它允许您根据您想要的芯片转储在 i2c-stub 总线上设置一个或多个假 I2C 芯片仿真。

#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;
}
于 2015-05-28T03:27:15.593 回答