23
23
the memory cells. During this programming cycle, the IC does not respond
24
24
to I2C requests. This feature 'NAK' polling is used to determine when
25
25
the program cycle has completed.
26
- */
26
+ */
27
27
bool i2cReady (uint8_t ID){
28
28
uint32_t timeout=millis ();
29
29
bool ready=false ;
30
30
while ((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
+ }
38
38
return ready;
39
39
}
40
40
@@ -47,29 +47,29 @@ uint8_t ID=0x50,i;
47
47
uint16_t size;
48
48
char buf[256 ];
49
49
while (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 ();
73
73
if (saveByte == zeroByte) { // have to test it
74
74
Wire.beginTransmission (ID);
75
75
Wire.write (highByte (size)); // set next test address
@@ -103,41 +103,41 @@ while(ID<0x58){
103
103
else {
104
104
testByte = ~zeroByte;
105
105
}
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
+ }
120
120
else testByte = zeroByte; // They were different so the eeprom Is Bigger
121
121
}
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 ();
141
141
if (err==0 ){
142
142
err= Wire.requestFrom (ID,1 );
143
143
if (err==1 ) {
@@ -152,15 +152,15 @@ while(ID<0x58){
152
152
}
153
153
}
154
154
}
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
+ }
164
164
}
165
165
166
166
void setup (){
0 commit comments