#pragma once #include #include #if defined(NRF52_PLATFORM) && !defined(NO_RETAINED_CLOCK) // CUSTOM (TeTeHacko): the wall clock survives a warm reset (`reboot`, `dfu`, // watchdog, reset pin) in RAM the startup code does not touch. NRF52Board.cpp // holds the record in .noinit -- placed by the linker after .bss, NOT at the top // of RAM: the Adafruit bootloader runs before every boot with its stack starting // at 0x20040000, and an earlier fixed record at 0x2003FFE0 came back overwritten // (7. 8. 2026). Approach from weebl2000/meshcore 9a561fd7. #define RETAINED_CLOCK_NOINIT 1 extern "C" { extern uint32_t g_retained_clock[3]; // magic, time, ~time extern uint8_t g_retained_clock_status; // RetainedClockStatus, for the boot printout uint32_t readResetReason(void); // latched by the core's init() } #define RETAINED_CLOCK_MAGIC 0xC10CB0DE enum RetainedClockStatus { RC_NOT_YET, RC_EMPTY, RC_POWER_ON, RC_NOT_NEWER, RC_RESTORED }; #endif class VolatileRTCClock : public mesh::RTCClock { uint32_t base_time; uint64_t accumulator; unsigned long prev_millis; #ifdef RETAINED_CLOCK_NOINIT bool restore_pending = true; uint32_t last_saved = 0; static void save(uint32_t t) { g_retained_clock[1] = t; g_retained_clock[2] = ~t; g_retained_clock[0] = RETAINED_CLOCK_MAGIC; // magic last: a reset mid-write leaves it invalid } // Not in the constructor: static constructors run before the core's init() // latches RESETREAS, so the reset reason would still read 0 there. void restoreOnce() { if (!restore_pending) return; restore_pending = false; uint32_t t = g_retained_clock[1]; bool valid = g_retained_clock[0] == RETAINED_CLOCK_MAGIC && g_retained_clock[2] == ~t; if (!valid) { g_retained_clock_status = RC_EMPTY; return; } // No reset bit = power-on/brownout: RAM may still hold the record through a // short outage, but it is stale by however long the power was gone. if (readResetReason() == 0) { g_retained_clock_status = RC_POWER_ON; return; } if (t <= base_time + accumulator / 1000) { g_retained_clock_status = RC_NOT_NEWER; return; } base_time = t; accumulator = 0; prev_millis = millis(); g_retained_clock_status = RC_RESTORED; } #endif public: // FIRMWARE_BUILD_EPOCH = compile time when the build is stamped, else upstream's // 15 May 2024 literal. See the define in MeshCore.h for why this matters. VolatileRTCClock() { base_time = FIRMWARE_BUILD_EPOCH; accumulator = 0; prev_millis = millis(); } uint32_t getCurrentTime() override { #ifdef RETAINED_CLOCK_NOINIT restoreOnce(); #endif return base_time + accumulator/1000; } void setCurrentTime(uint32_t time) override { base_time = time; accumulator = 0; prev_millis = millis(); #ifdef RETAINED_CLOCK_NOINIT restore_pending = false; // an explicit set wins over whatever was retained save(time); last_saved = time; #endif } void tick() override { unsigned long now = millis(); accumulator += (now - prev_millis); prev_millis = now; #ifdef RETAINED_CLOCK_NOINIT restoreOnce(); uint32_t t = base_time + accumulator / 1000; if (t != last_saved) { save(t); last_saved = t; } #endif } }; class ArduinoMillis : public mesh::MillisecondClock { public: unsigned long getMillis() override { return millis(); } }; class StdRNG : public mesh::RNG { public: void begin(long seed) { randomSeed(seed); } void random(uint8_t* dest, size_t sz) override { for (int i = 0; i < sz; i++) { dest[i] = (::random(0, 256) & 0xFF); } } };