timesync code sanitziations

This commit is contained in:
Klaus K Wilting 2020-03-11 23:49:06 +01:00
parent e8664c8505
commit 42ee21a2f7
4 changed files with 25 additions and 28 deletions

View File

@ -20,8 +20,8 @@ enum timesync_t {
};
void timesync_init(void);
void timesync_sendReq(void);
void timesync_storeReq(uint32_t timestamp, timesync_t timestamp_type);
void timesync_request(void);
void timesync_store(uint32_t timestamp, timesync_t timestamp_type);
void IRAM_ATTR timesync_processReq(void *taskparameter);
void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag);

View File

@ -251,7 +251,7 @@ void lora_send(void *pvParameters) {
// if last packet sent was a timesync request, store TX timestamp
if (SendBuffer.MessagePort == TIMEPORT)
// store LMIC time when we started transmit of timesync request
timesync_storeReq(osticks2ms(os_getTime()), timesync_tx);
timesync_store(osticks2ms(os_getTime()), timesync_tx);
#endif
ESP_LOGI(TAG, "%d byte(s) sent to LORA", SendBuffer.MessageSize);

View File

@ -30,30 +30,27 @@ void calibrateTime(void) {
// kick off asychronous lora timesync if we have
#if (HAS_LORA) && (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)
timesync_sendReq();
timesync_request();
#endif
// only if we lost time, we try to fallback to local time source RTS or GPS
// (only!) if we lost time, we try to fallback to local time source RTS or GPS
if (timeSource == _unsynced) {
// has RTC -> fallback to RTC time
#ifdef HAS_RTC
t = get_rtctime();
if (t) {
timeSource = _rtc;
goto finish;
}
#endif
// no RTC -> fallback to GPS time
#if (HAS_GPS)
// fetch recent time from last NMEA record
t = fetch_gpsTime(&t_msec);
if (t) {
t = get_gpstime(&t_msec);
timeSource = _gps;
goto finish;
}
#endif
if (t)
setMyTime((uint32_t)t, t_msec, timeSource); // set time
} // fallback
else
@ -61,10 +58,6 @@ void calibrateTime(void) {
// no fallback time source available -> we can't set time
return;
finish:
setMyTime((uint32_t)t, t_msec, timeSource); // set time
} // calibrateTime()
// adjust system time, calibrate RTC and RTC_INT pps

View File

@ -42,7 +42,7 @@ void timesync_init() {
}
// kickoff asnychronous timesync handshake
void timesync_sendReq(void) {
void timesync_request(void) {
// exit if a timesync handshake is already running
if (timeSyncPending)
return;
@ -60,7 +60,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
uint32_t rcv_seqNo = TIME_SYNC_END_FLAG, time_offset_ms;
// this task is an endless loop, waiting in blocked mode, until it is
// unblocked by timesync_sendReq(). It then waits to be notified from
// unblocked by timesync_request(). It then waits to be notified from
// timesync_serverAnswer(), which is called from LMIC each time a timestamp
// from the timesource via LORAWAN arrived.
@ -158,7 +158,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
}
// store incoming timestamps
void timesync_storeReq(uint32_t timestamp, timesync_t timestamp_type) {
void timesync_store(uint32_t timestamp, timesync_t timestamp_type) {
ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: timestamp(t%d)=%d", millis() / 1000.0,
time_sync_seqNo, sample_idx, timestamp_type, timestamp);
timesync_timestamp[sample_idx][timestamp_type] = timestamp;
@ -174,6 +174,9 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
// mask application irq to ensure accurate timing
mask_user_IRQ();
// store LMIC time when we received the timesync answer
ostime_t rxTime = osticks2ms(os_getTime());
int rc = 0;
uint8_t rcv_seqNo = *(uint8_t *)pUserData;
uint16_t timestamp_msec = 0;
@ -184,8 +187,8 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
// pUserData: contains pointer to payload buffer
// flag: length of buffer
// store LMIC time when we received the timesync answer
timesync_storeReq(osticks2ms(os_getTime()), timesync_rx);
// Store the instant the time request of the node was received on the gateway
timesync_store(rxTime, timesync_rx);
// parse timesync_answer:
// byte meaning
@ -240,8 +243,9 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
// Calculate UTCTime, considering the difference between GPS and UTC time
timestamp_sec = lmicTime.tNetwork + GPS_UTC_DIFF;
// Add delay between the instant the time was transmitted and the current time
timestamp_msec = osticks2ms(os_getTime() - lmicTime.tLocal);
// Add delay between the instant the time was received on the gateway and the
// current time on the node
timestamp_msec = rxTime - lmicTime.tLocal;
goto Finish;
#endif // (TIME_SYNC_LORAWAN)
@ -250,8 +254,8 @@ Finish:
// check if calculated time is recent
if (timeIsValid(timestamp_sec)) {
// store time received from gateway
timesync_storeReq(timestamp_sec, gwtime_sec);
timesync_storeReq(timestamp_msec, gwtime_msec);
timesync_store(timestamp_sec, gwtime_sec);
timesync_store(timestamp_msec, gwtime_msec);
// success
rc = 1;
} else {