further fixes in mutex lock & irq masking

This commit is contained in:
Verkehrsrot 2019-07-23 20:07:24 +02:00
parent 7d2d753252
commit 8869909720
4 changed files with 23 additions and 26 deletions

View File

@ -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();

View File

@ -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;
}
void unmask_user_IRQ() { xTaskNotify(irqHandlerTask, UNMASK_IRQ, eSetBits); }

View File

@ -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

View File

@ -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)