Skip to content

Commit 8a72982

Browse files
committed
beautify
1 parent c0e459a commit 8a72982

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/rtc.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::pac::{self, rcc::RegisterBlock, PWR, RCC, RTC};
88
use crate::rcc::Enable;
99
use core::fmt;
1010
use core::marker::PhantomData;
11-
use fugit::{Duration, ExtU32, Rate, RateExtU32};
11+
use fugit::RateExtU32;
1212
use time::{Date, PrimitiveDateTime, Time, Weekday};
1313

1414
/// Invalid input error
@@ -541,51 +541,53 @@ impl<CS: FrequencySource> Rtc<CS> {
541541
)
542542
}
543543

544-
/// Configures the wakeup timer to trigger periodically every `interval` seconds
544+
/// Configures the wakeup timer to trigger periodically every `interval` duration
545545
///
546546
/// # Panics
547547
///
548-
/// Panics if interval is greater than 2¹⁷-1.
548+
/// Panics if interval is greater than 2¹⁷-1 seconds.
549549
pub fn enable_wakeup(&mut self, interval: fugit::MicrosDurationU64) {
550550
self.modify(false, |regs| {
551551
regs.cr.modify(|_, w| w.wute().clear_bit());
552552
regs.isr.modify(|_, w| w.wutf().clear_bit());
553553
while regs.isr.read().wutwf().bit_is_clear() {}
554554

555-
if interval < 32u32.secs::<1, 1_000_000>() {
555+
use crate::pac::rtc::cr::WUCKSEL_A;
556+
if interval < fugit::MicrosDurationU64::secs(32) {
556557
// Use RTCCLK as the wakeup timer clock source
557-
let frequency: Rate<u64, 1, 1> = (CS::frequency() / 2).into();
558-
let freq_duration: Duration<u64, 1, 1000000> = frequency.into_duration();
558+
let frequency: fugit::Hertz<u64> = (CS::frequency() / 2).into();
559+
let freq_duration: fugit::MicrosDurationU64 = frequency.into_duration();
559560
let ticks_per_interval = interval / freq_duration;
560561

561562
let mut prescaler = 0;
562563
while ticks_per_interval >> prescaler > 1 << 16 {
563564
prescaler += 1;
564565
}
565566

566-
let prescaler_bits = match prescaler {
567-
0 => 0b11, // RTCCLK/2
568-
1 => 0b10, // RTCCLK/4
569-
2 => 0b01, // RTCCLK/8
570-
3 => 0b00, // RTCCLK/16
567+
let wucksel = match prescaler {
568+
0 => WUCKSEL_A::Div2,
569+
1 => WUCKSEL_A::Div4,
570+
2 => WUCKSEL_A::Div8,
571+
3 => WUCKSEL_A::Div16,
571572
_ => unreachable!("Longer durations should use ck_spre"),
572573
};
573574

574575
let interval = u16::try_from((ticks_per_interval >> prescaler) - 1).unwrap();
575576

576-
regs.cr
577-
.modify(|_, w| unsafe { w.wucksel().bits(prescaler_bits) });
577+
regs.cr.modify(|_, w| w.wucksel().variant(wucksel));
578578
regs.wutr.write(|w| w.wut().bits(interval));
579579
} else {
580580
// Use ck_spre (1Hz) as the wakeup timer clock source
581581
let interval = interval.to_secs();
582582
if interval > 1 << 16 {
583-
regs.cr.modify(|_, w| unsafe { w.wucksel().bits(0b110) });
583+
regs.cr
584+
.modify(|_, w| w.wucksel().variant(WUCKSEL_A::ClockSpareWithOffset));
584585
let interval = u16::try_from(interval - (1 << 16) - 1)
585586
.expect("Interval was too large for wakeup timer");
586587
regs.wutr.write(|w| w.wut().bits(interval));
587588
} else {
588-
regs.cr.modify(|_, w| unsafe { w.wucksel().bits(0b100) });
589+
regs.cr
590+
.modify(|_, w| w.wucksel().variant(WUCKSEL_A::ClockSpare));
589591
let interval = u16::try_from(interval - 1)
590592
.expect("Interval was too large for wakeup timer");
591593
regs.wutr.write(|w| w.wut().bits(interval));

0 commit comments

Comments
 (0)