|
5 | 5 |
|
6 | 6 | #if defined(NRF52_PLATFORM) |
7 | 7 |
|
| 8 | +// noinit variables survive watchdog, soft, pin, and lockup resets (RAM retained). |
| 9 | +// Lost on power-on and System OFF (magic check handles this). |
| 10 | +extern uint32_t _noinit_backup_time __attribute__((section(".noinit"))); |
| 11 | +extern uint32_t _noinit_backup_magic __attribute__((section(".noinit"))); |
| 12 | +#define NRF52_BACKUP_MAGIC 0xAA55CC33 |
| 13 | +#define NRF52_TIME_MIN 1772323200 // 1 Mar 2026 |
| 14 | + |
| 15 | +class NRF52RTCClock : public mesh::RTCClock { |
| 16 | + uint32_t base_time; |
| 17 | + uint64_t accumulator; |
| 18 | + unsigned long prev_millis; |
| 19 | +public: |
| 20 | + NRF52RTCClock() { |
| 21 | + if (_noinit_backup_magic == NRF52_BACKUP_MAGIC && _noinit_backup_time > NRF52_TIME_MIN) { |
| 22 | + base_time = _noinit_backup_time; |
| 23 | + } else { |
| 24 | + base_time = NRF52_TIME_MIN; |
| 25 | + } |
| 26 | + accumulator = 0; |
| 27 | + prev_millis = millis(); |
| 28 | + } |
| 29 | + uint32_t getCurrentTime() override { return base_time + accumulator / 1000; } |
| 30 | + void setCurrentTime(uint32_t time) override { |
| 31 | + base_time = time; |
| 32 | + accumulator = 0; |
| 33 | + prev_millis = millis(); |
| 34 | + _noinit_backup_time = time; |
| 35 | + _noinit_backup_magic = NRF52_BACKUP_MAGIC; |
| 36 | + } |
| 37 | + void tick() override { |
| 38 | + unsigned long now = millis(); |
| 39 | + accumulator += (now - prev_millis); |
| 40 | + prev_millis = now; |
| 41 | + uint32_t current = base_time + accumulator / 1000; |
| 42 | + if (current > NRF52_TIME_MIN && current != _noinit_backup_time) { |
| 43 | + _noinit_backup_time = current; |
| 44 | + _noinit_backup_magic = NRF52_BACKUP_MAGIC; |
| 45 | + } |
| 46 | + } |
| 47 | +}; |
| 48 | + |
8 | 49 | #ifdef NRF52_POWER_MANAGEMENT |
9 | 50 | // Shutdown Reason Codes (stored in GPREGRET before SYSTEMOFF) |
10 | 51 | #define SHUTDOWN_REASON_NONE 0x00 |
|
0 commit comments