clock time handling improvements

This commit is contained in:
Klaus K Wilting 2019-02-17 22:12:14 +01:00
parent bb187ef0bd
commit 1b42afafd7
8 changed files with 37 additions and 33 deletions

View File

@ -11,6 +11,5 @@
void clock_init(void);
void clock_loop(void *pvParameters);
time_t telegram_time(void);
#endif // _CLOCKCONTROLLER_H

View File

@ -3,6 +3,7 @@
#include <U8x8lib.h>
#include "cyclic.h"
#include "rtctime.h"
extern uint8_t volatile DisplayState;
extern HAS_DISPLAY u8x8;

View File

@ -23,5 +23,6 @@ void timepulse_start(void);
int sync_TimePulse(void);
int sync_SysTime(time_t);
int sync_SysTime(uint32_t t);
time_t best_time(void);
#endif // _RTCTIME_H

View File

@ -40,7 +40,7 @@ void clock_loop(void *pvParameters) { // ClockTask
// preload first DCF frame before start
#ifdef HAS_DCF77
DCF77_Frame(t1(telegram_time()));
DCF77_Frame(t1(best_time()));
#endif
// output time telegram for second following sec beginning with timepulse
@ -51,7 +51,7 @@ void clock_loop(void *pvParameters) { // ClockTask
if (timeStatus() == timeNotSet) // do we have valid time?
continue;
t = telegram_time(); // time to send to clock
t = best_time(); // time to send to clock
#if defined HAS_IF482
@ -71,24 +71,4 @@ void clock_loop(void *pvParameters) { // ClockTask
} // for
} // clock_loop()
// helper function to fetch current second from most precise time source
time_t telegram_time(void) {
time_t t;
#ifdef HAS_GPS // gps is our primary time source if present
t = get_gpstime();
if (t) // did we get a valid time?
return t;
#endif
#ifdef HAS_RTC // rtc is our secondary time source if present
t = get_rtctime();
if (t)
return t;
#endif
// else we use systime as fallback source
return now();
}
#endif // HAS_IF482 || defined HAS_DCF77

View File

@ -4,6 +4,9 @@
// a nice & free logic test program for DCF77 can be found here:
https://www-user.tu-chemnitz.de/~heha/viewzip.cgi/hs/Funkuhr.zip/
//
// a DCF77 digital scope for Arduino boards can be found here:
https://github.com/udoklein/dcf77
//
*/
#ifdef HAS_DCF77

View File

@ -221,7 +221,7 @@ void refreshtheDisplay() {
// update LoRa status display (line 6)
u8x8.printf("%-16s", display_line6);
#else // we want a systime display instead LoRa status
time_t t = myTZ.toLocal(now());
time_t t = myTZ.toLocal(best_time());
char timeState =
(timeStatus() == timeSet) ? timesyncSymbol : timeNosyncSymbol;
char timePulse = TimePulseTick ? '.' : ':';

View File

@ -91,16 +91,14 @@ time_t get_gpstime(void) {
// !! never call now() or delay in this function, this would break this
// function to be used as SyncProvider for Time.h
time_t t = 0; // 0 effects calling SyncProvider() to not set time
if ((gps.time.age() < 1500) && (gps.time.isValid())) {
// get current gps time
time_t t =
tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(),
t = tmConvert_t(gps.date.year(), gps.date.month(), gps.date.day(),
gps.time.hour(), gps.time.minute(), gps.time.second());
return t;
} else {
ESP_LOGW(TAG, "GPS has no confident time");
return 0; // sync failure, 0 effects calling SyncProvider() to not set time
}
return t;
} // get_gpstime()
// GPS serial feed FreeRTos Task

View File

@ -93,6 +93,30 @@ int sync_TimePulse(void) {
return 0; // failure
}
// helper function to fetch current second from most precise time source
time_t best_time(void) {
time_t t;
#ifdef HAS_GPS // gps is our primary time source if present
t = get_gpstime();
if (t) // did we get a valid time?
return t;
#endif
/*
// Reading RTC time from chip take too long on i2c bus, causes jitter
#ifdef HAS_RTC // rtc is our secondary time source if present
t = get_rtctime();
if (t)
return t;
#endif
*/
// else we use systime as fallback source
return now();
}
#ifdef HAS_RTC // we have hardware RTC
RtcDS3231<TwoWire> Rtc(Wire); // RTC hardware i2c interface
@ -166,14 +190,12 @@ int set_rtctime(uint32_t t) { // t is epoch seconds starting 1.1.1970
time_t get_rtctime(void) {
// !! never call now() or delay in this function, this would break this
// function to be used as SyncProvider for Time.h
time_t t = 0;
time_t t = 0; // 0 effects calling SyncProvider() to not set time
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
if (Rtc.IsDateTimeValid()) {
RtcDateTime tt = Rtc.GetDateTime();
t = tt.Epoch32Time();
} else {
ESP_LOGW(TAG, "RTC has no confident time");
}
I2C_MUTEX_UNLOCK(); // release i2c bus access
}