@@ -50,6 +50,8 @@ int pin_microSD_CS;
50
50
int pin_zed_tx_ready;
51
51
int pin_zed_reset;
52
52
int pin_batteryLevel_alert;
53
+ int pin_i2c_sda = 21 ;
54
+ int pin_i2c_scl = 22 ;
53
55
54
56
int pin_muxA;
55
57
int pin_muxB;
@@ -199,50 +201,43 @@ void setup()
199
201
Serial.begin (115200 );
200
202
delay (250 );
201
203
202
- // int pinNumber1 = 21;
203
- // int pinNumber2 = 22;
204
- // clearBuffer();
205
- // pinMode(pinNumber1, OUTPUT);
206
- // pinMode(pinNumber2, OUTPUT);
207
- //
208
- // Serial.printf("\n\rToggling pin %d. Press x to exit\n\r", pinNumber1);
209
- // Serial.printf("\n\rToggling pin %d. Press x to exit\n\r", pinNumber2);
210
- //
211
- // while (Serial.available() == 0)
212
- // {
213
- // digitalWrite(pinNumber1, HIGH);
214
- // digitalWrite(pinNumber2, HIGH);
215
- // for (int x = 0 ; x < 100 ; x++)
216
- // {
217
- // delay(30);
218
- // if (Serial.available()) break;
219
- // }
220
- //
221
- // digitalWrite(pinNumber1, LOW);
222
- // digitalWrite(pinNumber2, LOW);
223
- // for (int x = 0 ; x < 100 ; x++)
224
- // {
225
- // delay(30);
226
- // if (Serial.available()) break;
227
- // }
228
- // }
229
- // pinMode(pinNumber1, INPUT);
230
- // pinMode(pinNumber2, INPUT);
231
- //
232
- // Serial.println("Done");
233
-
234
- Wire.begin ();
235
-
236
- // begin/end wire transmission should take a few ms. If it's taking longer,
237
- // it's likely the I2C bus being shorted or pulled in
238
- unsigned long startTime = millis ();
239
- Wire.beginTransmission (0x15 ); // Dummy address
240
- int endValue = Wire.endTransmission ();
241
- if (millis () - startTime > 100 ) i2cBorked = true ;
204
+ // int pinNumber1 = 21;
205
+ // int pinNumber2 = 22;
206
+ // clearBuffer();
207
+ // pinMode(pinNumber1, OUTPUT);
208
+ // pinMode(pinNumber2, OUTPUT);
209
+ //
210
+ // Serial.printf("\n\rToggling pin %d. Press x to exit\n\r", pinNumber1);
211
+ // Serial.printf("\n\rToggling pin %d. Press x to exit\n\r", pinNumber2);
212
+ //
213
+ // while (Serial.available() == 0)
214
+ // {
215
+ // digitalWrite(pinNumber1, HIGH);
216
+ // digitalWrite(pinNumber2, HIGH);
217
+ // for (int x = 0 ; x < 100 ; x++)
218
+ // {
219
+ // delay(30);
220
+ // if (Serial.available()) break;
221
+ // }
222
+ //
223
+ // digitalWrite(pinNumber1, LOW);
224
+ // digitalWrite(pinNumber2, LOW);
225
+ // for (int x = 0 ; x < 100 ; x++)
226
+ // {
227
+ // delay(30);
228
+ // if (Serial.available()) break;
229
+ // }
230
+ // }
231
+ // pinMode(pinNumber1, INPUT);
232
+ // pinMode(pinNumber2, INPUT);
233
+ //
234
+ // Serial.println("Done");
235
+
236
+ beginI2C ();
242
237
243
238
beginBoard (); // Determine what hardware platform we are running on and check on button
244
239
245
- if (i2cBorked == false )
240
+ if (online. i2c == true )
246
241
{
247
242
beginGNSS (); // Connect to GNSS to get module type
248
243
@@ -260,3 +255,72 @@ void loop()
260
255
{
261
256
delay (10 );
262
257
}
258
+
259
+ // Verify the I2C bus is clear of impediments
260
+ void beginI2C ()
261
+ {
262
+ typedef enum
263
+ {
264
+ I2C_CLEAR = 0 ,
265
+ I2C_SCL_GND, // SCL Shorted to Ground
266
+ I2C_SDA_GND, // SDA Shorted to Ground
267
+ I2C_SHORTED, // SDA/SCL Shorted together
268
+ I2C_SCL_VCC, // SCL Shorted to VCC
269
+ I2C_SDA_VCC, // SDA Shorted to VCC
270
+ } I2cState;
271
+ I2cState i2cState = I2C_CLEAR;
272
+
273
+ pinMode (pin_i2c_sda, INPUT_PULLUP);
274
+ pinMode (pin_i2c_scl, INPUT_PULLUP);
275
+
276
+ // Both pins should float high
277
+ if (digitalRead (pin_i2c_scl) == LOW) i2cState = I2C_SCL_GND;
278
+ if (digitalRead (pin_i2c_sda) == LOW) i2cState = I2C_SDA_GND;
279
+
280
+ // SCL should be independant of SDA
281
+ pinMode (pin_i2c_sda, OUTPUT);
282
+ digitalWrite (pin_i2c_sda, LOW);
283
+
284
+ // Skip check if SCL is already shorted to GND
285
+ if (i2cState == I2C_CLEAR)
286
+ if (digitalRead (pin_i2c_scl) == LOW) i2cState = I2C_SHORTED;
287
+
288
+ // SDA should drive low
289
+ if (digitalRead (pin_i2c_sda) == HIGH) i2cState = I2C_SDA_VCC;
290
+
291
+ // SCL should drive low
292
+ pinMode (pin_i2c_scl, OUTPUT);
293
+ digitalWrite (pin_i2c_scl, LOW);
294
+ if (digitalRead (pin_i2c_scl) == HIGH) i2cState = I2C_SCL_VCC;
295
+
296
+ if (i2cState == I2C_CLEAR)
297
+ {
298
+ Wire.begin ();
299
+ online.i2c = true ;
300
+ return ;
301
+ }
302
+
303
+ Serial.print (" I2C Error: " );
304
+ switch (i2cState)
305
+ {
306
+ case (I2C_SCL_GND):
307
+ Serial.print (" SCL shorted to GND" );
308
+ break ;
309
+ case (I2C_SDA_GND):
310
+ Serial.print (" SDA shorted to GND" );
311
+ break ;
312
+ case (I2C_SHORTED):
313
+ Serial.print (" SCL shorted to SDA" );
314
+ break ;
315
+ case (I2C_SDA_VCC):
316
+ Serial.print (" SDA shorted to VCC" );
317
+ break ;
318
+ case (I2C_SCL_VCC):
319
+ Serial.print (" SCL shorted to VCC" );
320
+ break ;
321
+ default :
322
+ Serial.print (" Unknown" );
323
+ break ;
324
+ }
325
+ Serial.println ();
326
+ }
0 commit comments