2020-10-07 23:28:57 +00:00
|
|
|
#include "RTC.h"
|
2020-10-07 23:46:20 +00:00
|
|
|
#include "configuration.h"
|
2022-04-27 09:05:08 +00:00
|
|
|
#include "main.h"
|
2020-10-07 23:28:57 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2020-10-07 23:46:20 +00:00
|
|
|
static RTCQuality currentQuality = RTCQualityNone;
|
|
|
|
|
|
|
|
RTCQuality getRTCQuality()
|
|
|
|
{
|
|
|
|
return currentQuality;
|
|
|
|
}
|
2020-10-07 23:28:57 +00:00
|
|
|
|
|
|
|
// 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*/
|
2022-04-27 09:05:08 +00:00
|
|
|
#ifdef RV3028_RTC
|
|
|
|
if(rtc_found == RV3028_RTC) {
|
|
|
|
uint32_t now = millis();
|
|
|
|
Melopero_RV3028 rtc;
|
|
|
|
rtc.initI2C();
|
|
|
|
tm t;
|
|
|
|
t.tm_year = rtc.getYear() - 1900;
|
|
|
|
t.tm_mon = rtc.getMonth() - 1;
|
|
|
|
t.tm_mday = rtc.getDate();
|
|
|
|
t.tm_hour = rtc.getHour();
|
|
|
|
t.tm_min = rtc.getMinute();
|
|
|
|
t.tm_sec = rtc.getSecond();
|
|
|
|
tv.tv_sec = mktime(&t);
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
DEBUG_MSG("Read RTC time from RV3028 as %ld\n", tv.tv_sec);
|
|
|
|
timeStartMsec = now;
|
|
|
|
zeroOffsetSecs = tv.tv_sec;
|
|
|
|
if (currentQuality == RTCQualityNone) {
|
|
|
|
currentQuality = RTCQualityDevice;
|
|
|
|
}
|
|
|
|
}
|
2022-04-28 06:18:03 +00:00
|
|
|
#elif defined(PCF8563_RTC)
|
|
|
|
if(rtc_found == PCF8563_RTC) {
|
|
|
|
uint32_t now = millis();
|
|
|
|
PCF8563_Class rtc;
|
2022-11-12 07:12:53 +00:00
|
|
|
#ifdef RTC_USE_WIRE1
|
|
|
|
rtc.begin(Wire1);
|
|
|
|
#else
|
2022-04-28 06:18:03 +00:00
|
|
|
rtc.begin();
|
2022-11-12 07:12:53 +00:00
|
|
|
#endif
|
2022-04-28 06:18:03 +00:00
|
|
|
auto tc = rtc.getDateTime();
|
|
|
|
tm t;
|
2022-09-28 18:13:41 +00:00
|
|
|
t.tm_year = tc.year - 1900;
|
|
|
|
t.tm_mon = tc.month - 1;
|
2022-04-28 06:18:03 +00:00
|
|
|
t.tm_mday = tc.day;
|
|
|
|
t.tm_hour = tc.hour;
|
|
|
|
t.tm_min = tc.minute;
|
|
|
|
t.tm_sec = tc.second;
|
|
|
|
tv.tv_sec = mktime(&t);
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
DEBUG_MSG("Read RTC time from PCF8563 as %ld\n", tv.tv_sec);
|
|
|
|
timeStartMsec = now;
|
|
|
|
zeroOffsetSecs = tv.tv_sec;
|
|
|
|
if (currentQuality == RTCQualityNone) {
|
|
|
|
currentQuality = RTCQualityDevice;
|
|
|
|
}
|
|
|
|
}
|
2022-04-27 09:05:08 +00:00
|
|
|
#else
|
2020-10-07 23:28:57 +00:00
|
|
|
if (!gettimeofday(&tv, NULL)) {
|
|
|
|
uint32_t now = millis();
|
2022-04-27 09:05:08 +00:00
|
|
|
DEBUG_MSG("Read RTC time as %ld\n", tv.tv_sec);
|
2020-10-07 23:28:57 +00:00
|
|
|
timeStartMsec = now;
|
|
|
|
zeroOffsetSecs = tv.tv_sec;
|
|
|
|
}
|
2022-04-27 09:05:08 +00:00
|
|
|
#endif
|
2020-10-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// If we haven't yet set our RTC this boot, set it from a GPS derived time
|
2020-10-07 23:46:20 +00:00
|
|
|
bool perhapsSetRTC(RTCQuality q, const struct timeval *tv)
|
2020-10-07 23:28:57 +00:00
|
|
|
{
|
2020-12-27 04:54:44 +00:00
|
|
|
static uint32_t lastSetMsec = 0;
|
|
|
|
uint32_t now = millis();
|
|
|
|
|
|
|
|
bool shouldSet;
|
2020-10-07 23:46:20 +00:00
|
|
|
if (q > currentQuality) {
|
2020-12-31 05:46:43 +00:00
|
|
|
currentQuality = q;
|
2020-12-27 04:54:44 +00:00
|
|
|
shouldSet = true;
|
|
|
|
DEBUG_MSG("Upgrading time to RTC %ld secs (quality %d)\n", tv->tv_sec, q);
|
2021-03-30 15:11:33 +00:00
|
|
|
} else if(q == RTCQualityGPS && (now - lastSetMsec) > (12 * 60 * 60 * 1000UL)) {
|
2021-03-20 00:21:08 +00:00
|
|
|
// Every 12 hrs we will slam in a new GPS time, to correct for local RTC clock drift
|
2020-12-27 04:54:44 +00:00
|
|
|
shouldSet = true;
|
2021-03-15 02:00:20 +00:00
|
|
|
DEBUG_MSG("Reapplying external time to correct clock drift %ld secs\n", tv->tv_sec);
|
2020-12-27 04:54:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
shouldSet = false;
|
|
|
|
|
|
|
|
if (shouldSet) {
|
2021-05-03 01:30:03 +00:00
|
|
|
lastSetMsec = now;
|
|
|
|
|
|
|
|
// This delta value works on all platforms
|
|
|
|
timeStartMsec = now;
|
|
|
|
zeroOffsetSecs = tv->tv_sec;
|
|
|
|
|
|
|
|
// If this platform has a setable RTC, set it
|
2022-04-27 09:05:08 +00:00
|
|
|
#ifdef RV3028_RTC
|
|
|
|
if(rtc_found == RV3028_RTC) {
|
|
|
|
Melopero_RV3028 rtc;
|
|
|
|
rtc.initI2C();
|
|
|
|
tm *t = localtime(&tv->tv_sec);
|
|
|
|
rtc.setTime(t->tm_year + 1900, t->tm_mon + 1, t->tm_wday, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
|
|
|
|
DEBUG_MSG("RV3028_RTC setTime %02d-%02d-%02d %02d:%02d:%02d %ld\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, tv->tv_sec);
|
|
|
|
}
|
2022-04-28 06:18:03 +00:00
|
|
|
#elif defined(PCF8563_RTC)
|
|
|
|
if(rtc_found == PCF8563_RTC) {
|
|
|
|
PCF8563_Class rtc;
|
2022-11-12 07:12:53 +00:00
|
|
|
#ifdef RTC_USE_WIRE1
|
|
|
|
rtc.begin(Wire1);
|
|
|
|
#else
|
|
|
|
rtc.begin();
|
|
|
|
#endif
|
2022-04-28 06:18:03 +00:00
|
|
|
tm *t = localtime(&tv->tv_sec);
|
2022-09-28 18:13:41 +00:00
|
|
|
rtc.setDateTime(t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec);
|
2022-04-28 06:18:03 +00:00
|
|
|
DEBUG_MSG("PCF8563_RTC setDateTime %02d-%02d-%02d %02d:%02d:%02d %ld\n", t->tm_year + 1900, t->tm_mon + 1, t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, tv->tv_sec);
|
|
|
|
}
|
2022-07-31 12:11:47 +00:00
|
|
|
#elif defined(ARCH_ESP32)
|
2020-10-07 23:28:57 +00:00
|
|
|
settimeofday(tv, NULL);
|
2021-05-03 01:30:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// nrf52 doesn't have a readable RTC (yet - software not written)
|
2022-07-31 12:11:47 +00:00
|
|
|
#ifdef HAS_RTC
|
2020-10-07 23:28:57 +00:00
|
|
|
readFromRTC();
|
2021-03-15 02:00:20 +00:00
|
|
|
#endif
|
2021-05-03 01:30:03 +00:00
|
|
|
|
2020-10-07 23:28:57 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-07 23:46:20 +00:00
|
|
|
bool perhapsSetRTC(RTCQuality q, struct tm &t)
|
2020-10-07 23:28:57 +00:00
|
|
|
{
|
|
|
|
/* 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 {
|
2020-10-07 23:46:20 +00:00
|
|
|
return perhapsSetRTC(q, &tv);
|
2020-10-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getTime()
|
|
|
|
{
|
2021-05-03 01:30:03 +00:00
|
|
|
return (((uint32_t) millis() - timeStartMsec) / 1000) + zeroOffsetSecs;
|
2020-10-07 23:28:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 02:01:13 +00:00
|
|
|
uint32_t getValidTime(RTCQuality minQuality)
|
2020-10-07 23:28:57 +00:00
|
|
|
{
|
2020-10-09 02:01:13 +00:00
|
|
|
return (currentQuality >= minQuality) ? getTime() : 0;
|
2020-10-07 23:28:57 +00:00
|
|
|
}
|