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 {