time sync accuracy fixes

This commit is contained in:
Verkehrsrot committed 2019-03-31 19:13:06 +02:00
1 parent 6aba3c128b
commit 3376a7f57e
10 files changed
+43 -28

No files matched your search

+20 -8
View File
@@ -18,15 +18,14 @@ void irqHandler(void *pvParameters) {
&InterruptStatus, // Receives the notification value
portMAX_DELAY); // wait forever
// interrupt handler to be enabled?
if (InterruptStatus & UNMASK_IRQ)
if (InterruptStatus & UNMASK_IRQ) // interrupt handler to be enabled?
mask_irq = false;
else if (mask_irq)
continue; // suppress processing if interrupt handler is disabled
// interrupt handler to be disabled?
if (InterruptStatus & MASK_IRQ)
else if (mask_irq) // suppress processing if interrupt handler is disabled
continue;
else if (InterruptStatus & MASK_IRQ) { // interrupt handler to be disabled?
mask_irq = true;
continue;
}
// button pressed?
#ifdef HAS_BUTTON
@@ -36,7 +35,7 @@ void irqHandler(void *pvParameters) {
// display needs refresh?
#ifdef HAS_DISPLAY
if (InterruptStatus & DISPLAY_IRQ)
if (InterruptStatus & DISPLAY_IRQ)
refreshtheDisplay();
#endif
@@ -85,3 +84,16 @@ void IRAM_ATTR ButtonIRQ() {
portYIELD_FROM_ISR();
}
#endif
int mask_user_IRQ() {
// begin of time critical section: lock I2C bus to ensure accurate timing
if (!I2C_MUTEX_LOCK())
return 1; // failure
xTaskNotify(irqHandlerTask, MASK_IRQ, eSetBits);
}
int unmask_user_IRQ() {
// end of time critical section: release I2C bus
I2C_MUTEX_UNLOCK();
xTaskNotify(irqHandlerTask, UNMASK_IRQ, eSetBits);
}
+2 -3
View File
@@ -495,8 +495,7 @@ void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime,
}
// begin of time critical section: lock I2C bus to ensure accurate timing
// don't move the mutex, will impact accuracy of time up to 1 sec!
if (!I2C_MUTEX_LOCK())
if (!mask_user_IRQ())
return; // failure
// Update userUTCTime, considering the difference between the GPS and UTC
@@ -514,7 +513,7 @@ void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime,
setMyTime(*pUserUTCTime + requestDelaySec, 0);
// end of time critical section: release I2C bus
I2C_MUTEX_UNLOCK();
unmask_user_IRQ();
} // user_request_network_time_callback
#endif // TIME_SYNC_LORAWAN
-1
View File
@@ -415,7 +415,6 @@ void setup() {
!defined HAS_GPS && !defined HAS_RTC)
#warning you did not specify a time source, time will not be synched
#endif
#else
// start pps timepulse
ESP_LOGI(TAG, "Starting Timekeeper...");
assert(timepulse_init()); // setup timepulse
+1
View File
@@ -277,6 +277,7 @@ void get_time(uint8_t val[]) {
ESP_LOGI(TAG, "Remote command: get time");
payload.reset();
payload.addTime(now());
payload.addByte(timeStatus() << 4 | timeSource);
SendPayload(STATUSPORT, prio_high);
};
+2 -2
View File
@@ -175,9 +175,9 @@ TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config,
return round(txTime);
}
#if defined HAS_IF482 || defined HAS_DCF77
#if (defined HAS_IF482 || defined HAS_DCF77)
#if defined HAS_DCF77 && defined HAS_IF482
#if (defined HAS_DCF77 && defined HAS_IF482)
#error You must define at most one of IF482 or DCF77!
#endif
+9 -9
View File
@@ -9,7 +9,7 @@ algorithm in applications without granted license by the patent holder.
*/
#if(TIME_SYNC_LORASERVER)
#if (TIME_SYNC_LORASERVER)
#include "timesync.h"
@@ -111,8 +111,7 @@ void process_timesync_req(void *taskparameter) {
} // for
// begin of time critical section: lock I2C bus to ensure accurate timing
// don't move the mutex, will impact accuracy of time up to 1 sec!
if (!I2C_MUTEX_LOCK())
if (!mask_user_IRQ())
goto error; // failure
// average time offset from collected diffs
@@ -133,9 +132,10 @@ void process_timesync_req(void *taskparameter) {
setMyTime(time_to_set, time_to_set_fraction_msec);
// end of time critical section: release I2C bus
I2C_MUTEX_UNLOCK();
unmask_user_IRQ();
finish:
lora_time_sync_pending = false;
timeSyncReqTask = NULL;
vTaskDelete(NULL); // end task
@@ -215,7 +215,8 @@ int recv_timesync_ans(uint8_t buf[], uint8_t buf_len) {
// adjust system time, calibrate RTC and RTC_INT pps
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec) {
time_t time_to_set = (time_t)t_sec;
// advance time 1 sec wait time
time_t time_to_set = (time_t)(t_sec + 1);
ESP_LOGD(TAG, "[%0.3f] Calculated UTC epoch time: %d.%03d sec",
millis() / 1000.0, time_to_set, t_msec);
@@ -225,16 +226,15 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec) {
// wait until top of second with millisecond precision
vTaskDelay(pdMS_TO_TICKS(1000 - t_msec));
// set RTC time and calibrate RTC_INT pulse on top of second
#ifdef HAS_RTC
time_to_set++; // advance time 1 sec wait time
// set RTC time and calibrate RTC_INT pulse on top of second
set_rtctime(time_to_set, no_mutex);
#endif
// sync pps timer to top of second
#if (!defined GPS_INT && !defined RTC_INT)
// sync pps timer to top of second
timerWrite(ppsIRQ, 0); // reset pps timer
CLOCKIRQ(); // fire clock pps, this advances time 1 sec
CLOCKIRQ(); // fire clock pps, this advances time 1 sec
#endif
setTime(time_to_set); // set the time on top of second