timesync code sanitized
This commit is contained in:
parent
bf70f2f9f2
commit
139738a14d
@ -405,11 +405,11 @@ Note: all settings are stored in NVRAM and will be reloaded when device starts.
|
|||||||
bytes 1..4 = time/date in UTC epoch seconds (LSB)
|
bytes 1..4 = time/date in UTC epoch seconds (LSB)
|
||||||
byte 5 = time source & status, see below
|
byte 5 = time source & status, see below
|
||||||
|
|
||||||
bits 0..3 time source
|
bits 0..3 last seen time source
|
||||||
0x00 = GPS
|
0x00 = GPS
|
||||||
0x01 = RTC
|
0x01 = RTC
|
||||||
0x02 = LORA
|
0x02 = LORA
|
||||||
0x03 = unsynched
|
0x03 = unsynched (never synched)
|
||||||
|
|
||||||
bits 4..7 time status
|
bits 4..7 time status
|
||||||
0x00 = timeNotSet (never synched)
|
0x00 = timeNotSet (never synched)
|
||||||
|
@ -26,7 +26,7 @@ void timepulse_start(void);
|
|||||||
void timeSync(void);
|
void timeSync(void);
|
||||||
uint8_t timepulse_init(void);
|
uint8_t timepulse_init(void);
|
||||||
time_t timeIsValid(time_t const t);
|
time_t timeIsValid(time_t const t);
|
||||||
time_t timeProvider(void);
|
void calibrateTime(void);
|
||||||
time_t compiledUTC(void);
|
time_t compiledUTC(void);
|
||||||
TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config,
|
TickType_t tx_Ticks(uint32_t framesize, unsigned long baud, uint32_t config,
|
||||||
int8_t rxPin, int8_t txPins);
|
int8_t rxPin, int8_t txPins);
|
||||||
|
@ -15,6 +15,6 @@ void send_timesync_req(void);
|
|||||||
int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len);
|
int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len);
|
||||||
void process_timesync_req(void *taskparameter);
|
void process_timesync_req(void *taskparameter);
|
||||||
void store_time_sync_req(uint32_t t_millisec);
|
void store_time_sync_req(uint32_t t_millisec);
|
||||||
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec);
|
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec, timesource_t timesource);
|
||||||
|
|
||||||
#endif
|
#endif
|
@ -59,9 +59,7 @@ void irqHandler(void *pvParameters) {
|
|||||||
// is time to be synced?
|
// is time to be synced?
|
||||||
if (InterruptStatus & TIMESYNC_IRQ) {
|
if (InterruptStatus & TIMESYNC_IRQ) {
|
||||||
now(); // ensure sysTime is recent
|
now(); // ensure sysTime is recent
|
||||||
time_t t = timeProvider();
|
calibrateTime();
|
||||||
if (timeIsValid(t))
|
|
||||||
setTime(t);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -464,11 +464,6 @@ void lora_enqueuedata(MessageBuffer_t *message, sendprio_t prio) {
|
|||||||
|
|
||||||
void lora_queuereset(void) { xQueueReset(LoraSendQueue); }
|
void lora_queuereset(void) { xQueueReset(LoraSendQueue); }
|
||||||
|
|
||||||
void lora_housekeeping(void) {
|
|
||||||
// ESP_LOGD(TAG, "loraloop %d bytes left",
|
|
||||||
// uxTaskGetStackHighWaterMark(LoraTask));
|
|
||||||
}
|
|
||||||
|
|
||||||
#if (TIME_SYNC_LORAWAN)
|
#if (TIME_SYNC_LORAWAN)
|
||||||
void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime,
|
void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime,
|
||||||
int flagSuccess) {
|
int flagSuccess) {
|
||||||
@ -510,7 +505,7 @@ void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime,
|
|||||||
time_t requestDelaySec = osticks2ms(ticksNow - ticksRequestSent) / 1000;
|
time_t requestDelaySec = osticks2ms(ticksNow - ticksRequestSent) / 1000;
|
||||||
|
|
||||||
// Update system time with time read from the network
|
// Update system time with time read from the network
|
||||||
setMyTime(*pUserUTCTime + requestDelaySec, 0);
|
setMyTime(*pUserUTCTime + requestDelaySec, 0, _lora);
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
// end of time critical section: release app irq lock
|
// end of time critical section: release app irq lock
|
||||||
@ -523,7 +518,7 @@ finish:
|
|||||||
void lmictask(void *pvParameters) {
|
void lmictask(void *pvParameters) {
|
||||||
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
|
configASSERT(((uint32_t)pvParameters) == 1); // FreeRTOS check
|
||||||
|
|
||||||
os_init(); // initialize lmic run-time environment on core 1
|
os_init(); // initialize lmic run-time environment
|
||||||
LMIC_reset(); // initialize lmic MAC
|
LMIC_reset(); // initialize lmic MAC
|
||||||
LMIC_setLinkCheckMode(0);
|
LMIC_setLinkCheckMode(0);
|
||||||
// This tells LMIC to make the receive windows bigger, in case your clock is
|
// This tells LMIC to make the receive windows bigger, in case your clock is
|
||||||
|
@ -22,9 +22,10 @@ Ticker timesyncer;
|
|||||||
|
|
||||||
void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
|
void timeSync() { xTaskNotify(irqHandlerTask, TIMESYNC_IRQ, eSetBits); }
|
||||||
|
|
||||||
time_t timeProvider(void) {
|
void calibrateTime(void) {
|
||||||
|
|
||||||
time_t t = 0;
|
time_t t = 0;
|
||||||
|
timesource_t timeSource;
|
||||||
|
|
||||||
#if (HAS_GPS)
|
#if (HAS_GPS)
|
||||||
// fetch recent time from last NMEA record
|
// fetch recent time from last NMEA record
|
||||||
@ -34,9 +35,7 @@ time_t timeProvider(void) {
|
|||||||
set_rtctime(t); // calibrate RTC
|
set_rtctime(t); // calibrate RTC
|
||||||
#endif
|
#endif
|
||||||
timeSource = _gps;
|
timeSource = _gps;
|
||||||
timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); // regular repeat
|
goto finish;
|
||||||
ESP_LOGD(TAG, "GPS time = %d", t);
|
|
||||||
return t;
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -45,8 +44,7 @@ time_t timeProvider(void) {
|
|||||||
t = get_rtctime();
|
t = get_rtctime();
|
||||||
if (t) {
|
if (t) {
|
||||||
timeSource = _rtc;
|
timeSource = _rtc;
|
||||||
timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, timeSync); // short retry
|
goto finish
|
||||||
ESP_LOGD(TAG, "RTC time = %d", t);
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@ -58,14 +56,17 @@ time_t timeProvider(void) {
|
|||||||
LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
|
LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (!t) {
|
finish:
|
||||||
timeSource = _unsynced;
|
|
||||||
timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, timeSync); // short retry
|
if (t) { // sync successful
|
||||||
|
setMyTime(t, 0, timeSource); // set time
|
||||||
|
timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync);
|
||||||
|
ESP_LOGD(TAG, "time = %d | source: %c", t, timeSetSymbols[timeSource]);
|
||||||
|
} else { // sync failed, we want to retry shortly
|
||||||
|
timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, timeSync);
|
||||||
}
|
}
|
||||||
|
|
||||||
return t;
|
} // calibrateTime()
|
||||||
|
|
||||||
} // timeProvider()
|
|
||||||
|
|
||||||
// helper function to setup a pulse per second for time synchronisation
|
// helper function to setup a pulse per second for time synchronisation
|
||||||
uint8_t timepulse_init() {
|
uint8_t timepulse_init() {
|
||||||
|
@ -128,7 +128,7 @@ void process_timesync_req(void *taskparameter) {
|
|||||||
// calculate fraction milliseconds
|
// calculate fraction milliseconds
|
||||||
time_to_set_fraction_msec = (uint16_t)(time_offset_ms.count() % 1000);
|
time_to_set_fraction_msec = (uint16_t)(time_offset_ms.count() % 1000);
|
||||||
|
|
||||||
setMyTime(time_to_set, time_to_set_fraction_msec);
|
setMyTime(time_to_set, time_to_set_fraction_msec, _lora);
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
// end of time critical section: release app irq lock
|
// end of time critical section: release app irq lock
|
||||||
@ -208,7 +208,8 @@ int recv_timesync_ans(uint8_t seq_no, uint8_t buf[], uint8_t buf_len) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// adjust system time, calibrate RTC and RTC_INT pps
|
// adjust system time, calibrate RTC and RTC_INT pps
|
||||||
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec) {
|
void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec,
|
||||||
|
timesource_t timesource) {
|
||||||
|
|
||||||
time_t time_to_set = (time_t)(t_sec + 1);
|
time_t time_to_set = (time_t)(t_sec + 1);
|
||||||
|
|
||||||
@ -218,7 +219,8 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec) {
|
|||||||
if (timeIsValid(time_to_set)) {
|
if (timeIsValid(time_to_set)) {
|
||||||
|
|
||||||
// wait until top of second with millisecond precision
|
// wait until top of second with millisecond precision
|
||||||
vTaskDelay(pdMS_TO_TICKS(1000 - t_msec));
|
if (t_msec)
|
||||||
|
vTaskDelay(pdMS_TO_TICKS(1000 - t_msec));
|
||||||
|
|
||||||
// set RTC time and calibrate RTC_INT pulse on top of second
|
// set RTC time and calibrate RTC_INT pulse on top of second
|
||||||
#ifdef HAS_RTC
|
#ifdef HAS_RTC
|
||||||
@ -233,7 +235,7 @@ void IRAM_ATTR setMyTime(uint32_t t_sec, uint16_t t_msec) {
|
|||||||
|
|
||||||
setTime(time_to_set); // set the time on top of second
|
setTime(time_to_set); // set the time on top of second
|
||||||
|
|
||||||
timeSource = _lora;
|
timeSource = timesource;
|
||||||
timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); // regular repeat
|
timesyncer.attach(TIME_SYNC_INTERVAL * 60, timeSync); // 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);
|
||||||
|
Loading…
Reference in New Issue
Block a user