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" #include "timekeeper.h"
void irqHandler(void *pvParameters); void irqHandler(void *pvParameters);
int mask_user_IRQ(); void mask_user_IRQ();
int unmask_user_IRQ(); void unmask_user_IRQ();
#ifdef HAS_DISPLAY #ifdef HAS_DISPLAY
void IRAM_ATTR DisplayIRQ(); void IRAM_ATTR DisplayIRQ();

View File

@ -119,19 +119,6 @@ void IRAM_ATTR GpsIRQ() {
} }
#endif #endif
int mask_user_IRQ() { void mask_user_IRQ() { xTaskNotify(irqHandlerTask, MASK_IRQ, eSetBits); }
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
}
}
int unmask_user_IRQ() { void unmask_user_IRQ() { xTaskNotify(irqHandlerTask, UNMASK_IRQ, eSetBits); }
// end of time critical section: release I2C bus
I2C_MUTEX_UNLOCK();
xTaskNotify(irqHandlerTask, UNMASK_IRQ, eSetBits);
return 0;
}

View File

@ -507,9 +507,15 @@ void IRAM_ATTR user_request_network_time_callback(void *pVoidUserUTCTime,
return; return;
} }
// begin of time critical section: lock I2C bus to ensure accurate timing // begin of time critical section
if (!mask_user_IRQ())
return; // failure // 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 // 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
@ -525,7 +531,9 @@ void IRAM_ATTR 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
setMyTime(*pUserUTCTime + requestDelaySec, 0); 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(); unmask_user_IRQ();
} // user_request_network_time_callback } // 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 } // end of for loop to collect timestamp samples
// begin of time critical section: lock app irq's and I2C bus // lock I2C bus and application irq to ensure accurate timing
if (!mask_user_IRQ()) { if (!I2C_MUTEX_LOCK()) {
ESP_LOGW(TAG, ESP_LOGW(TAG, "[%0.3f] Timesync handshake error: i2c bus locking failed",
"[%0.3f] Timesync handshake error: irq masking failed",
millis() / 1000.0); millis() / 1000.0);
goto finish; // failure goto finish; // failure
} }
mask_user_IRQ();
// average time offset over all collected diffs // average time offset over all collected diffs
time_offset_ms /= TIME_SYNC_SAMPLES; 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); setMyTime(time_to_set, time_to_set_fraction_msec);
finish: 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(); unmask_user_IRQ();
timeSyncPending = false; timeSyncPending = false;
} // infinite while(1) } // infinite while(1)