timesync code sanitziations
This commit is contained in:
parent
e8664c8505
commit
42ee21a2f7
@ -20,8 +20,8 @@ enum timesync_t {
|
|||||||
};
|
};
|
||||||
|
|
||||||
void timesync_init(void);
|
void timesync_init(void);
|
||||||
void timesync_sendReq(void);
|
void timesync_request(void);
|
||||||
void timesync_storeReq(uint32_t timestamp, timesync_t timestamp_type);
|
void timesync_store(uint32_t timestamp, timesync_t timestamp_type);
|
||||||
void IRAM_ATTR timesync_processReq(void *taskparameter);
|
void IRAM_ATTR timesync_processReq(void *taskparameter);
|
||||||
void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag);
|
void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag);
|
||||||
|
|
||||||
|
@ -251,7 +251,7 @@ void lora_send(void *pvParameters) {
|
|||||||
// if last packet sent was a timesync request, store TX timestamp
|
// if last packet sent was a timesync request, store TX timestamp
|
||||||
if (SendBuffer.MessagePort == TIMEPORT)
|
if (SendBuffer.MessagePort == TIMEPORT)
|
||||||
// store LMIC time when we started transmit of timesync request
|
// 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
|
#endif
|
||||||
|
|
||||||
ESP_LOGI(TAG, "%d byte(s) sent to LORA", SendBuffer.MessageSize);
|
ESP_LOGI(TAG, "%d byte(s) sent to LORA", SendBuffer.MessageSize);
|
||||||
|
@ -30,30 +30,27 @@ void calibrateTime(void) {
|
|||||||
|
|
||||||
// kick off asychronous lora timesync if we have
|
// kick off asychronous lora timesync if we have
|
||||||
#if (HAS_LORA) && (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)
|
#if (HAS_LORA) && (TIME_SYNC_LORASERVER) || (TIME_SYNC_LORAWAN)
|
||||||
timesync_sendReq();
|
timesync_request();
|
||||||
#endif
|
#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) {
|
if (timeSource == _unsynced) {
|
||||||
|
|
||||||
// has RTC -> fallback to RTC time
|
// has RTC -> fallback to RTC time
|
||||||
#ifdef HAS_RTC
|
#ifdef HAS_RTC
|
||||||
t = get_rtctime();
|
t = get_rtctime();
|
||||||
if (t) {
|
timeSource = _rtc;
|
||||||
timeSource = _rtc;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// no RTC -> fallback to GPS time
|
// no RTC -> fallback to GPS time
|
||||||
#if (HAS_GPS)
|
#if (HAS_GPS)
|
||||||
// fetch recent time from last NMEA record
|
t = get_gpstime(&t_msec);
|
||||||
t = fetch_gpsTime(&t_msec);
|
timeSource = _gps;
|
||||||
if (t) {
|
|
||||||
timeSource = _gps;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
if (t)
|
||||||
|
setMyTime((uint32_t)t, t_msec, timeSource); // set time
|
||||||
|
|
||||||
} // fallback
|
} // fallback
|
||||||
|
|
||||||
else
|
else
|
||||||
@ -61,10 +58,6 @@ void calibrateTime(void) {
|
|||||||
// no fallback time source available -> we can't set time
|
// no fallback time source available -> we can't set time
|
||||||
return;
|
return;
|
||||||
|
|
||||||
finish:
|
|
||||||
|
|
||||||
setMyTime((uint32_t)t, t_msec, timeSource); // set time
|
|
||||||
|
|
||||||
} // calibrateTime()
|
} // calibrateTime()
|
||||||
|
|
||||||
// adjust system time, calibrate RTC and RTC_INT pps
|
// adjust system time, calibrate RTC and RTC_INT pps
|
||||||
|
@ -42,7 +42,7 @@ void timesync_init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// kickoff asnychronous timesync handshake
|
// kickoff asnychronous timesync handshake
|
||||||
void timesync_sendReq(void) {
|
void timesync_request(void) {
|
||||||
// exit if a timesync handshake is already running
|
// exit if a timesync handshake is already running
|
||||||
if (timeSyncPending)
|
if (timeSyncPending)
|
||||||
return;
|
return;
|
||||||
@ -60,7 +60,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
|
|||||||
uint32_t rcv_seqNo = TIME_SYNC_END_FLAG, time_offset_ms;
|
uint32_t rcv_seqNo = TIME_SYNC_END_FLAG, time_offset_ms;
|
||||||
|
|
||||||
// this task is an endless loop, waiting in blocked mode, until it is
|
// 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
|
// timesync_serverAnswer(), which is called from LMIC each time a timestamp
|
||||||
// from the timesource via LORAWAN arrived.
|
// from the timesource via LORAWAN arrived.
|
||||||
|
|
||||||
@ -158,7 +158,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// store incoming timestamps
|
// 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,
|
ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: timestamp(t%d)=%d", millis() / 1000.0,
|
||||||
time_sync_seqNo, sample_idx, timestamp_type, timestamp);
|
time_sync_seqNo, sample_idx, timestamp_type, timestamp);
|
||||||
timesync_timestamp[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 application irq to ensure accurate timing
|
||||||
mask_user_IRQ();
|
mask_user_IRQ();
|
||||||
|
|
||||||
|
// store LMIC time when we received the timesync answer
|
||||||
|
ostime_t rxTime = osticks2ms(os_getTime());
|
||||||
|
|
||||||
int rc = 0;
|
int rc = 0;
|
||||||
uint8_t rcv_seqNo = *(uint8_t *)pUserData;
|
uint8_t rcv_seqNo = *(uint8_t *)pUserData;
|
||||||
uint16_t timestamp_msec = 0;
|
uint16_t timestamp_msec = 0;
|
||||||
@ -184,8 +187,8 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
|
|||||||
// pUserData: contains pointer to payload buffer
|
// pUserData: contains pointer to payload buffer
|
||||||
// flag: length of buffer
|
// flag: length of buffer
|
||||||
|
|
||||||
// store LMIC time when we received the timesync answer
|
// Store the instant the time request of the node was received on the gateway
|
||||||
timesync_storeReq(osticks2ms(os_getTime()), timesync_rx);
|
timesync_store(rxTime, timesync_rx);
|
||||||
|
|
||||||
// parse timesync_answer:
|
// parse timesync_answer:
|
||||||
// byte meaning
|
// 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
|
// Calculate UTCTime, considering the difference between GPS and UTC time
|
||||||
timestamp_sec = lmicTime.tNetwork + GPS_UTC_DIFF;
|
timestamp_sec = lmicTime.tNetwork + GPS_UTC_DIFF;
|
||||||
// Add delay between the instant the time was transmitted and the current time
|
// Add delay between the instant the time was received on the gateway and the
|
||||||
timestamp_msec = osticks2ms(os_getTime() - lmicTime.tLocal);
|
// current time on the node
|
||||||
|
timestamp_msec = rxTime - lmicTime.tLocal;
|
||||||
goto Finish;
|
goto Finish;
|
||||||
|
|
||||||
#endif // (TIME_SYNC_LORAWAN)
|
#endif // (TIME_SYNC_LORAWAN)
|
||||||
@ -250,8 +254,8 @@ Finish:
|
|||||||
// check if calculated time is recent
|
// check if calculated time is recent
|
||||||
if (timeIsValid(timestamp_sec)) {
|
if (timeIsValid(timestamp_sec)) {
|
||||||
// store time received from gateway
|
// store time received from gateway
|
||||||
timesync_storeReq(timestamp_sec, gwtime_sec);
|
timesync_store(timestamp_sec, gwtime_sec);
|
||||||
timesync_storeReq(timestamp_msec, gwtime_msec);
|
timesync_store(timestamp_msec, gwtime_msec);
|
||||||
// success
|
// success
|
||||||
rc = 1;
|
rc = 1;
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user