int main(){ int i; int page_count=5; char wr_buffer[(page_count*PAGE_SIZE)]; char rd_buffer[page_count*PAGE_SIZE]; int res=0; /* OPEN the file */ if ((FILE_DESC = open("/dev/i2c-flash",O_RDWR)) < 0) { fprintf(stderr,"APP: Error: Failed to open the bus: %s\n",strerror(errno)); exit(1); } printf("APP: File opened successfully\n"); /* Populate the buffer to write */ for(i=0; i<(page_count*PAGE_SIZE); i++){ wr_buffer[i]='*'; } /* SEEK to page number */ if(seek_EEPROM(510) ==-1) // return -1; /* write until it returns with 0 */ while((res=write(FILE_DESC, wr_buffer, page_count)) !=0){ writeOutput(res); usleep(300000); } writeOutput(res); /* SEEK to page number */ if(seek_EEPROM(510) ==-1) // return -1; while( (res=read(FILE_DESC,rd_buffer, page_count)) != 0) { readOutput(res, rd_buffer, page_count); usleep(300000); } readOutput(res, rd_buffer, page_count); return 0; }
/* FUNCTION: int read_EEPROM(void *buf, int count) * DESCRIPTION: Reads a sequence of count pages from the EEPROM * device into user memory pointed by buf. The * pages to be read start from the current page * poisition of the EEPROM. The page position is * then advanced by count and, if reaching the end * of pages, wrapped around to the beginning of * the EEPROM. * RETURN: 0 is succeed * -1 if failure */ int read_EEPROM(void *buf, int count) { //allocate some memory for the read //PAGE_BYTES -1 since char is of size 1 //and we don't care about the page //offset. That is within the device char *tBuf = malloc(PAGE_BYTES); int buffPtr = 0; int address; int i = 0; //control for the for loop int res; //loop through the number of pages that need to //be read for (i; i < count; i++) { seek_EEPROM(currPage / PAGE_BYTES); //read from the //TODO This doesn't return correctly when there are //spaces. Due to printf res = read(fd, buf + (i * PAGE_BYTES), PAGE_BYTES); if (res < 0) { printf("\nReading from the page failed!\n"); return -1; } inc_offset((currPage / PAGE_BYTES) + 1); /* if (read(fd, tbuf, PAGE_BYTES) != PAGE_BYTES) { printf("\nI2C Read Failed!\n"); return -1; } */ } return 0; //SUCCESS!! }
int main(int argc, char **argv) { char filename[20]; // device to open int uInput; // user input int pageRWS; // num of pages to read, write char inp[64]; // input from user char *buf; // or set //create the file name according to adapter num snprintf(filename, 19, "/dev/i2c-%d", ADAPT_NUMB); //first we open the device and verify that it was //opened correctly fd = open(filename, O_RDWR); if (fd < 0) { printf("\n%s could not be opened, or was not found\n", filename); exit(1); } //set slave address, check for errors if (ioctl(fd, I2C_SLAVE, SLAVE_ADDR) < 0) { printf("\n%d address not found.\n"); exit(1); } //do some reading or writing do { printf("\n1.Read\n2.Write\n3.Set\n4.Quit\n"); scanf("%d", &uInput); switch(uInput) { case 1 : printf("\nRead Statement\n"); printf("\nHow many pages to read?\n"); scanf("%d", &pageRWS); buf = malloc(sizeof(char) * PAGE_BYTES * pageRWS); memset(buf, 0, sizeof(char) * 64 * pageRWS); read_EEPROM(buf, pageRWS); printBuf(buf, PAGE_BYTES * pageRWS, PAGE_BYTES); free(buf); break; case 2 : printf("\nWrite Statement\n"); printf("\nHow many pages to write?\n"); scanf("%d", &pageRWS); buf = malloc((sizeof(char) * PAGE_BYTES * pageRWS)); memset(buf, 0, sizeof(char) * 64 * pageRWS); printf("\nData to write:\n"); scanf("%s", buf); write_EEPROM(buf, pageRWS); free(buf); break; case 3 : printf("\nSet Statement\n"); printf("\nNew device offset?\n"); scanf("%d", &pageRWS); seek_EEPROM(pageRWS); break; case 4 : printf("\nQuit\n"); break; default: printf("\nUnknown Command\n"); break; } } while (uInput != 4); close(fd); return 0; }