timekeeper use syncprovider by Time.h

This commit is contained in:
Klaus K Wilting 2019-02-25 00:26:46 +01:00
parent 6ab4a0bd9f
commit 5fb4c7bec2
8 changed files with 23 additions and 48 deletions

View File

@ -5,15 +5,12 @@
#include "senddata.h"
#include "rcommand.h"
#include "spislave.h"
#include "timekeeper.h"
#include <lmic.h>
#ifdef HAS_BME
#include "bme680mems.h"
#endif
void doHousekeeping(void);
uint64_t uptime(void);
void reset_counters(void);

View File

@ -35,7 +35,4 @@ void lora_queuereset(void);
void lora_housekeeping(void);
void user_request_network_time_callback(void *pVoidUserUTCTime,
int flagSuccess);
time_t set_loratime(time_t t);
time_t get_loratime(void);
#endif

View File

@ -18,9 +18,9 @@ extern const char timeSetSymbols[];
void IRAM_ATTR CLOCKIRQ(void);
void clock_init(void);
void clock_loop(void *pvParameters);
void time_sync(void);
time_t time_sync(void);
void timepulse_start(void);
uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller);
time_t syncTime(getExternalTime getTimeFunction, timesource_t const caller);
uint8_t timepulse_init(void);
uint8_t TimeIsValid(time_t const t);
time_t syncProvider_CB(void);

View File

@ -19,7 +19,6 @@ void doHousekeeping() {
spi_housekeeping();
lora_housekeeping();
time_sync();
// task storage debugging //
ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d",

View File

@ -132,7 +132,7 @@ void refreshtheDisplay() {
uint8_t msgWaiting;
char timeState, buff[16];
time_t t;
time_t t = myTZ.toLocal(now()); // note: call now() here *before* locking mutex!
// block i2c bus access
if (I2C_MUTEX_LOCK()) {
@ -216,7 +216,6 @@ void refreshtheDisplay() {
// update LoRa status display (line 6)
u8x8.printf("%-16s", display_line6);
#else // we want a systime display instead LoRa status
t = myTZ.toLocal(now());
timeState = TimePulseTick ? ' ' : timeSetSymbols[timeSource];
TimePulseTick = false;
u8x8.printf("%02d:%02d:%02d%c %2d.%3s", hour(t), minute(t), second(t),

View File

@ -472,19 +472,11 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
*pUserUTCTime += requestDelaySec;
// Update system time with time read from the network
set_loratime(*pUserUTCTime); // store time in time sync provider function
if (syncTime(get_loratime, _lora))
if (TimeIsValid(*pUserUTCTime)) {
setTime(*pUserUTCTime);
ESP_LOGI(TAG, "Received recent time from LoRa");
}
else
ESP_LOGI(TAG, "Invalid time received from LoRa");
#endif // HAS_LORA
} // user_request_network_time_callback
time_t set_loratime(time_t t) {
static time_t loratime = 0; // stores time for retrieval
if (t > loratime)
loratime = t; // store time if it is recent
return loratime;
}
time_t get_loratime(void) { return set_loratime(0); }

View File

@ -363,9 +363,8 @@ void setup() {
timepulse_start();
#ifdef TIME_SYNC_INTERVAL
setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus() via Time.h
setSyncProvider(syncProvider_CB); // is called by Time.h
setSyncProvider(time_sync); // is called by Time.h
#endif
time_sync(); // sync time
// start wifi in monitor mode and start channel rotation timer
ESP_LOGI(TAG, "Starting Wifi...");

View File

@ -8,22 +8,23 @@ const char timeSetSymbols[] = {'G', 'R', 'L', '?'};
getExternalTime TimeSourcePtr; // pointer to time source function
void time_sync() {
time_t time_sync() {
// check synchonization of systime, called by cyclic.cpp
time_t t = 0;
#ifdef TIME_SYNC_INTERVAL
if (timeStatus() == timeSet) // timeStatus() is flipped in Time.h
return;
#ifdef HAS_GPS
if (syncTime(get_gpstime, _gps))
return; // attempt sync with GPS time
t = syncTime(get_gpstime, _gps);
if (t)
return t; // attempt sync with GPS time
#endif
// no GPS -> fallback to RTC time while trying lora sync
#ifdef HAS_RTC
if (!syncTime(get_rtctime, _rtc)) // sync with RTC time
t = syncTime(get_rtctime, _rtc); // sync with RTC time
if (!t)
ESP_LOGW(TAG, "no confident RTC time");
#endif
@ -33,26 +34,25 @@ void time_sync() {
#endif
#endif // TIME_SYNC_INTERVAL
return t;
} // time_sync()
// sync time on start of next second from GPS or RTC
uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) {
time_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) {
TimeSourcePtr = getTimeFunction;
time_t t;
TimeSourcePtr = getTimeFunction;
if (!TimeSourcePtr)
goto error;
if ((caller == _gps || caller == _rtc)) // ticking timesource?
xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // then wait on pps
xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // wait for pps
t = TimeSourcePtr(); // get time from given timesource
if (TimeIsValid(t)) {
if (caller == _gps) // gps time concerns past second
t++;
setTime(t); // flips timeStatus() in Time.h
timeSource = caller;
ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d",
timeSetSymbols[timeSource], hour(t), minute(t), second(t));
@ -62,7 +62,7 @@ uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) {
set_rtctime(t);
#endif
return 1; // success
return t; // success
}
error:
@ -72,14 +72,6 @@ error:
} // syncTime()
// callback function called by Time.h in interval set in main.cpp
time_t syncProvider_CB(void) {
timeSource = _unsynced;
return 0;
}
// helper function to setup a pulse per second for time synchronisation
uint8_t timepulse_init() {