diff --git a/include/cyclic.h b/include/cyclic.h index 829f9610..c4234c38 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -11,6 +11,9 @@ #include "bme680mems.h" #endif +extern Ticker housekeeper; + +void housekeeping(void); void doHousekeeping(void); uint64_t uptime(void); void reset_counters(void); diff --git a/include/globals.h b/include/globals.h index 72036687..0f232247 100644 --- a/include/globals.h +++ b/include/globals.h @@ -8,6 +8,7 @@ #include #include #include +#include // std::set for unified array functions #include diff --git a/include/irqhandler.h b/include/irqhandler.h index 64cb12fe..d818e138 100644 --- a/include/irqhandler.h +++ b/include/irqhandler.h @@ -5,6 +5,7 @@ #define BUTTON_IRQ 0x02 #define SENDCOUNTER_IRQ 0x04 #define CYCLIC_IRQ 0x08 +#define TIMESYNC_IRQ 0x10 #include "globals.h" #include "cyclic.h" @@ -12,8 +13,6 @@ #include "timekeeper.h" void irqHandler(void *pvParameters); -void IRAM_ATTR homeCycleIRQ(); -void IRAM_ATTR SendCycleIRQ(); #ifdef HAS_DISPLAY #include "display.h" diff --git a/include/senddata.h b/include/senddata.h index 82cf4581..e0eb4ecf 100644 --- a/include/senddata.h +++ b/include/senddata.h @@ -5,9 +5,12 @@ #include "lorawan.h" #include "cyclic.h" +extern Ticker sendcycler; + void SendPayload(uint8_t port, sendprio_t prio); void sendCounter(void); void checkSendQueues(void); void flushQueues(); +void sendcycle(void); #endif // _SENDDATA_H_ \ No newline at end of file diff --git a/include/timekeeper.h b/include/timekeeper.h index 410d7dec..4714456a 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -4,6 +4,7 @@ #include "globals.h" #include "rtctime.h" #include "TimeLib.h" +#include "irqhandler.h" #ifdef HAS_GPS #include "gpsread.h" @@ -15,11 +16,13 @@ #endif extern const char timeSetSymbols[]; +extern Ticker timesyncer; void IRAM_ATTR CLOCKIRQ(void); void clock_init(void); void clock_loop(void *pvParameters); void timepulse_start(void); +void timeSync(void); uint8_t timepulse_init(void); time_t timeIsValid(time_t const t); time_t timeProvider(void); diff --git a/lib/microTime/src/Time.cpp b/lib/microTime/src/Time.cpp index a4cd2d3a..4dd745ad 100644 --- a/lib/microTime/src/Time.cpp +++ b/lib/microTime/src/Time.cpp @@ -260,10 +260,9 @@ time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync #endif #ifdef usePPS -time_t SyncToPPS() { +void SyncToPPS() { sysTime++; prevMicros = micros(); - return sysTime; } #endif diff --git a/lib/microTime/src/TimeLib.h b/lib/microTime/src/TimeLib.h index 6ed36bc3..45efb398 100644 --- a/lib/microTime/src/TimeLib.h +++ b/lib/microTime/src/TimeLib.h @@ -154,7 +154,7 @@ time_t now(uint32_t &sysTimeMicros); // return the current time as seconds and #endif #ifdef usePPS -time_t SyncToPPS(); +void SyncToPPS(); #endif void setTime(time_t t); void setTime(int hr, int min, int sec, int day, int month, int yr); diff --git a/src/configmanager.cpp b/src/configmanager.cpp index dd4fdcbd..419246ae 100644 --- a/src/configmanager.cpp +++ b/src/configmanager.cpp @@ -19,7 +19,7 @@ void defaultConfig() { cfg.screenon = 1; // 0=disabled, 1=enabled cfg.countermode = 0; // 0=cyclic, 1=cumulative, 2=cyclic confirmed cfg.rssilimit = 0; // threshold for rssilimiter, negative value! - cfg.sendcycle = SEND_SECS; // payload send cycle [seconds/2] + cfg.sendcycle = SEND_CYCLE; // payload send cycle [seconds/2] cfg.wifichancycle = WIFI_CHANNEL_SWITCH_INTERVAL; // wifi channel switch cycle [seconds/100] cfg.blescantime = diff --git a/src/cyclic.cpp b/src/cyclic.cpp index c9341f03..26f86eb4 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -7,6 +7,10 @@ // Local logging tag static const char TAG[] = __FILE__; +Ticker housekeeper; + +void housekeeping() { xTaskNotify(irqHandlerTask, CYCLIC_IRQ, eSetBits); } + // do all housekeeping void doHousekeeping() { diff --git a/src/gpsread.cpp b/src/gpsread.cpp index ec900251..a41601c8 100644 --- a/src/gpsread.cpp +++ b/src/gpsread.cpp @@ -95,7 +95,7 @@ time_t get_gpstime(void) { if ((gps.time.age() < gpsDelay_ms) && (gps.time.isValid()) && (gps.date.isValid())) { - ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d,", + ESP_LOGD(TAG, "GPS time age: %dms, is valid: %s, second: %d", gps.time.age(), (gps.time.isValid() && gps.date.isValid()) ? "yes" : "no", gps.time.second()); diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index a2af0cf7..06bf82e7 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -34,6 +34,11 @@ void irqHandler(void *pvParameters) { doHousekeeping(); } + // time to be synced? + if (InterruptStatus & TIMESYNC_IRQ) { + setTime(timeProvider()); + } + // is time to send the payload? if (InterruptStatus & SENDCOUNTER_IRQ) sendCounter(); @@ -44,16 +49,6 @@ void irqHandler(void *pvParameters) { // esp32 hardware timer triggered interrupt service routines // they notify the irq handler task -void IRAM_ATTR homeCycleIRQ() { - xTaskNotifyFromISR(irqHandlerTask, CYCLIC_IRQ, eSetBits, NULL); - portYIELD_FROM_ISR(); -} - -void IRAM_ATTR SendCycleIRQ() { - xTaskNotifyFromISR(irqHandlerTask, SENDCOUNTER_IRQ, eSetBits, NULL); - portYIELD_FROM_ISR(); -} - #ifdef HAS_DISPLAY void IRAM_ATTR DisplayIRQ() { xTaskNotifyFromISR(irqHandlerTask, DISPLAY_IRQ, eSetBits, NULL); diff --git a/src/main.cpp b/src/main.cpp index 547b2d25..345c54c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -313,16 +313,6 @@ void setup() { timerAlarmWrite(displaytimer, DISPLAYREFRESH_MS * 1000, true); #endif - // setup send cycle trigger IRQ using esp32 hardware timer 2 - sendCycle = timerBegin(2, 8000, true); - timerAttachInterrupt(sendCycle, &SendCycleIRQ, true); - timerAlarmWrite(sendCycle, cfg.sendcycle * 2 * 10000, true); - - // setup house keeping cycle trigger IRQ using esp32 hardware timer 3 - homeCycle = timerBegin(3, 8000, true); - timerAttachInterrupt(homeCycle, &homeCycleIRQ, true); - timerAlarmWrite(homeCycle, HOMECYCLE * 10000, true); - // show payload encoder #if PAYLOAD_ENCODER == 1 strcat_P(features, " PLAIN"); @@ -358,19 +348,6 @@ void setup() { #endif #endif -#if defined HAS_IF482 || defined HAS_DCF77 - // start pps timepulse - ESP_LOGI(TAG, "Starting Timekeeper..."); - assert(timepulse_init()); // setup timepulse - timepulse_start(); -#endif - -#ifdef TIME_SYNC_INTERVAL - // set time source and sync time - setSyncInterval(TIME_SYNC_INTERVAL * 60); - setSyncProvider(&timeProvider); -#endif - // start wifi in monitor mode and start channel rotation timer ESP_LOGI(TAG, "Starting Wifi..."); wifi_sniffer_init(); @@ -406,12 +383,12 @@ void setup() { assert(irqHandlerTask != NULL); // has interrupt handler task started? // start timer triggered interrupts - ESP_LOGI(TAG, "Starting Interrupts..."); + ESP_LOGI(TAG, "Starting Timers..."); #ifdef HAS_DISPLAY timerAlarmEnable(displaytimer); #endif - timerAlarmEnable(sendCycle); - timerAlarmEnable(homeCycle); + sendcycler.attach(SEND_CYCLE, sendcycle); + housekeeper.attach(HOMECYCLE, housekeeping); // start button interrupt #ifdef HAS_BUTTON @@ -422,7 +399,19 @@ void setup() { #endif #endif // HAS_BUTTON +#ifdef TIME_SYNC_INTERVAL + // start pps timepulse + ESP_LOGI(TAG, "Starting Timekeeper..."); + assert(timepulse_init()); // setup timepulse + timepulse_start(); + timeSync(); // init systime + timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); +#endif + #if defined HAS_IF482 || defined HAS_DCF77 +#ifndef TIME_SYNC_INTERVAL +#error you must define TIME_SNYC_INTERVAL in paxcounter.conf +#endif ESP_LOGI(TAG, "Starting Clock Controller..."); clock_init(); #endif diff --git a/src/paxcounter.conf b/src/paxcounter.conf index 7e449439..c60d0ee9 100644 --- a/src/paxcounter.conf +++ b/src/paxcounter.conf @@ -10,7 +10,7 @@ #define VERBOSE 1 // comment out to silence the device, for mute use build option // Payload send cycle and encoding -#define SEND_SECS 30 // payload send cycle [seconds/2] -> 60 sec. +#define SEND_CYCLE 30 // payload send cycle [seconds/2] -> 60 sec. #define PAYLOAD_ENCODER 2 // payload encoder: 1=Plain, 2=Packed, 3=Cayenne LPP dynamic, 4=Cayenne LPP packed // Set this to include BLE counting and vendor filter functions diff --git a/src/payload.cpp b/src/payload.cpp index 4e0d0d16..8326d8a8 100644 --- a/src/payload.cpp +++ b/src/payload.cpp @@ -456,7 +456,7 @@ void PayloadConvert::addButton(uint8_t value) { void PayloadConvert::addTime(time_t value) { #if (PAYLOAD_ENCODER == 4) uint32_t t = (uint32_t)value; - uint32_t tx_period = (uint32_t)SEND_SECS * 2; + uint32_t tx_period = (uint32_t)SEND_CYCLE * 2; buffer[cursor++] = 0x03; // set config mask to UTCTime + TXPeriod // UTCTime in seconds buffer[cursor++] = (byte)((t & 0xFF000000) >> 24); diff --git a/src/senddata.cpp b/src/senddata.cpp index 6886117a..54efd4d2 100644 --- a/src/senddata.cpp +++ b/src/senddata.cpp @@ -1,6 +1,10 @@ // Basic Config #include "senddata.h" +Ticker sendcycler; + +void sendcycle() { xTaskNotify(irqHandlerTask, SENDCOUNTER_IRQ, eSetBits); } + // put data to send in RTos Queues used for transmit over channels Lora and SPI void SendPayload(uint8_t port, sendprio_t prio) { diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 94ebbf00..1a06bf2b 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -6,12 +6,12 @@ static const char TAG[] = __FILE__; // symbol to display current time source const char timeSetSymbols[] = {'G', 'R', 'L', '?'}; -getExternalTime TimeSourcePtr; // pointer to time source function +Ticker timesyncer; + +void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); } time_t timeProvider(void) { - ESP_LOGD(TAG, "time synched"); - time_t t = 0; #ifdef HAS_GPS @@ -33,7 +33,7 @@ time_t timeProvider(void) { } #endif -// kick off asychron lora sync if we have +// kick off asychronous lora sync if we have #if defined HAS_LORA && defined TIME_SYNC_LORA LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime); #endif @@ -101,11 +101,11 @@ void timepulse_start(void) { void IRAM_ATTR CLOCKIRQ(void) { BaseType_t xHigherPriorityTaskWoken; - time_t t = SyncToPPS(); // calibrates UTC systime, see Time.h + SyncToPPS(); // calibrates UTC systime, see Time.h xHigherPriorityTaskWoken = pdFALSE; if (ClockTask != NULL) - xTaskNotifyFromISR(ClockTask, uint32_t(t), eSetBits, + xTaskNotifyFromISR(ClockTask, uint32_t(now()), eSetBits, &xHigherPriorityTaskWoken); #if defined GPS_INT || defined RTC_INT