int I2CReadEEPROM(int SlaveAddress, int mem_addr, char *i2cData, int len) - Read
ID: 3682352 • Letter: I
Question
int I2CReadEEPROM(int SlaveAddress, int mem_addr, char *i2cData, int len) - Reads len number of bytes from the slave device with device ID SlaveAddress starting at memory location mem_addr into byte buffer *i2cData and returns a value of 0 if no error was detected or a non zero value if an I2C error was detected. Provide a “pseudocode” description of an algorithm (or C code if you prefer) that calculates the following: (1) the number of bytes that will be written into the first page; (2) the number of full pages that will be written afterwards (possibly zero); and (3) the number of bytes remaining that will be written to a final, partial, page (also possibly zero).
Explanation / Answer
I am using two resources the Linux I2C-Dev documentation and an example from a similar EEPROM device. #include #include #include #include #include #include #include #include #include #include #include #define DEVICE_PATH "/dev/i2c-1" #define PAGE_SIZE 64 #define DEVICE_ADDR 0x50 // 0b1010xxxx int file_desc; char buffer[PAGE_SIZE + 2]; // 64 bytes + 2 for the address void teardownI2C() { int result = close(file_desc); } void setupI2C() { file_desc = open(DEVICE_PATH, O_RDWR); if(file_desc < 0) { printf("%s ", strerror(errno)); exit(1); } if(ioctl(file_desc, I2C_SLAVE, DEVICE_ADDR) < 0) { printf("%s ", strerror(errno)); teardownI2C(); exit(1); } } int write_to_device(char addr_hi, char addr_lo, char * buf, int len) { struct i2c_rdwr_ioctl_data msg_rdwr; struct i2c_msg i2cmsg; char my_buf[PAGE_SIZE + 2]; if(len > PAGE_SIZE + 2) { printf("Can't write more than %d bytes at a time. ", PAGE_SIZE); return -1; } int i; my_buf[0] = addr_hi; my_buf[1] = addr_lo; for(i= 0; iRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.