我正在尝试使用 Arch Linux 在 Cubieboard 2 上通过 i2c 读取和写入 AT24MAC402 EEPROM。我正在使用 i2c-dev 库和 i2c-tools。
数据表:http: //www.atmel.com/images/atmel-8807-seeprom-at24mac402-602-datasheet.pdf
我可以成功地写入(有点……)到一个选定的地址,并从该地址开始顺序写入许多位。问题是:
- 一旦选择了第一个地址,就无法重新选择要写入的另一个地址。
- 无法将 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");
}