Skip to content

Commit 4f12dc0

Browse files
authored
Merge pull request #2937 from benskigomez/fix/gps-timesync-millis-overflow
sensors: fix millis() rollover that stalls GPS time-sync on long-uptime nodes
2 parents 1c5a842 + 63731d3 commit 4f12dc0

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

src/helpers/sensors/EnvironmentSensorManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,11 +889,11 @@ void EnvironmentSensorManager::stop_gps() {
889889
void EnvironmentSensorManager::loop() {
890890

891891
#if ENV_INCLUDE_GPS
892-
static long next_gps_update = 0;
892+
static unsigned long next_gps_update = 0;
893893
if (gps_active) {
894894
_location->loop();
895895
}
896-
if (millis() > next_gps_update) {
896+
if ((long)(millis() - next_gps_update) > 0) {
897897

898898
if(gps_active){
899899
#ifdef RAK_WISBLOCK_GPS

src/helpers/sensors/MicroNMEALocationProvider.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class MicroNMEALocationProvider : public LocationProvider {
4242
int8_t _claims = 0;
4343
int _pin_reset;
4444
int _pin_en;
45-
long next_check = 0;
45+
unsigned long next_check = 0;
4646
long time_valid = 0;
4747
unsigned long _last_time_sync = 0;
4848
static const unsigned long TIME_SYNC_INTERVAL = 1800000; // Re-sync every 30 minutes
4949

5050
public :
5151
MicroNMEALocationProvider(Stream& ser, mesh::RTCClock* clock = NULL, int pin_reset = GPS_RESET, int pin_en = GPS_EN,RefCountedDigitalPin* peripher_power=NULL) :
52-
_gps_serial(&ser), nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _pin_reset(pin_reset), _pin_en(pin_en), _clock(clock), _peripher_power(peripher_power) {
52+
nmea(_nmeaBuffer, sizeof(_nmeaBuffer)), _clock(clock), _gps_serial(&ser), _peripher_power(peripher_power), _pin_reset(pin_reset), _pin_en(pin_en) {
5353
if (_pin_reset != -1) {
5454
pinMode(_pin_reset, OUTPUT);
5555
digitalWrite(_pin_reset, GPS_RESET_FORCE);
@@ -62,9 +62,7 @@ public :
6262

6363
void claim() {
6464
_claims++;
65-
if (_claims > 0) {
66-
if (_peripher_power) _peripher_power->claim();
67-
}
65+
if (_peripher_power) _peripher_power->claim();
6866
}
6967

7068
void release() {
@@ -143,7 +141,7 @@ public :
143141

144142
if (!isValid()) time_valid = 0;
145143

146-
if (millis() > next_check) {
144+
if ((long)(millis() - next_check) > 0) {
147145
next_check = millis() + 1000;
148146
// Re-enable time sync periodically when GPS has valid fix
149147
if (!_time_sync_needed && _clock != NULL && (millis() - _last_time_sync) > TIME_SYNC_INTERVAL) {

0 commit comments

Comments
 (0)