|
|
|
@ -5,6 +5,47 @@ |
|
|
|
|
|
|
|
#if defined(NRF52_PLATFORM) |
|
|
|
|
|
|
|
// noinit variables survive watchdog, soft, pin, and lockup resets (RAM retained).
|
|
|
|
// Lost on power-on and System OFF (magic check handles this).
|
|
|
|
extern uint32_t _noinit_backup_time __attribute__((section(".noinit"))); |
|
|
|
extern uint32_t _noinit_backup_magic __attribute__((section(".noinit"))); |
|
|
|
#define NRF52_BACKUP_MAGIC 0xAA55CC33 |
|
|
|
#define NRF52_TIME_MIN 1772323200 // 1 Mar 2026
|
|
|
|
|
|
|
|
class NRF52RTCClock : public mesh::RTCClock { |
|
|
|
uint32_t base_time; |
|
|
|
uint64_t accumulator; |
|
|
|
unsigned long prev_millis; |
|
|
|
public: |
|
|
|
NRF52RTCClock() { |
|
|
|
if (_noinit_backup_magic == NRF52_BACKUP_MAGIC && _noinit_backup_time > NRF52_TIME_MIN) { |
|
|
|
base_time = _noinit_backup_time; |
|
|
|
} else { |
|
|
|
base_time = NRF52_TIME_MIN; |
|
|
|
} |
|
|
|
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(); |
|
|
|
_noinit_backup_time = time; |
|
|
|
_noinit_backup_magic = NRF52_BACKUP_MAGIC; |
|
|
|
} |
|
|
|
void tick() override { |
|
|
|
unsigned long now = millis(); |
|
|
|
accumulator += (now - prev_millis); |
|
|
|
prev_millis = now; |
|
|
|
uint32_t current = base_time + accumulator / 1000; |
|
|
|
if (current > NRF52_TIME_MIN && current != _noinit_backup_time) { |
|
|
|
_noinit_backup_time = current; |
|
|
|
_noinit_backup_magic = NRF52_BACKUP_MAGIC; |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
#ifdef NRF52_POWER_MANAGEMENT |
|
|
|
// Shutdown Reason Codes (stored in GPREGRET before SYSTEMOFF)
|
|
|
|
#define SHUTDOWN_REASON_NONE 0x00 |
|
|
|
|