diff --git a/docs/software/gps-todo.txt b/docs/software/gps-todo.txt index f22e53f9f..9612da317 100644 --- a/docs/software/gps-todo.txt +++ b/docs/software/gps-todo.txt @@ -6,8 +6,8 @@ bin/run.sh --set region 8 time only mode ./bin/run.sh --set gps_operation 3 -allow locking while cpu sleeps -test with crummy antenna +set time provisionally from net even if we have gps + ublox parsing failure record power measurements and update spreadsheet diff --git a/src/gps/GPS.cpp b/src/gps/GPS.cpp index 29a6179dd..f39772d30 100644 --- a/src/gps/GPS.cpp +++ b/src/gps/GPS.cpp @@ -4,7 +4,7 @@ #include "configuration.h" #include "sleep.h" #include -#include +#include "RTC.h" // If we have a serial GPS port it will not be null #ifdef GPS_RX_PIN @@ -23,75 +23,9 @@ uint8_t GPS::i2cAddress = GPS_I2C_ADDRESS; uint8_t GPS::i2cAddress = 0; #endif -bool timeSetFromGPS; // We try to set our time from GPS each time we wake from sleep GPS *gps; -// stuff that really should be in in the instance instead... -static uint32_t - timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time -static uint64_t zeroOffsetSecs; // GPS based time in secs since 1970 - only updated once on initial lock - -void readFromRTC() -{ - struct timeval tv; /* btw settimeofday() is helpfull here too*/ - - if (!gettimeofday(&tv, NULL)) { - uint32_t now = millis(); - - DEBUG_MSG("Read RTC time as %ld (cur millis %u) valid=%d\n", tv.tv_sec, now, timeSetFromGPS); - timeStartMsec = now; - zeroOffsetSecs = tv.tv_sec; - } -} - -/// If we haven't yet set our RTC this boot, set it from a GPS derived time -bool perhapsSetRTC(const struct timeval *tv) -{ - if (!timeSetFromGPS) { - timeSetFromGPS = true; - DEBUG_MSG("Setting RTC %ld secs\n", tv->tv_sec); -#ifndef NO_ESP32 - settimeofday(tv, NULL); -#else - DEBUG_MSG("ERROR TIME SETTING NOT IMPLEMENTED!\n"); -#endif - readFromRTC(); - return true; - } else { - return false; - } -} - -bool perhapsSetRTC(struct tm &t) -{ - /* Convert to unix time - The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 - (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). - */ - time_t res = mktime(&t); - struct timeval tv; - tv.tv_sec = res; - tv.tv_usec = 0; // time.centisecond() * (10 / 1000); - - // DEBUG_MSG("Got time from GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec); - if (t.tm_year < 0 || t.tm_year >= 300) { - // DEBUG_MSG("Ignoring invalid GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec); - return false; - } else { - return perhapsSetRTC(&tv); - } -} - -uint32_t getTime() -{ - return ((millis() - timeStartMsec) / 1000) + zeroOffsetSecs; -} - -uint32_t getValidTime() -{ - return timeSetFromGPS ? getTime() : 0; -} bool GPS::setup() { diff --git a/src/gps/GPS.h b/src/gps/GPS.h index 1f8429a44..3c0b0c480 100644 --- a/src/gps/GPS.h +++ b/src/gps/GPS.h @@ -1,25 +1,11 @@ #pragma once -#include "../concurrency/PeriodicTask.h" #include "GPSStatus.h" #include "Observer.h" -#include "sys/time.h" - -/// If we haven't yet set our RTC this boot, set it from a GPS derived time -bool perhapsSetRTC(const struct timeval *tv); -bool perhapsSetRTC(struct tm &t); // Generate a string representation of DOP const char *getDOPString(uint32_t dop); -/// Return time since 1970 in secs. Until we have a GPS lock we will be returning time based at zero -uint32_t getTime(); - -/// Return time since 1970 in secs. If we don't have a GPS lock return zero -uint32_t getValidTime(); - -void readFromRTC(); - /** * A gps class that only reads from the GPS periodically (and FIXME - eventually keeps the gps powered down except when reading) * diff --git a/src/gps/NMEAGPS.cpp b/src/gps/NMEAGPS.cpp index 1b6bb1b35..41b1d4768 100644 --- a/src/gps/NMEAGPS.cpp +++ b/src/gps/NMEAGPS.cpp @@ -1,5 +1,6 @@ #include "NMEAGPS.h" #include "configuration.h" +#include "RTC.h" static int32_t toDegInt(RawDegrees d) { diff --git a/src/gps/RTC.cpp b/src/gps/RTC.cpp new file mode 100644 index 000000000..de55ebdbc --- /dev/null +++ b/src/gps/RTC.cpp @@ -0,0 +1,72 @@ +#include "configuration.h" +#include "RTC.h" +#include +#include + +bool timeSetFromGPS; // We try to set our time from GPS each time we wake from sleep + +// stuff that really should be in in the instance instead... +static uint32_t + timeStartMsec; // Once we have a GPS lock, this is where we hold the initial msec clock that corresponds to that time +static uint64_t zeroOffsetSecs; // GPS based time in secs since 1970 - only updated once on initial lock + +void readFromRTC() +{ + struct timeval tv; /* btw settimeofday() is helpfull here too*/ + + if (!gettimeofday(&tv, NULL)) { + uint32_t now = millis(); + + DEBUG_MSG("Read RTC time as %ld (cur millis %u) valid=%d\n", tv.tv_sec, now, timeSetFromGPS); + timeStartMsec = now; + zeroOffsetSecs = tv.tv_sec; + } +} + +/// If we haven't yet set our RTC this boot, set it from a GPS derived time +bool perhapsSetRTC(const struct timeval *tv) +{ + if (!timeSetFromGPS) { + timeSetFromGPS = true; + DEBUG_MSG("Setting RTC %ld secs\n", tv->tv_sec); +#ifndef NO_ESP32 + settimeofday(tv, NULL); +#else + DEBUG_MSG("ERROR TIME SETTING NOT IMPLEMENTED!\n"); +#endif + readFromRTC(); + return true; + } else { + return false; + } +} + +bool perhapsSetRTC(struct tm &t) +{ + /* Convert to unix time + The Unix epoch (or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 + (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). + */ + time_t res = mktime(&t); + struct timeval tv; + tv.tv_sec = res; + tv.tv_usec = 0; // time.centisecond() * (10 / 1000); + + // DEBUG_MSG("Got time from GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec); + if (t.tm_year < 0 || t.tm_year >= 300) { + // DEBUG_MSG("Ignoring invalid GPS month=%d, year=%d, unixtime=%ld\n", t.tm_mon, t.tm_year, tv.tv_sec); + return false; + } else { + return perhapsSetRTC(&tv); + } +} + +uint32_t getTime() +{ + return ((millis() - timeStartMsec) / 1000) + zeroOffsetSecs; +} + +uint32_t getValidTime() +{ + return timeSetFromGPS ? getTime() : 0; +} diff --git a/src/gps/RTC.h b/src/gps/RTC.h new file mode 100644 index 000000000..869de4b29 --- /dev/null +++ b/src/gps/RTC.h @@ -0,0 +1,19 @@ +#pragma once + +#include "configuration.h" +#include "sys/time.h" +#include + +extern bool timeSetFromGPS; // We try to set our time from GPS each time we wake from sleep + +/// If we haven't yet set our RTC this boot, set it from a GPS derived time +bool perhapsSetRTC(const struct timeval *tv); +bool perhapsSetRTC(struct tm &t); + +/// Return time since 1970 in secs. Until we have a GPS lock we will be returning time based at zero +uint32_t getTime(); + +/// Return time since 1970 in secs. If we don't have a GPS lock return zero +uint32_t getValidTime(); + +void readFromRTC(); \ No newline at end of file diff --git a/src/gps/UBloxGPS.cpp b/src/gps/UBloxGPS.cpp index f59b3bf03..36541719d 100644 --- a/src/gps/UBloxGPS.cpp +++ b/src/gps/UBloxGPS.cpp @@ -1,4 +1,5 @@ #include "UBloxGPS.h" +#include "RTC.h" #include "error.h" #include "sleep.h" #include diff --git a/src/main.cpp b/src/main.cpp index a6affea1f..1e1f1e014 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -34,6 +34,7 @@ // #include "rom/rtc.h" #include "DSRRouter.h" // #include "debug.h" +#include "RTC.h" #include "SPILock.h" #include "graphics/Screen.h" #include "main.h" diff --git a/src/mesh/MeshService.cpp b/src/mesh/MeshService.cpp index b514b0c5b..5201fcd91 100644 --- a/src/mesh/MeshService.cpp +++ b/src/mesh/MeshService.cpp @@ -10,6 +10,7 @@ #include "MeshService.h" #include "NodeDB.h" #include "PowerFSM.h" +#include "RTC.h" #include "main.h" #include "mesh-pb-constants.h" #include "power.h" diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 349e7e67d..1224ae6dd 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -10,6 +10,7 @@ #include "NodeDB.h" #include "PacketHistory.h" #include "PowerFSM.h" +#include "RTC.h" #include "Router.h" #include "configuration.h" #include "error.h" diff --git a/src/mesh/Router.cpp b/src/mesh/Router.cpp index 74bb21960..6a3382000 100644 --- a/src/mesh/Router.cpp +++ b/src/mesh/Router.cpp @@ -1,6 +1,6 @@ #include "Router.h" #include "CryptoEngine.h" -#include "GPS.h" +#include "RTC.h" #include "configuration.h" #include "mesh-pb-constants.h" #include