nrf52: hodiny přežijí warm reset v .noinit (#5)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
99656142f1
commit
8b55e36f95
5 files changed
+94
-114
No files matched your search
@@ -32,6 +32,11 @@ MEMORY
|
||||
* demonstrated it surviving. Do not build anything new on this mechanism
|
||||
* without proving retention first, and treat a missing hardfault report as
|
||||
* "unknown" rather than "no fault happened".
|
||||
*
|
||||
* 18. 10. 2026: what DOES survive is the middle of RAM. The clock record now
|
||||
* lives in .noinit (src/helpers/NRF52Board.cpp), which the linker places right
|
||||
* after .bss -- measured on x1 (XIAO, companion): set +30 days, soft `reboot`,
|
||||
* the clock came back +30 days. The hardfault record should move there too.
|
||||
*/
|
||||
RAM (rwx) : ORIGIN = 0x20006000, LENGTH = 0x2003FFF0 - 0x20006000
|
||||
}
|
||||
|
||||
@@ -711,64 +711,6 @@ static unsigned long userBtnDownAt = 0;
|
||||
#define USER_BTN_HOLD_OFF_MILLIS 1500
|
||||
#endif
|
||||
|
||||
#if defined(RETAINED_CLOCK_ADDR) && defined(NRF52_PLATFORM)
|
||||
// CUSTOM (TeTeHacko): carry the wall clock across a soft reset in retained RAM.
|
||||
//
|
||||
// ESP32 already gets this for free -- ESP32RTCClock::begin() only re-seeds the
|
||||
// clock when esp_reset_reason() == ESP_RST_POWERON, so a reboot there keeps the
|
||||
// time. nRF52 had nothing: VolatileRTCClock's constructor runs on every boot and
|
||||
// throws the clock back to the build epoch, so a node with no GPS and no I2C RTC
|
||||
// lost a correct clock to any `reboot`, `dfu` or watchdog.
|
||||
//
|
||||
// RAM survives a soft reset physically; the only thing that destroys it is the
|
||||
// startup code zeroing .bss. So the record lives at a fixed address kept OUTSIDE
|
||||
// the linker's RAM region -- the same trick, and the neighbouring 16 bytes, as the
|
||||
// hardfault record printed in setup() below. Reserved in
|
||||
// boards/nrf52840_s140_v7_extrafs.ld; RETAINED_CLOCK_ADDR is only defined for envs
|
||||
// that use that script, because the other nRF52 ldscripts run RAM to 0x20040000
|
||||
// and writing there would land in live memory.
|
||||
//
|
||||
// Cost: 12 bytes of RAM and a store every 60 s. No flash, so no wear.
|
||||
#define RETAINED_CLOCK_MAGIC 0xC10CB0DE
|
||||
|
||||
extern "C" { extern uint32_t __StackTop; } // = ORIGIN(RAM) + LENGTH(RAM)
|
||||
|
||||
static volatile uint32_t* _retainedClock() {
|
||||
// Refuse to touch the address unless it really is above everything the linker
|
||||
// handed out. This is what catches RETAINED_CLOCK_ADDR being paired with an
|
||||
// ldscript that does not reserve it -- otherwise the symptom would be silent
|
||||
// corruption of whatever variable happens to live there.
|
||||
if ((uint32_t) RETAINED_CLOCK_ADDR < (uint32_t) (uintptr_t) &__StackTop) return NULL;
|
||||
return (volatile uint32_t*) (uintptr_t) RETAINED_CLOCK_ADDR;
|
||||
}
|
||||
|
||||
static void retainedClockSave(uint32_t now) {
|
||||
volatile uint32_t* rc = _retainedClock();
|
||||
if (rc == NULL) return;
|
||||
rc[1] = now;
|
||||
rc[2] = ~now;
|
||||
rc[0] = RETAINED_CLOCK_MAGIC; // magic last: a reset mid-write leaves it invalid
|
||||
}
|
||||
|
||||
// 0 = nothing trustworthy stored. The complement makes a chance match of
|
||||
// uninitialised RAM about 2^-64, so this does not need a checksum.
|
||||
static uint32_t retainedClockLoad() {
|
||||
volatile uint32_t* rc = _retainedClock();
|
||||
if (rc == NULL || rc[0] != RETAINED_CLOCK_MAGIC) return 0;
|
||||
uint32_t t = rc[1];
|
||||
return (rc[2] == ~t) ? t : 0;
|
||||
}
|
||||
|
||||
// Called by `clkreboot` (CommonCLI.cpp) through a weak symbol. WITHOUT THIS the
|
||||
// retained record defeats the one command that exists to fix a wrong clock:
|
||||
// clkreboot writes the build epoch and reboots, and the restore above would then
|
||||
// see the stale record sitting above it and put the bad time straight back. The
|
||||
// only way out of an over-set clock would be a power cycle.
|
||||
extern "C" void retainedClockInvalidate() {
|
||||
volatile uint32_t* rc = _retainedClock();
|
||||
if (rc != NULL) rc[0] = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
@@ -916,46 +858,18 @@ void setup() {
|
||||
halt();
|
||||
}
|
||||
|
||||
#if defined(RETAINED_CLOCK_ADDR) && defined(NRF52_PLATFORM)
|
||||
// Deliberately AFTER radio_init(), which is what calls rtc_clock.begin(Wire) and
|
||||
// therefore what decides whether this node has a hardware RTC at all. Run any
|
||||
// earlier and getCurrentTime() below would read the volatile fallback instead of
|
||||
// the chip, and the "do not go backwards" test would compare against the wrong
|
||||
// clock. See the record layout above, near the hardfault record it sits beside.
|
||||
#ifdef RETAINED_CLOCK_NOINIT
|
||||
// Deliberately AFTER radio_init(), which calls rtc_clock.begin(Wire) and so
|
||||
// decides whether a hardware RTC answers. VolatileRTCClock restores a clock
|
||||
// retained across a warm reset on its first read (ArduinoHelpers.h); NOT silent,
|
||||
// because "nothing restored" looks exactly like "working" otherwise.
|
||||
{
|
||||
uint32_t saved = retainedClockLoad();
|
||||
uint32_t rr = readResetReason();
|
||||
if (saved == 0) {
|
||||
// NOT silent. A quiet "nothing stored" branch is indistinguishable from
|
||||
// "the feature is working", and that cost a whole flash-and-reboot cycle
|
||||
// to notice: the clock came up at the build epoch with no message at all,
|
||||
// which looked identical to a node that had simply never saved. Print the
|
||||
// raw words so the next boot says WHY -- gated off, wrong address, or a
|
||||
// record that really was wiped.
|
||||
volatile uint32_t* rc = _retainedClock();
|
||||
if (rc == NULL) {
|
||||
Serial.printf("boot: retained hodiny VYPNUTY -- adresa %08lX je pod __StackTop %08lX\n",
|
||||
(unsigned long) RETAINED_CLOCK_ADDR, (unsigned long) (uintptr_t) &__StackTop);
|
||||
} else {
|
||||
Serial.printf("boot: retained hodiny prazdne (%08lX: %08lX %08lX %08lX, cekal magic %08lX)\n",
|
||||
(unsigned long) RETAINED_CLOCK_ADDR, (unsigned long) rc[0],
|
||||
(unsigned long) rc[1], (unsigned long) rc[2],
|
||||
(unsigned long) RETAINED_CLOCK_MAGIC);
|
||||
}
|
||||
} else if (rr == 0) {
|
||||
// No bit set = power-on/brownout. RAM can still hold its contents through a
|
||||
// short outage, so the record may well validate -- but it is then stale by
|
||||
// however long the power was gone, which is unknowable. Build epoch is the
|
||||
// honest answer.
|
||||
Serial.println("boot: retained hodiny ignorovany (power-on, stara hodnota by lhala)");
|
||||
} else if (saved > rtc_clock.getCurrentTime()) {
|
||||
rtc_clock.setCurrentTime(saved);
|
||||
Serial.printf("boot: hodiny obnoveny z retained RAM (epoch %lu)\n",
|
||||
(unsigned long) saved);
|
||||
}
|
||||
// else: the clock is already ahead of the record -- a battery-backed I2C RTC,
|
||||
// or a build newer than the record. Leave it alone; this is the guard that
|
||||
// keeps the restore from ever making a good clock worse.
|
||||
uint32_t now = rtc_clock.getCurrentTime();
|
||||
static const char* const why[] = { "nepouzito (HW RTC?)", "prazdne", "ignorovany (power-on, stara hodnota by lhala)",
|
||||
"nenovejsi nez hodiny", "obnoveny" };
|
||||
Serial.printf("boot: retained hodiny (.noinit %08lX) %s, epoch %lu\n",
|
||||
(unsigned long) (uintptr_t) g_retained_clock,
|
||||
why[g_retained_clock_status < 5 ? g_retained_clock_status : 0], (unsigned long) now);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1234,20 +1148,6 @@ void loop() {
|
||||
#endif
|
||||
rtc_clock.tick();
|
||||
|
||||
#if defined(RETAINED_CLOCK_ADDR) && defined(NRF52_PLATFORM)
|
||||
// Stash the clock once a minute, so a reboot resumes from at most a minute ago.
|
||||
// It has to be periodic rather than written on the way out: a watchdog reset or
|
||||
// a lockup never reaches a shutdown path, and those are exactly the reboots
|
||||
// nobody planned for. int32_t delta so the 49-day millis() wrap cannot park this
|
||||
// either permanently on or permanently off -- same reason as the gps duty timers.
|
||||
{
|
||||
static unsigned long next_clock_save = 0;
|
||||
if ((int32_t) (millis() - next_clock_save) >= 0) {
|
||||
next_clock_save = millis() + 60000;
|
||||
retainedClockSave(rtc_clock.getCurrentTime());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef HAS_EXTERNAL_WATCHDOG
|
||||
external_watchdog.loop();
|
||||
|
||||
@@ -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