Binary files 24Cxxx-1.0/24Cxxx and 24Cxxx-1.0.mine/24Cxxx differ
diff -urN 24Cxxx-1.0/24Cxxx.c 24Cxxx-1.0.mine/24Cxxx.c
--- 24Cxxx-1.0/24Cxxx.c	Sat Sep 23 12:35:35 2000
+++ 24Cxxx-1.0.mine/24Cxxx.c	Mon Jun 18 23:34:30 2001
@@ -1,4 +1,4 @@
-/************************************************************************ 
+/************************************************************************
  *
  * 24Cxx version 1.0 - Copyright (C) 2000 Luca Ferretti <luca.ferretti@wind.it>
  *
@@ -19,14 +19,14 @@
  *            See the file COPYING for more details.
  *
  *************************************************************************
- *            	  
- * PROGRAM : 24Cxxx.c  
- * RELEASE : 1.0 
+ *
+ * PROGRAM : 24Cxxx.c
+ * RELEASE : 1.0
  * DATE    : 17 Sep 2000
  * PURPOSE : Read / Write / Verify the content of an i2c eeprom
  *           type 24Cxxx connected to a serial port via Ludipipo|JDM i/f
- * 
- ************************************************************************/	
+ *
+ ************************************************************************/
 
 #include "24Cxxx.h"
 
@@ -36,9 +36,9 @@
 	unsigned char buff1[MAX_E2PROM_SIZE];
 	char port[255]=DEFAULT_PORT;
 	char file_name[255]=DEFAULT_FNAME;
-	int i,eeprom_size=B_SIZE,verify_errors=0;
+	int i,file_size,eeprom_size=B_SIZE,verify_errors=0;
 	char optchar;
-	// Parse cmd line opts	
+	// Parse cmd line opts
 	if(argc<2)
 		usage();
 	opterr=0;
@@ -49,7 +49,7 @@
 				continue;
 			case 'r' :
 				options|=R_BIT_MASK;
-				if (optarg != 0) 
+				if (optarg != 0)
 					eeprom_size=(atoi(optarg)*1024)/8;
 				continue;
 			case 'v' :
@@ -70,15 +70,16 @@
 				usage();
 		}
 	}
