-
Notifications
You must be signed in to change notification settings - Fork 143
nrf5x: Use RTC for timekeeping, track overflows for extra bits #662
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
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Grazfather
commented
Aug 29, 2025
e4353fa
to
7aaa055
Compare
Grazfather
commented
Aug 30, 2025
lol I feel attacked |
afa496d
to
4c45035
Compare
tact1m4n3
reviewed
Sep 2, 2025
4c45035
to
0097e3e
Compare
If we wanted to get really elaborate we could throw a compile error if the RTC interrupt handler is overwritten and hal.time is referenced. If the user wants to dismiss that warning he would have to set a flag in HAL_Options. Imo, these warnings that prevent the user from making a mistake should be part of the microzig at some point in the future but maybe now isn't the time :) |
9262b53
to
883687e
Compare
tact1m4n3
reviewed
Sep 2, 2025
tact1m4n3
reviewed
Sep 2, 2025
This reverts commit cec408d.
5e2879f
to
ec46848
Compare
ec46848
to
c044785
Compare
tact1m4n3
approved these changes
Sep 3, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The nrf TIMER is 32 bits, and with prescaler set to 4, it would count at exactly 1MHz.
This would overflow every 4294 seconds, which is not enough for many uses.
We could increase the prescaler up to 9, splitting the 32MHz clock to 62500Hz, which would still overflow every 68719 seconds, which is still less than a day.
The original plan was to set an interrupt to fire on overflow, to increment a counter, which could be used to add bits to this value, easily getting us 64 bits. The problem is that it introduced a race condition, because we could try to build the time extra when it overflowed, which would yield us an incorrect count (e.g. off by 4294 seconds). The way that embassy gets around this is by incrementing the counter when it overflows, as well as when it hits the halfway point, and then, depending on the value of the extra bits, choose to toggle the top bit of the counter or not.
Unfortunately, the TIMER register does not have the ability to fire an interrupt when it overflows, only when it hits a compare value. We could probably use two compare values (half way and on overflow), but I think it's probably better to follow the lead of embassy and use RTC, which has an explicit overflow event.
By user 23 bits of timer, with an interrupt that fires at 0x800000 and on overflow, we can track the time for 23 + 32 == 55 ticks.
This timer runs at a slower 32'768 Hz, which means we sacrifice granularity, but for timekeeping usually in the ms (not us) scale, it should not be a problem.