From e5dfbb11375be430b2611eb8968d37a44905234e Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sat, 7 Nov 2020 12:42:39 +0100 Subject: [PATCH] Fix RTC using manual time --- tasmota/support_rtc.ino | 80 ++++++++++++++++++++++------------------ tasmota/support_wifi.ino | 6 +-- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/tasmota/support_rtc.ino b/tasmota/support_rtc.ino index ffa350370..ff1561df8 100644 --- a/tasmota/support_rtc.ino +++ b/tasmota/support_rtc.ino @@ -371,42 +371,46 @@ uint32_t RuleToTime(TimeRule r, int yr) void RtcSecond(void) { static uint32_t last_sync = 0; + static bool mutex = false; + if (mutex) { return; } + + if (Rtc.time_synced) { + mutex = true; + + Rtc.time_synced = false; + last_sync = Rtc.utc_time; + + if (Rtc.restart_time == 0) { + Rtc.restart_time = Rtc.utc_time - TasmotaGlobal.uptime; // save first synced time as restart time + } + + TIME_T tmpTime; + BreakTime(Rtc.utc_time, tmpTime); + RtcTime.year = tmpTime.year + 1970; + Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); + Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); + + // Do not use AddLog_P( here (interrupt routine) if syslog or mqttlog is enabled. UDP/TCP will force exception 9 + PrepLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: " D_UTC_TIME " %s, " D_DST_TIME " %s, " D_STD_TIME " %s"), + GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_DST).c_str(), GetDateAndTime(DT_STD).c_str()); + + if (Rtc.local_time < START_VALID_TIME) { // 2016-01-01 + TasmotaGlobal.rules_flag.time_init = 1; + } else { + TasmotaGlobal.rules_flag.time_set = 1; + } + } else { + Rtc.utc_time++; // Increment every second + } Rtc.millis = millis(); - if (!Rtc.user_time_entry) { - if (Rtc.time_synced) { - Rtc.time_synced = false; - last_sync = Rtc.utc_time; - - if (Rtc.restart_time == 0) { - Rtc.restart_time = Rtc.utc_time - TasmotaGlobal.uptime; // save first synced time as restart time - } - - TIME_T tmpTime; - BreakTime(Rtc.utc_time, tmpTime); - RtcTime.year = tmpTime.year + 1970; - Rtc.daylight_saving_time = RuleToTime(Settings.tflag[1], RtcTime.year); - Rtc.standard_time = RuleToTime(Settings.tflag[0], RtcTime.year); - - // Do not use AddLog_P( here (interrupt routine) if syslog or mqttlog is enabled. UDP/TCP will force exception 9 - PrepLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: " D_UTC_TIME " %s, " D_DST_TIME " %s, " D_STD_TIME " %s"), - GetDateAndTime(DT_UTC).c_str(), GetDateAndTime(DT_DST).c_str(), GetDateAndTime(DT_STD).c_str()); - - if (Rtc.local_time < START_VALID_TIME) { // 2016-01-01 - TasmotaGlobal.rules_flag.time_init = 1; - } else { - TasmotaGlobal.rules_flag.time_set = 1; - } - } - if ((Rtc.utc_time > (2 * 60 * 60)) && (last_sync < Rtc.utc_time - (2 * 60 * 60))) { // Every two hours a warning - // Do not use AddLog_P( here (interrupt routine) if syslog or mqttlog is enabled. UDP/TCP will force exception 9 - PrepLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: Not synced")); - last_sync = Rtc.utc_time; - } + if ((Rtc.utc_time > (2 * 60 * 60)) && (last_sync < Rtc.utc_time - (2 * 60 * 60))) { // Every two hours a warning + // Do not use AddLog_P( here (interrupt routine) if syslog or mqttlog is enabled. UDP/TCP will force exception 9 + PrepLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: Not synced")); + last_sync = Rtc.utc_time; } - Rtc.utc_time++; // Increment every second Rtc.local_time = Rtc.utc_time; if (Rtc.local_time > START_VALID_TIME) { // 2016-01-01 int16_t timezone_minutes = Settings.timezone_minutes; @@ -453,10 +457,17 @@ void RtcSecond(void) } RtcTime.year += 1970; + + mutex = false; } -void RtcSetTime(uint32_t epoch) -{ +void RtcSync(void) { + Rtc.time_synced = true; + RtcSecond(); +// AddLog_P(LOG_LEVEL_DEBUG, PSTR("RTC: Synced")); +} + +void RtcSetTime(uint32_t epoch) { if (epoch < START_VALID_TIME) { // 2016-01-01 Rtc.user_time_entry = false; TasmotaGlobal.ntp_force_sync = true; @@ -466,8 +477,7 @@ void RtcSetTime(uint32_t epoch) } } -void RtcInit(void) -{ +void RtcInit(void) { Rtc.utc_time = 0; BreakTime(Rtc.utc_time, RtcTime); TickerRtc.attach(1, RtcSecond); diff --git a/tasmota/support_wifi.ino b/tasmota/support_wifi.ino index 1779666cf..4309a7622 100644 --- a/tasmota/support_wifi.ino +++ b/tasmota/support_wifi.ino @@ -705,7 +705,7 @@ void wifiKeepAlive(void) { void WifiPollNtp() { static uint8_t ntp_sync_minute = 0; - if (TasmotaGlobal.global_state.network_down) { return; } + if (TasmotaGlobal.global_state.network_down || Rtc.user_time_entry) { return; } uint8_t uptime_minute = (TasmotaGlobal.uptime / 60) % 60; // 0 .. 59 if ((ntp_sync_minute > 59) && (uptime_minute > 2)) { @@ -722,9 +722,7 @@ void WifiPollNtp() { if (ntp_time > START_VALID_TIME) { Rtc.utc_time = ntp_time; ntp_sync_minute = 60; // Sync so block further requests - Rtc.time_synced = true; - RtcSecond(); -// AddLog_P(LOG_LEVEL_DEBUG, PSTR("NTP: Synced")); + RtcSync(); } else { ntp_sync_minute++; // Try again in next minute }