From 4069f4809cfb3e762db5377cda04d8ea228fcfc2 Mon Sep 17 00:00:00 2001 From: Olexandr Tyshchenko Date: Sun, 8 Nov 2026 23:24:29 +0200 Subject: [PATCH 1/2] NmeaGpsDriver: optional UART baud rate autodetection (GPS_BAUD_AUTODETECT) Bytes arriving on the UART does not mean the baud rate is right: at the wrong rate the parser only sees garbage and never gets a fix. Modules such as the M5Stack GROVE GPS now default to 115200, while boards configure 9600. With GPS_BAUD_AUTODETECT, detect() probes common GNSS rates, starting with the configured one, and keeps the first one that yields an NMEA sentence with a valid checksum. The rate is stored in the driver, so later power cycles reopen the UART with it. Without the flag nothing changes. Ported from #3729 onto the GpsDriver refactor. --- src/helpers/sensors/NmeaGpsDriver.h | 79 +++++++++++++++++++++++++++-- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/src/helpers/sensors/NmeaGpsDriver.h b/src/helpers/sensors/NmeaGpsDriver.h index 25acff455..37cb92b2b 100644 --- a/src/helpers/sensors/NmeaGpsDriver.h +++ b/src/helpers/sensors/NmeaGpsDriver.h @@ -4,6 +4,9 @@ #include #include +#include +#include +#include #ifndef UART_TYPE #ifdef NRF52_PLATFORM @@ -115,6 +118,68 @@ class NmeaGpsDriver : public GpsDriver { _started = true; } +#ifdef GPS_BAUD_AUTODETECT + void reopenSerial(uint32_t baud) { + _hardwareSerial->end(); + if (_txPin != -1 && _rxPin != -1) { + _hardwareSerial->setPins(_txPin, _rxPin); + } + _hardwareSerial->begin(baud); + drain(); + } + + // True if a complete NMEA sentence with a valid checksum arrives within timeout_ms. + bool seesValidNmea(uint32_t timeout_ms) { + char line[100]; + int len = -1; // -1 = waiting for '$' + const uint32_t start = millis(); + while ((uint32_t)(millis() - start) < timeout_ms) { + if (available() <= 0) { delay(2); continue; } + const char c = (char)read(); + if (c == '$') { len = 0; continue; } + if (len < 0) continue; + if (c == '\r' || c == '\n') { + line[len] = '\0'; + const char* star = strchr(line, '*'); + if (star != nullptr && strlen(star) >= 3) { + uint8_t sum = 0; + for (const char* p = line; p < star; p++) sum ^= (uint8_t)*p; + if (sum == (uint8_t)strtoul(star + 1, nullptr, 16)) return true; + } + len = -1; + } else if (len < (int)sizeof(line) - 1) { + line[len++] = c; + } else { + len = -1; // longer than any NMEA sentence: wrong baud rate + } + } + return false; + } + + // Bytes arriving does not mean the baud rate is right: at the wrong rate the + // parser only sees garbage and never gets a fix. Probe common GNSS rates, + // starting with the configured one, and keep the first that yields valid NMEA. + // The rate is kept in _baudRate, so later power cycles reopen the UART with it. + void detectBaudRate() { + static const uint32_t bauds[] = { GPS_BAUD_RATE, 9600, 115200, 38400, 57600, 19200, 4800 }; + const uint32_t configured = _baudRate; + for (size_t i = 0; i < sizeof(bauds) / sizeof(bauds[0]); i++) { + bool tried = false; + for (size_t j = 0; j < i; j++) tried |= (bauds[j] == bauds[i]); + if (tried) continue; + reopenSerial(bauds[i]); + if (seesValidNmea(2000)) { + _baudRate = bauds[i]; + MESH_DEBUG_PRINTLN("GPS baud rate: %lu", (unsigned long)_baudRate); + return; + } + } + _baudRate = configured; + reopenSerial(_baudRate); + MESH_DEBUG_PRINTLN("GPS baud rate not detected, using %lu", (unsigned long)_baudRate); + } +#endif + void stopHardware() { if (!_started) return; @@ -220,14 +285,18 @@ public: } bool detect(uint32_t timeout_ms) override { - if (available() > 0) return true; + bool found = available() > 0; const uint32_t deadline = millis() + timeout_ms; - while ((int32_t)(millis() - deadline) < 0) { - if (available() > 0) return true; - delay(10); + while (!found && (int32_t)(millis() - deadline) < 0) { + found = available() > 0; + if (!found) delay(10); } - return false; +#ifdef GPS_BAUD_AUTODETECT + // only on a real detection (timeout > 0), not on the quick "still active?" polls + if (found && timeout_ms > 0 && _started && _hardwareSerial != nullptr) detectBaudRate(); +#endif + return found; } int available() { From cbfa66df9dfd25d303fbb3517084913ff3d8afbb Mon Sep 17 00:00:00 2001 From: Olexandr Tyshchenko Date: Mon, 9 Nov 2026 22:59:47 +0200 Subject: [PATCH 2/2] NmeaGpsDriver: baud rates to probe come from the board, not a build flag --- src/helpers/sensors/NmeaGpsDriver.h | 60 ++++++++++++++++------------- variants/promicro/target.cpp | 7 +++- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/src/helpers/sensors/NmeaGpsDriver.h b/src/helpers/sensors/NmeaGpsDriver.h index 37cb92b2b..dfe75a4f8 100644 --- a/src/helpers/sensors/NmeaGpsDriver.h +++ b/src/helpers/sensors/NmeaGpsDriver.h @@ -91,6 +91,8 @@ class NmeaGpsDriver : public GpsDriver { int _enableActive = HIGH; int _resetActive = LOW; uint32_t _baudRate = 9600; + const uint32_t* _probeBauds = nullptr; // optional rates to try in detect(), set by the board + uint8_t _probeCount = 0; int _hardwareEnablePin = -1; int _hardwareEnableActive = HIGH; bool _started = false; @@ -118,7 +120,6 @@ class NmeaGpsDriver : public GpsDriver { _started = true; } -#ifdef GPS_BAUD_AUTODETECT void reopenSerial(uint32_t baud) { _hardwareSerial->end(); if (_txPin != -1 && _rxPin != -1) { @@ -157,28 +158,26 @@ class NmeaGpsDriver : public GpsDriver { } // Bytes arriving does not mean the baud rate is right: at the wrong rate the - // parser only sees garbage and never gets a fix. Probe common GNSS rates, - // starting with the configured one, and keep the first that yields valid NMEA. - // The rate is kept in _baudRate, so later power cycles reopen the UART with it. - void detectBaudRate() { - static const uint32_t bauds[] = { GPS_BAUD_RATE, 9600, 115200, 38400, 57600, 19200, 4800 }; - const uint32_t configured = _baudRate; - for (size_t i = 0; i < sizeof(bauds) / sizeof(bauds[0]); i++) { - bool tried = false; - for (size_t j = 0; j < i; j++) tried |= (bauds[j] == bauds[i]); - if (tried) continue; - reopenSerial(bauds[i]); - if (seesValidNmea(2000)) { - _baudRate = bauds[i]; - MESH_DEBUG_PRINTLN("GPS baud rate: %lu", (unsigned long)_baudRate); - return; - } - } - _baudRate = configured; - reopenSerial(_baudRate); - MESH_DEBUG_PRINTLN("GPS baud rate not detected, using %lu", (unsigned long)_baudRate); + // parser only sees garbage and never gets a fix. Try the configured rate, then the + // board's list, and keep the first that yields valid NMEA. The rate is kept in + // _baudRate, so later power cycles reopen the UART with it. + bool tryBaudRate(uint32_t baud) { + reopenSerial(baud); + if (!seesValidNmea(2000)) return false; + _baudRate = baud; + MESH_DEBUG_PRINTLN("GPS baud rate: %lu", (unsigned long)_baudRate); + return true; + } + + void detectBaudRate() { + const uint32_t configured = _baudRate; + if (tryBaudRate(configured)) return; + for (uint8_t i = 0; i < _probeCount; i++) { + if (_probeBauds[i] != configured && tryBaudRate(_probeBauds[i])) return; + } + reopenSerial(configured); + MESH_DEBUG_PRINTLN("GPS baud rate not detected, using %lu", (unsigned long)configured); } -#endif void stopHardware() { if (!_started) return; @@ -199,10 +198,12 @@ public: int enableActive = GPS_EN_ACTIVE, int resetActive = GPS_RESET_ACTIVE, uint32_t baudRate = GPS_BAUD_RATE, int hardwareEnablePin = PIN_GPS_SWITCH, - int hardwareEnableActive = GPS_SWITCH_ACTIVE) + int hardwareEnableActive = GPS_SWITCH_ACTIVE, + const uint32_t* probeBauds = nullptr, uint8_t probeCount = 0) : _hardwareSerial(&serial), _peripheralPower(peripheralPower), _txPin(txPin), _rxPin(rxPin), _resetPin(resetPin), _enablePin(enablePin), _enableActive(enableActive), _resetActive(resetActive), _baudRate(baudRate), + _probeBauds(probeBauds), _probeCount(probeBauds != nullptr ? probeCount : 0), _hardwareEnablePin(hardwareEnablePin), _hardwareEnableActive(hardwareEnableActive) { if (_resetPin != -1) { pinMode(_resetPin, OUTPUT); @@ -217,6 +218,13 @@ public: } } + // Board with a GPS whose baud rate is not known in advance: detect() also tries the + // rates in probeBauds (after the configured one). The array must stay valid (static). + NmeaGpsDriver(UART_TYPE& serial, const uint32_t* probeBauds, uint8_t probeCount) + : NmeaGpsDriver(serial, PIN_GPS_TX, PIN_GPS_RX, GPS_RESET, GPS_EN, nullptr, + GPS_EN_ACTIVE, GPS_RESET_ACTIVE, GPS_BAUD_RATE, PIN_GPS_SWITCH, + GPS_SWITCH_ACTIVE, probeBauds, probeCount) {} + void begin() override { #if !GPS_SWITCH_IS_HARDWARE _requestedOn = true; @@ -292,10 +300,10 @@ public: found = available() > 0; if (!found) delay(10); } -#ifdef GPS_BAUD_AUTODETECT // only on a real detection (timeout > 0), not on the quick "still active?" polls - if (found && timeout_ms > 0 && _started && _hardwareSerial != nullptr) detectBaudRate(); -#endif + if (found && timeout_ms > 0 && _probeCount > 0 && _started && _hardwareSerial != nullptr) { + detectBaudRate(); + } return found; } diff --git a/variants/promicro/target.cpp b/variants/promicro/target.cpp index 5758d9cda..4cdeb6e08 100644 --- a/variants/promicro/target.cpp +++ b/variants/promicro/target.cpp @@ -12,7 +12,12 @@ VolatileRTCClock fallback_clock; AutoDiscoverRTCClock rtc_clock(fallback_clock); #if ENV_INCLUDE_GPS #include - NmeaGpsDriver gps_driver(Serial1); + #ifdef GPS_BAUD_PROBE_LIST // e.g. -D GPS_BAUD_PROBE_LIST=9600,38400,115200 + static const uint32_t gps_probe_bauds[] = { GPS_BAUD_PROBE_LIST }; + NmeaGpsDriver gps_driver(Serial1, gps_probe_bauds, sizeof(gps_probe_bauds) / sizeof(gps_probe_bauds[0])); + #else + NmeaGpsDriver gps_driver(Serial1); + #endif MicroNMEALocationProvider nmea(gps_driver, &rtc_clock); EnvironmentSensorManager sensors = EnvironmentSensorManager(nmea); #else