diff options
| author | xoviat <[email protected]> | 2025-11-04 16:35:07 -0600 |
|---|---|---|
| committer | xoviat <[email protected]> | 2025-11-04 16:35:07 -0600 |
| commit | b9559c7713bc7f773cdef0df14f1158840d06d06 (patch) | |
| tree | d5e126d6c2d891bb47375925353e155177954699 | |
| parent | d23c027dd1a1c5f7cdf818750dddf6a250658423 (diff) | |
rtc: use consistent api between stop and non-stop
| -rw-r--r-- | embassy-stm32/src/low_power.rs | 7 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/low_power.rs | 4 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/mod.rs | 79 | ||||
| -rw-r--r-- | embassy-stm32/src/time_driver.rs | 8 | ||||
| -rw-r--r-- | examples/stm32c0/src/bin/rtc.rs | 4 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/rtc.rs | 4 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/rtc.rs | 4 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/rtc.rs | 4 | ||||
| -rw-r--r-- | examples/stm32h7rs/src/bin/rtc.rs | 4 | ||||
| -rw-r--r-- | examples/stm32l4/src/bin/rtc.rs | 4 | ||||
| -rw-r--r-- | examples/stm32u0/src/bin/rtc.rs | 4 | ||||
| -rw-r--r-- | examples/stm32wl/src/bin/rtc.rs | 4 | ||||
| -rw-r--r-- | tests/stm32/Cargo.toml | 1 | ||||
| -rw-r--r-- | tests/stm32/src/bin/rtc.rs | 15 | ||||
| -rw-r--r-- | tests/stm32/src/bin/stop.rs | 11 |
15 files changed, 92 insertions, 65 deletions
diff --git a/embassy-stm32/src/low_power.rs b/embassy-stm32/src/low_power.rs index 597cedf86..696dfe83f 100644 --- a/embassy-stm32/src/low_power.rs +++ b/embassy-stm32/src/low_power.rs | |||
| @@ -61,8 +61,6 @@ use crate::time_driver::get_driver; | |||
| 61 | 61 | ||
| 62 | const THREAD_PENDER: usize = usize::MAX; | 62 | const THREAD_PENDER: usize = usize::MAX; |
| 63 | 63 | ||
| 64 | use crate::rtc::Rtc; | ||
| 65 | |||
| 66 | static mut EXECUTOR: Option<Executor> = None; | 64 | static mut EXECUTOR: Option<Executor> = None; |
| 67 | 65 | ||
| 68 | /// Prevent the device from going into the stop mode if held | 66 | /// Prevent the device from going into the stop mode if held |
| @@ -133,11 +131,6 @@ foreach_interrupt! { | |||
| 133 | }; | 131 | }; |
| 134 | } | 132 | } |
| 135 | 133 | ||
| 136 | /// Reconfigure the RTC, if set. | ||
| 137 | pub fn reconfigure_rtc<R>(f: impl FnOnce(&mut Rtc) -> R) -> R { | ||
| 138 | get_driver().reconfigure_rtc(f) | ||
| 139 | } | ||
| 140 | |||
| 141 | /// Get whether the core is ready to enter the given stop mode. | 134 | /// Get whether the core is ready to enter the given stop mode. |
| 142 | /// | 135 | /// |
| 143 | /// This will return false if some peripheral driver is in use that | 136 | /// This will return false if some peripheral driver is in use that |
diff --git a/embassy-stm32/src/rtc/low_power.rs b/embassy-stm32/src/rtc/low_power.rs index e09d5afb0..e5bf30927 100644 --- a/embassy-stm32/src/rtc/low_power.rs +++ b/embassy-stm32/src/rtc/low_power.rs | |||
| @@ -4,7 +4,7 @@ use embassy_time::{Duration, TICK_HZ}; | |||
| 4 | use super::{DateTimeError, Rtc, RtcError, bcd2_to_byte}; | 4 | use super::{DateTimeError, Rtc, RtcError, bcd2_to_byte}; |
| 5 | use crate::interrupt::typelevel::Interrupt; | 5 | use crate::interrupt::typelevel::Interrupt; |
| 6 | use crate::peripherals::RTC; | 6 | use crate::peripherals::RTC; |
| 7 | use crate::rtc::SealedInstance; | 7 | use crate::rtc::{RtcTimeProvider, SealedInstance}; |
| 8 | 8 | ||
| 9 | /// Represents an instant in time that can be substracted to compute a duration | 9 | /// Represents an instant in time that can be substracted to compute a duration |
| 10 | pub(super) struct RtcInstant { | 10 | pub(super) struct RtcInstant { |
| @@ -117,7 +117,7 @@ impl WakeupPrescaler { | |||
| 117 | impl Rtc { | 117 | impl Rtc { |
| 118 | /// Return the current instant. | 118 | /// Return the current instant. |
| 119 | fn instant(&self) -> Result<RtcInstant, RtcError> { | 119 | fn instant(&self) -> Result<RtcInstant, RtcError> { |
| 120 | self.time_provider().read(|_, tr, ss| { | 120 | RtcTimeProvider::new().read(|_, tr, ss| { |
| 121 | let second = bcd2_to_byte((tr.st(), tr.su())); | 121 | let second = bcd2_to_byte((tr.st(), tr.su())); |
| 122 | 122 | ||
| 123 | RtcInstant::from(second, ss).map_err(RtcError::InvalidDateTime) | 123 | RtcInstant::from(second, ss).map_err(RtcError::InvalidDateTime) |
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index fa5b45e3c..cbb904fd3 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs | |||
| @@ -5,7 +5,9 @@ mod datetime; | |||
| 5 | mod low_power; | 5 | mod low_power; |
| 6 | 6 | ||
| 7 | #[cfg(feature = "low-power")] | 7 | #[cfg(feature = "low-power")] |
| 8 | use core::cell::Cell; | 8 | use core::cell::{Cell, RefCell, RefMut}; |
| 9 | #[cfg(feature = "low-power")] | ||
| 10 | use core::ops; | ||
| 9 | 11 | ||
| 10 | #[cfg(feature = "low-power")] | 12 | #[cfg(feature = "low-power")] |
| 11 | use critical_section::CriticalSection; | 13 | use critical_section::CriticalSection; |
| @@ -52,9 +54,8 @@ pub struct RtcTimeProvider { | |||
| 52 | } | 54 | } |
| 53 | 55 | ||
| 54 | impl RtcTimeProvider { | 56 | impl RtcTimeProvider { |
| 55 | #[cfg(feature = "low-power")] | ||
| 56 | /// Create a new RTC time provider instance. | 57 | /// Create a new RTC time provider instance. |
| 57 | pub fn new(_rtc: Peri<'static, RTC>) -> Self { | 58 | pub(self) const fn new() -> Self { |
| 58 | Self { _private: () } | 59 | Self { _private: () } |
| 59 | } | 60 | } |
| 60 | 61 | ||
| @@ -115,6 +116,50 @@ impl RtcTimeProvider { | |||
| 115 | } | 116 | } |
| 116 | } | 117 | } |
| 117 | 118 | ||
| 119 | #[cfg(feature = "low-power")] | ||
| 120 | /// Contains an RTC driver. | ||
| 121 | pub struct RtcContainer { | ||
| 122 | pub(self) mutex: &'static Mutex<CriticalSectionRawMutex, RefCell<Option<Rtc>>>, | ||
| 123 | } | ||
| 124 | |||
| 125 | #[cfg(feature = "low-power")] | ||
| 126 | impl RtcContainer { | ||
| 127 | pub(self) const fn new() -> Self { | ||
| 128 | Self { | ||
| 129 | mutex: &crate::time_driver::get_driver().rtc, | ||
| 130 | } | ||
| 131 | } | ||
| 132 | |||
| 133 | /// Acquire an RTC borrow. | ||
| 134 | pub fn borrow_mut<'a>(&self, cs: CriticalSection<'a>) -> RtcBorrow<'a> { | ||
| 135 | RtcBorrow { | ||
| 136 | ref_mut: self.mutex.borrow(cs).borrow_mut(), | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | #[cfg(feature = "low-power")] | ||
| 142 | /// Contains an RTC borrow. | ||
| 143 | pub struct RtcBorrow<'a> { | ||
| 144 | pub(self) ref_mut: RefMut<'a, Option<Rtc>>, | ||
| 145 | } | ||
| 146 | |||
| 147 | #[cfg(feature = "low-power")] | ||
| 148 | impl<'a> ops::Deref for RtcBorrow<'a> { | ||
| 149 | type Target = Rtc; | ||
| 150 | |||
| 151 | fn deref(&self) -> &Self::Target { | ||
| 152 | self.ref_mut.as_ref().unwrap() | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | #[cfg(feature = "low-power")] | ||
| 157 | impl<'a> ops::DerefMut for RtcBorrow<'a> { | ||
| 158 | fn deref_mut(&mut self) -> &mut Self::Target { | ||
| 159 | self.ref_mut.as_mut().unwrap() | ||
| 160 | } | ||
| 161 | } | ||
| 162 | |||
| 118 | /// RTC driver. | 163 | /// RTC driver. |
| 119 | pub struct Rtc { | 164 | pub struct Rtc { |
| 120 | #[cfg(feature = "low-power")] | 165 | #[cfg(feature = "low-power")] |
| @@ -156,8 +201,14 @@ pub enum RtcCalibrationCyclePeriod { | |||
| 156 | impl Rtc { | 201 | impl Rtc { |
| 157 | #[cfg(not(feature = "low-power"))] | 202 | #[cfg(not(feature = "low-power"))] |
| 158 | /// Create a new RTC instance. | 203 | /// Create a new RTC instance. |
| 159 | pub fn new(_rtc: Peri<'static, RTC>, rtc_config: RtcConfig) -> Self { | 204 | pub fn new(_rtc: Peri<'static, RTC>, rtc_config: RtcConfig) -> (Self, RtcTimeProvider) { |
| 160 | Self::new_inner(rtc_config) | 205 | (Self::new_inner(rtc_config), RtcTimeProvider::new()) |
| 206 | } | ||
| 207 | |||
| 208 | #[cfg(feature = "low-power")] | ||
| 209 | /// Create a new RTC instance. | ||
| 210 | pub fn new(_rtc: Peri<'static, RTC>) -> (RtcContainer, RtcTimeProvider) { | ||
| 211 | (RtcContainer::new(), RtcTimeProvider::new()) | ||
| 161 | } | 212 | } |
| 162 | 213 | ||
| 163 | pub(self) fn new_inner(rtc_config: RtcConfig) -> Self { | 214 | pub(self) fn new_inner(rtc_config: RtcConfig) -> Self { |
| @@ -179,8 +230,8 @@ impl Rtc { | |||
| 179 | // Wait for the clock to update after initialization | 230 | // Wait for the clock to update after initialization |
| 180 | #[cfg(not(rtc_v2_f2))] | 231 | #[cfg(not(rtc_v2_f2))] |
| 181 | { | 232 | { |
| 182 | let now = this.time_provider().read(|_, _, ss| Ok(ss)).unwrap(); | 233 | let now = RtcTimeProvider::new().read(|_, _, ss| Ok(ss)).unwrap(); |
| 183 | while now == this.time_provider().read(|_, _, ss| Ok(ss)).unwrap() {} | 234 | while now == RtcTimeProvider::new().read(|_, _, ss| Ok(ss)).unwrap() {} |
| 184 | } | 235 | } |
| 185 | 236 | ||
| 186 | #[cfg(feature = "low-power")] | 237 | #[cfg(feature = "low-power")] |
| @@ -194,11 +245,6 @@ impl Rtc { | |||
| 194 | freqs.rtc.to_hertz().unwrap() | 245 | freqs.rtc.to_hertz().unwrap() |
| 195 | } | 246 | } |
| 196 | 247 | ||
| 197 | /// Acquire a [`RtcTimeProvider`] instance. | ||
| 198 | pub const fn time_provider(&self) -> RtcTimeProvider { | ||
| 199 | RtcTimeProvider { _private: () } | ||
| 200 | } | ||
| 201 | |||
| 202 | /// Set the datetime to a new value. | 248 | /// Set the datetime to a new value. |
| 203 | /// | 249 | /// |
| 204 | /// # Errors | 250 | /// # Errors |
| @@ -242,15 +288,6 @@ impl Rtc { | |||
| 242 | Ok(()) | 288 | Ok(()) |
| 243 | } | 289 | } |
| 244 | 290 | ||
| 245 | /// Return the current datetime. | ||
| 246 | /// | ||
| 247 | /// # Errors | ||
| 248 | /// | ||
| 249 | /// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`]. | ||
| 250 | pub fn now(&self) -> Result<DateTime, RtcError> { | ||
| 251 | self.time_provider().now() | ||
| 252 | } | ||
| 253 | |||
| 254 | /// Check if daylight savings time is active. | 291 | /// Check if daylight savings time is active. |
| 255 | pub fn get_daylight_savings(&self) -> bool { | 292 | pub fn get_daylight_savings(&self) -> bool { |
| 256 | let cr = RTC::regs().cr().read(); | 293 | let cr = RTC::regs().cr().read(); |
diff --git a/embassy-stm32/src/time_driver.rs b/embassy-stm32/src/time_driver.rs index 1941788e8..7db51d72e 100644 --- a/embassy-stm32/src/time_driver.rs +++ b/embassy-stm32/src/time_driver.rs | |||
| @@ -215,7 +215,7 @@ pub(crate) struct RtcDriver { | |||
| 215 | period: AtomicU32, | 215 | period: AtomicU32, |
| 216 | alarm: Mutex<CriticalSectionRawMutex, AlarmState>, | 216 | alarm: Mutex<CriticalSectionRawMutex, AlarmState>, |
| 217 | #[cfg(feature = "low-power")] | 217 | #[cfg(feature = "low-power")] |
| 218 | rtc: Mutex<CriticalSectionRawMutex, RefCell<Option<Rtc>>>, | 218 | pub(crate) rtc: Mutex<CriticalSectionRawMutex, RefCell<Option<Rtc>>>, |
| 219 | #[cfg(feature = "low-power")] | 219 | #[cfg(feature = "low-power")] |
| 220 | /// The minimum pause time beyond which the executor will enter a low-power state. | 220 | /// The minimum pause time beyond which the executor will enter a low-power state. |
| 221 | min_stop_pause: Mutex<CriticalSectionRawMutex, Cell<embassy_time::Duration>>, | 221 | min_stop_pause: Mutex<CriticalSectionRawMutex, Cell<embassy_time::Duration>>, |
| @@ -421,12 +421,6 @@ impl RtcDriver { | |||
| 421 | } | 421 | } |
| 422 | 422 | ||
| 423 | #[cfg(feature = "low-power")] | 423 | #[cfg(feature = "low-power")] |
| 424 | /// Reconfigure the rtc | ||
| 425 | pub(crate) fn reconfigure_rtc<R>(&self, f: impl FnOnce(&mut Rtc) -> R) -> R { | ||
| 426 | critical_section::with(|cs| f(self.rtc.borrow(cs).borrow_mut().as_mut().unwrap())) | ||
| 427 | } | ||
| 428 | |||
| 429 | #[cfg(feature = "low-power")] | ||
| 430 | /// Pause the timer if ready; return err if not | 424 | /// Pause the timer if ready; return err if not |
| 431 | pub(crate) fn pause_time(&self) -> Result<(), ()> { | 425 | pub(crate) fn pause_time(&self) -> Result<(), ()> { |
| 432 | critical_section::with(|cs| { | 426 | critical_section::with(|cs| { |
diff --git a/examples/stm32c0/src/bin/rtc.rs b/examples/stm32c0/src/bin/rtc.rs index feb27f6d9..5ff705ca2 100644 --- a/examples/stm32c0/src/bin/rtc.rs +++ b/examples/stm32c0/src/bin/rtc.rs | |||
| @@ -21,12 +21,12 @@ async fn main(_spawner: Spawner) { | |||
| 21 | .and_hms_opt(10, 30, 15) | 21 | .and_hms_opt(10, 30, 15) |
| 22 | .unwrap(); | 22 | .unwrap(); |
| 23 | 23 | ||
| 24 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 24 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 25 | 25 | ||
| 26 | rtc.set_datetime(now.into()).expect("datetime not set"); | 26 | rtc.set_datetime(now.into()).expect("datetime not set"); |
| 27 | 27 | ||
| 28 | loop { | 28 | loop { |
| 29 | let now: NaiveDateTime = rtc.now().unwrap().into(); | 29 | let now: NaiveDateTime = time_provider.now().unwrap().into(); |
| 30 | 30 | ||
| 31 | info!("{}", now.and_utc().timestamp()); | 31 | info!("{}", now.and_utc().timestamp()); |
| 32 | 32 | ||
diff --git a/examples/stm32f4/src/bin/rtc.rs b/examples/stm32f4/src/bin/rtc.rs index feb27f6d9..5ff705ca2 100644 --- a/examples/stm32f4/src/bin/rtc.rs +++ b/examples/stm32f4/src/bin/rtc.rs | |||
| @@ -21,12 +21,12 @@ async fn main(_spawner: Spawner) { | |||
| 21 | .and_hms_opt(10, 30, 15) | 21 | .and_hms_opt(10, 30, 15) |
| 22 | .unwrap(); | 22 | .unwrap(); |
| 23 | 23 | ||
| 24 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 24 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 25 | 25 | ||
| 26 | rtc.set_datetime(now.into()).expect("datetime not set"); | 26 | rtc.set_datetime(now.into()).expect("datetime not set"); |
| 27 | 27 | ||
| 28 | loop { | 28 | loop { |
| 29 | let now: NaiveDateTime = rtc.now().unwrap().into(); | 29 | let now: NaiveDateTime = time_provider.now().unwrap().into(); |
| 30 | 30 | ||
| 31 | info!("{}", now.and_utc().timestamp()); | 31 | info!("{}", now.and_utc().timestamp()); |
| 32 | 32 | ||
diff --git a/examples/stm32g0/src/bin/rtc.rs b/examples/stm32g0/src/bin/rtc.rs index 21da204cc..d8b58de22 100644 --- a/examples/stm32g0/src/bin/rtc.rs +++ b/examples/stm32g0/src/bin/rtc.rs | |||
| @@ -17,12 +17,12 @@ async fn main(_spawner: Spawner) { | |||
| 17 | 17 | ||
| 18 | let now = DateTime::from(2023, 6, 14, DayOfWeek::Friday, 15, 59, 10, 0); | 18 | let now = DateTime::from(2023, 6, 14, DayOfWeek::Friday, 15, 59, 10, 0); |
| 19 | 19 | ||
| 20 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 20 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 21 | 21 | ||
| 22 | rtc.set_datetime(now.unwrap()).expect("datetime not set"); | 22 | rtc.set_datetime(now.unwrap()).expect("datetime not set"); |
| 23 | 23 | ||
| 24 | loop { | 24 | loop { |
| 25 | let now: DateTime = rtc.now().unwrap().into(); | 25 | let now: DateTime = time_provider.now().unwrap().into(); |
| 26 | 26 | ||
| 27 | info!("{}:{}:{}", now.hour(), now.minute(), now.second()); | 27 | info!("{}:{}:{}", now.hour(), now.minute(), now.second()); |
| 28 | 28 | ||
diff --git a/examples/stm32h7/src/bin/rtc.rs b/examples/stm32h7/src/bin/rtc.rs index 1bd71637b..965716d23 100644 --- a/examples/stm32h7/src/bin/rtc.rs +++ b/examples/stm32h7/src/bin/rtc.rs | |||
| @@ -23,7 +23,7 @@ async fn main(_spawner: Spawner) { | |||
| 23 | .and_hms_opt(10, 30, 15) | 23 | .and_hms_opt(10, 30, 15) |
| 24 | .unwrap(); | 24 | .unwrap(); |
| 25 | 25 | ||
| 26 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 26 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 27 | info!("Got RTC! {:?}", now.and_utc().timestamp()); | 27 | info!("Got RTC! {:?}", now.and_utc().timestamp()); |
| 28 | 28 | ||
| 29 | rtc.set_datetime(now.into()).expect("datetime not set"); | 29 | rtc.set_datetime(now.into()).expect("datetime not set"); |
| @@ -31,6 +31,6 @@ async fn main(_spawner: Spawner) { | |||
| 31 | // In reality the delay would be much longer | 31 | // In reality the delay would be much longer |
| 32 | Timer::after_millis(20000).await; | 32 | Timer::after_millis(20000).await; |
| 33 | 33 | ||
| 34 | let then: NaiveDateTime = rtc.now().unwrap().into(); | 34 | let then: NaiveDateTime = time_provider.now().unwrap().into(); |
| 35 | info!("Got RTC! {:?}", then.and_utc().timestamp()); | 35 | info!("Got RTC! {:?}", then.and_utc().timestamp()); |
| 36 | } | 36 | } |
diff --git a/examples/stm32h7rs/src/bin/rtc.rs b/examples/stm32h7rs/src/bin/rtc.rs index 1bd71637b..965716d23 100644 --- a/examples/stm32h7rs/src/bin/rtc.rs +++ b/examples/stm32h7rs/src/bin/rtc.rs | |||
| @@ -23,7 +23,7 @@ async fn main(_spawner: Spawner) { | |||
| 23 | .and_hms_opt(10, 30, 15) | 23 | .and_hms_opt(10, 30, 15) |
| 24 | .unwrap(); | 24 | .unwrap(); |
| 25 | 25 | ||
| 26 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 26 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 27 | info!("Got RTC! {:?}", now.and_utc().timestamp()); | 27 | info!("Got RTC! {:?}", now.and_utc().timestamp()); |
| 28 | 28 | ||
| 29 | rtc.set_datetime(now.into()).expect("datetime not set"); | 29 | rtc.set_datetime(now.into()).expect("datetime not set"); |
| @@ -31,6 +31,6 @@ async fn main(_spawner: Spawner) { | |||
| 31 | // In reality the delay would be much longer | 31 | // In reality the delay would be much longer |
| 32 | Timer::after_millis(20000).await; | 32 | Timer::after_millis(20000).await; |
| 33 | 33 | ||
| 34 | let then: NaiveDateTime = rtc.now().unwrap().into(); | 34 | let then: NaiveDateTime = time_provider.now().unwrap().into(); |
| 35 | info!("Got RTC! {:?}", then.and_utc().timestamp()); | 35 | info!("Got RTC! {:?}", then.and_utc().timestamp()); |
| 36 | } | 36 | } |
diff --git a/examples/stm32l4/src/bin/rtc.rs b/examples/stm32l4/src/bin/rtc.rs index 1d26cd008..8b92075cc 100644 --- a/examples/stm32l4/src/bin/rtc.rs +++ b/examples/stm32l4/src/bin/rtc.rs | |||
| @@ -39,7 +39,7 @@ async fn main(_spawner: Spawner) { | |||
| 39 | .and_hms_opt(10, 30, 15) | 39 | .and_hms_opt(10, 30, 15) |
| 40 | .unwrap(); | 40 | .unwrap(); |
| 41 | 41 | ||
| 42 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 42 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 43 | info!("Got RTC! {:?}", now.and_utc().timestamp()); | 43 | info!("Got RTC! {:?}", now.and_utc().timestamp()); |
| 44 | 44 | ||
| 45 | rtc.set_datetime(now.into()).expect("datetime not set"); | 45 | rtc.set_datetime(now.into()).expect("datetime not set"); |
| @@ -47,6 +47,6 @@ async fn main(_spawner: Spawner) { | |||
| 47 | // In reality the delay would be much longer | 47 | // In reality the delay would be much longer |
| 48 | Timer::after_millis(20000).await; | 48 | Timer::after_millis(20000).await; |
| 49 | 49 | ||
| 50 | let then: NaiveDateTime = rtc.now().unwrap().into(); | 50 | let then: NaiveDateTime = time_provider.now().unwrap().into(); |
| 51 | info!("Got RTC! {:?}", then.and_utc().timestamp()); | 51 | info!("Got RTC! {:?}", then.and_utc().timestamp()); |
| 52 | } | 52 | } |
diff --git a/examples/stm32u0/src/bin/rtc.rs b/examples/stm32u0/src/bin/rtc.rs index d071cfbc7..56d16ccf7 100644 --- a/examples/stm32u0/src/bin/rtc.rs +++ b/examples/stm32u0/src/bin/rtc.rs | |||
| @@ -36,7 +36,7 @@ async fn main(_spawner: Spawner) { | |||
| 36 | .and_hms_opt(10, 30, 15) | 36 | .and_hms_opt(10, 30, 15) |
| 37 | .unwrap(); | 37 | .unwrap(); |
| 38 | 38 | ||
| 39 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 39 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 40 | info!("Got RTC! {:?}", now.and_utc().timestamp()); | 40 | info!("Got RTC! {:?}", now.and_utc().timestamp()); |
| 41 | 41 | ||
| 42 | rtc.set_datetime(now.into()).expect("datetime not set"); | 42 | rtc.set_datetime(now.into()).expect("datetime not set"); |
| @@ -44,6 +44,6 @@ async fn main(_spawner: Spawner) { | |||
| 44 | // In reality the delay would be much longer | 44 | // In reality the delay would be much longer |
| 45 | Timer::after_millis(20000).await; | 45 | Timer::after_millis(20000).await; |
| 46 | 46 | ||
| 47 | let then: NaiveDateTime = rtc.now().unwrap().into(); | 47 | let then: NaiveDateTime = time_provider.now().unwrap().into(); |
| 48 | info!("Got RTC! {:?}", then.and_utc().timestamp()); | 48 | info!("Got RTC! {:?}", then.and_utc().timestamp()); |
| 49 | } | 49 | } |
diff --git a/examples/stm32wl/src/bin/rtc.rs b/examples/stm32wl/src/bin/rtc.rs index d3709120f..2185142c9 100644 --- a/examples/stm32wl/src/bin/rtc.rs +++ b/examples/stm32wl/src/bin/rtc.rs | |||
| @@ -44,7 +44,7 @@ async fn main(_spawner: Spawner) { | |||
| 44 | .and_hms_opt(10, 30, 15) | 44 | .and_hms_opt(10, 30, 15) |
| 45 | .unwrap(); | 45 | .unwrap(); |
| 46 | 46 | ||
| 47 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 47 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 48 | info!("Got RTC! {:?}", now.and_utc().timestamp()); | 48 | info!("Got RTC! {:?}", now.and_utc().timestamp()); |
| 49 | 49 | ||
| 50 | rtc.set_datetime(now.into()).expect("datetime not set"); | 50 | rtc.set_datetime(now.into()).expect("datetime not set"); |
| @@ -52,6 +52,6 @@ async fn main(_spawner: Spawner) { | |||
| 52 | // In reality the delay would be much longer | 52 | // In reality the delay would be much longer |
| 53 | Timer::after_millis(20000).await; | 53 | Timer::after_millis(20000).await; |
| 54 | 54 | ||
| 55 | let then: NaiveDateTime = rtc.now().unwrap().into(); | 55 | let then: NaiveDateTime = time_provider.now().unwrap().into(); |
| 56 | info!("Got RTC! {:?}", then.and_utc().timestamp()); | 56 | info!("Got RTC! {:?}", then.and_utc().timestamp()); |
| 57 | } | 57 | } |
diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 1161e827b..e75a4ebb1 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml | |||
| @@ -94,6 +94,7 @@ rand_core = { version = "0.9.1", default-features = false } | |||
| 94 | rand_chacha = { version = "0.9.0", default-features = false } | 94 | rand_chacha = { version = "0.9.0", default-features = false } |
| 95 | static_cell = "2" | 95 | static_cell = "2" |
| 96 | portable-atomic = { version = "1.5", features = [] } | 96 | portable-atomic = { version = "1.5", features = [] } |
| 97 | critical-section = "1.1" | ||
| 97 | 98 | ||
| 98 | chrono = { version = "^0.4", default-features = false, optional = true} | 99 | chrono = { version = "^0.4", default-features = false, optional = true} |
| 99 | sha2 = { version = "0.10.8", default-features = false } | 100 | sha2 = { version = "0.10.8", default-features = false } |
diff --git a/tests/stm32/src/bin/rtc.rs b/tests/stm32/src/bin/rtc.rs index 5c80eb250..43cf4a411 100644 --- a/tests/stm32/src/bin/rtc.rs +++ b/tests/stm32/src/bin/rtc.rs | |||
| @@ -9,11 +9,9 @@ use chrono::{NaiveDate, NaiveDateTime}; | |||
| 9 | use common::*; | 9 | use common::*; |
| 10 | use defmt::assert; | 10 | use defmt::assert; |
| 11 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 12 | #[cfg(feature = "stop")] | ||
| 13 | use embassy_stm32::low_power::reconfigure_rtc; | ||
| 14 | use embassy_stm32::rcc::LsConfig; | 12 | use embassy_stm32::rcc::LsConfig; |
| 15 | #[cfg(feature = "stop")] | 13 | #[cfg(feature = "stop")] |
| 16 | use embassy_stm32::rtc::RtcTimeProvider; | 14 | use embassy_stm32::rtc::Rtc; |
| 17 | #[cfg(not(feature = "stop"))] | 15 | #[cfg(not(feature = "stop"))] |
| 18 | use embassy_stm32::rtc::{Rtc, RtcConfig}; | 16 | use embassy_stm32::rtc::{Rtc, RtcConfig}; |
| 19 | use embassy_time::Timer; | 17 | use embassy_time::Timer; |
| @@ -31,23 +29,22 @@ async fn main(_spawner: Spawner) { | |||
| 31 | .unwrap(); | 29 | .unwrap(); |
| 32 | 30 | ||
| 33 | #[cfg(not(feature = "stop"))] | 31 | #[cfg(not(feature = "stop"))] |
| 34 | let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); | 32 | let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); |
| 35 | 33 | ||
| 36 | #[cfg(feature = "stop")] | 34 | #[cfg(feature = "stop")] |
| 37 | let time_provider = RtcTimeProvider::new(p.RTC); | 35 | let (rtc, time_provider) = Rtc::new(p.RTC); |
| 38 | 36 | ||
| 39 | #[cfg(not(feature = "stop"))] | 37 | #[cfg(not(feature = "stop"))] |
| 40 | rtc.set_datetime(now.into()).expect("datetime not set"); | 38 | rtc.set_datetime(now.into()).expect("datetime not set"); |
| 41 | 39 | ||
| 42 | #[cfg(feature = "stop")] | 40 | #[cfg(feature = "stop")] |
| 43 | reconfigure_rtc(|rtc| rtc.set_datetime(now.into()).expect("datetime not set")); | 41 | critical_section::with(|cs| { |
| 42 | rtc.borrow_mut(cs).set_datetime(now.into()).expect("datetime not set"); | ||
| 43 | }); | ||
| 44 | 44 | ||
| 45 | info!("Waiting 5 seconds"); | 45 | info!("Waiting 5 seconds"); |
| 46 | Timer::after_millis(5000).await; | 46 | Timer::after_millis(5000).await; |
| 47 | 47 | ||
| 48 | #[cfg(not(feature = "stop"))] | ||
| 49 | let then: NaiveDateTime = rtc.now().unwrap().into(); | ||
| 50 | #[cfg(feature = "stop")] | ||
| 51 | let then: NaiveDateTime = time_provider.now().unwrap().into(); | 48 | let then: NaiveDateTime = time_provider.now().unwrap().into(); |
| 52 | 49 | ||
| 53 | let seconds = (then - now).num_seconds(); | 50 | let seconds = (then - now).num_seconds(); |
diff --git a/tests/stm32/src/bin/stop.rs b/tests/stm32/src/bin/stop.rs index a9dbac676..4b3a775bb 100644 --- a/tests/stm32/src/bin/stop.rs +++ b/tests/stm32/src/bin/stop.rs | |||
| @@ -10,8 +10,9 @@ use common::*; | |||
| 10 | use cortex_m_rt::entry; | 10 | use cortex_m_rt::entry; |
| 11 | use embassy_executor::Spawner; | 11 | use embassy_executor::Spawner; |
| 12 | use embassy_stm32::Config; | 12 | use embassy_stm32::Config; |
| 13 | use embassy_stm32::low_power::{Executor, StopMode, reconfigure_rtc, stop_ready}; | 13 | use embassy_stm32::low_power::{Executor, StopMode, stop_ready}; |
| 14 | use embassy_stm32::rcc::LsConfig; | 14 | use embassy_stm32::rcc::LsConfig; |
| 15 | use embassy_stm32::rtc::Rtc; | ||
| 15 | use embassy_time::Timer; | 16 | use embassy_time::Timer; |
| 16 | 17 | ||
| 17 | #[entry] | 18 | #[entry] |
| @@ -56,7 +57,7 @@ async fn async_main(spawner: Spawner) { | |||
| 56 | config.rcc.hsi = Some(HSIPrescaler::DIV4); // 64 MHz HSI will need a /4 | 57 | config.rcc.hsi = Some(HSIPrescaler::DIV4); // 64 MHz HSI will need a /4 |
| 57 | } | 58 | } |
| 58 | 59 | ||
| 59 | let _p = init_with_config(config); | 60 | let p = init_with_config(config); |
| 60 | info!("Hello World!"); | 61 | info!("Hello World!"); |
| 61 | 62 | ||
| 62 | let now = NaiveDate::from_ymd_opt(2020, 5, 15) | 63 | let now = NaiveDate::from_ymd_opt(2020, 5, 15) |
| @@ -64,7 +65,11 @@ async fn async_main(spawner: Spawner) { | |||
| 64 | .and_hms_opt(10, 30, 15) | 65 | .and_hms_opt(10, 30, 15) |
| 65 | .unwrap(); | 66 | .unwrap(); |
| 66 | 67 | ||
| 67 | reconfigure_rtc(|rtc| rtc.set_datetime(now.into()).expect("datetime not set")); | 68 | let (rtc, _time_provider) = Rtc::new(p.RTC); |
| 69 | |||
| 70 | critical_section::with(|cs| { | ||
| 71 | rtc.borrow_mut(cs).set_datetime(now.into()).expect("datetime not set"); | ||
| 72 | }); | ||
| 68 | 73 | ||
| 69 | spawner.spawn(task_1().unwrap()); | 74 | spawner.spawn(task_1().unwrap()); |
| 70 | spawner.spawn(task_2().unwrap()); | 75 | spawner.spawn(task_2().unwrap()); |
