diff --git a/include/globals.h b/include/globals.h index bab56cb3..f132c040 100644 --- a/include/globals.h +++ b/include/globals.h @@ -99,6 +99,7 @@ typedef struct { enum sendprio_t { prio_low, prio_normal, prio_high }; enum timesource_t { _gps, _rtc, _lora, _unsynced }; +enum mutexselect_t { no_mutex, do_mutex}; extern std::set, Mallocator> macs; extern std::array::iterator it; diff --git a/include/rtctime.h b/include/rtctime.h index 3429d44e..08baa9f6 100644 --- a/include/rtctime.h +++ b/include/rtctime.h @@ -9,7 +9,7 @@ extern RtcDS3231 Rtc; // make RTC instance globally available uint8_t rtc_init(void); -uint8_t set_rtctime(time_t t); +uint8_t set_rtctime(time_t t, mutexselect_t mutex); void sync_rtctime(void); time_t get_rtctime(void); float get_rtctemp(void); diff --git a/src/rtctime.cpp b/src/rtctime.cpp index ceabbb20..086beb29 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -46,14 +46,15 @@ uint8_t rtc_init(void) { } // rtc_init() -uint8_t set_rtctime(time_t t) { // t is UTC in seconds epoch time - if (I2C_MUTEX_LOCK()) { - Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000 +uint8_t set_rtctime(time_t t, mutexselect_t mutex) { // t is sec epoch time + if (!mutex || I2C_MUTEX_LOCK()) { #ifdef RTC_INT // sync rtc 1Hz pulse on top of second Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeNone); // off Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock); // start #endif - I2C_MUTEX_UNLOCK(); + Rtc.SetDateTime(RtcDateTime(t - SECS_YR_2000)); // epoch -> sec2000 + if (mutex) + I2C_MUTEX_UNLOCK(); ESP_LOGI(TAG, "RTC time synced"); return 1; // success } else {