aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxoviat <[email protected]>2023-09-25 21:40:05 +0000
committerGitHub <[email protected]>2023-09-25 21:40:05 +0000
commitc79a84a98a92bb3f1280b4a7657936fcd827a0e0 (patch)
tree74286550c032e68ec2558d41fa87dd321660642c
parent481d2998ef49ef550f6fcf4e8a815b6170aed23b (diff)
parent96edbd84fbdc10a5b9b37a343941bc057be59467 (diff)
Merge pull request #1949 from xoviat/rtc
stm32/rtc: cleanup and fixes
-rw-r--r--embassy-stm32/Cargo.toml4
-rw-r--r--embassy-stm32/src/rcc/bd.rs5
-rw-r--r--embassy-stm32/src/rtc/mod.rs10
-rw-r--r--embassy-stm32/src/rtc/v2.rs20
-rw-r--r--embassy-stm32/src/rtc/v3.rs17
5 files changed, 9 insertions, 47 deletions
diff --git a/embassy-stm32/Cargo.toml b/embassy-stm32/Cargo.toml
index ef0ef229b..660ab0532 100644
--- a/embassy-stm32/Cargo.toml
+++ b/embassy-stm32/Cargo.toml
@@ -59,7 +59,7 @@ sdio-host = "0.5.0"
59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true } 59embedded-sdmmc = { git = "https://github.com/embassy-rs/embedded-sdmmc-rs", rev = "a4f293d3a6f72158385f79c98634cb8a14d0d2fc", optional = true }
60critical-section = "1.1" 60critical-section = "1.1"
61atomic-polyfill = "1.0.1" 61atomic-polyfill = "1.0.1"
62stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-74025d56c0ba061703f360558ce80f51d1165060" } 62stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-1551a1c01a993bb5ffc603311f80097c14e03f85" }
63vcell = "0.1.3" 63vcell = "0.1.3"
64bxcan = "0.7.0" 64bxcan = "0.7.0"
65nb = "1.0.0" 65nb = "1.0.0"
@@ -78,7 +78,7 @@ critical-section = { version = "1.1", features = ["std"] }
78[build-dependencies] 78[build-dependencies]
79proc-macro2 = "1.0.36" 79proc-macro2 = "1.0.36"
80quote = "1.0.15" 80quote = "1.0.15"
81stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-74025d56c0ba061703f360558ce80f51d1165060", default-features = false, features = ["metadata"]} 81stm32-metapac = { git = "https://github.com/embassy-rs/stm32-data-generated", tag = "stm32-data-1551a1c01a993bb5ffc603311f80097c14e03f85", default-features = false, features = ["metadata"]}
82 82
83[features] 83[features]
84default = ["rt"] 84default = ["rt"]
diff --git a/embassy-stm32/src/rcc/bd.rs b/embassy-stm32/src/rcc/bd.rs
index 5bae3edd9..de27130f2 100644
--- a/embassy-stm32/src/rcc/bd.rs
+++ b/embassy-stm32/src/rcc/bd.rs
@@ -88,11 +88,6 @@ impl BackupDomain {
88 ))] 88 ))]
89 #[allow(dead_code, unused_variables)] 89 #[allow(dead_code, unused_variables)]
90 pub fn configure_ls(clock_source: RtcClockSource, lsi: bool, lse: Option<LseDrive>) { 90 pub fn configure_ls(clock_source: RtcClockSource, lsi: bool, lse: Option<LseDrive>) {
91 if lsi || lse.is_some() {
92 use crate::rtc::sealed::Instance;
93 crate::peripherals::RTC::enable_peripheral_clk();
94 }
95
96 if lsi { 91 if lsi {
97 #[cfg(rtc_v3u5)] 92 #[cfg(rtc_v3u5)]
98 let csr = crate::pac::RCC.bdcr(); 93 let csr = crate::pac::RCC.bdcr();
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs
index 7eafedec4..73b78f253 100644
--- a/embassy-stm32/src/rtc/mod.rs
+++ b/embassy-stm32/src/rtc/mod.rs
@@ -111,11 +111,12 @@ impl RtcTimeProvider {
111 } 111 }
112} 112}
113 113
114#[non_exhaustive]
115/// RTC Abstraction 114/// RTC Abstraction
116pub struct Rtc { 115pub struct Rtc {
117 #[cfg(feature = "low-power")] 116 #[cfg(feature = "low-power")]
118 stop_time: Mutex<CriticalSectionRawMutex, Cell<Option<RtcInstant>>>, 117 stop_time: Mutex<CriticalSectionRawMutex, Cell<Option<RtcInstant>>>,
118 #[cfg(not(feature = "low-power"))]
119 _private: (),
119} 120}
120 121
121#[non_exhaustive] 122#[non_exhaustive]
@@ -154,9 +155,14 @@ impl Default for RtcCalibrationCyclePeriod {
154 155
155impl Rtc { 156impl Rtc {
156 pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self { 157 pub fn new(_rtc: impl Peripheral<P = RTC>, rtc_config: RtcConfig) -> Self {
158 #[cfg(any(rcc_wle, rcc_wl5, rcc_g4, rcc_g0, rtc_v2l4, rtc_v2wb))]
159 <RTC as crate::rcc::sealed::RccPeripheral>::enable();
160
157 let mut this = Self { 161 let mut this = Self {
158 #[cfg(feature = "low-power")] 162 #[cfg(feature = "low-power")]
159 stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)), 163 stop_time: Mutex::const_new(CriticalSectionRawMutex::new(), Cell::new(None)),
164 #[cfg(not(feature = "low-power"))]
165 _private: (),
160 }; 166 };
161 167
162 let frequency = Self::frequency(); 168 let frequency = Self::frequency();
@@ -292,8 +298,6 @@ pub(crate) mod sealed {
292 crate::pac::RTC 298 crate::pac::RTC
293 } 299 }
294 300
295 fn enable_peripheral_clk();
296
297 /// Read content of the backup register. 301 /// Read content of the backup register.
298 /// 302 ///
299 /// The registers retain their values during wakes from standby mode or system resets. They also 303 /// The registers retain their values during wakes from standby mode or system resets. They also
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs
index ab562d2b6..4608d3114 100644
--- a/embassy-stm32/src/rtc/v2.rs
+++ b/embassy-stm32/src/rtc/v2.rs
@@ -75,15 +75,6 @@ impl super::Rtc {
75 #[cfg(any(rcc_wb, rcc_f4, rcc_f410))] 75 #[cfg(any(rcc_wb, rcc_f4, rcc_f410))]
76 unsafe { crate::rcc::get_freqs() }.rtc.unwrap(); 76 unsafe { crate::rcc::get_freqs() }.rtc.unwrap();
77 77
78 /*
79 If the requested duration is u64::MAX, don't even set the alarm
80
81 Otherwise clamp the requested duration to u32::MAX so that we can do math
82 */
83 if requested_duration.as_ticks() == u64::MAX {
84 return;
85 }
86
87 let requested_duration = requested_duration.as_ticks().clamp(0, u32::MAX as u64); 78 let requested_duration = requested_duration.as_ticks().clamp(0, u32::MAX as u64);
88 let rtc_hz = Self::frequency().0 as u64; 79 let rtc_hz = Self::frequency().0 as u64;
89 let rtc_ticks = requested_duration * rtc_hz / TICK_HZ; 80 let rtc_ticks = requested_duration * rtc_hz / TICK_HZ;
@@ -286,17 +277,6 @@ impl sealed::Instance for crate::peripherals::RTC {
286 #[cfg(all(feature = "low-power", stm32l0))] 277 #[cfg(all(feature = "low-power", stm32l0))]
287 type WakeupInterrupt = crate::interrupt::typelevel::RTC; 278 type WakeupInterrupt = crate::interrupt::typelevel::RTC;
288 279
289 fn enable_peripheral_clk() {
290 #[cfg(any(rtc_v2l4, rtc_v2wb))]
291 {
292 // enable peripheral clock for communication
293 crate::pac::RCC.apb1enr1().modify(|w| w.set_rtcapben(true));
294
295 // read to allow the pwr clock to enable
296 crate::pac::PWR.cr1().read();
297 }
298 }
299
300 fn read_backup_register(rtc: &Rtc, register: usize) -> Option<u32> { 280 fn read_backup_register(rtc: &Rtc, register: usize) -> Option<u32> {
301 if register < Self::BACKUP_REGISTER_COUNT { 281 if register < Self::BACKUP_REGISTER_COUNT {
302 Some(rtc.bkpr(register).read().bkp()) 282 Some(rtc.bkpr(register).read().bkp())
diff --git a/embassy-stm32/src/rtc/v3.rs b/embassy-stm32/src/rtc/v3.rs
index 9ac9f9f85..a6b2655d8 100644
--- a/embassy-stm32/src/rtc/v3.rs
+++ b/embassy-stm32/src/rtc/v3.rs
@@ -128,23 +128,6 @@ impl super::Rtc {
128impl sealed::Instance for crate::peripherals::RTC { 128impl sealed::Instance for crate::peripherals::RTC {
129 const BACKUP_REGISTER_COUNT: usize = 32; 129 const BACKUP_REGISTER_COUNT: usize = 32;
130 130
131 fn enable_peripheral_clk() {
132 #[cfg(any(rcc_wle, rcc_wl5, rcc_g4))]
133 {
134 // enable peripheral clock for communication
135 crate::pac::RCC.apb1enr1().modify(|w| w.set_rtcapben(true));
136 }
137
138 #[cfg(rcc_g0)]
139 {
140 // enable peripheral clock for communication
141 crate::pac::RCC.apbenr1().modify(|w| w.set_rtcapben(true));
142 }
143
144 // read to allow the pwr clock to enable
145 crate::pac::PWR.cr1().read();
146 }
147
148 fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> { 131 fn read_backup_register(_rtc: &Rtc, register: usize) -> Option<u32> {
149 #[allow(clippy::if_same_then_else)] 132 #[allow(clippy::if_same_then_else)]
150 if register < Self::BACKUP_REGISTER_COUNT { 133 if register < Self::BACKUP_REGISTER_COUNT {