timekeeper code sanitizations

This commit is contained in:
Klaus K Wilting 2019-02-24 13:47:18 +01:00
parent c02b8bc751
commit 171207af22
6 changed files with 17 additions and 20 deletions

View File

@ -8,9 +8,8 @@
extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available extern RtcDS3231<TwoWire> Rtc; // make RTC instance globally available
int rtc_init(void); uint8_t rtc_init(void);
int set_rtctime(uint32_t t); uint8_t set_rtctime(time_t t);
int set_rtctime(time_t t);
void sync_rtctime(void); void sync_rtctime(void);
time_t get_rtctime(void); time_t get_rtctime(void);
float get_rtctemp(void); float get_rtctemp(void);

View File

@ -13,7 +13,7 @@
#include "dcf77.h" #include "dcf77.h"
#endif #endif
enum timesources { pps, rtc, lora, unsynced }; enum timesources { _gps, _rtc, _lora, _unsynced };
void IRAM_ATTR CLOCKIRQ(void); void IRAM_ATTR CLOCKIRQ(void);
void clock_init(void); void clock_init(void);

View File

@ -472,7 +472,7 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
*pUserUTCTime += requestDelaySec; *pUserUTCTime += requestDelaySec;
// Update system time with time read from the network // Update system time with time read from the network
if (syncTime(*pUserUTCTime, lora)) { // have we got a valid time? if (syncTime(*pUserUTCTime, _lora)) { // have we got a valid time?
ESP_LOGI(TAG, "LORA has set the system time"); ESP_LOGI(TAG, "LORA has set the system time");
} else } else
ESP_LOGI(TAG, "Unable to sync system time with LORA"); ESP_LOGI(TAG, "Unable to sync system time with LORA");

View File

@ -362,7 +362,9 @@ void setup() {
assert(timepulse_init()); // setup timepulse assert(timepulse_init()); // setup timepulse
timepulse_start(); timepulse_start();
time_sync(); // sync time time_sync(); // sync time
#ifdef TIME_SYNC_INTERVAL
setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus() setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus()
#endif
// start wifi in monitor mode and start channel rotation timer // start wifi in monitor mode and start channel rotation timer
ESP_LOGI(TAG, "Starting Wifi..."); ESP_LOGI(TAG, "Starting Wifi...");

View File

@ -8,7 +8,7 @@ static const char TAG[] = "main";
RtcDS3231<TwoWire> Rtc(Wire); // RTC hardware i2c interface RtcDS3231<TwoWire> Rtc(Wire); // RTC hardware i2c interface
// initialize RTC // initialize RTC
int rtc_init(void) { uint8_t rtc_init(void) {
if (I2C_MUTEX_LOCK()) { // block i2c bus access if (I2C_MUTEX_LOCK()) { // block i2c bus access
@ -24,6 +24,7 @@ int rtc_init(void) {
Rtc.SetIsRunning(true); Rtc.SetIsRunning(true);
} }
// If you want to initialize a fresh RTC to compiled time, use this code
/* /*
RtcDateTime tt = Rtc.GetDateTime(); RtcDateTime tt = Rtc.GetDateTime();
time_t t = tt.Epoch32Time(); // sec2000 -> epoch time_t t = tt.Epoch32Time(); // sec2000 -> epoch
@ -45,7 +46,7 @@ int rtc_init(void) {
} // rtc_init() } // rtc_init()
int set_rtctime(time_t t) { // t is UTC in seconds epoch time uint8_t set_rtctime(time_t t) { // t is UTC in seconds epoch time
if (I2C_MUTEX_LOCK()) { if (I2C_MUTEX_LOCK()) {
Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000 Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000
I2C_MUTEX_UNLOCK(); I2C_MUTEX_UNLOCK();
@ -57,11 +58,6 @@ int set_rtctime(time_t t) { // t is UTC in seconds epoch time
} // failure } // failure
} // set_rtctime() } // set_rtctime()
int set_rtctime(uint32_t t) { // t is UTC in seconds epoch time
return set_rtctime(static_cast<time_t>(t));
// set_rtctime()
}
time_t get_rtctime(void) { time_t get_rtctime(void) {
// !! never call now() or delay in this function, this would break this // !! never call now() or delay in this function, this would break this
// function to be used as SyncProvider for Time.h // function to be used as SyncProvider for Time.h

View File

@ -13,13 +13,13 @@ void time_sync() {
return; return;
#ifdef HAS_GPS #ifdef HAS_GPS
if (syncTime(get_gpstime(), pps)) if (syncTime(get_gpstime(), _gps))
return; // attempt sync with GPS time return; // attempt sync with GPS time
#endif #endif
// no GPS -> fallback to RTC time // no GPS -> fallback to RTC time while trying lora sync
#ifdef HAS_RTC #ifdef HAS_RTC
if (!syncTime(get_rtctime(), rtc)) // sync with RTC time if (!syncTime(get_rtctime(), _rtc)) // sync with RTC time
ESP_LOGW(TAG, "no confident RTC time"); ESP_LOGW(TAG, "no confident RTC time");
#endif #endif
@ -40,20 +40,20 @@ uint8_t syncTime(time_t const t, uint8_t const caller) {
if (TimeIsValid(t)) { if (TimeIsValid(t)) {
uint8_t const TimeIsPulseSynced = uint8_t const TimeIsPulseSynced =
wait_for_pulse(); // wait for next 1pps timepulse wait_for_pulse(); // wait for next 1pps timepulse
setTime(t); setTime(t); // sync time and reset timeStatus() to timeSet
adjustTime(1); // forward time to next second adjustTime(1); // forward time to next second
timeSource = timeSetSymbols[caller]; timeSource = timeSetSymbols[caller];
ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d", timeSource, ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d", timeSource,
hour(t), minute(t), second(t)); hour(t), minute(t), second(t));
#ifdef HAS_RTC #ifdef HAS_RTC
if ((TimeIsPulseSynced) && (caller != rtc)) if ((TimeIsPulseSynced) && (caller != _rtc))
set_rtctime(now()); set_rtctime(now());
#endif #endif
return 1; // success return 1; // success
} else { } else {
ESP_LOGD(TAG, "Time source %c sync attempt failed", timeSetSymbols[caller]); ESP_LOGD(TAG, "Time source %c sync attempt failed", timeSetSymbols[caller]);
timeSource = timeSetSymbols[unsynced]; timeSource = timeSetSymbols[_unsynced];
return 0; // failure return 0; // failure
} }
} // syncTime() } // syncTime()