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

Provides an HFCLK timer driver #629

Closed
wants to merge 2 commits into from

Conversation

huntc
Copy link
Contributor

@huntc huntc commented Feb 18, 2022

The existing (default) timer driver for nRF implements one using the RTC, which uses the LFCLK. An additional feature has been provided where the HFCLK can be used for the timer driver by using TIMER1. An STM example has also been ported so that either driver implementation can be exercised.

To use the HFCLK driver, simply use the time-driver-timer1 feature in place of the time-driver-rtc1 one when depending on embassy-nrf.

@huntc huntc marked this pull request as ready for review February 19, 2022 08:18
@huntc huntc requested a review from Dirbaio February 21, 2022 23:22
Copy link
Member

@Dirbaio Dirbaio left a comment

Choose a reason for hiding this comment

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

  • Could you split into several files? time_driver/{mod.rs, rtc.rs, timer.rs}
  • The overflow handling is not correct.
    • RTC is 24bit, TIMER is 32bit. Many constants need updating, in calc_now and the rest of the code.
    • RTC has OVERFLW event, TIMER doesn't. We still need some way to detect overflow. Alternatives:
      • Set one CC at 0, another at 0x8000_0000. This uses 2 CCs so we can support only 1 alarm (4 ccs total, minus 1 dummy for reading the counter, minus 2 for overflow handling)
      • Set one CC at 0x8000_0000, when it fires change it to 0, when it fires change it back to 0x8000_0000 etc.

@@ -34,6 +34,7 @@ nrf9160-ns = ["_nrf9160"]

gpiote = []
time-driver-rtc1 = ["_time-driver"]
time-driver-timer1 = ["_time-driver"]
Copy link
Member

Choose a reason for hiding this comment

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

You need to set embassy/time-tick-32768hz when using RTC, embassy/time-tick-1mhz (or whatever the tick rate of the timer driver is) when using TIMER.

let r = timer();
r.mode.write(|w| w.mode().timer());
r.prescaler
.write(|w| unsafe { w.prescaler().bits(Frequency::F62500Hz as u8) });
Copy link
Member

Choose a reason for hiding this comment

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

Why this particular prescaler? I thought you wanted this to have higher precision. Higher tick rate means more preicision. With 1Mhz rate you can measure down to 1us of time. With 16Mhz you can measure down to 62.5ns.

let period = self.period.load(Ordering::Relaxed);
compiler_fence(Ordering::Acquire);
let r = timer();
r.tasks_capture[3].write(|w| unsafe { w.bits(1) });
Copy link
Member

Choose a reason for hiding this comment

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

This will overwrite whatever's in CC3 which you're currently using for (half of the) overflow handling. It's super unfortunate that you can't read the counter value directly. WTF Nordic? but it is what it is. We have to sacrifice a CC channel to read the counter >_<

This means it can't support 3 alarms unlike RTC. It can max 2 or 1 depending on how you implement overflows

@huntc huntc marked this pull request as draft February 23, 2022 03:58
The existing (default) timer driver for nRF implements one using the RTC, which uses the LFCLK. An additional feature has been provided where the HFCLK can be used for the timer driver by using TIMER1. An STM example has also been ported so that either driver implementation can be exercised.

To use the HFCLK driver, simply use the `time-driver-timer1` feature in place of the `time-driver-rtc1` one when depending on embassy-nrf.
@huntc
Copy link
Contributor Author

huntc commented Feb 23, 2022

  • Could you split into several files? time_driver/{mod.rs, rtc.rs, timer.rs}

Resolved in commit d99c748

@Dirbaio
Copy link
Member

Dirbaio commented Dec 3, 2024

closing due to outdatedness. Feature is still wanted tho!

@Dirbaio Dirbaio closed this Dec 3, 2024
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.

2 participants