Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NRF: Persist RTC offset across reboots #2924

Merged
merged 5 commits into from May 18, 2020

Conversation

xobs
Copy link
Collaborator

@xobs xobs commented May 18, 2020

This patchset introduces a new .uninitialized linker section and places the RTC value into this section. It uses sentinal values to determine if the RTC offset is valid.

As part of this rework, the port_saved_word API was reworked to also use a value in this new section.

xobs added 5 commits May 18, 2020 11:52
This section immediately follows the .bss section, and is designed to
contain uninitialized variables that should persist across reboots.

The section is placed directly after .bss, under the theory that the
size of Circuit Python's .bss + .data is bigger than the bootloader's
.bss + .data, so there is less likely to be a conflict.

Signed-off-by: Sean Cross <sean@xobs.io>
Previously, it was placed following .bss.  However, now that there is a
new section after .bss, the heap must be moved forward.

Signed-off-by: Sean Cross <sean@xobs.io>
Circuit Python supports saving a single word of data across reboots.
Previously, this data was placed immediately following the .bss.

However, this appeared to not work, as Circuit Python zeroes out the
heap when it starts up, and the heap begins immediately after the .bss.

Switch to using the new .uninitialized section in order to store this
word across resets.

Signed-off-by: Sean Cross <sean@xobs.io>
Store the RTC value in the .uninitialized section, but make sure to
flank it with some known values.  That way we can determine if the RTC
value has been initialized, or if it's random uninitialized garbage.

As part of this, add a `common_hal_rtc_init()` routine to determine if
the value is correct, or reset it to 0 if it is not valid.

Signed-off-by: Sean Cross <sean@xobs.io>
Call the new function to set the RTC, or reset it if the offset is not
valid.

Signed-off-by: Sean Cross <sean@xobs.io>
Copy link
Member

@tannewt tannewt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me! Do you plan on turning off all RAM banks except the one this lands in?

@tannewt tannewt merged commit af68dde into adafruit:master May 18, 2020
@xobs
Copy link
Collaborator Author

xobs commented May 19, 2020

No, because right now we don't actually power off -- we need to keep Bluetooth running.

The plan is to check to see if it's the year 2000, and if so set the current time to the time of code.py (under the theory that the file was just [re]-created), then that should persist across reboots and crashes for months:

import os
import rtc
import time
system_rtc = rtc.RTC()
if system_rtc.datetime.tm_year == 2000:
    system_rtc.datetime = time.localtime(os.stat("/code.py")[8])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants