timekeeper changes (experimental)

This commit is contained in:
Klaus K Wilting 2019-03-02 13:32:02 +01:00
parent 19ffae05e6
commit d2530ba91a
8 changed files with 34 additions and 28 deletions

View File

@ -3,6 +3,7 @@
#include "globals.h" #include "globals.h"
#include "rtctime.h" #include "rtctime.h"
#include "TimeLib.h"
#ifdef HAS_GPS #ifdef HAS_GPS
#include "gpsread.h" #include "gpsread.h"
@ -18,11 +19,10 @@ extern const char timeSetSymbols[];
void IRAM_ATTR CLOCKIRQ(void); void IRAM_ATTR CLOCKIRQ(void);
void clock_init(void); void clock_init(void);
void clock_loop(void *pvParameters); void clock_loop(void *pvParameters);
void timeSync(void);
void timepulse_start(void); void timepulse_start(void);
uint8_t timepulse_init(void); uint8_t timepulse_init(void);
time_t TimeIsValid(time_t const t); time_t timeIsValid(time_t const t);
time_t syncProvider_CB(void); time_t timeProvider(void);
time_t compiledUTC(void); time_t compiledUTC(void);
time_t tmConvert(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh, uint8_t mm, time_t tmConvert(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh, uint8_t mm,
uint8_t ss); uint8_t ss);

1
lib/microTime Submodule

@ -0,0 +1 @@
Subproject commit 6d5b82c554590f49864cc44ce295ec91dcf1114e

View File

@ -100,7 +100,7 @@ time_t get_gpstime(void) {
t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(), t = tmConvert(gps.date.year(), gps.date.month(), gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second()); gps.time.hour(), gps.time.minute(), gps.time.second());
} }
return TimeIsValid(t); return timeIsValid(t);
} // get_gpstime() } // get_gpstime()
// GPS serial feed FreeRTos Task // GPS serial feed FreeRTos Task

View File

@ -32,7 +32,6 @@ void irqHandler(void *pvParameters) {
// are cyclic tasks due? // are cyclic tasks due?
if (InterruptStatus & CYCLIC_IRQ) { if (InterruptStatus & CYCLIC_IRQ) {
doHousekeeping(); doHousekeeping();
timeSync();
} }
// is time to send the payload? // is time to send the payload?

View File

@ -464,7 +464,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 (TimeIsValid(*pUserUTCTime)) { if (timeIsValid(*pUserUTCTime)) {
xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // wait for pps xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // wait for pps
setTime(*pUserUTCTime + 1); setTime(*pUserUTCTime + 1);
timeSource = _lora; timeSource = _lora;

View File

@ -414,9 +414,12 @@ void setup() {
#endif #endif
#endif // HAS_BUTTON #endif // HAS_BUTTON
// set time source
setSyncInterval(TIME_SYNC_INTERVAL * 60);
setSyncProvider(&timeProvider);
#if defined HAS_IF482 || defined HAS_DCF77 #if defined HAS_IF482 || defined HAS_DCF77
ESP_LOGI(TAG, "Starting Clock Controller..."); ESP_LOGI(TAG, "Starting Clock Controller...");
timeSync();
clock_init(); clock_init();
#endif #endif

View File

@ -29,7 +29,7 @@ uint8_t rtc_init(void) {
RtcDateTime tt = Rtc.GetDateTime(); RtcDateTime tt = Rtc.GetDateTime();
time_t t = tt.Epoch32Time(); // sec2000 -> epoch time_t t = tt.Epoch32Time(); // sec2000 -> epoch
if (!Rtc.IsDateTimeValid() || !TimeIsValid(t)) { if (!Rtc.IsDateTimeValid() || !timeIsValid(t)) {
ESP_LOGW(TAG, "RTC has no recent time, setting to compilation date"); ESP_LOGW(TAG, "RTC has no recent time, setting to compilation date");
Rtc.SetDateTime( Rtc.SetDateTime(
RtcDateTime(compiledUTC() - SECS_YR_2000)); // epoch -> sec2000 RtcDateTime(compiledUTC() - SECS_YR_2000)); // epoch -> sec2000
@ -67,7 +67,7 @@ time_t get_rtctime(void) {
} }
I2C_MUTEX_UNLOCK(); I2C_MUTEX_UNLOCK();
} }
return TimeIsValid(t); return timeIsValid(t);
} // get_rtctime() } // get_rtctime()
float get_rtctemp(void) { float get_rtctemp(void) {

View File

@ -8,24 +8,22 @@ const char timeSetSymbols[] = {'G', 'R', 'L', '?'};
getExternalTime TimeSourcePtr; // pointer to time source function getExternalTime TimeSourcePtr; // pointer to time source function
// syncs systime from external time source and sets/reads RTC, called by time_t timeProvider(void) {
// cyclic.cpp
void timeSync(void) { ESP_LOGD(TAG, "time synched");
time_t t = 0; time_t t = 0;
#ifdef HAS_GPS #ifdef HAS_GPS
xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1100)); // wait for pps // xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1100)); // wait for pps
t = get_gpstime(); // fetch recent time from last NEMA record t = get_gpstime(); // fetch recent time from last NEMA record
if (t) { if (t) {
t++; // last NMEA record concerns past second, so we add one // t++; // last NMEA record concerns past second, so we add one
ESP_LOGD(TAG, "millis: %d, second: %d", millis(), second(t));
setTime(t);
#ifdef HAS_RTC #ifdef HAS_RTC
set_rtctime(t); // calibrate RTC set_rtctime(t); // calibrate RTC
#endif #endif
timeSource = _gps; timeSource = _gps;
return; return t;
} }
#endif #endif
@ -33,12 +31,11 @@ void timeSync(void) {
#ifdef HAS_RTC #ifdef HAS_RTC
t = get_rtctime(); t = get_rtctime();
if (t) { if (t) {
setTime(t);
timeSource = _rtc; timeSource = _rtc;
} }
#endif #endif
// try lora sync if we have // kick off asychron lora sync if we have
#if defined HAS_LORA && defined TIME_SYNC_LORA #if defined HAS_LORA && defined TIME_SYNC_LORA
LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime); LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
#endif #endif
@ -46,7 +43,9 @@ void timeSync(void) {
if (!t) if (!t)
timeSource = _unsynced; timeSource = _unsynced;
} // timeSync() return t;
} // timeProvider()
// helper function to setup a pulse per second for time synchronisation // helper function to setup a pulse per second for time synchronisation
uint8_t timepulse_init() { uint8_t timepulse_init() {
@ -103,6 +102,8 @@ void timepulse_start(void) {
// interrupt service routine triggered by either pps or esp32 hardware timer // interrupt service routine triggered by either pps or esp32 hardware timer
void IRAM_ATTR CLOCKIRQ(void) { void IRAM_ATTR CLOCKIRQ(void) {
SyncToPPS(); // calibrate systime from Time.h
if (ClockTask != NULL) if (ClockTask != NULL)
xTaskNotifyFromISR(ClockTask, uint32_t(now()), eSetBits, NULL); xTaskNotifyFromISR(ClockTask, uint32_t(now()), eSetBits, NULL);
@ -115,15 +116,15 @@ void IRAM_ATTR CLOCKIRQ(void) {
} }
// helper function to check plausibility of a time // helper function to check plausibility of a time
time_t TimeIsValid(time_t const t) { time_t timeIsValid(time_t const t) {
// is it a time in the past? we use compile date to guess // is it a time in the past? we use compile date to guess
return (t >= compiledUTC() ? t : 0); return (t >= compiledUTC() ? t : 0);
} }
// helper function to convert compile time to UTC time // helper function to convert compile time to UTC time
time_t compiledUTC(void) { time_t compiledUTC(void) {
time_t t = RtcDateTime(__DATE__, __TIME__).Epoch32Time(); static time_t t = myTZ.toUTC(RtcDateTime(__DATE__, __TIME__).Epoch32Time());
return myTZ.toUTC(t); return t;
} }
// helper function to convert gps date/time into time_t // helper function to convert gps date/time into time_t
@ -182,7 +183,7 @@ void clock_loop(void *pvParameters) { // ClockTask
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
TickType_t wakeTime; TickType_t wakeTime;
uint32_t ppstime; uint32_t printtime;
time_t t; time_t t;
#define t1(t) (t + DCF77_FRAME_SIZE + 1) // future minute for next DCF77 frame #define t1(t) (t + DCF77_FRAME_SIZE + 1) // future minute for next DCF77 frame
@ -196,18 +197,19 @@ void clock_loop(void *pvParameters) { // ClockTask
// output time telegram for second following sec beginning with timepulse // output time telegram for second following sec beginning with timepulse
for (;;) { for (;;) {
xTaskNotifyWait(0x00, ULONG_MAX, &ppstime, xTaskNotifyWait(0x00, ULONG_MAX, &printtime,
portMAX_DELAY); // wait for timepulse portMAX_DELAY); // wait for timepulse
// no confident time -> suppress clock output // no confident time -> suppress clock output
if (timeStatus() == timeNotSet) if (timeStatus() == timeNotSet)
continue; continue;
t = time_t(ppstime); t = time_t(printtime);
#if defined HAS_IF482 #if defined HAS_IF482
IF482_Pulse(t2(t)); // next second // IF482_Pulse(t2(t)); // next second
IF482_Pulse(t); // next second
#elif defined HAS_DCF77 #elif defined HAS_DCF77
@ -218,7 +220,8 @@ void clock_loop(void *pvParameters) { // ClockTask
minute(t1(t))) // have recent frame? (timepulses could be missed!) minute(t1(t))) // have recent frame? (timepulses could be missed!)
continue; continue;
else else
DCF77_Pulse(t2(t), DCFpulse); // then output next second of this frame // DCF77_Pulse(t2(t), DCFpulse); // then output next second of this frame
DCF77_Pulse(t, DCFpulse); // then output next second of this frame
#endif #endif