2323 the memory cells. During this programming cycle, the IC does not respond
2424 to I2C requests. This feature 'NAK' polling is used to determine when
2525 the program cycle has completed.
26- */
26+ */
2727bool i2cReady (uint8_t ID){
2828uint32_t timeout=millis ();
2929bool ready=false ;
3030while ((millis ()-timeout<100 )&&(!ready)){
31- Wire.beginTransmission (ID);
32- int err=Wire.endTransmission ();
33- ready=(err==0 );
34- if (!ready){
35- if (err!=2 )Serial.printf (" {%d}:%s" ,err,Wire.getErrorText (err));
36- }
37- }
31+ Wire.beginTransmission (ID);
32+ int err=Wire.endTransmission ();
33+ ready=(err==0 );
34+ if (!ready){
35+ if (err!=2 )Serial.printf (" {%d}:%s" ,err,Wire.getErrorText (err));
36+ }
37+ }
3838return ready;
3939}
4040
@@ -47,29 +47,29 @@ uint8_t ID=0x50,i;
4747uint16_t size;
4848char buf[256 ];
4949while (ID<0x58 ){
50- i=0 ;
51- size = 0x1000 ; // Start at 4k, 16bit address devices,
52- i += sprintf_P (&buf[i],PSTR (" 0x%02X: " ),ID);
53- if (i2cReady (ID)) { // EEPROM answered
54- uint8_t zeroByte;
55- Wire.beginTransmission (ID);
56- Wire.write ((uint8_t )0 ); // set address ptr to 0, two bytes High
57- Wire.write ((uint8_t )0 ); // set address ptr to 0, two bytes Low
58- uint8_t err=Wire.endTransmission ();
59- if (err==0 ){// worked, device exists at this ID
60- err=Wire.requestFrom (ID,(uint8_t )1 );
61- if (err==1 ){// got the value of the byte at address 0
62- zeroByte=Wire.read ();
63- uint8_t saveByte,testByte;
64- do {
65- if (i2cReady (ID)){
66- Wire.beginTransmission (ID);
67- Wire.write (highByte (size)); // set next test address
68- Wire.write (lowByte (size));
69- Wire.endTransmission ();
70- err=Wire.requestFrom (ID,(uint8_t )1 );
71- if (err==1 ){
72- saveByte=Wire.read ();
50+ i=0 ;
51+ size = 0x1000 ; // Start at 4k, 16bit address devices,
52+ i += sprintf_P (&buf[i],PSTR (" 0x%02X: " ),ID);
53+ if (i2cReady (ID)) { // EEPROM answered
54+ uint8_t zeroByte;
55+ Wire.beginTransmission (ID);
56+ Wire.write ((uint8_t )0 ); // set address ptr to 0, two bytes High
57+ Wire.write ((uint8_t )0 ); // set address ptr to 0, two bytes Low
58+ uint8_t err=Wire.endTransmission ();
59+ if (err==0 ){// worked, device exists at this ID
60+ err=Wire.requestFrom (ID,(uint8_t )1 );
61+ if (err==1 ){// got the value of the byte at address 0
62+ zeroByte=Wire.read ();
63+ uint8_t saveByte,testByte;
64+ do {
65+ if (i2cReady (ID)){
66+ Wire.beginTransmission (ID);
67+ Wire.write (highByte (size)); // set next test address
68+ Wire.write (lowByte (size));
69+ Wire.endTransmission ();
70+ err=Wire.requestFrom (ID,(uint8_t )1 );
71+ if (err==1 ){
72+ saveByte=Wire.read ();
7373 if (saveByte == zeroByte) { // have to test it
7474 Wire.beginTransmission (ID);
7575 Wire.write (highByte (size)); // set next test address
@@ -103,41 +103,41 @@ while(ID<0x58){
103103 else {
104104 testByte = ~zeroByte;
105105 }
106-
107- // restore byte
108- if (!i2cReady (ID)){
109- i+=sprintf_P (&buf[i],PSTR (" notReady4.\n " ));
110- Serial.print (buf);
111- ID++;
112- break ;
113- }
114- Wire.beginTransmission (ID);
115- Wire.write (highByte (size)); // set next test address
116- Wire.write (lowByte (size));
117- Wire.write ((uint8_t )saveByte); // restore it
118- Wire.endTransmission ();
119- }
106+
107+ // restore byte
108+ if (!i2cReady (ID)){
109+ i+=sprintf_P (&buf[i],PSTR (" notReady4.\n " ));
110+ Serial.print (buf);
111+ ID++;
112+ break ;
113+ }
114+ Wire.beginTransmission (ID);
115+ Wire.write (highByte (size)); // set next test address
116+ Wire.write (lowByte (size));
117+ Wire.write ((uint8_t )saveByte); // restore it
118+ Wire.endTransmission ();
119+ }
120120 else testByte = zeroByte; // They were different so the eeprom Is Bigger
121121 }
122- else testByte=~zeroByte;
123- }
124- else testByte=~zeroByte;
125- if (testByte==zeroByte){
126- size = size <<1 ;
127- }
128- }while ((testByte==zeroByte)&&(size>0 ));
129- if (size==0 ) i += sprintf_P (&buf[i],PSTR (" 64k Bytes" ));
130- else i+=sprintf_P (&buf[i],PSTR (" %dk Bytes" ),size/1024 );
131- if (!i2cReady (ID)){
132- i+=sprintf_P (&buf[i],PSTR (" notReady3.\n " ));
133- Serial.print (buf);
134- ID++;
135- continue ;
136- }
137- Wire.beginTransmission (ID);
138- Wire.write ((uint8_t )0 ); // set address ptr to 0, two bytes High
139- Wire.write ((uint8_t )0 ); // set address ptr to 0, two bytes Low
140- err=Wire.endTransmission ();
122+ else testByte=~zeroByte;
123+ }
124+ else testByte=~zeroByte;
125+ if (testByte==zeroByte){
126+ size = size <<1 ;
127+ }
128+ }while ((testByte==zeroByte)&&(size>0 ));
129+ if (size==0 ) i += sprintf_P (&buf[i],PSTR (" 64k Bytes" ));
130+ else i+=sprintf_P (&buf[i],PSTR (" %dk Bytes" ),size/1024 );
131+ if (!i2cReady (ID)){
132+ i+=sprintf_P (&buf[i],PSTR (" notReady3.\n " ));
133+ Serial.print (buf);
134+ ID++;
135+ continue ;
136+ }
137+ Wire.beginTransmission (ID);
138+ Wire.write ((uint8_t )0 ); // set address ptr to 0, two bytes High
139+ Wire.write ((uint8_t )0 ); // set address ptr to 0, two bytes Low
140+ err=Wire.endTransmission ();
141141 if (err==0 ){
142142 err= Wire.requestFrom (ID,1 );
143143 if (err==1 ) {
@@ -152,15 +152,15 @@ while(ID<0x58){
152152 }
153153 }
154154 }
155- else i+=sprintf_P (&buf[i],PSTR (" Read 0 Failure" ));
156- }
157- else i+=sprintf_P (&buf[i],PSTR (" Write Adr 0 Failure" ));
158-
159- }
160- else i+=sprintf_P (&buf[i],PSTR (" Not Present" ));
161- Serial.println (buf);
162- ID++;
163- }
155+ else i+=sprintf_P (&buf[i],PSTR (" Read 0 Failure" ));
156+ }
157+ else i+=sprintf_P (&buf[i],PSTR (" Write Adr 0 Failure" ));
158+
159+ }
160+ else i+=sprintf_P (&buf[i],PSTR (" Not Present" ));
161+ Serial.println (buf);
162+ ID++;
163+ }
164164}
165165
166166void setup (){
0 commit comments