Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
I2C EEPROM读/写立体板2 Arch Linux_Linux_Archlinux_I2c_Eeprom - Fatal编程技术网

I2C EEPROM读/写立体板2 Arch Linux

I2C EEPROM读/写立体板2 Arch Linux,linux,archlinux,i2c,eeprom,Linux,Archlinux,I2c,Eeprom,我正在尝试使用Arch Linux通过i2c读取和写入Cubieboard 2上的AT24MAC402 EEPROM。我正在使用i2c开发库和i2c工具 数据表: 我可以成功地写一些。。。到所选地址,并从该地址开始顺序写入多个位。这些问题是: 选择第一个地址后,无法重新选择要写入的其他地址。 无法通过虚拟写入将EEPROM指向我希望读取的位置,因此对EEPROM几乎没有实际控制。 在连续数小时查看数据表后,我似乎无法像使用I2C开发库那样控制I2C通信。。如果我能直接将X位或X字节写入EEPR

我正在尝试使用Arch Linux通过i2c读取和写入Cubieboard 2上的AT24MAC402 EEPROM。我正在使用i2c开发库和i2c工具

数据表:

我可以成功地写一些。。。到所选地址,并从该地址开始顺序写入多个位。这些问题是:

选择第一个地址后,无法重新选择要写入的其他地址。 无法通过虚拟写入将EEPROM指向我希望读取的位置,因此对EEPROM几乎没有实际控制。 在连续数小时查看数据表后,我似乎无法像使用I2C开发库那样控制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");

    }

我确实正确地使用了来自的函数

为了测试代码,我获取了i2cdump生成的输出,并将其作为dump工具中i2c存根的输入,它允许您根据要仿真的芯片的转储在i2c存根总线上设置一个或多个伪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;
}