首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >I2C EEPROM读写Cubieboard 2 Arch

I2C EEPROM读写Cubieboard 2 Arch
EN

Stack Overflow用户
提问于 2014-06-12 21:01:42
回答 1查看 1.1K关注 0票数 0

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

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

我可以成功地写(有点.)到所选的地址,然后从该地址开始依次写入多个比特。这些问题是:

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

通过查看数据表(连续几个小时),我似乎没有使用i2c-dev库所需的对I2C通信的控制能力。如果我能直接将X位或X字节直接写入EEPROM,那就太好了。

总之,我想建议我如何正确地阅读和写入这个EEPROM。

代码语言:javascript
复制
    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");

    }
EN

回答 1

Stack Overflow用户

发布于 2015-05-28 03:27:15

我确实正确地使用了来自linux/i2c-dev.h的函数。

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

代码语言:javascript
复制
#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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24193819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档