timesync cleanups

This commit is contained in:
cyberman54 2022-03-05 14:07:48 +01:00
parent c9c4f2e714
commit dd41b6e9da
4 changed files with 78 additions and 83 deletions

View File

@ -10,7 +10,8 @@
#include "dcf77.h"
#include "esp_sntp.h"
#define HAS_LORA_TIME ((HAS_LORA) && ((TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)))
#define HAS_LORA_TIME \
((HAS_LORA) && ((TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)))
#define SECS_YR_2000 (946684800UL) // the time at the start of y2k
#define GPS_UTC_DIFF 315964800UL // seconds diff between gps and utc epoch
@ -26,16 +27,14 @@ extern DRAM_ATTR bool TimePulseTick; // 1sec pps flag set by GPS or RTC
extern DRAM_ATTR unsigned long lastPPS;
extern hw_timer_t *ppsIRQ;
void IRAM_ATTR CLOCKIRQ(void);
void IRAM_ATTR GPSIRQ(void);
void clock_init(void);
void clock_loop(void *pvParameters);
//void IRAM_ATTR CLOCKIRQ(void);
//void IRAM_ATTR GPSIRQ(void);
//void clock_loop(void *pvParameters);
void setTimeSyncIRQ(void);
uint8_t timepulse_init(void);
void time_init(void);
bool timeIsValid(time_t const t);
void calibrateTime(void);
bool setMyTime(uint32_t t_sec, uint16_t t_msec,
timesource_t mytimesource);
bool setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t mytimesource);
time_t compileTime(void);
time_t mkgmtime(const struct tm *ptm);
TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config,

View File

@ -18,10 +18,6 @@ void doHousekeeping() {
if ((RTC_runmode == RUNMODE_UPDATE) || (RTC_runmode == RUNMODE_MAINTENANCE))
do_reset(true); // warmstart
// try to get time if we don't yet have a recent timesource
if (timeSource == _unsynced || timeSource == _set)
calibrateTime();
// print heap and task storage information
ESP_LOGD(TAG, "Heap: Free:%d, Min:%d, Size:%d, Alloc:%d, StackHWM:%d",
ESP.getFreeHeap(), ESP.getMinFreeHeap(), ESP.getHeapSize(),

View File

@ -476,20 +476,8 @@ void setup() {
// only if we have a timesource we do timesync
#if ((HAS_LORA_TIME) || (HAS_GPS) || (HAS_RTC))
#if (defined HAS_IF482 || defined HAS_DCF77)
ESP_LOGI(TAG, "Starting Clock Controller...");
clock_init();
#endif
#if (HAS_LORA_TIME)
timesync_init(); // create loraserver time sync task
#endif
ESP_LOGI(TAG, "Starting Timekeeper...");
_ASSERT(timepulse_init()); // starts pps and cyclic time sync
time_init();
strcat_P(features, " TIME");
#endif // timesync
// show compiled features

View File

@ -31,6 +31,40 @@ Ticker timesyncer;
void setTimeSyncIRQ() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
// interrupt service routine triggered by GPS PPS
void IRAM_ATTR GPSIRQ(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// take timestamp
lastPPS = millis(); // last time of pps
// yield only if we should
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
// interrupt service routine triggered by esp32 hardware timer
void IRAM_ATTR CLOCKIRQ(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// advance wall clock, if we have
#if (defined HAS_IF482 || defined HAS_DCF77)
xTaskNotifyFromISR(ClockTask, uint32_t(time(NULL)), eSetBits,
&xHigherPriorityTaskWoken);
#endif
// flip time pulse ticker, if needed
#ifdef HAS_DISPLAY
TimePulseTick = !TimePulseTick; // flip global variable pulse ticker
#endif
// yield only if we should
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
void calibrateTime(void) {
// kick off asynchronous lora timesync if we have
@ -125,7 +159,7 @@ bool setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t mytimesource) {
}
// helper function to setup a pulse per second for time synchronisation
uint8_t timepulse_init() {
void timepulse_init(void) {
// set esp-idf API sntp sync mode
// sntp_init();
@ -155,49 +189,15 @@ uint8_t timepulse_init() {
ESP_LOGI(TAG, "Timepulse: internal (ESP32 hardware timer)");
#endif
// start cyclic time sync
timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ);
// get time if we don't have one
if (timeSource != _set)
setTimeSyncIRQ(); // init systime by RTC or GPS or LORA
// start cyclic time sync
timesyncer.attach(TIME_SYNC_INTERVAL * 60, setTimeSyncIRQ);
} // timepulse_init
// interrupt service routine triggered by GPS PPS
void IRAM_ATTR GPSIRQ(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// take timestamp
lastPPS = millis(); // last time of pps
// yield only if we should
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
// interrupt service routine triggered by esp32 hardware timer
void IRAM_ATTR CLOCKIRQ(void) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
// advance wall clock, if we have
#if (defined HAS_IF482 || defined HAS_DCF77)
xTaskNotifyFromISR(ClockTask, uint32_t(time(NULL)), eSetBits,
&xHigherPriorityTaskWoken);
#endif
// flip time pulse ticker, if needed
#ifdef HAS_DISPLAY
TimePulseTick = !TimePulseTick; // flip global variable pulse ticker
#endif
// yield only if we should
if (xHigherPriorityTaskWoken)
portYIELD_FROM_ISR();
}
// helper function to check plausibility of a given epoch time
bool timeIsValid(time_t const t) {
// is t a time in the past? we use compile time to guess
@ -218,26 +218,6 @@ TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config,
return round(txTime);
}
void clock_init(void) {
// setup clock output interface
#ifdef HAS_IF482
IF482.begin(HAS_IF482);
#elif defined HAS_DCF77
pinMode(HAS_DCF77, OUTPUT);
#endif
xTaskCreatePinnedToCore(clock_loop, // task function
"clockloop", // name of task
3072, // stack size of task
(void *)1, // task parameter
6, // priority of the task
&ClockTask, // task handle
1); // CPU core
_ASSERT(ClockTask != NULL); // has clock task started?
} // clock_init
void clock_loop(void *taskparameter) { // ClockTask
uint32_t current_time = 0, previous_time = 0;
@ -320,6 +300,26 @@ void clock_loop(void *taskparameter) { // ClockTask
} // for
} // clock_loop()
void clock_init(void) {
// setup clock output interface
#ifdef HAS_IF482
IF482.begin(HAS_IF482);
#elif defined HAS_DCF77
pinMode(HAS_DCF77, OUTPUT);
#endif
xTaskCreatePinnedToCore(clock_loop, // task function
"clockloop", // name of task
3072, // stack size of task
(void *)1, // task parameter
6, // priority of the task
&ClockTask, // task handle
1); // CPU core
_ASSERT(ClockTask != NULL); // has clock task started?
} // clock_init
// we use compile date to create a time_t reference "in the past"
time_t compileTime(void) {
@ -379,3 +379,15 @@ time_t mkgmtime(const struct tm *ptm) {
secs += ptm->tm_sec;
return secs;
}
void time_init(void) {
#if (HAS_LORA_TIME)
timesync_init(); // create loraserver time sync task
#endif
ESP_LOGI(TAG, "Starting time pulse...");
timepulse_init(); // starts pps and cyclic time sync
#if (defined HAS_IF482 || defined HAS_DCF77)
ESP_LOGI(TAG, "Starting clock controller...");
clock_init();
#endif
}