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 <silseva@fastwebnet.it>
This commit is contained in:
Silvano Seva 2025-10-03 19:32:02 +02:00
parent b3b057699b
commit c95f9ab6b6
4 changed files with 26 additions and 19 deletions

View File

@ -63,6 +63,7 @@ typedef struct
bool m17_can_rx; // Check M17 CAN on RX bool m17_can_rx; // Check M17 CAN on RX
char m17_dest[10]; // M17 destination char m17_dest[10]; // M17 destination
bool showBatteryIcon; // Battery display true: icon, false: percentage bool showBatteryIcon; // Battery display true: icon, false: percentage
bool gpsSetTime; // Use GPS to ajust RTC time
} }
__attribute__((packed)) settings_t; __attribute__((packed)) settings_t;
@ -89,6 +90,7 @@ static const settings_t default_settings =
false, // Check M17 CAN on RX false, // Check M17 CAN on RX
"", // Empty M17 destination "", // Empty M17 destination
false, // Display battery icon false, // Display battery icon
false, // Update RTC with GPS
}; };
#endif /* SETTINGS_H */ #endif /* SETTINGS_H */

View File

@ -29,6 +29,23 @@
static bool gpsEnabled = false; 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) void gps_task(const struct gpsDevice *dev)
{ {
char sentence[2*MINMEA_MAX_LENGTH]; 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.month = frame.date.month;
gps_data.timestamp.year = frame.date.year; gps_data.timestamp.year = frame.date.year;
gps_data.speed = KNOTS2KMH(minmea_toint(&frame.speed)); gps_data.speed = KNOTS2KMH(minmea_toint(&frame.speed));
#ifdef CONFIG_RTC
if(frame.valid)
syncRtc(gps_data.timestamp);
#endif
} }
} }
break; break;
@ -171,20 +192,4 @@ void gps_task(const struct gpsDevice *dev)
pthread_mutex_lock(&state_mutex); pthread_mutex_lock(&state_mutex);
state.gps_data = gps_data; state.gps_data = gps_data;
pthread_mutex_unlock(&state_mutex); 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
} }

View File

@ -2156,10 +2156,10 @@ void ui_updateFSM(bool *sync_rtx)
state.settings.gps_enabled); state.settings.gps_enabled);
break; break;
case G_SET_TIME: case G_SET_TIME:
state.gps_set_time = !state.gps_set_time; state.settings.gpsSetTime = !state.settings.gpsSetTime;
vp_announceSettingsOnOffToggle(&currentLanguage->gpsSetTime, vp_announceSettingsOnOffToggle(&currentLanguage->gpsSetTime,
queueFlags, queueFlags,
state.gps_set_time); state.settings.gpsSetTime);
break; break;
case G_TIMEZONE: case G_TIMEZONE:
if(msg.keys & KEY_LEFT || msg.keys & KEY_DOWN || if(msg.keys & KEY_LEFT || msg.keys & KEY_DOWN ||

View File

@ -316,7 +316,7 @@ int _ui_getSettingsGPSValueName(char *buf, uint8_t max_len, uint8_t index)
currentLanguage->off); currentLanguage->off);
break; break;
case G_SET_TIME: 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->on :
currentLanguage->off); currentLanguage->off);
break; break;