diff --git a/include/clockcontroller.h b/include/clockcontroller.h index bfb7970d..c9748ce8 100644 --- a/include/clockcontroller.h +++ b/include/clockcontroller.h @@ -11,6 +11,5 @@ void clock_init(void); void clock_loop(void *pvParameters); -time_t telegram_time(void); #endif // _CLOCKCONTROLLER_H \ No newline at end of file diff --git a/include/display.h b/include/display.h index 99379b58..91f1be0a 100644 --- a/include/display.h +++ b/include/display.h @@ -3,6 +3,7 @@ #include #include "cyclic.h" +#include "rtctime.h" extern uint8_t volatile DisplayState; extern HAS_DISPLAY u8x8; diff --git a/include/rtctime.h b/include/rtctime.h index 384dbd7b..b0ac1221 100644 --- a/include/rtctime.h +++ b/include/rtctime.h @@ -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 \ No newline at end of file diff --git a/src/clockcontroller.cpp b/src/clockcontroller.cpp index dca38411..7c01b377 100644 --- a/src/clockcontroller.cpp +++ b/src/clockcontroller.cpp @@ -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 \ No newline at end of file diff --git a/src/dcf77.cpp b/src/dcf77.cpp index 96127943..20b6344c 100644 --- a/src/dcf77.cpp +++ b/src/dcf77.cpp @@ -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 diff --git a/src/display.cpp b/src/display.cpp index 07afdebb..aae90c4a 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -41,6 +41,16 @@ const char lora_datarate[] = {"1211100908078CNA1211109C8C7C"}; const char lora_datarate[] = {"121110090807FSNA"}; #endif +// time display symbols +#if defined HAS_GPS || defined HAS_RTC +const char timeNosyncSymbol = '?'; +#if defined HAS_IF482 +const char timesyncSymbol = '+'; +#elif defined HAS_DCF77 +const char timesyncSymbol = '*'; +#endif +#endif + // helper arry for converting month values to text const char *printmonth[] = {"xxx", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; @@ -130,6 +140,11 @@ void init_display(const char *Productname, const char *Version) { void refreshtheDisplay() { + uint8_t msgWaiting; + char timeSync, timeState; + char buff[16]; // 16 chars line buffer + time_t t; + // block i2c bus access if (I2C_MUTEX_LOCK()) { @@ -143,17 +158,6 @@ void refreshtheDisplay() { if (!DisplayState) return; - uint8_t msgWaiting; - char buff[16]; // 16 chars line buffer -#if (defined HAS_DCF77) || (defined HAS_IF482) - const char timeNosyncSymbol = '?'; -#if (defined HAS_IF482) - const char timesyncSymbol = '+'; -#else - const char timesyncSymbol = '*'; -#endif -#endif - // update counter (lines 0-1) snprintf( buff, sizeof(buff), "PAX:%-4d", @@ -221,12 +225,12 @@ 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()); - char timeState = - (timeStatus() == timeSet) ? timesyncSymbol : timeNosyncSymbol; - char timePulse = TimePulseTick ? '.' : ':'; - u8x8.printf("%02d:%02d%c%02d%c %2d.%3s", hour(t), minute(t), timePulse, - second(t), timeState, day(t), printmonth[month(t)]); + t = myTZ.toLocal(best_time()); + timeSync = (timeStatus() == timeSet) ? timesyncSymbol : timeNosyncSymbol; + timeState = TimePulseTick ? timeSync : ' '; + TimePulseTick = false; + u8x8.printf("%02d:%02d:%02d%c %2d.%3s", hour(t), minute(t), second(t), + timeState, day(t), printmonth[month(t)]); #endif // update LMiC event display (line 7) diff --git a/src/gpsread.cpp b/src/gpsread.cpp index fe159a12..592488bf 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -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 diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 71022516..4991b943 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -25,11 +25,13 @@ public: MyHalConfig_t myHalConfig{}; // LMIC pin mapping + const lmic_pinmap lmic_pins = { .nss = LORA_CS, .rxtx = LMIC_UNUSED_PIN, - .rst = LORA_RST, - .dio = {LORA_IRQ, LORA_IO1, LORA_IO2}, + .rst = LORA_RST == NOT_A_PIN ? LMIC_UNUSED_PIN : LORA_RST, + .dio = {LORA_IRQ, LORA_IO1, + LORA_IO2 == NOT_A_PIN ? LMIC_UNUSED_PIN : LORA_IO2}, // optional: set polarity of rxtx pin. .rxtx_rx_active = 0, // optional: set RSSI cal for listen-before-talk diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 7bd9ad0f..d565440a 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -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 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 }