firmware: auto-detect GPS UART baud
This commit is contained in:
@@ -3,12 +3,48 @@
|
||||
#include "sensor_node_config.h"
|
||||
namespace { TinyGPSPlus parser; }
|
||||
|
||||
void initGps(HardwareSerial& serial) { serial.begin(GPS_BAUD, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN); }
|
||||
namespace {
|
||||
uint32_t activeBaud = GPS_FALLBACK_BAUD;
|
||||
|
||||
bool baudHasValidNmea(HardwareSerial& serial, uint32_t baud) {
|
||||
TinyGPSPlus probe;
|
||||
serial.end();
|
||||
serial.begin(baud, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
|
||||
unsigned long started = millis();
|
||||
while (millis() - started < GPS_BAUD_DETECT_MS) {
|
||||
while (serial.available()) probe.encode(serial.read());
|
||||
if (probe.passedChecksum() > 0) return true;
|
||||
delay(1);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void initGps(HardwareSerial& serial) {
|
||||
const uint32_t candidates[] = {9600, 38400, 115200, 4800};
|
||||
Serial.println("Detecting GPS UART baud...");
|
||||
for (uint32_t baud : candidates) {
|
||||
Serial.print("Trying GPS baud ");
|
||||
Serial.println(baud);
|
||||
if (!baudHasValidNmea(serial, baud)) continue;
|
||||
activeBaud = baud;
|
||||
Serial.print("Valid GPS NMEA detected at ");
|
||||
Serial.println(activeBaud);
|
||||
return;
|
||||
}
|
||||
|
||||
activeBaud = GPS_FALLBACK_BAUD;
|
||||
serial.end();
|
||||
serial.begin(activeBaud, SERIAL_8N1, GPS_RX_PIN, GPS_TX_PIN);
|
||||
Serial.println("No valid GPS NMEA detected; falling back to 9600 baud");
|
||||
}
|
||||
|
||||
void updateGps(HardwareSerial& serial, GpsState& s) {
|
||||
while (serial.available()) { s.lastByteMs = millis(); parser.encode(serial.read()); }
|
||||
s.charactersProcessed = parser.charsProcessed();
|
||||
s.sentencesPassed = parser.passedChecksum();
|
||||
s.sentencesFailed = parser.failedChecksum();
|
||||
s.baud = activeBaud;
|
||||
s.online = s.lastByteMs && millis() - s.lastByteMs <= SENSOR_STALE_MS;
|
||||
s.fix = parser.location.isValid() && parser.location.age() <= SENSOR_STALE_MS;
|
||||
s.satellites = parser.satellites.isValid() ? parser.satellites.value() : 0;
|
||||
|
||||
Reference in New Issue
Block a user