From 56eb87d05af06deb51dc4df4ad61d84cac4c1535 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Ma=C5=82ecki?= Date: Sat, 9 May 2026 01:01:16 +0200 Subject: [PATCH] add 'get rtcclock' CLI command to report RTC source and current time Returns the hardware RTC type (DS3231, RV3028, PCF8563, RX8130CE) or 'internal' when no external module was detected, alongside the current date and time in UTC. Useful for verifying that an external RTC is active and providing correct time to the node. --- src/MeshCore.h | 2 ++ src/helpers/AutoDiscoverRTCClock.cpp | 5 +++++ src/helpers/AutoDiscoverRTCClock.h | 5 ++++- src/helpers/CommonCLI.cpp | 7 +++++++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/MeshCore.h b/src/MeshCore.h index 2db1d4c3e..3cf60a349 100644 --- a/src/MeshCore.h +++ b/src/MeshCore.h @@ -92,6 +92,8 @@ public: */ virtual void tick() { /* no op */} + virtual const char* getSourceName() const { return "internal"; } + uint32_t getCurrentTimeUnique() { uint32_t t = getCurrentTime(); if (t <= last_unique) { diff --git a/src/helpers/AutoDiscoverRTCClock.cpp b/src/helpers/AutoDiscoverRTCClock.cpp index 53afcfcaa..b870cefd3 100644 --- a/src/helpers/AutoDiscoverRTCClock.cpp +++ b/src/helpers/AutoDiscoverRTCClock.cpp @@ -61,6 +61,11 @@ void AutoDiscoverRTCClock::begin(TwoWire& wire) { MESH_DEBUG_PRINTLN("RX8130CE: Initialized"); } + if (ds3231_success) _source_name = "DS3231"; + else if (rv3028_success) _source_name = "RV3028"; + else if (rtc_8563_success) _source_name = "PCF8563"; + else if (rtc_8130_success) _source_name = "RX8130CE"; + _has_hw_rtc = ds3231_success || rv3028_success || rtc_8563_success || rtc_8130_success; if (_has_hw_rtc) { syncSystemClock(); diff --git a/src/helpers/AutoDiscoverRTCClock.h b/src/helpers/AutoDiscoverRTCClock.h index 21b2ef6e7..a52ac372d 100644 --- a/src/helpers/AutoDiscoverRTCClock.h +++ b/src/helpers/AutoDiscoverRTCClock.h @@ -12,12 +12,15 @@ class AutoDiscoverRTCClock : public mesh::RTCClock { mesh::RTCClock* _fallback; bool _has_hw_rtc; unsigned long _last_sync_ms; + const char* _source_name; bool i2c_probe(TwoWire& wire, uint8_t addr); void syncSystemClock(); public: AutoDiscoverRTCClock(mesh::RTCClock& fallback) - : _fallback(&fallback), _has_hw_rtc(false), _last_sync_ms(0) { } + : _fallback(&fallback), _has_hw_rtc(false), _last_sync_ms(0), _source_name("internal") { } + + const char* getSourceName() const override { return _source_name; } void begin(TwoWire& wire); uint32_t getCurrentTime() override; diff --git a/src/helpers/CommonCLI.cpp b/src/helpers/CommonCLI.cpp index d495aada5..9f1249f7b 100644 --- a/src/helpers/CommonCLI.cpp +++ b/src/helpers/CommonCLI.cpp @@ -886,6 +886,13 @@ void CommonCLI::handleGetCmd(uint32_t sender_timestamp, char* command, char* rep #else strcpy(reply, "ERROR: Power management not supported"); #endif + } else if (memcmp(config, "rtcclock", 8) == 0) { + uint32_t now = getRTCClock()->getCurrentTime(); + DateTime dt = DateTime(now); + sprintf(reply, "> %s | %04d-%02d-%02d %02d:%02d:%02d UTC", + getRTCClock()->getSourceName(), + dt.year(), dt.month(), dt.day(), + dt.hour(), dt.minute(), dt.second()); } else { sprintf(reply, "??: %s", config); }