Я пытаюсь читать и писать в EEPROM AT24MAC402 через i2c на Cubieboard 2 с помощью Arch Linux. Я использую библиотеку i2c-dev и i2c-tools.
Лист данных: http://www.atmel.com/images/atmel-8807-seeprom-at24mac402-602-datasheet.pdf
Я могу успешно писать (вроде ...) на выбранный адрес и последовательно записывать много битов, начиная с этого адреса. Проблемы следующие:
- Невозможно повторно выбрать другой адрес для записи после выбора первого адреса.
- Невозможно указать EEPROM в том месте, откуда я хочу читать (фиктивная запись), и поэтому практически не имею реального контроля над EEPROM.
Глядя на таблицу (часами подряд), кажется, что у меня нет такого контроля над связью I2C, который мне может понадобиться с помощью библиотеки i2c-dev. Было бы здорово, если бы я мог просто написать 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");
}