nrf52: hodiny přežijí warm reset v .noinit (#5)
VolatileRTCClock ukládá čas do g_retained_clock (sekce .noinit, linker ji dá hned za .bss) a při prvním čtení po warm resetu ho obnoví; po power-on (RESETREAS 0) ne. Dřívější pokus 7. 8. na vrcholu RAM selhal, protože tam sahá zásobník bootloaderu — střed RAM přežije: x1 (XIAO, companion) nastaven +30 dní, reboot, hodiny zpět +30 dní. BaseChatMesh::bootstrapRTCfromContacts posouvá hodiny už jen dopředu: bezpodmínečný set vracel čas pod build epoch a rušil obnovu, která ještě neproběhla (první test proto vyšel negativně). Repeater: mrtvý mechanismus s pevnou RETAINED_CLOCK_ADDR nahrazen výpisem stavu obnovy při bootu; ukládání obstarává tick(). Podle weebl2000/meshcore 9a561fd7. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
26c0f46ee5
commit
7261865583
5 files changed
+94
-114
No files matched your search
@@ -3,21 +3,79 @@
|
||||
#include <Mesh.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#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 { return base_time + accumulator/1000; }
|
||||
void setCurrentTime(uint32_t time) override { base_time = time; 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
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -64,7 +64,12 @@ void BaseChatMesh::bootstrapRTCfromContacts() {
|
||||
latest = contacts[i].lastmod;
|
||||
}
|
||||
}
|
||||
if (latest != 0) {
|
||||
// CUSTOM (TeTeHacko): only ever forward. Upstream sets this unconditionally,
|
||||
// which was harmless while the clock started in 2024; ours starts at the build
|
||||
// epoch (or a time retained across a warm reset, read by getCurrentTime()
|
||||
// here), and an old contact would otherwise pull the clock back -- and, on
|
||||
// nRF52, cancel the retained-clock restore it ran ahead of.
|
||||
if (latest != 0 && latest + 1 > getRTCClock()->getCurrentTime()) {
|
||||
getRTCClock()->setCurrentTime(latest + 1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,17 @@
|
||||
#if defined(NRF52_PLATFORM)
|
||||
#include "NRF52Board.h"
|
||||
|
||||
#if !defined(NO_RETAINED_CLOCK)
|
||||
// CUSTOM (TeTeHacko): the wall-clock record VolatileRTCClock carries across a warm
|
||||
// reset (ArduinoHelpers.h). .noinit: the startup code neither copies nor zeroes it.
|
||||
extern "C" {
|
||||
uint32_t g_retained_clock[3] __attribute__((section(".noinit")));
|
||||
uint8_t g_retained_clock_status = 0;
|
||||
// `clkreboot` (CommonCLI.cpp, weak) -- otherwise the record would put the
|
||||
// clock it is throwing away straight back after the reboot.
|
||||
void retainedClockInvalidate() { g_retained_clock[0] = 0; }
|
||||
}
|
||||
#endif
|
||||
#include <target.h>
|
||||
|
||||
#include <bluefruit.h>
|
||||
|
||||
Reference in new issue
Block a user