-	if (optind<argc){ 			// set eeprom size if passed 
+	if (optind<argc){ 			// set eeprom size if passed
 		eeprom_size=(atoi(argv[optind])*1024)/8;
 		pg_size=get_pg_size(argv[optind]);
 	}
 	// end of parsing cmd line
+	file_size=eeprom_size;
 
 	if (!(options & W_BIT_MASK || options & R_BIT_MASK || options & V_BIT_MASK))
 		usage();
-	
+
 	if ((options & W_BIT_MASK) && (options & R_BIT_MASK)) {
 		fprintf(stdout,"\nRead & Write @ the same time ?\n");
 		usage();
@@ -87,29 +88,35 @@
 	fprintf(stdout,"%s",header);
 	fd=open_port(port);
 
-	schedulize_srt();			// let's go in soft RT mode	
-	
+	schedulize_srt();			// let's go in soft RT mode
+
 	vcc_on;					// Power up IC ...
-	usleep(POWERUP_DELAY);			// .. and wait a moment 
-	
-	if (options & R_BIT_MASK) {	
-		read_e2prom(buff,eeprom_size);
-		save_buffer(file_name,buff,eeprom_size);
-	}
-	if (options & W_BIT_MASK) {	
-		eeprom_size=read_buffer(file_name,buff); 
-		write_e2prom(buff,eeprom_size);
+	usleep(POWERUP_DELAY);			// .. and wait a moment
+
+	if (options & R_BIT_MASK) {
+		read_e2prom(buff,eeprom_size,file_size);
+		save_buffer(file_name,buff,file_size);
+	}
+	if (options & W_BIT_MASK) {
+		file_size=read_buffer(file_name,buff);
+		write_e2prom(buff,eeprom_size,file_size);
 	}
 	if (options & V_BIT_MASK) {
 		fprintf(stdout,"Verify in progress...\n");
 		if ( !(options & W_BIT_MASK) && !(options & R_BIT_MASK) )
-			eeprom_size=read_buffer(file_name,buff);
-		read_e2prom(buff1,eeprom_size);
-		for (i=0;i<eeprom_size;i++) {
+			file_size=read_buffer(file_name,buff);
+		read_e2prom(buff1,eeprom_size,file_size);
+		for (i=0;i<file_size;i++) {
 			if (buff[i] != buff1[i]) {
 				verify_errors++;
-				fprintf(stderr," ERROR @ location %d[%.4xh] \"%s\" data='%.2x' e2prom data='%.2x'\n",i,i,file_name,buff[i],buff1[i]); 
+				fprintf(stderr," ERROR @ location %d[%.4xh] \"%s\" data='%.2x' e2prom data='%.2x'\n",i,i,file_name,buff[i],buff1[i]);
+			}
+			if(verify_errors>10)
+			{
+				fprintf(stderr," Too many errors - aborting");
+				break;
 			}
+
 		}
 		if (verify_errors)
 			fprintf(stdout,"Verify : %d errors found\n",verify_errors);
@@ -129,18 +136,18 @@
 	struct sched_param sp;
 	sp.sched_priority=sched_get_priority_max(SCHED_FIFO);
 	sched_setscheduler(0,SCHED_FIFO,&sp);
-}	
+}
 
-int open_port(char * port) { // Returns the file descriptor or die on error 
-	int fd;  
-	fd = open(port, O_RDWR ); 
-	if (fd ==  -1) {  
+int open_port(char * port) { // Returns the file descriptor or die on error
+	int fd;
+	fd = open(port, O_RDWR );
+	if (fd ==  -1) {
 		fprintf(stderr,"\nERROR: Unable to open device %s\n",port);
 		exit(1);
-	} else fcntl(fd, F_SETFL, 0); return (fd); 
+	} else fcntl(fd, F_SETFL, 0); return (fd);
 }
 
-void delay(int delay) {		
+void delay(int delay) {
 	while(delay--);
 }
 
@@ -149,7 +156,7 @@
 	scl_lo; delay(TXX);
 	sda_hi; delay(TXX);
 	scl_hi; delay(TXX);
-	ioctl(fd, TIOCMGET, &status); 
+	ioctl(fd, TIOCMGET, &status);
 	status= status & TIOCM_CTS;
 	if ( status )
 		return 0x01;
@@ -159,10 +166,10 @@
 
 void send_bit(unsigned char bit) {
 	scl_lo; delay(TXX);
-	if (bit) 
+	if (bit)
 		sda_hi;
-	else	
-		sda_lo; 
+	else
+		sda_lo;
 	delay(TXX);
 	scl_hi; delay(TXX);
 }
@@ -181,16 +188,16 @@
 	unsigned char ack;
 	ack=recv_bit();
 	scl_lo;
-	if (ack) { 
+	if (ack) {
 		idle();
-		close(fd);  
+		close(fd);
 		fprintf(stderr,"\n\nACK ERROR !\n\n");
 		fprintf(stderr,"Suggestion : increase -s value.\n\n");
 		exit(3);
-	}	
+	}
 	return (ack);
 }
-		
+
 void send_start(void) {
 	sda_hi; delay(TXX);
 	scl_hi; delay(TXX);
@@ -202,7 +209,7 @@
 	scl_hi; delay(TXX);
 	sda_hi; delay(TXX);
 }
-		
+
 void send_byte(unsigned char byte) {
 	int i;
 	for (i=7;i>=0;i--) 				// Send
@@ -216,15 +223,21 @@
 		byte|=recv_bit()<<i;			// MSb first
 	return byte;
 }
-		
-void read_e2prom(unsigned char *buffer, int size) {
+
+void read_e2prom(unsigned char *buffer,int eeprom_size, int size) {
 	int i;
-	fprintf(stdout,"Reading %d bytes...",size);	
+	fprintf(stdout,"Reading %d bytes...",size);
 	fflush(stdout);
-	send_start(); send_byte(W_HEADER); 
+	send_start(); send_byte(W_HEADER);
 	recv_ack();
 	send_byte(0x00);	// let's start reading from addr 0
-	recv_ack(); 
+	recv_ack();
+	if(eeprom_size>2048)
+	{
+	    send_byte(0x00);	// two byte adressing on these eeproms
+	    recv_ack();
+	}
+
 	send_start(); send_byte(R_HEADER);
 	recv_ack();
 	for (i=0;i<size;i++) {
@@ -233,7 +246,7 @@
 		progress();
 	}
 	send_stop();
-	fprintf(stdout,"done\n");	
+	fprintf(stdout,"done\n");
 }
 
 void save_buffer(char *f_name,unsigned char *buffer,int size) {
@@ -246,8 +259,8 @@
 		fprintf(stderr,"\nERROR : cannot create %s\n\n",f_name);
 		exit(1);
 	}
-	for(i=0;i<size;i++) 
-		putc(*buffer++,fp);	
+	for(i=0;i<size;i++)
+		putc(*buffer++,fp);
 	fclose(fp);
 	fprintf(stdout,"done\n");
 }
@@ -265,27 +278,27 @@
 	fseek(fp, 0L, SEEK_END);		// Go 2 *real* EOF
 	fsize=ftell(fp);			// now we have bytes GT
 	fseek(fp,0L,SEEK_SET);			// Rewind 2 beg
-	i=fsize;	
-	while (i--) 
+	i=fsize;
+	while (i--)
 		*buffer++=getc(fp);
 	fclose(fp);
 	fprintf(stdout,"done (%d bytes)\n",fsize);
 	return(fsize);
 }
-     
-void write_e2prom(unsigned char *buffer,int size) {
+
+void write_e2prom(unsigned char *buffer,int eeprom_size,int size) {
 	int i,k;
 	fprintf(stdout,"Writing %d bytes...",size);
 	fflush(stdout);
 	for (i=0;i<size;i++) {
 		send_start();
-		if (size<=2048) 		// byte addr protocol
+		if (eeprom_size<=2048) 		// byte addr protocol
 	       		send_byte(W_HEADER+((i/0x100)<<1)); // + 3b addr MSB
 	       	else {				// word addr protocol
 			send_byte(W_HEADER);
 			recv_ack();
 			send_byte(i/0x100);
-		}	
+		}
 		recv_ack();
 		send_byte((i%0x100));		// Addr LSB
 		recv_ack();
@@ -312,7 +325,7 @@
 }
 
 int get_pg_size(char *KB) {
-	#define HASH_N 10 
+	#define HASH_N 10
 	//char *ee[HASH_N] = { "1","2","4","8","16","32","64","128","256","512" };
 	char *ee[HASH_N] = E2PROMS ;
 	//int pp[HASH_N] = { 8,8,16,16,16,32,32,32,32,32 };
@@ -321,20 +334,20 @@
 	int i;
 	hcreate(HASH_N);
 	for (i = 0; i < HASH_N; i++)  {
-		ps.key = ee[i];     
+		ps.key = ee[i];
 		ps.data = (char *) pp[i];
-		psp=hsearch(ps,ENTER);  
+		psp=hsearch(ps,ENTER);
 	}
 	ps.key = KB;
        	psp=hsearch(ps,FIND);
 	if (psp)
-	       	return((int)psp->data); 
-	else 
+	       	return((int)psp->data);
+	else
 		return(8);
 }
 
 void usage(void) {
-	fprintf(stdout,"%s",header);	
+	fprintf(stdout,"%s",header);
 	fprintf(stdout,"   Read/Program/Verify i2c eeproms via Ludipipo|JDM serial interface\n");
 	fprintf(stdout,"\nUSAGE : 24Cxxx [SIZE] MODE [OPTIONS] \n\n");
 	fprintf(stdout,"SIZE : 		set eeprom size to SIZE Kbits (default:16),\n");
@@ -349,4 +362,3 @@
 	fprintf(stdout,"				(default:10000 [10ms])\n\n");
 	exit(99);
 }
-    
diff -urN 24Cxxx-1.0/24Cxxx.h 24Cxxx-1.0.mine/24Cxxx.h
--- 24Cxxx-1.0/24Cxxx.h	Sat Sep 23 13:33:38 2000
+++ 24Cxxx-1.0.mine/24Cxxx.h	Mon Jun 18 23:29:18 2001
@@ -1,4 +1,4 @@
-/************************************************************************ 
+/************************************************************************
  *
  * 24Cxx version 1.0 - Copyright (C) 2000 Luca Ferretti <luca.ferretti@wind.it>
  *
@@ -19,20 +19,20 @@
  *            See the file COPYING for more details.
  *
  *************************************************************************
- *            	  
- * PROGRAM : 24Cxxx.h  
- * RELEASE : 1.0 
+ *
+ * PROGRAM : 24Cxxx.h
+ * RELEASE : 1.0
  * DATE    : 17 Sep 2000
- * 
- ************************************************************************/	
+ *
+ ************************************************************************/
 
 // System include
-#include <stdio.h>  
+#include <stdio.h>
 #include <stdlib.h>
-#include <string.h>  
-#include <unistd.h>  
-#include <fcntl.h>  
-#include <termios.h>  
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <termios.h>
 #include <sys/ioctl.h>
 #include <time.h>
 #include <search.h>
@@ -40,38 +40,38 @@
 
 // K defs
 #define VERSION		"1.0"
-#define K_BIT		16		// Default 
-#define B_SIZE		( K_BIT * 1024 ) / 8 	// R U a Genius ? 	
+#define K_BIT		16		// Default
+#define B_SIZE		( K_BIT * 1024 ) / 8 	// R U a Genius ?
 #define DEFAULT_FNAME	"./eeprom.bin"
 #define DEFAULT_PORT	"/dev/ttyS0"
-#define MAX_E2PROM_SIZE 65536		// 512K is,AFAIK,the biggest one	
+#define MAX_E2PROM_SIZE 65536		// 512K is,AFAIK,the biggest one
 #define POWERUP_DELAY	500000		// uS between power on & ops
-#define TXX		0	 	// Bit ops timing (dummy loop) 
-#define WCT		10000		// uS between each pgm cycle 
+#define TXX		0	 	// Bit ops timing (dummy loop)
+#define WCT		10000		// uS between each pgm cycle
 					// This is the SAFEST SETTING possible
 					// w/o waste of time.
 
-#define SLAVE_ADDR	0xa0		// identify a serial eeprom 
+#define SLAVE_ADDR	0xa0		// identify a serial eeprom
 					// (the hi nibble)
-#define BLOCK_SEL	0x00		// 3 bits 
-#define READ_BIT    	0x01		// lsb that identify 	
-#define WRITE_BIT   	0x00		//   op type 
+#define BLOCK_SEL	0x00		// 3 bits
+#define READ_BIT    	0x01		// lsb that identify
+#define WRITE_BIT   	0x00		//   op type
 #define R_HEADER	SLAVE_ADDR + BLOCK_SEL + READ_BIT
-#define W_HEADER	SLAVE_ADDR + BLOCK_SEL + WRITE_BIT 
+#define W_HEADER	SLAVE_ADDR + BLOCK_SEL + WRITE_BIT
 
 #define E2PROMS	 { "1","2","4","8","16","32","64","128","256","512" }
-#define PG_SIZES {  8,  8,  16, 16, 16,  32,  32,  32,   32,   32 }
+#define PG_SIZES {  8,  8,  16, 16, 16,  32,  32,  32,   64,   32 }
 
 #define vcc_on	(ioctl (fd, TIOCSBRK, 0)) 	// TxD --> Vcc
 #define vcc_off	(ioctl (fd, TIOCCBRK, 0))
-#define scl_lo	( ioctl (fd,TIOCMBIC,&rts) )	// RTS --> SCL	
-#define scl_hi	( ioctl (fd,TIOCMBIS,&rts) )			 
-#define sda_lo	( ioctl (fd,TIOCMBIC,&dtr) )	// DTR --> SDA		 
-#define sda_hi	( ioctl (fd,TIOCMBIS,&dtr) )		 
+#define scl_lo	( ioctl (fd,TIOCMBIC,&rts) )	// RTS --> SCL
+#define scl_hi	( ioctl (fd,TIOCMBIS,&rts) )
+#define sda_lo	( ioctl (fd,TIOCMBIC,&dtr) )	// DTR --> SDA
+#define sda_hi	( ioctl (fd,TIOCMBIS,&dtr) )
 
 #define	W_BIT_MASK	0x01			// Options
-#define	R_BIT_MASK	0x02			//  bits 
-#define	V_BIT_MASK	0x04			//   masks 
+#define	R_BIT_MASK	0x02			//  bits
+#define	V_BIT_MASK	0x04			//   masks
 
 // Prototypes defs
 int open_port(char * port);
@@ -79,16 +79,16 @@
 void delay(int delay);
 unsigned char recv_bit(void);			// CTS <-- SDA
 unsigned char recv_ack(void);
-void send_ack(void); 
+void send_ack(void);
 void send_start(void);
 void send_stop(void);
 void send_byte(unsigned char byte);
 unsigned char recv_byte(void);
-void read_e2prom(unsigned char *buffer,int size);
-void write_e2prom(unsigned char *buffer,int size);
+void read_e2prom(unsigned char *buffer,int eeprom_size,int size);
+void write_e2prom(unsigned char *buffer,int eeprom_size,int size);
 int get_pg_size(char *KB);
-void save_buffer(char *f_name,unsigned char *buffer,int size); 
-int read_buffer(char *f_name,unsigned char *buffer); 
+void save_buffer(char *f_name,unsigned char *buffer,int size);
+int read_buffer(char *f_name,unsigned char *buffer);
 void usage(void);
 void progress(void);
 void schedulize_srt(void);
