Skip to content

Commit b9559c7

Browse files
committed
rtc: use consistent api between stop and non-stop
1 parent d23c027 commit b9559c7

File tree

15 files changed

+92
-65
lines changed

15 files changed

+92
-65
lines changed

embassy-stm32/src/low_power.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ use crate::time_driver::get_driver;
6161

6262
const THREAD_PENDER: usize = usize::MAX;
6363

64-
use crate::rtc::Rtc;
65-
6664
static mut EXECUTOR: Option<Executor> = None;
6765

6866
/// Prevent the device from going into the stop mode if held
@@ -133,11 +131,6 @@ foreach_interrupt! {
133131
};
134132
}
135133

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-
141134
/// Get whether the core is ready to enter the given stop mode.
142135
///
143136
/// This will return false if some peripheral driver is in use that

embassy-stm32/src/rtc/low_power.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use embassy_time::{Duration, TICK_HZ};
44
use super::{DateTimeError, Rtc, RtcError, bcd2_to_byte};
55
use crate::interrupt::typelevel::Interrupt;
66
use crate::peripherals::RTC;
7-
use crate::rtc::SealedInstance;
7+
use crate::rtc::{RtcTimeProvider, SealedInstance};
88

99
/// Represents an instant in time that can be substracted to compute a duration
1010
pub(super) struct RtcInstant {
@@ -117,7 +117,7 @@ impl WakeupPrescaler {
117117
impl Rtc {
118118
/// Return the current instant.
119119
fn instant(&self) -> Result<RtcInstant, RtcError> {
120-
self.time_provider().read(|_, tr, ss| {
120+
RtcTimeProvider::new().read(|_, tr, ss| {
121121
let second = bcd2_to_byte((tr.st(), tr.su()));
122122

123123
RtcInstant::from(second, ss).map_err(RtcError::InvalidDateTime)

embassy-stm32/src/rtc/mod.rs

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ mod datetime;
55
mod low_power;
66

77
#[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;
911

1012
#[cfg(feature = "low-power")]
1113
use critical_section::CriticalSection;
@@ -52,9 +54,8 @@ pub struct RtcTimeProvider {
5254
}
5355

5456
impl RtcTimeProvider {
55-
#[cfg(feature = "low-power")]
5657
/// Create a new RTC time provider instance.
57-
pub fn new(_rtc: Peri<'static, RTC>) -> Self {
58+
pub(self) const fn new() -> Self {
5859
Self { _private: () }
5960
}
6061

@@ -115,6 +116,50 @@ impl RtcTimeProvider {
115116
}
116117
}
117118

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+
118163
/// RTC driver.
119164
pub struct Rtc {
120165
#[cfg(feature = "low-power")]
@@ -156,8 +201,14 @@ pub enum RtcCalibrationCyclePeriod {
156201
impl Rtc {
157202
#[cfg(not(feature = "low-power"))]
158203
/// Create a new RTC instance.
159-
pub fn new(_rtc: Peri<'static, RTC>, rtc_config: RtcConfig) -> Self {
160-
Self::new_inner(rtc_config)
204+
pub fn new(_rtc: Peri<'static, RTC>, rtc_config: RtcConfig) -> (Self, RtcTimeProvider) {
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())
161212
}
162213

163214
pub(self) fn new_inner(rtc_config: RtcConfig) -> Self {
@@ -179,8 +230,8 @@ impl Rtc {
179230
// Wait for the clock to update after initialization
180231
#[cfg(not(rtc_v2_f2))]
181232
{
182-
let now = this.time_provider().read(|_, _, ss| Ok(ss)).unwrap();
183-
while now == this.time_provider().read(|_, _, ss| Ok(ss)).unwrap() {}
233+
let now = RtcTimeProvider::new().read(|_, _, ss| Ok(ss)).unwrap();
234+
while now == RtcTimeProvider::new().read(|_, _, ss| Ok(ss)).unwrap() {}
184235
}
185236

186237
#[cfg(feature = "low-power")]
@@ -194,11 +245,6 @@ impl Rtc {
194245
freqs.rtc.to_hertz().unwrap()
195246
}
196247

197-
/// Acquire a [`RtcTimeProvider`] instance.
198-
pub const fn time_provider(&self) -> RtcTimeProvider {
199-
RtcTimeProvider { _private: () }
200-
}
201-
202248
/// Set the datetime to a new value.
203249
///
204250
/// # Errors
@@ -242,15 +288,6 @@ impl Rtc {
242288
Ok(())
243289
}
244290

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-
254291
/// Check if daylight savings time is active.
255292
pub fn get_daylight_savings(&self) -> bool {
256293
let cr = RTC::regs().cr().read();

embassy-stm32/src/time_driver.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ pub(crate) struct RtcDriver {
215215
period: AtomicU32,
216216
alarm: Mutex<CriticalSectionRawMutex, AlarmState>,
217217
#[cfg(feature = "low-power")]
218-
rtc: Mutex<CriticalSectionRawMutex, RefCell<Option<Rtc>>>,
218+
pub(crate) rtc: Mutex<CriticalSectionRawMutex, RefCell<Option<Rtc>>>,
219219
#[cfg(feature = "low-power")]
220220
/// The minimum pause time beyond which the executor will enter a low-power state.
221221
min_stop_pause: Mutex<CriticalSectionRawMutex, Cell<embassy_time::Duration>>,
@@ -420,12 +420,6 @@ impl RtcDriver {
420420
assert!(self.rtc.borrow(cs).replace(Some(rtc)).is_none());
421421
}
422422

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-
429423
#[cfg(feature = "low-power")]
430424
/// Pause the timer if ready; return err if not
431425
pub(crate) fn pause_time(&self) -> Result<(), ()> {

examples/stm32c0/src/bin/rtc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ async fn main(_spawner: Spawner) {
2121
.and_hms_opt(10, 30, 15)
2222
.unwrap();
2323

24-
let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
24+
let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default());
2525

2626
rtc.set_datetime(now.into()).expect("datetime not set");
2727

2828
loop {
29-
let now: NaiveDateTime = rtc.now().unwrap().into();
29+
let now: NaiveDateTime = time_provider.now().unwrap().into();
3030

3131
info!("{}", now.and_utc().timestamp());
3232

examples/stm32f4/src/bin/rtc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ async fn main(_spawner: Spawner) {
2121
.and_hms_opt(10, 30, 15)
2222
.unwrap();
2323

24-
let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
24+
let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default());
2525

2626
rtc.set_datetime(now.into()).expect("datetime not set");
2727

2828
loop {
29-
let now: NaiveDateTime = rtc.now().unwrap().into();
29+
let now: NaiveDateTime = time_provider.now().unwrap().into();
3030

3131
info!("{}", now.and_utc().timestamp());
3232

examples/stm32g0/src/bin/rtc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ async fn main(_spawner: Spawner) {
1717

1818
let now = DateTime::from(2023, 6, 14, DayOfWeek::Friday, 15, 59, 10, 0);
1919

20-
let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
20+
let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default());
2121

2222
rtc.set_datetime(now.unwrap()).expect("datetime not set");
2323

2424
loop {
25-
let now: DateTime = rtc.now().unwrap().into();
25+
let now: DateTime = time_provider.now().unwrap().into();
2626

2727
info!("{}:{}:{}", now.hour(), now.minute(), now.second());
2828

examples/stm32h7/src/bin/rtc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ async fn main(_spawner: Spawner) {
2323
.and_hms_opt(10, 30, 15)
2424
.unwrap();
2525

26-
let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
26+
let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default());
2727
info!("Got RTC! {:?}", now.and_utc().timestamp());
2828

2929
rtc.set_datetime(now.into()).expect("datetime not set");
3030

3131
// In reality the delay would be much longer
3232
Timer::after_millis(20000).await;
3333

34-
let then: NaiveDateTime = rtc.now().unwrap().into();
34+
let then: NaiveDateTime = time_provider.now().unwrap().into();
3535
info!("Got RTC! {:?}", then.and_utc().timestamp());
3636
}

examples/stm32h7rs/src/bin/rtc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ async fn main(_spawner: Spawner) {
2323
.and_hms_opt(10, 30, 15)
2424
.unwrap();
2525

26-
let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
26+
let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default());
2727
info!("Got RTC! {:?}", now.and_utc().timestamp());
2828

2929
rtc.set_datetime(now.into()).expect("datetime not set");
3030

3131
// In reality the delay would be much longer
3232
Timer::after_millis(20000).await;
3333

34-
let then: NaiveDateTime = rtc.now().unwrap().into();
34+
let then: NaiveDateTime = time_provider.now().unwrap().into();
3535
info!("Got RTC! {:?}", then.and_utc().timestamp());
3636
}

examples/stm32l4/src/bin/rtc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ async fn main(_spawner: Spawner) {
3939
.and_hms_opt(10, 30, 15)
4040
.unwrap();
4141

42-
let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
42+
let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default());
4343
info!("Got RTC! {:?}", now.and_utc().timestamp());
4444

4545
rtc.set_datetime(now.into()).expect("datetime not set");
4646

4747
// In reality the delay would be much longer
4848
Timer::after_millis(20000).await;
4949

50-
let then: NaiveDateTime = rtc.now().unwrap().into();
50+
let then: NaiveDateTime = time_provider.now().unwrap().into();
5151
info!("Got RTC! {:?}", then.and_utc().timestamp());
5252
}

0 commit comments

Comments
 (0)