time accuracy finetuning

This commit is contained in:
Verkehrsrot 2019-03-24 19:50:24 +01:00
parent e156770fc3
commit 5f2d2d0dd5
2 changed files with 24 additions and 26 deletions

View File

@ -36,7 +36,7 @@ void irqHandler(void *pvParameters) {
// display needs refresh? // display needs refresh?
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY
if (InterruptStatus & DISPLAY_IRQ) if (InterruptStatus & DISPLAY_IRQ)
refreshtheDisplay(); refreshtheDisplay();
#endif #endif

View File

@ -53,7 +53,7 @@ void send_timesync_req() {
"timesync_req", // name of task "timesync_req", // name of task
2048, // stack size of task 2048, // stack size of task
(void *)1, // task parameter (void *)1, // task parameter
2, // priority of the task 4, // priority of the task
&timeSyncReqTask, // task handle &timeSyncReqTask, // task handle
1); // CPU core 1); // CPU core
} }
@ -104,23 +104,26 @@ void process_timesync_req(void *taskparameter) {
if (i < TIME_SYNC_SAMPLES - 1) { if (i < TIME_SYNC_SAMPLES - 1) {
// wait until next cycle // wait until next cycle
vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000)); vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000));
} else { } else { // before sending last time sample...
// send flush to open a receive window for last time_sync_answer // ...send flush to open a receive window for last time_sync_answer
payload.reset(); // payload.reset();
payload.addByte(0x99); // payload.addByte(0x99);
SendPayload(RCMDPORT, prio_high); // SendPayload(RCMDPORT, prio_high);
// Send a alive open a receive window for last time_sync_answer // ...send a alive open a receive window for last time_sync_answer
// void LMIC_sendAlive(); LMIC_sendAlive();
} }
} }
} // for } // for
// begin of time critical section: lock I2C bus to ensure accurate timing
I2C_MUTEX_LOCK();
// average time offset from collected diffs // average time offset from collected diffs
time_offset_ms /= TIME_SYNC_SAMPLES; time_offset_ms /= TIME_SYNC_SAMPLES;
// calculate time offset with millisecond precision using time base // calculate time offset with millisecond precision using LMIC's time base,
// of LMIC os, since we use LMIC's ostime_t txEnd as tx timestamp. // since we use LMIC's ostime_t txEnd as tx timestamp.
// apply calibration factor for processing time // Finally apply calibration const for processing time.
time_offset_ms += time_offset_ms +=
milliseconds(osticks2ms(os_getTime())) + milliseconds(TIME_SYNC_FIXUP); milliseconds(osticks2ms(os_getTime())) + milliseconds(TIME_SYNC_FIXUP);
@ -136,36 +139,31 @@ void process_timesync_req(void *taskparameter) {
// adjust system time // adjust system time
if (timeIsValid(time_to_set)) { if (timeIsValid(time_to_set)) {
#ifdef HAS_RTC // wait until top of second with 4ms precision
// get and lock access to i2c before we start time sync time_to_set++; // advance time 1 sec wait time
if (I2C_MUTEX_LOCK()) { vTaskDelay(pdMS_TO_TICKS(1000 - time_to_set_fraction_msec));
#endif
// wait until top of second with 4ms precision
time_to_set++; // advance time 1 sec wait time
vTaskDelay(pdMS_TO_TICKS(1000 - time_to_set_fraction_msec));
#ifdef HAS_RTC #ifdef HAS_RTC
// set RTC time and, if he have, calibrate RTC_INT pulse on top of second // set RTC time and calibrate RTC_INT pulse on top of second
set_rtctime(time_to_set, no_mutex); set_rtctime(time_to_set, no_mutex);
I2C_MUTEX_UNLOCK();
} // release i2c bus access
#endif #endif
#if (!defined GPS_INT && !defined RTC_INT) #if (!defined GPS_INT && !defined RTC_INT)
// sync pps timer to top of second // sync pps timer to top of second
timerRestart(ppsIRQ); // reset pps timer timerRestart(ppsIRQ); // reset pps timer
CLOCKIRQ(); // fire clock pps CLOCKIRQ(); // fire clock pps
#endif #endif
setTime(time_to_set); // set the time on top of second setTime(time_to_set); // set the time on top of second
// end of time critical section: release I2C bus
I2C_MUTEX_UNLOCK();
timeSource = _lora; timeSource = _lora;
timesyncer.attach(TIME_SYNC_INTERVAL * 60, timesyncer.attach(TIME_SYNC_INTERVAL * 60,
timeSync); // set to regular repeat timeSync); // set to regular repeat
ESP_LOGI(TAG, "[%0.3f] Timesync finished, time was adjusted", ESP_LOGI(TAG, "[%0.3f] Timesync finished, time was adjusted",
millis() / 1000.0); millis() / 1000.0);
} else } else
ESP_LOGW(TAG, "[%0.3f] Timesync failed, outdated time calculated", ESP_LOGW(TAG, "[%0.3f] Timesync failed, outdated time calculated",
millis() / 1000.0); millis() / 1000.0);