From c95f9ab6b616f93a11ecd74afb9c91fd5eb8422c Mon Sep 17 00:00:00 2001 From: Silvano Seva Date: Fri, 3 Oct 2025 19:32:02 +0200 Subject: [PATCH] rtc: add settings entry for RTC sync with GPS Added settings menu entry to make RTC synchronization with GPS persistent across reboots. When the synchronization is enabled the RTC will be updated on the first valid RMC sentence received. Signed-off-by: Silvano Seva --- openrtx/include/core/settings.h | 2 ++ openrtx/src/core/gps.c | 37 ++++++++++++++++++-------------- openrtx/src/ui/default/ui.c | 4 ++-- openrtx/src/ui/default/ui_menu.c | 2 +- 4 files changed, 26 insertions(+), 19 deletions(-) diff --git a/openrtx/include/core/settings.h b/openrtx/include/core/settings.h index b55adf60..0baf41ac 100644 --- a/openrtx/include/core/settings.h +++ b/openrtx/include/core/settings.h @@ -63,6 +63,7 @@ typedef struct bool m17_can_rx; // Check M17 CAN on RX char m17_dest[10]; // M17 destination bool showBatteryIcon; // Battery display true: icon, false: percentage + bool gpsSetTime; // Use GPS to ajust RTC time } __attribute__((packed)) settings_t; @@ -89,6 +90,7 @@ static const settings_t default_settings = false, // Check M17 CAN on RX "", // Empty M17 destination false, // Display battery icon + false, // Update RTC with GPS }; #endif /* SETTINGS_H */ diff --git a/openrtx/src/core/gps.c b/openrtx/src/core/gps.c index 1c245f7d..f289c2ce 100644 --- a/openrtx/src/core/gps.c +++ b/openrtx/src/core/gps.c @@ -29,6 +29,23 @@ static bool gpsEnabled = false; +#ifdef CONFIG_RTC +static bool rtcSyncDone = false; +static void syncRtc(datetime_t timestamp) +{ + if(state.settings.gpsSetTime == false) { + rtcSyncDone = false; + return; + } + + if(rtcSyncDone) + return; + + platform_setTime(timestamp); + rtcSyncDone = true; +} +#endif + void gps_task(const struct gpsDevice *dev) { char sentence[2*MINMEA_MAX_LENGTH]; @@ -81,6 +98,10 @@ void gps_task(const struct gpsDevice *dev) gps_data.timestamp.month = frame.date.month; gps_data.timestamp.year = frame.date.year; gps_data.speed = KNOTS2KMH(minmea_toint(&frame.speed)); + #ifdef CONFIG_RTC + if(frame.valid) + syncRtc(gps_data.timestamp); + #endif } } break; @@ -171,20 +192,4 @@ void gps_task(const struct gpsDevice *dev) pthread_mutex_lock(&state_mutex); state.gps_data = gps_data; pthread_mutex_unlock(&state_mutex); - - // Synchronize RTC with GPS UTC clock, only when fix is done - #ifdef CONFIG_RTC - if(state.gps_set_time) - { - if((sId == MINMEA_SENTENCE_RMC) && (gps_data.fix_quality > 0)) - { - platform_setTime(gps_data.timestamp); - - // Done, clear the flag - pthread_mutex_lock(&state_mutex); - state.gps_set_time = false; - pthread_mutex_unlock(&state_mutex); - } - } - #endif } diff --git a/openrtx/src/ui/default/ui.c b/openrtx/src/ui/default/ui.c index 184aa656..a746b630 100644 --- a/openrtx/src/ui/default/ui.c +++ b/openrtx/src/ui/default/ui.c @@ -2156,10 +2156,10 @@ void ui_updateFSM(bool *sync_rtx) state.settings.gps_enabled); break; case G_SET_TIME: - state.gps_set_time = !state.gps_set_time; + state.settings.gpsSetTime = !state.settings.gpsSetTime; vp_announceSettingsOnOffToggle(¤tLanguage->gpsSetTime, queueFlags, - state.gps_set_time); + state.settings.gpsSetTime); break; case G_TIMEZONE: if(msg.keys & KEY_LEFT || msg.keys & KEY_DOWN || diff --git a/openrtx/src/ui/default/ui_menu.c b/openrtx/src/ui/default/ui_menu.c index 9c588dd0..9545cd20 100644 --- a/openrtx/src/ui/default/ui_menu.c +++ b/openrtx/src/ui/default/ui_menu.c @@ -316,7 +316,7 @@ int _ui_getSettingsGPSValueName(char *buf, uint8_t max_len, uint8_t index) currentLanguage->off); break; case G_SET_TIME: - sniprintf(buf, max_len, "%s", (last_state.gps_set_time) ? + sniprintf(buf, max_len, "%s", (last_state.settings.gpsSetTime) ? currentLanguage->on : currentLanguage->off); break;