Keep time across reset for NRF52
This commit is contained in:
1 parent
fe44cb4326
commit
a9845842d1
2 files changed
+46
-2
No files matched your search
@@ -0,0 +1,6 @@
|
||||
#include <stdint.h>
|
||||
|
||||
extern "C" {
|
||||
__attribute__((section(".persistent_magic"))) uint32_t persistent_magic;
|
||||
__attribute__((section(".persistent_data"))) uint32_t persistent_time;
|
||||
}
|
||||
@@ -3,19 +3,57 @@
|
||||
#include <Mesh.h>
|
||||
#include <Arduino.h>
|
||||
|
||||
#ifdef NRF52_PLATFORM
|
||||
#define CLOCK_MAGIC_NUM 0xAA55CC33
|
||||
#define RTC_TIME_MIN 1772323200 // 1 Mar 2026
|
||||
|
||||
extern uint32_t persistent_magic;
|
||||
extern uint32_t persistent_time;
|
||||
#endif
|
||||
|
||||
class VolatileRTCClock : public mesh::RTCClock {
|
||||
uint32_t base_time;
|
||||
uint64_t accumulator;
|
||||
unsigned long prev_millis;
|
||||
|
||||
public:
|
||||
VolatileRTCClock() { base_time = 1715770351; accumulator = 0; prev_millis = millis(); } // 15 May 2024, 8:50pm
|
||||
VolatileRTCClock() {
|
||||
#ifdef NRF52_PLATFORM
|
||||
if (persistent_magic == CLOCK_MAGIC_NUM && persistent_time >= RTC_TIME_MIN) {
|
||||
base_time = persistent_time;
|
||||
} else {
|
||||
base_time = RTC_TIME_MIN;
|
||||
}
|
||||
#else
|
||||
base_time = 1715770351;
|
||||
#endif
|
||||
|
||||
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(); }
|
||||
|
||||
void setCurrentTime(uint32_t time) override {
|
||||
base_time = time;
|
||||
accumulator = 0;
|
||||
prev_millis = millis();
|
||||
|
||||
#ifdef NRF52_PLATFORM
|
||||
persistent_magic = CLOCK_MAGIC_NUM;
|
||||
persistent_time = time;
|
||||
#endif
|
||||
}
|
||||
|
||||
void tick() override {
|
||||
unsigned long now = millis();
|
||||
accumulator += (now - prev_millis);
|
||||
prev_millis = now;
|
||||
|
||||
#ifdef NRF52_PLATFORM
|
||||
persistent_magic = CLOCK_MAGIC_NUM;
|
||||
persistent_time = getCurrentTime();
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user