timesync fixes (using ostime_t now)
This commit is contained in:
parent
cb65d0ed6b
commit
2a55e9a8c6
@ -7,7 +7,7 @@
|
|||||||
#include "timekeeper.h"
|
#include "timekeeper.h"
|
||||||
|
|
||||||
#define TIME_SYNC_SAMPLES 2 // number of time requests for averaging
|
#define TIME_SYNC_SAMPLES 2 // number of time requests for averaging
|
||||||
#define TIME_SYNC_CYCLE 2 // seconds between two time requests
|
#define TIME_SYNC_CYCLE 20 // seconds between two time requests
|
||||||
#define TIME_SYNC_TIMEOUT 120 // timeout seconds waiting for timeserver answer
|
#define TIME_SYNC_TIMEOUT 120 // timeout seconds waiting for timeserver answer
|
||||||
#define TIME_SYNC_TRIGGER 100 // time deviation in millisec triggering a sync
|
#define TIME_SYNC_TRIGGER 100 // time deviation in millisec triggering a sync
|
||||||
#define TIME_SYNC_FRAME_LENGTH 0x06 // timeserver answer frame length
|
#define TIME_SYNC_FRAME_LENGTH 0x06 // timeserver answer frame length
|
||||||
|
@ -490,6 +490,9 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
|
|||||||
// Update system time with time read from the network
|
// Update system time with time read from the network
|
||||||
if (timeIsValid(*pUserUTCTime)) {
|
if (timeIsValid(*pUserUTCTime)) {
|
||||||
setTime(*pUserUTCTime);
|
setTime(*pUserUTCTime);
|
||||||
|
#ifdef HAS_RTC
|
||||||
|
set_rtctime(*pUserUTCTime); // calibrate RTC if we have one
|
||||||
|
#endif
|
||||||
timeSource = _lora;
|
timeSource = _lora;
|
||||||
timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); // regular repeat
|
timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); // regular repeat
|
||||||
ESP_LOGI(TAG, "Received recent time from LoRa");
|
ESP_LOGI(TAG, "Received recent time from LoRa");
|
||||||
|
@ -20,8 +20,8 @@ static const char TAG[] = __FILE__;
|
|||||||
|
|
||||||
TaskHandle_t timeSyncReqTask;
|
TaskHandle_t timeSyncReqTask;
|
||||||
|
|
||||||
static uint8_t time_sync_seqNo{};
|
static uint8_t time_sync_seqNo = 0;
|
||||||
static bool lora_time_sync_pending{false};
|
static bool lora_time_sync_pending = false;
|
||||||
|
|
||||||
typedef std::chrono::system_clock myClock;
|
typedef std::chrono::system_clock myClock;
|
||||||
typedef myClock::time_point myClock_timepoint;
|
typedef myClock::time_point myClock_timepoint;
|
||||||
@ -44,8 +44,8 @@ void send_timesync_req() {
|
|||||||
|
|
||||||
lora_time_sync_pending = true;
|
lora_time_sync_pending = true;
|
||||||
|
|
||||||
// initialize timestamp array
|
// clear timestamp array
|
||||||
for (uint8_t i{}; i < TIME_SYNC_SAMPLES; i++)
|
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++)
|
||||||
time_sync_tx[i] = time_sync_rx[i] = myClock_timepoint();
|
time_sync_tx[i] = time_sync_rx[i] = myClock_timepoint();
|
||||||
|
|
||||||
// kick off temporary task for timeserver handshake processing
|
// kick off temporary task for timeserver handshake processing
|
||||||
@ -63,12 +63,11 @@ void send_timesync_req() {
|
|||||||
// task for sending time sync requests
|
// task for sending time sync requests
|
||||||
void process_timesync_req(void *taskparameter) {
|
void process_timesync_req(void *taskparameter) {
|
||||||
|
|
||||||
uint8_t k{};
|
uint32_t seq_no = 0, time_to_set_us, time_to_set_ms;
|
||||||
uint16_t time_to_set_fraction_msec;
|
uint16_t time_to_set_fraction_msec;
|
||||||
uint32_t seq_no{}, time_to_set_us;
|
uint8_t k = 0, i = 0;
|
||||||
long long int time_to_set_ms;
|
|
||||||
time_t time_to_set;
|
time_t time_to_set;
|
||||||
auto time_offset{myClock_msecTick::zero()};
|
auto time_offset = myClock_msecTick::zero();
|
||||||
|
|
||||||
// wait until we are joined
|
// wait until we are joined
|
||||||
while (!LMIC.devaddr) {
|
while (!LMIC.devaddr) {
|
||||||
@ -76,7 +75,7 @@ void process_timesync_req(void *taskparameter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// enqueue timestamp samples in lora sendqueue
|
// enqueue timestamp samples in lora sendqueue
|
||||||
for (uint8_t i{}; i < TIME_SYNC_SAMPLES; i++) {
|
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
||||||
|
|
||||||
// wrap around seqNo 0 .. 254
|
// wrap around seqNo 0 .. 254
|
||||||
time_sync_seqNo = (time_sync_seqNo >= 255) ? 0 : time_sync_seqNo + 1;
|
time_sync_seqNo = (time_sync_seqNo >= 255) ? 0 : time_sync_seqNo + 1;
|
||||||
@ -122,20 +121,19 @@ void process_timesync_req(void *taskparameter) {
|
|||||||
ESP_LOGD(TAG, "[%0.3f] avg time diff: %0.3f sec", millis() / 1000.0,
|
ESP_LOGD(TAG, "[%0.3f] avg time diff: %0.3f sec", millis() / 1000.0,
|
||||||
myClock_secTick(time_offset).count());
|
myClock_secTick(time_offset).count());
|
||||||
|
|
||||||
// calculate absolute time with millisecond precision
|
// calculate absolute time offset with millisecond precision using time base
|
||||||
time_to_set_ms = (long long)now(time_to_set_us) * 1000LL +
|
// of LMIC os, since we use LMIC's ostime_t txEnd as tx timestamp
|
||||||
time_to_set_us / 1000LL + time_offset.count();
|
time_offset += milliseconds(osticks2ms(os_getTime()));
|
||||||
// convert to seconds
|
// convert to seconds
|
||||||
time_to_set = (time_t)(time_to_set_ms / 1000LL);
|
time_to_set = static_cast<time_t>(myClock_secTick(time_offset).count());
|
||||||
// calculate fraction milliseconds
|
// calculate fraction milliseconds
|
||||||
time_to_set_fraction_msec = (uint16_t)(time_to_set_ms % 1000LL);
|
time_to_set_fraction_msec = static_cast<uint16_t>(time_offset.count() % 1000);
|
||||||
|
|
||||||
ESP_LOGD(TAG, "[%0.3f] Calculated UTC epoch time: %d.%03d sec",
|
ESP_LOGD(TAG, "[%0.3f] Calculated UTC epoch time: %d.%03d sec",
|
||||||
millis() / 1000.0, time_to_set, time_to_set_fraction_msec);
|
millis() / 1000.0, time_to_set, time_to_set_fraction_msec);
|
||||||
|
|
||||||
// adjust system time
|
// adjust system time
|
||||||
if (timeIsValid(time_to_set)) {
|
if (timeIsValid(time_to_set)) {
|
||||||
|
|
||||||
if (abs(time_offset.count()) >=
|
if (abs(time_offset.count()) >=
|
||||||
TIME_SYNC_TRIGGER) { // milliseconds threshold
|
TIME_SYNC_TRIGGER) { // milliseconds threshold
|
||||||
|
|
||||||
@ -150,9 +148,12 @@ void process_timesync_req(void *taskparameter) {
|
|||||||
CLOCKIRQ(); // fire clock pps interrupt
|
CLOCKIRQ(); // fire clock pps interrupt
|
||||||
}
|
}
|
||||||
|
|
||||||
setTime(time_to_set + 1);
|
setTime(++time_to_set); // +1 sec after waiting for top of seceond
|
||||||
timeSource = _lora;
|
#ifdef HAS_RTC
|
||||||
|
set_rtctime(time_to_set); // calibrate RTC if we have one
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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 adjusted by %.3f sec",
|
ESP_LOGI(TAG, "[%0.3f] Timesync finished, time adjusted by %.3f sec",
|
||||||
@ -170,15 +171,15 @@ finish:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// called from lorawan.cpp after time_sync_req was sent
|
// called from lorawan.cpp after time_sync_req was sent
|
||||||
void store_time_sync_req(uint32_t t_millisec) {
|
void store_time_sync_req(uint32_t t_txEnd_ms) {
|
||||||
|
|
||||||
uint8_t k{time_sync_seqNo % TIME_SYNC_SAMPLES};
|
uint8_t k = time_sync_seqNo % TIME_SYNC_SAMPLES;
|
||||||
|
|
||||||
time_sync_tx[k] += milliseconds(t_millisec);
|
time_sync_tx[k] += milliseconds(t_txEnd_ms);
|
||||||
|
|
||||||
ESP_LOGD(TAG, "[%0.3f] Timesync request #%d sent at %d.%03d",
|
ESP_LOGD(TAG, "[%0.3f] Timesync request #%d sent at %d.%03d",
|
||||||
millis() / 1000.0, time_sync_seqNo, t_millisec / 1000,
|
millis() / 1000.0, time_sync_seqNo, t_txEnd_ms / 1000,
|
||||||
t_millisec % 1000);
|
t_txEnd_ms % 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// process timeserver timestamp answer, called from lorawan.cpp
|
// process timeserver timestamp answer, called from lorawan.cpp
|
||||||
@ -188,16 +189,16 @@ int recv_timesync_ans(uint8_t buf[], uint8_t buf_len) {
|
|||||||
if ((!lora_time_sync_pending) || (buf_len != TIME_SYNC_FRAME_LENGTH))
|
if ((!lora_time_sync_pending) || (buf_len != TIME_SYNC_FRAME_LENGTH))
|
||||||
return 0; // failure
|
return 0; // failure
|
||||||
|
|
||||||
uint8_t seq_no{buf[0]}, k{seq_no % TIME_SYNC_SAMPLES};
|
uint8_t seq_no = buf[0], k = seq_no % TIME_SYNC_SAMPLES;
|
||||||
uint16_t timestamp_msec; // convert 1/250th sec fractions to ms
|
uint16_t timestamp_msec; // convert 1/250th sec fractions to ms
|
||||||
uint32_t timestamp_sec;
|
uint32_t timestamp_sec;
|
||||||
|
|
||||||
// get the timeserver time.
|
// get the timeserver time.
|
||||||
// The first 4 bytes contain the UTC seconds since unix epoch.
|
// The first 4 bytes contain the UTC seconds since unix epoch.
|
||||||
// Octet order is little endian. Casts are necessary, because buf is an array
|
// Octet order is big endian. Casts are necessary, because buf is an array
|
||||||
// of single byte values, and they might overflow when shifted
|
// of single byte values, and they might overflow when shifted
|
||||||
timestamp_sec = ((uint32_t)buf[1]) | (((uint32_t)buf[2]) << 8) |
|
timestamp_sec = ((uint32_t)buf[4]) | (((uint32_t)buf[3]) << 8) |
|
||||||
(((uint32_t)buf[3]) << 16) | (((uint32_t)buf[4]) << 24);
|
(((uint32_t)buf[2]) << 16) | (((uint32_t)buf[1]) << 24);
|
||||||
|
|
||||||
// The 5th byte contains the fractional seconds in 2^-8 second steps
|
// The 5th byte contains the fractional seconds in 2^-8 second steps
|
||||||
timestamp_msec = 4 * buf[5];
|
timestamp_msec = 4 * buf[5];
|
||||||
|
Loading…
Reference in New Issue
Block a user