diff --git a/arch/esp32/esp32.ini b/arch/esp32/esp32.ini index f57ad549d..5853baf76 100644 --- a/arch/esp32/esp32.ini +++ b/arch/esp32/esp32.ini @@ -30,6 +30,7 @@ build_flags = -DCONFIG_BT_NIMBLE_MAX_CCCDS=20 -DCONFIG_BT_NIMBLE_HOST_TASK_STACK_SIZE=5120 -DESP_OPENSSL_SUPPRESS_LEGACY_WARNING + -DSERIAL_BUFFER_SIZE=4096 ;-DDEBUG_HEAP lib_deps = diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index 0e60d149e..85cb046f4 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -174,7 +174,7 @@ bool GPS::setupGPS() #ifdef ARCH_ESP32 // In esp32 framework, setRxBufferSize needs to be initialized before Serial - _serial_gps->setRxBufferSize(2048); // the default is 256 + _serial_gps->setRxBufferSize(SERIAL_BUFFER_SIZE); // the default is 256 #endif // if the overrides are not dialled in, set them from the board definitions, if they exist @@ -834,6 +834,14 @@ void GPS::forceWake(bool on) } } +// clear the GPS rx buffer as quickly as possible +void GPS::clearBuffer() +{ + int x = _serial_gps->available(); + while (x--) + _serial_gps->read(); +} + /// Prepare the GPS for the cpu entering deep or light sleep, expect to be gone for at least 100s of msecs int GPS::prepareSleep(void *unused) { diff --git a/src/gps/GPS.h b/src/gps/GPS.h index 847a60016..89f6c491d 100644 --- a/src/gps/GPS.h +++ b/src/gps/GPS.h @@ -91,6 +91,9 @@ class GPS : private concurrency::OSThread // Some GPS modules (ublock) require factory reset virtual bool factoryReset() { return true; } + // Empty the input buffer as quickly as possible + void clearBuffer(); + protected: /// Do gps chipset specific init, return true for success virtual bool setupGPS(); diff --git a/src/gps/NMEAGPS.cpp b/src/gps/NMEAGPS.cpp index b2cbcfa81..cf08b6be2 100644 --- a/src/gps/NMEAGPS.cpp +++ b/src/gps/NMEAGPS.cpp @@ -253,8 +253,14 @@ bool NMEAGPS::hasFlow() bool NMEAGPS::whileIdle() { bool isValid = false; +#ifdef SERIAL_BUFFER_SIZE + if (_serial_gps->available() >= SERIAL_BUFFER_SIZE - 1) { + LOG_WARN("GPS Buffer full with %u bytes waiting. Flushing to avoid corruption.\n", _serial_gps->available()); + clearBuffer(); + } +#endif // if (_serial_gps->available() > 0) - // LOG_DEBUG("GPS Bytes Waiting: %u\n", _serial_gps->available()); + LOG_DEBUG("GPS Bytes Waiting: %u\n", _serial_gps->available()); // First consume any chars that have piled up at the receiver while (_serial_gps->available() > 0) { int c = _serial_gps->read(); @@ -263,4 +269,4 @@ bool NMEAGPS::whileIdle() } return isValid; -} +} \ No newline at end of file