From e5a5031f204c2c63912beba1c340c27d01d54672 Mon Sep 17 00:00:00 2001 From: Liam Murphy Date: Tue, 29 Jun 2021 11:29:32 +1000 Subject: [PATCH] Get rid of the TODO about variant names, stop the timer before setting BITMODE and set a default frequency. --- embassy-nrf/src/timer.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/embassy-nrf/src/timer.rs b/embassy-nrf/src/timer.rs index d71a5c4e7..02f02bc17 100644 --- a/embassy-nrf/src/timer.rs +++ b/embassy-nrf/src/timer.rs @@ -64,7 +64,7 @@ macro_rules! impl_timer { #[repr(u8)] pub enum Frequency { - // TODO: These variant names are terrible, what should they be? + // I'd prefer not to prefix these with `F`, but Rust identifiers can't start with digits. F16MHz = 0, F8MHz = 1, F4MHz = 2, @@ -108,6 +108,14 @@ impl<'d, T: Instance> Timer<'d, T> { pub(crate) fn new_irqless(_timer: impl Unborrow + 'd) -> Self { let regs = T::regs(); + let mut this = Self { + phantom: PhantomData, + }; + + // Stop the timer before doing anything else, + // since changing BITMODE while running can cause 'unpredictable behaviour' according to the specification. + this.stop(); + // Set the instance to timer mode. regs.mode.write(|w| w.mode().timer()); @@ -115,16 +123,12 @@ impl<'d, T: Instance> Timer<'d, T> { // TODO: is there a reason someone would want to set this lower? regs.bitmode.write(|w| w.bitmode()._32bit()); - let mut this = Self { - phantom: PhantomData, - }; - - // Initialize the timer as stopped. - this.stop(); - // Initialize the counter at 0. this.clear(); + // Default to the max frequency of the lower power clock + this.set_frequency(Frequency::F1MHz); + for n in 0..T::CCS { let cc = this.cc(n); // Initialize all the shorts as disabled.