From 88699097202e0a002e76bb491953250a7f1576a2 Mon Sep 17 00:00:00 2001 From: Verkehrsrot Date: Tue, 23 Jul 2019 20:07:24 +0200 Subject: [PATCH] further fixes in mutex lock & irq masking --- include/irqhandler.h | 4 ++-- src/irqhandler.cpp | 17 ++--------------- src/lorawan.cpp | 16 ++++++++++++---- src/timesync.cpp | 12 +++++++----- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/include/irqhandler.h b/include/irqhandler.h index db5dd2f1..3a06d3da 100644 --- a/include/irqhandler.h +++ b/include/irqhandler.h @@ -17,8 +17,8 @@ #include "timekeeper.h" void irqHandler(void *pvParameters); -int mask_user_IRQ(); -int unmask_user_IRQ(); +void mask_user_IRQ(); +void unmask_user_IRQ(); #ifdef HAS_DISPLAY void IRAM_ATTR DisplayIRQ(); diff --git a/src/irqhandler.cpp b/src/irqhandler.cpp index 1ecfa59f..fabda2f2 100644 --- a/src/irqhandler.cpp +++ b/src/irqhandler.cpp @@ -119,19 +119,6 @@ void IRAM_ATTR GpsIRQ() { } #endif -int mask_user_IRQ() { - if (!I2C_MUTEX_LOCK()) { - ESP_LOGW(TAG, "[%0.3f] i2c mutex lock failed", millis() / 1000.0); - return 0; // failure - } else { - xTaskNotify(irqHandlerTask, MASK_IRQ, eSetBits); - return 1; // success - } -} +void mask_user_IRQ() { xTaskNotify(irqHandlerTask, MASK_IRQ, eSetBits); } -int unmask_user_IRQ() { - // end of time critical section: release I2C bus - I2C_MUTEX_UNLOCK(); - xTaskNotify(irqHandlerTask, UNMASK_IRQ, eSetBits); - return 0; -} \ No newline at end of file +void unmask_user_IRQ() { xTaskNotify(irqHandlerTask, UNMASK_IRQ, eSetBits); } \ No newline at end of file diff --git a/src/lorawan.cpp b/src/lorawan.cpp index 12cae030..07d893c8 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -507,9 +507,15 @@ void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime, return; } - // begin of time critical section: lock I2C bus to ensure accurate timing - if (!mask_user_IRQ()) - return; // failure + // begin of time critical section + + // lock I2C bus and application irq to ensure accurate timing + if (!I2C_MUTEX_LOCK()) { + ESP_LOGW(TAG, "[%0.3f] Timesync handshake error: i2c bus locking failed", + millis() / 1000.0); + goto finish; // failure + } + mask_user_IRQ(); // Update userUTCTime, considering the difference between the GPS and UTC // time, and the leap seconds until year 2019 @@ -525,7 +531,9 @@ void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime, // Update system time with time read from the network setMyTime(*pUserUTCTime + requestDelaySec, 0); - // end of time critical section: release I2C bus +finish: + // end of time critical section: release I2C bus and app irq + I2C_MUTEX_UNLOCK();r unmask_user_IRQ(); } // user_request_network_time_callback diff --git a/src/timesync.cpp b/src/timesync.cpp index ec85370b..00d80bcf 100644 --- a/src/timesync.cpp +++ b/src/timesync.cpp @@ -110,13 +110,13 @@ void process_timesync_req(void *taskparameter) { } } // end of for loop to collect timestamp samples - // begin of time critical section: lock app irq's and I2C bus - if (!mask_user_IRQ()) { - ESP_LOGW(TAG, - "[%0.3f] Timesync handshake error: irq masking failed", + // lock I2C bus and application irq to ensure accurate timing + if (!I2C_MUTEX_LOCK()) { + ESP_LOGW(TAG, "[%0.3f] Timesync handshake error: i2c bus locking failed", millis() / 1000.0); goto finish; // failure } + mask_user_IRQ(); // average time offset over all collected diffs time_offset_ms /= TIME_SYNC_SAMPLES; @@ -136,8 +136,10 @@ void process_timesync_req(void *taskparameter) { setMyTime(time_to_set, time_to_set_fraction_msec); finish: - // end of time critical section: release I2C bus and re-enable app irq's + // end of time critical section: release I2C bus and app irq + I2C_MUTEX_UNLOCK(); unmask_user_IRQ(); + timeSyncPending = false; } // infinite while(1)