@@ -211,7 +211,7 @@ boolean SFE_UBLOX_GPS::checkUblox()
211
211
// Returns true if new bytes are available
212
212
boolean SFE_UBLOX_GPS::checkUbloxI2C ()
213
213
{
214
- if (millis () - lastCheck >= I2C_POLLING_WAIT_MS )
214
+ if (millis () - lastCheck >= i2cPollingWait )
215
215
{
216
216
// Get the number of bytes available from the module
217
217
uint16_t bytesAvailable = 0 ;
@@ -241,6 +241,31 @@ boolean SFE_UBLOX_GPS::checkUbloxI2C()
241
241
return (false );
242
242
}
243
243
244
+ // Check for bit error
245
+ // This error is rare but if we incorrectly interpret the first bit of the two 'data available' bytes as 1
246
+ // then we have far too many bytes to check
247
+ // Correct back down to
248
+ if (bytesAvailable & ((uint16_t )1 << 15 ))
249
+ {
250
+ // Clear the MSbit
251
+ bytesAvailable &= ~((uint16_t )1 << 15 );
252
+
253
+ if (_printDebug == true )
254
+ {
255
+ _debugSerial->print (" Bytes available error:" );
256
+ _debugSerial->println (bytesAvailable);
257
+ }
258
+ }
259
+
260
+ if (bytesAvailable > 100 )
261
+ {
262
+ if (_printDebug == true )
263
+ {
264
+ _debugSerial->print (" Bytes available:" );
265
+ _debugSerial->println (bytesAvailable);
266
+ }
267
+ }
268
+
244
269
while (bytesAvailable)
245
270
{
246
271
_i2cPort->beginTransmission (_gpsI2Caddress);
@@ -303,16 +328,6 @@ boolean SFE_UBLOX_GPS::checkUbloxSerial()
303
328
// Take a given byte and file it into the proper array
304
329
void SFE_UBLOX_GPS::process (uint8_t incoming)
305
330
{
306
-
307
- if (_printDebug == true )
308
- {
309
- // if (currentSentence == NONE && incoming == 0xB5) //UBX binary frames start with 0xB5, aka μ
310
- // _debugSerial->println(); //Show new packet start
311
-
312
- // _debugSerial->print(" ");
313
- // _debugSerial->print(incoming, HEX);
314
- }
315
-
316
331
if (currentSentence == NONE || currentSentence == NMEA)
317
332
{
318
333
if (incoming == 0xB5 ) // UBX binary frames start with 0xB5, aka μ
@@ -510,15 +525,15 @@ void SFE_UBLOX_GPS::processUBX(uint8_t incoming, ubxPacket *incomingUBX)
510
525
debugPrintln (" Checksum failed. Response too big?" );
511
526
512
527
// Drive an external pin to allow for easier logic analyzation
513
- // digitalWrite(2, LOW);
514
- // delay(10);
515
- // digitalWrite(2, HIGH);
516
-
517
- _debugSerial->print (" Received: " );
518
- printPacket (incomingUBX);
528
+ digitalWrite (2 , LOW);
529
+ delay (10 );
530
+ digitalWrite (2 , HIGH);
519
531
520
532
_debugSerial->print (" Size: " );
521
533
_debugSerial->print (incomingUBX->len );
534
+ _debugSerial->print (" Received: " );
535
+ printPacket (incomingUBX);
536
+
522
537
_debugSerial->print (" checksumA: " );
523
538
_debugSerial->print (incomingUBX->checksumA );
524
539
_debugSerial->print (" checksumB: " );
@@ -891,7 +906,7 @@ boolean SFE_UBLOX_GPS::waitForResponse(uint8_t requestedClass, uint8_t requested
891
906
}
892
907
}
893
908
894
- delay ( 1 );
909
+ delayMicroseconds ( 500 );
895
910
}
896
911
897
912
debugPrintln (" waitForResponse timeout" );
@@ -1251,6 +1266,9 @@ boolean SFE_UBLOX_GPS::setNavigationFrequency(uint8_t navFreq, uint16_t maxWait)
1251
1266
{
1252
1267
// if(updateRate > 40) updateRate = 40; //Not needed: module will correct out of bounds values
1253
1268
1269
+ // Adjust the I2C polling timeout based on update rate
1270
+ i2cPollingWait = 1000 / (navFreq * 4 ); // This is the number of ms to wait between checks for new I2C data
1271
+
1254
1272
// Query the module for the latest lat/long
1255
1273
packetCfg.cls = UBX_CLASS_CFG;
1256
1274
packetCfg.id = UBX_CFG_RATE;
0 commit comments