aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHenrik Berg <[email protected]>2023-07-11 18:41:45 +0200
committerHenrik Berg <[email protected]>2023-07-12 14:22:48 +0200
commita93714327eb85b02c7c4a419f2a76df579258975 (patch)
tree2670be57d39d87bcf097bd9f3362f3bb949e2acb
parent029b156563e70e00cf0ffdf9d5ec23964e5ecc77 (diff)
RP: Rename Rtc to match STM32 impl. Remove setting RTC in new().
-rw-r--r--embassy-rp/src/rtc/mod.rs20
-rw-r--r--examples/rp/src/bin/rtc.rs14
2 files changed, 19 insertions, 15 deletions
diff --git a/embassy-rp/src/rtc/mod.rs b/embassy-rp/src/rtc/mod.rs
index b18f12fc4..90b796a9c 100644
--- a/embassy-rp/src/rtc/mod.rs
+++ b/embassy-rp/src/rtc/mod.rs
@@ -12,26 +12,24 @@ pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError};
12use crate::clocks::clk_rtc_freq; 12use crate::clocks::clk_rtc_freq;
13 13
14/// A reference to the real time clock of the system 14/// A reference to the real time clock of the system
15pub struct RealTimeClock<'d, T: Instance> { 15pub struct Rtc<'d, T: Instance> {
16 inner: PeripheralRef<'d, T>, 16 inner: PeripheralRef<'d, T>,
17} 17}
18 18
19impl<'d, T: Instance> RealTimeClock<'d, T> { 19impl<'d, T: Instance> Rtc<'d, T> {
20 /// Create a new instance of the real time clock, with the given date as an initial value. 20 /// Create a new instance of the real time clock, with the given date as an initial value.
21 /// 21 ///
22 /// # Errors 22 /// # Errors
23 /// 23 ///
24 /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range. 24 /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range.
25 pub fn new(inner: impl Peripheral<P = T> + 'd, initial_date: DateTime) -> Result<Self, RtcError> { 25 pub fn new(inner: impl Peripheral<P = T> + 'd) -> Self {
26 into_ref!(inner); 26 into_ref!(inner);
27 27
28 // Set the RTC divider 28 // Set the RTC divider
29 inner.regs().clkdiv_m1().write(|w| w.set_clkdiv_m1(clk_rtc_freq() - 1)); 29 inner.regs().clkdiv_m1().write(|w| w.set_clkdiv_m1(clk_rtc_freq() - 1));
30 30
31 let mut result = Self { inner }; 31 let result = Self { inner };
32 result.set_leap_year_check(true); // should be on by default, make sure this is the case. 32 result
33 result.set_datetime(initial_date)?;
34 Ok(result)
35 } 33 }
36 34
37 /// Enable or disable the leap year check. The rp2040 chip will always add a Feb 29th on every year that is divisable by 4, but this may be incorrect (e.g. on century years). This function allows you to disable this check. 35 /// Enable or disable the leap year check. The rp2040 chip will always add a Feb 29th on every year that is divisable by 4, but this may be incorrect (e.g. on century years). This function allows you to disable this check.
@@ -43,7 +41,7 @@ impl<'d, T: Instance> RealTimeClock<'d, T> {
43 }); 41 });
44 } 42 }
45 43
46 /// Checks to see if this RealTimeClock is running 44 /// Checks to see if this Rtc is running
47 pub fn is_running(&self) -> bool { 45 pub fn is_running(&self) -> bool {
48 self.inner.regs().ctrl().read().rtc_active() 46 self.inner.regs().ctrl().read().rtc_active()
49 } 47 }
@@ -113,8 +111,8 @@ impl<'d, T: Instance> RealTimeClock<'d, T> {
113 /// # fn main() { } 111 /// # fn main() { }
114 /// # #[cfg(not(feature = "chrono"))] 112 /// # #[cfg(not(feature = "chrono"))]
115 /// # fn main() { 113 /// # fn main() {
116 /// # use embassy_rp::rtc::{RealTimeClock, DateTimeFilter}; 114 /// # use embassy_rp::rtc::{Rtc, DateTimeFilter};
117 /// # let mut real_time_clock: RealTimeClock<embassy_rp::peripherals::RTC> = unsafe { core::mem::zeroed() }; 115 /// # let mut real_time_clock: Rtc<embassy_rp::peripherals::RTC> = unsafe { core::mem::zeroed() };
118 /// let now = real_time_clock.now().unwrap(); 116 /// let now = real_time_clock.now().unwrap();
119 /// real_time_clock.schedule_alarm( 117 /// real_time_clock.schedule_alarm(
120 /// DateTimeFilter::default() 118 /// DateTimeFilter::default()
@@ -150,7 +148,7 @@ impl<'d, T: Instance> RealTimeClock<'d, T> {
150 } 148 }
151} 149}
152 150
153/// Errors that can occur on methods on [RealTimeClock] 151/// Errors that can occur on methods on [Rtc]
154#[derive(Clone, Debug, PartialEq, Eq)] 152#[derive(Clone, Debug, PartialEq, Eq)]
155pub enum RtcError { 153pub enum RtcError {
156 /// An invalid DateTime was given or stored on the hardware. 154 /// An invalid DateTime was given or stored on the hardware.
diff --git a/examples/rp/src/bin/rtc.rs b/examples/rp/src/bin/rtc.rs
index a49c8f627..6300950d4 100644
--- a/examples/rp/src/bin/rtc.rs
+++ b/examples/rp/src/bin/rtc.rs
@@ -4,7 +4,7 @@
4 4
5use defmt::*; 5use defmt::*;
6use embassy_executor::Spawner; 6use embassy_executor::Spawner;
7use embassy_rp::rtc::{DateTime, DayOfWeek, RealTimeClock}; 7use embassy_rp::rtc::{DateTime, DayOfWeek, Rtc};
8use embassy_time::{Duration, Timer}; 8use embassy_time::{Duration, Timer};
9use {defmt_rtt as _, panic_probe as _}; 9use {defmt_rtt as _, panic_probe as _};
10 10
@@ -23,11 +23,17 @@ async fn main(_spawner: Spawner) {
23 second: 50, 23 second: 50,
24 }; 24 };
25 25
26 let rtc_result = RealTimeClock::new(p.RTC, now); 26 let mut rtc = Rtc::new(p.RTC);
27 if let Ok(rtc) = rtc_result { 27 if rtc.set_datetime(now).is_ok() {
28 // In reality the delay would be much longer 28 // In reality the delay would be much longer
29 Timer::after(Duration::from_millis(20000)).await; 29 Timer::after(Duration::from_millis(20000)).await;
30 30
31 let _then: DateTime = rtc.now().unwrap(); 31 if let Ok(dt) = rtc.now() {
32 info!(
33 "Now: {}-{}-{} {}:{}:{}",
34 dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second,
35 );
36 }
32 } 37 }
38 info!("Done.");
33} 39}