-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Feat nrf time rtc support #6202
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
Conversation
2b5ddf2
to
80b7ece
Compare
Looks really good! |
From my side, it looks good as well. |
Great, thanks! |
Add time.ticks_ms / time.ticks_us support using RTC1 as timebase. Also add the time.ticks_add resp. time.ticks_diff helper functions. This feature can be enabled using MICROPY_PY_TIME_TICKS. If disabled the system uses the legacy sleep methods. In addition support for MICROPY_EVENT_POLL_HOOK was added to the time.sleep_ms(x) function, making this function more power efficient and allows support for select.poll / asyncio. To support this, the RTC's CCR0 was used to schedule a ~1msec event to wakeup the CPU. Some important notes about the RTC timebase: - Since the granularity of RTC1's ticks are app. 30usec, time.ticks_us is not perfect, but moreless usable. For tighter measurments, the ticker's 1MHz counter should be used. - time.ticks_ms(x) should *not* be called in an irq with higher prio than the RTC overflow irq (3). If so, it introduces a race condition and possibly leads to wrong tick calculations. - See also micropython#6171 for further informations.
80b7ece
to
fd33bcf
Compare
I force pushed the review feedbacks, changes are:
From my point of view ready to go in. |
This commit adds time.ticks_ms/us support using RTC1 as the timebase. It also adds the time.ticks_add/diff helper functions. This feature can be enabled using MICROPY_PY_TIME_TICKS. If disabled the system uses the legacy sleep methods and does not have any ticks functions. In addition support for MICROPY_EVENT_POLL_HOOK was added to the time.sleep_ms(x) function, making this function more power efficient and allows support for select.poll/asyncio. To support this, the RTC's CCR0 was used to schedule a ~1msec event to wakeup the CPU. Some important notes about the RTC timebase: - Since the granularity of RTC1's ticks are approx 30usec, time.ticks_us is not perfect, does not have 1us resolution, but is otherwise quite usable. For tighter measurments the ticker's 1MHz counter should be used. - time.ticks_ms(x) should *not* be called in an IRQ with higher prio than the RTC overflow irq (3). If so it introduces a race condition and possibly leads to wrong tick calculations. See #6171 and #6202.
This commit adds time.ticks_ms/us support using RTC1 as the timebase. It also adds the time.ticks_add/diff helper functions. This feature can be enabled using MICROPY_PY_TIME_TICKS. If disabled the system uses the legacy sleep methods and does not have any ticks functions. In addition support for MICROPY_EVENT_POLL_HOOK was added to the time.sleep_ms(x) function, making this function more power efficient and allows support for select.poll/asyncio. To support this, the RTC's CCR0 was used to schedule a ~1msec event to wakeup the CPU. Some important notes about the RTC timebase: - Since the granularity of RTC1's ticks are approx 30usec, time.ticks_us is not perfect, does not have 1us resolution, but is otherwise quite usable. For tighter measurments the ticker's 1MHz counter should be used. - time.ticks_ms(x) should *not* be called in an IRQ with higher prio than the RTC overflow irq (3). If so it introduces a race condition and possibly leads to wrong tick calculations. See micropython#6171 and micropython#6202.
samd: Don't rely on RTC interrupt
This is a cleanup version of the inputs collected in #6171
(I didn't want to force push the update to that PR because it contains implementation details
via ticker module that might be useful for others)
Basically it adds support for time.ticks_ms / time.ticks_us via RTC1, configured in 30usec tick mode. It also
adds the time.ticks_add and time.ticks_diff helper functions.
For this to work properly the RTC1's overflow irq is handled and a msec / usec ticks counter appropiately
generated (see also commit comment for this relatively complex matter). Thanks for @dpgeorge for
basically providing the solution there!
The PR also contains support for MICROPY_EVENT_POLL_HOOK. The wakeup from WFI is done via RTC1's
CCR0, configured in app. 1msec intervals. Basically that makes the nrf port ready for asyncio.
This feature can be enabled using MICROPY_PY_TIME_TICKS. If disabled, the system uses the legacy
ticks and sleep methods.