From 1563e72a2def0405201e98e471b4e1d740ba9f6f Mon Sep 17 00:00:00 2001 From: AkkerKid Date: Tue, 13 Oct 2026 15:41:05 -0400 Subject: [PATCH] Fix nRF52 clock and uptime jumping +27.96 h every 48.5 days (millis wrap) On the Adafruit nRF52 core millis() is tick2ms(xTaskGetTickCount()) with a 32-bit tick counter at configTICK_RATE_HZ (1024), so it wraps at 2^32 * 1000 / 1024 = 4,194,304,000 ms (48.544 days), not at 2^32 ms. VolatileRTCClock::tick() and the repeater/room-server uptime counters do a uint32 subtraction across that wrap and gain 2^32 - 4,194,304,000 = 100,663,296 ms (27 h 57 m 43 s) each time. Because time/clock sync refuse to go backwards the offset is permanent until a reboot, so long-running nRF52 nodes sit at +28 h, +56 h, +84 h... Add MonotonicMillis, which measures elapsed time in the raw FreeRTOS tick on NRF52_PLATFORM (which really wraps at 2^32) and in millis() elsewhere, and use it for VolatileRTCClock and both uptime counters so the arithmetic lives in one place. ESP32 (64-bit time()), RP2040 and STM32 (true 2^32 millis wrap) are unchanged in behaviour. Introduced by 3d9378d (repeater v1.10.0), which fixed the earlier 49-day wrap-back by assuming a 2^32 modulus. Repro without waiting 48 days (verified on a XIAO nRF52840 running Xiao_nrf52_repeater, same core and clock path as the RAK4631): build with -D configINITIAL_TICK_COUNT=0xFF000000 and, in the core's FreeRTOS tasks.c vTaskStartScheduler(), initialise xTickCount from configINITIAL_TICK_COUNT instead of 0 (FreeRTOS 10.0.1 zeroes it; the nRF52 port's tick ISR also catches the low 24 bits up to RTC1, so the low 24 bits of the preset must be zero). The raw tick then wraps 2^24 ticks = 4 h 33 m after boot. Unpatched: `clock` jumps +100,663 s at that moment and `time` cannot bring it back. Patched: the clock does not move across the wrap. Fixes #3565 --- examples/simple_repeater/MyMesh.cpp | 8 +--- examples/simple_repeater/MyMesh.h | 3 +- examples/simple_room_server/MyMesh.cpp | 8 +--- examples/simple_room_server/MyMesh.h | 3 +- src/helpers/ArduinoHelpers.h | 65 +++++++++++++++++++++----- 5 files changed, 60 insertions(+), 27 deletions(-) diff --git a/examples/simple_repeater/MyMesh.cpp b/examples/simple_repeater/MyMesh.cpp index 61b6c149e98..eff8f773b36 100644 --- a/examples/simple_repeater/MyMesh.cpp +++ b/examples/simple_repeater/MyMesh.cpp @@ -222,7 +222,7 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t stats.n_packets_recv = radio_driver.getPacketsRecv(); stats.n_packets_sent = radio_driver.getPacketsSent(); stats.total_air_time_secs = getTotalAirTime() / 1000; - stats.total_up_time_secs = uptime_millis / 1000; + stats.total_up_time_secs = uptime.getMillis() / 1000; stats.n_sent_flood = getNumSentFlood(); stats.n_sent_direct = getNumSentDirect(); stats.n_recv_flood = getNumRecvFlood(); @@ -874,8 +874,6 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc , bridge(&_prefs, _mgr, &rtc) #endif { - last_millis = 0; - uptime_millis = 0; next_local_advert = next_flood_advert = 0; dirty_contacts_expiry = 0; set_radio_at = revert_radio_at = 0; @@ -1326,9 +1324,7 @@ void MyMesh::loop() { } // update uptime - uint32_t now = millis(); - uptime_millis += now - last_millis; - last_millis = now; + uptime.update(); } // To check if there is pending work diff --git a/examples/simple_repeater/MyMesh.h b/examples/simple_repeater/MyMesh.h index a9ac453a0fa..538f7213f2d 100644 --- a/examples/simple_repeater/MyMesh.h +++ b/examples/simple_repeater/MyMesh.h @@ -80,8 +80,7 @@ struct NeighbourInfo { class MyMesh : public mesh::Mesh, public CommonCLICallbacks { FILESYSTEM* _fs; - uint32_t last_millis; - uint64_t uptime_millis; + MonotonicMillis uptime; unsigned long next_local_advert, next_flood_advert; bool _logging; NodePrefs _prefs; diff --git a/examples/simple_room_server/MyMesh.cpp b/examples/simple_room_server/MyMesh.cpp index da4c776c0ea..959fb4b46a9 100644 --- a/examples/simple_room_server/MyMesh.cpp +++ b/examples/simple_room_server/MyMesh.cpp @@ -163,7 +163,7 @@ int MyMesh::handleRequest(ClientInfo *sender, uint32_t sender_timestamp, uint8_t stats.n_packets_recv = radio_driver.getPacketsRecv(); stats.n_packets_sent = radio_driver.getPacketsSent(); stats.total_air_time_secs = getTotalAirTime() / 1000; - stats.total_up_time_secs = uptime_millis / 1000; + stats.total_up_time_secs = uptime.getMillis() / 1000; stats.n_sent_flood = getNumSentFlood(); stats.n_sent_direct = getNumSentDirect(); stats.n_recv_flood = getNumRecvFlood(); @@ -634,8 +634,6 @@ MyMesh::MyMesh(mesh::MainBoard &board, mesh::Radio &radio, mesh::MillisecondCloc _cli(board, rtc, sensors, region_map, acl, &_prefs, this), telemetry(MAX_PACKET_PAYLOAD - 4) { - last_millis = 0; - uptime_millis = 0; next_local_advert = next_flood_advert = 0; dirty_contacts_expiry = 0; _logging = false; @@ -1075,7 +1073,5 @@ void MyMesh::loop() { // TODO: periodically check for OLD/inactive entries in known_clients[], and evict // update uptime - uint32_t now = millis(); - uptime_millis += now - last_millis; - last_millis = now; + uptime.update(); } diff --git a/examples/simple_room_server/MyMesh.h b/examples/simple_room_server/MyMesh.h index 5cf949c6bd4..7b0927842b1 100644 --- a/examples/simple_room_server/MyMesh.h +++ b/examples/simple_room_server/MyMesh.h @@ -91,8 +91,7 @@ struct PostInfo { class MyMesh : public mesh::Mesh, public CommonCLICallbacks { FILESYSTEM* _fs; - uint32_t last_millis; - uint64_t uptime_millis; + MonotonicMillis uptime; unsigned long next_local_advert, next_flood_advert; bool _logging; bool region_load_active; diff --git a/src/helpers/ArduinoHelpers.h b/src/helpers/ArduinoHelpers.h index 97596daa31f..c2f6caf7f28 100644 --- a/src/helpers/ArduinoHelpers.h +++ b/src/helpers/ArduinoHelpers.h @@ -3,20 +3,63 @@ #include #include +/** + * Monotonic elapsed-time accumulator that is wrap-safe on every platform. + * + * On the Adafruit nRF52 core, millis() is tick2ms(xTaskGetTickCount()): a 32-bit + * FreeRTOS tick counter at configTICK_RATE_HZ (1024 Hz) scaled by 1000/1024. So + * millis() wraps at 2^32 * 1000 / 1024 = 4,194,304,000 ms (48.544 days), NOT at + * 2^32 ms. A uint32 subtraction across that wrap yields a phantom + * 2^32 - 4,194,304,000 = 100,663,296 ms (27 h 57 m 43 s) of elapsed time. + * Measuring in raw ticks, which really do wrap at 2^32, is exact. + */ +class MonotonicMillis { + uint64_t _total; // ticks on nRF52, milliseconds elsewhere +#if defined(NRF52_PLATFORM) + uint32_t _prev; +#else + unsigned long _prev; +#endif +public: + MonotonicMillis() { reset(); } + + void reset() { + _total = 0; +#if defined(NRF52_PLATFORM) + _prev = xTaskGetTickCount(); +#else + _prev = millis(); +#endif + } + + void update() { +#if defined(NRF52_PLATFORM) + uint32_t now = xTaskGetTickCount(); +#else + uint32_t now = millis(); +#endif + _total += (uint32_t)(now - _prev); // both counters wrap at exactly 2^32 + _prev = now; + } + + uint64_t getMillis() const { +#if defined(NRF52_PLATFORM) + return (_total * 1000) / configTICK_RATE_HZ; +#else + return _total; +#endif + } +}; + class VolatileRTCClock : public mesh::RTCClock { uint32_t base_time; - uint64_t accumulator; - unsigned long prev_millis; + MonotonicMillis elapsed; public: - VolatileRTCClock() { base_time = 1715770351; accumulator = 0; prev_millis = millis(); } // 15 May 2024, 8:50pm - uint32_t getCurrentTime() override { return base_time + accumulator/1000; } - void setCurrentTime(uint32_t time) override { base_time = time; accumulator = 0; prev_millis = millis(); } - - void tick() override { - unsigned long now = millis(); - accumulator += (now - prev_millis); - prev_millis = now; - } + VolatileRTCClock() { base_time = 1715770351; } // 15 May 2024, 8:50pm + uint32_t getCurrentTime() override { return base_time + elapsed.getMillis() / 1000; } + void setCurrentTime(uint32_t time) override { base_time = time; elapsed.reset(); } + + void tick() override { elapsed.update(); } }; class ArduinoMillis : public mesh::MillisecondClock {