timesync.cpp: code sanitizations

This commit is contained in:
Verkehrsrot 2019-03-28 14:19:02 +01:00
parent 0640e4dfb1
commit 72f9b321f7
2 changed files with 12 additions and 5 deletions

View File

@ -462,8 +462,8 @@ void lora_housekeeping(void) {
// uxTaskGetStackHighWaterMark(LoraTask)); // uxTaskGetStackHighWaterMark(LoraTask));
} }
void user_request_network_time_callback(void *pVoidUserUTCTime, void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime,
int flagSuccess) { int flagSuccess) {
// Explicit conversion from void* to uint32_t* to avoid compiler errors // Explicit conversion from void* to uint32_t* to avoid compiler errors
time_t *pUserUTCTime = (time_t *)pVoidUserUTCTime; time_t *pUserUTCTime = (time_t *)pVoidUserUTCTime;
@ -487,6 +487,11 @@ void user_request_network_time_callback(void *pVoidUserUTCTime,
return; return;
} }
// begin of time critical section: lock I2C bus to ensure accurate timing
// don't move the mutex, will impact accuracy of time up to 1 sec!
if (!I2C_MUTEX_LOCK())
return; // failure
// Update userUTCTime, considering the difference between the GPS and UTC // Update userUTCTime, considering the difference between the GPS and UTC
// time, and the leap seconds until year 2019 // time, and the leap seconds until year 2019
*pUserUTCTime = lmicTimeReference.tNetwork + 315964800; *pUserUTCTime = lmicTimeReference.tNetwork + 315964800;
@ -501,6 +506,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
adjustTime(*pUserUTCTime + requestDelaySec, 0); adjustTime(*pUserUTCTime + requestDelaySec, 0);
// end of time critical section: release I2C bus
I2C_MUTEX_UNLOCK();
} // user_request_network_time_callback } // user_request_network_time_callback
#endif // HAS_LORA #endif // HAS_LORA

View File

@ -63,9 +63,9 @@ void send_timesync_req() {
void process_timesync_req(void *taskparameter) { void process_timesync_req(void *taskparameter) {
uint8_t k = 0; uint8_t k = 0;
uint16_t time_to_set_fraction_msec;
uint32_t seq_no = 0, time_to_set; uint32_t seq_no = 0, time_to_set;
auto time_offset_ms = myClock_msecTick::zero(); auto time_offset_ms = myClock_msecTick::zero();
uint16_t time_to_set_fraction_msec;
// wait until we are joined // wait until we are joined
while (!LMIC.devaddr) { while (!LMIC.devaddr) {
@ -135,7 +135,6 @@ void process_timesync_req(void *taskparameter) {
// end of time critical section: release I2C bus // end of time critical section: release I2C bus
I2C_MUTEX_UNLOCK(); I2C_MUTEX_UNLOCK();
finish:
lora_time_sync_pending = false; lora_time_sync_pending = false;
timeSyncReqTask = NULL; timeSyncReqTask = NULL;
vTaskDelete(NULL); // end task vTaskDelete(NULL); // end task
@ -213,7 +212,7 @@ int recv_timesync_ans(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
int adjustTime(uint32_t t_sec, uint16_t t_msec) { int IRAM_ATTR adjustTime(uint32_t t_sec, uint16_t t_msec) {
time_t time_to_set = (time_t)t_sec; time_t time_to_set = (time_t)t_sec;