8000 nrf: port: save rtc value across reboots · adafruit/circuitpython@daf7c28 · GitHub
[go: up one dir, main page]

Skip to content

Commit daf7c28

Browse files
committed
nrf: port: save rtc value across reboots
As part of the reset process, save the current tick count to an uninitialized memory location. That way, the current tick value will be preserved across reboots. A reboot will cause us to lose a certain number of ticks, depending on how long a reboot takes, however if reboots are infrequent then this will not be a large amount of time lost. Signed-off-by: Sean Cross <sean@xobs.io>
1 parent 5edc29c commit daf7c28

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

ports/nrf/supervisor/port.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,19 @@ const nrfx_rtc_config_t rtc_config = {
7878
.interrupt_priority = 6
7979
};
8080

81-
static volatile uint64_t overflowed_ticks = 0;
81+
#define OVERFLOW_CHECK_PREFIX 0x2cad564f
82+
#define OVERFLOW_CHECK_SUFFIX 0x11343ef7
83+
static volatile struct {
84+
uint32_t prefix;
85+
uint64_t overflowed_ticks;
86+
uint32_t suffix;
87+
} overflow_tracker __attribute__((section(".uninitialized")));
8288

8389
void rtc_handler(nrfx_rtc_int_type_t int_type) {
8490
if (int_type == NRFX_RTC_INT_OVERFLOW) {
8591
// Our RTC is 24 bits and we're clocking it at 32.768khz which is 32 (2 ** 5) subticks per
8692
// tick.
87-
overflowed_ticks += (1L<< (24 - 5));
93+
overflow_tracker.overflowed_ticks += (1L<< (24 - 5));
8894
} else if (int_type == NRFX_RTC_INT_TICK && nrfx_rtc_counter_get(&rtc_instance) % 32 == 0) {
8995
// Do things common to all ports when the tick occurs
9096
supervisor_tick();
@@ -101,6 +107,17 @@ void tick_init(void) {
101107
nrfx_rtc_init(&rtc_instance, &rtc_config, rtc_handler);
102108
nrfx_rtc_enable(&rtc_instance);
103109
nrfx_rtc_overflow_enable(&rtc_instance, true);
110+
111+
// If the check prefix and suffix aren't correct, then the structure
112+
// in memory isn't correct and the clock will be wildly wrong. Initialize
113+
// the prefix and suffix so that we know the value is correct, and reset
114+
// the time to 0.
115+
if (overflow_tracker.prefix != OVERFLOW_CHECK_PREFIX ||
116+
overflow_tracker.suffix != OVERFLOW_CHECK_SUFFIX) {
117+
overflow_tracker.prefix = OVERFLOW_CHECK_PREFIX;
118+
overflow_tracker.suffix = OVERFLOW_CHECK_SUFFIX;
119+
overflow_tracker.overflowed_ticks = 0;
120+
}
104121
}
105122

106123
safe_mode_t port_init(void) {
@@ -205,6 +222,10 @@ void reset_to_bootloader(void) {
205222
}
206223

207224
void reset_cpu(void) {
225+
// We're getting ready to reset, so save the counter off.
226+
// This counter will get reset to zero during the reboot.
227+
uint32_t ticks = nrfx_rtc_counter_get(&rtc_instance);
228+
overflow_tracker.overflowed_ticks += ticks;
208229
NVIC_SystemReset();
209230
}
210231

@@ -248,7 +269,7 @@ uint64_t port_get_raw_ticks(uint8_t* subticks) {
248269
if (subticks != NULL) {
249270
*subticks = (rtc % 32);
250271
}
251-
return overflowed_ticks + rtc / 32;
272+
return overflow_tracker.overflowed_ticks + rtc / 32;
252273
}
253274

254275
// Enable 1/1024 second tick.

0 commit comments

Comments
 (0)
0