aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-08-27 16:06:33 -0500
committerxoviat <[email protected]>2023-08-27 16:06:33 -0500
commit9f928010a86be9e0f8b5fa4257c3edd70261c0dc (patch)
tree02c1c1b6682a12e18803f2fed6b9aac618f8dff5
parent88146eb53e40ea2ab43c2db77f3f62c6d08c9b36 (diff)
stm32/rtc: use psc to compute instants
-rw-r--r--embassy-stm32/src/rtc/mod.rs53
-rw-r--r--embassy-stm32/src/rtc/v2.rs4
2 files changed, 19 insertions, 38 deletions
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs
index c408b2d61..8bda0926e 100644
--- a/embassy-stm32/src/rtc/mod.rs
+++ b/embassy-stm32/src/rtc/mod.rs
@@ -48,31 +48,6 @@ struct RtcInstant {
48} 48}
49 49
50#[cfg(feature = "low-power")] 50#[cfg(feature = "low-power")]
51impl RtcInstant {
52 pub fn now() -> Self {
53 let tr = RTC::regs().tr().read();
54 let tr2 = RTC::regs().tr().read();
55 let ssr = RTC::regs().ssr().read().ss();
56 let ssr2 = RTC::regs().ssr().read().ss();
57
58 let st = bcd2_to_byte((tr.st(), tr.su()));
59 let st2 = bcd2_to_byte((tr2.st(), tr2.su()));
60
61 assert!(st == st2);
62 assert!(ssr == ssr2);
63
64 let _ = RTC::regs().dr().read();
65
66 let subsecond = ssr;
67 let second = st;
68
69 // trace!("rtc: instant now: st, ssr: {}, {}", st, ssr);
70
71 Self { second, subsecond }
72 }
73}
74
75#[cfg(feature = "low-power")]
76impl core::ops::Sub for RtcInstant { 51impl core::ops::Sub for RtcInstant {
77 type Output = embassy_time::Duration; 52 type Output = embassy_time::Duration;
78 53
@@ -85,20 +60,13 @@ impl core::ops::Sub for RtcInstant {
85 self.second 60 self.second
86 }; 61 };
87 62
88 // TODO: read prescaler 63 let psc = RTC::regs().prer().read().prediv_s() as u32;
89 64
90 let self_ticks = second as u32 * 256 + (255 - self.subsecond as u32); 65 let self_ticks = second as u32 * (psc + 1) + (psc - self.subsecond as u32);
91 let other_ticks = rhs.second as u32 * 256 + (255 - rhs.subsecond as u32); 66 let other_ticks = rhs.second as u32 * (psc + 1) + (psc - rhs.subsecond as u32);
92 let rtc_ticks = self_ticks - other_ticks; 67 let rtc_ticks = self_ticks - other_ticks;
93 68
94 // trace!( 69 Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / psc) as u64)
95 // "rtc: instant sub: self, other, rtc ticks: {}, {}, {}",
96 // self_ticks,
97 // other_ticks,
98 // rtc_ticks
99 // );
100
101 Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / 256u32) as u64)
102 } 70 }
103} 71}
104 72
@@ -198,6 +166,19 @@ impl Rtc {
198 Ok(()) 166 Ok(())
199 } 167 }
200 168
169 /// Return the current instant.
170 fn instant(&self) -> RtcInstant {
171 let r = RTC::regs();
172 let tr = r.tr().read();
173 let subsecond = r.ssr().read().ss();
174 let second = bcd2_to_byte((tr.st(), tr.su()));
175
176 // Unlock the registers
177 r.dr();
178
179 RtcInstant { second, subsecond }
180 }
181
201 /// Return the current datetime. 182 /// Return the current datetime.
202 /// 183 ///
203 /// # Errors 184 /// # Errors
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs
index 1e0ca9b48..62b398689 100644
--- a/embassy-stm32/src/rtc/v2.rs
+++ b/embassy-stm32/src/rtc/v2.rs
@@ -110,7 +110,7 @@ impl super::Rtc {
110 110
111 trace!("rtc: start wakeup alarm for {} ms", duration.as_millis()); 111 trace!("rtc: start wakeup alarm for {} ms", duration.as_millis());
112 112
113 critical_section::with(|cs| assert!(self.stop_time.borrow(cs).replace(Some(RtcInstant::now())).is_none())) 113 critical_section::with(|cs| assert!(self.stop_time.borrow(cs).replace(Some(self.instant())).is_none()))
114 } 114 }
115 115
116 #[cfg(feature = "low-power")] 116 #[cfg(feature = "low-power")]
@@ -132,7 +132,7 @@ impl super::Rtc {
132 132
133 critical_section::with(|cs| { 133 critical_section::with(|cs| {
134 if let Some(stop_time) = self.stop_time.borrow(cs).take() { 134 if let Some(stop_time) = self.stop_time.borrow(cs).take() {
135 Some(RtcInstant::now() - stop_time) 135 Some(self.instant() - stop_time)
136 } else { 136 } else {
137 None 137 None
138 } 138 }