From b9559c7713bc7f773cdef0df14f1158840d06d06 Mon Sep 17 00:00:00 2001 From: xoviat Date: Tue, 4 Nov 2025 16:35:07 -0600 Subject: rtc: use consistent api between stop and non-stop --- tests/stm32/Cargo.toml | 1 + tests/stm32/src/bin/rtc.rs | 15 ++++++--------- tests/stm32/src/bin/stop.rs | 11 ++++++++--- 3 files changed, 15 insertions(+), 12 deletions(-) (limited to 'tests') 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 } rand_chacha = { version = "0.9.0", default-features = false } static_cell = "2" portable-atomic = { version = "1.5", features = [] } +critical-section = "1.1" chrono = { version = "^0.4", default-features = false, optional = true} 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}; use common::*; use defmt::assert; use embassy_executor::Spawner; -#[cfg(feature = "stop")] -use embassy_stm32::low_power::reconfigure_rtc; use embassy_stm32::rcc::LsConfig; #[cfg(feature = "stop")] -use embassy_stm32::rtc::RtcTimeProvider; +use embassy_stm32::rtc::Rtc; #[cfg(not(feature = "stop"))] use embassy_stm32::rtc::{Rtc, RtcConfig}; use embassy_time::Timer; @@ -31,23 +29,22 @@ async fn main(_spawner: Spawner) { .unwrap(); #[cfg(not(feature = "stop"))] - let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); + let (mut rtc, time_provider) = Rtc::new(p.RTC, RtcConfig::default()); #[cfg(feature = "stop")] - let time_provider = RtcTimeProvider::new(p.RTC); + let (rtc, time_provider) = Rtc::new(p.RTC); #[cfg(not(feature = "stop"))] rtc.set_datetime(now.into()).expect("datetime not set"); #[cfg(feature = "stop")] - reconfigure_rtc(|rtc| rtc.set_datetime(now.into()).expect("datetime not set")); + critical_section::with(|cs| { + rtc.borrow_mut(cs).set_datetime(now.into()).expect("datetime not set"); + }); info!("Waiting 5 seconds"); Timer::after_millis(5000).await; - #[cfg(not(feature = "stop"))] - let then: NaiveDateTime = rtc.now().unwrap().into(); - #[cfg(feature = "stop")] let then: NaiveDateTime = time_provider.now().unwrap().into(); 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::*; use cortex_m_rt::entry; use embassy_executor::Spawner; use embassy_stm32::Config; -use embassy_stm32::low_power::{Executor, StopMode, reconfigure_rtc, stop_ready}; +use embassy_stm32::low_power::{Executor, StopMode, stop_ready}; use embassy_stm32::rcc::LsConfig; +use embassy_stm32::rtc::Rtc; use embassy_time::Timer; #[entry] @@ -56,7 +57,7 @@ async fn async_main(spawner: Spawner) { config.rcc.hsi = Some(HSIPrescaler::DIV4); // 64 MHz HSI will need a /4 } - let _p = init_with_config(config); + let p = init_with_config(config); info!("Hello World!"); let now = NaiveDate::from_ymd_opt(2020, 5, 15) @@ -64,7 +65,11 @@ async fn async_main(spawner: Spawner) { .and_hms_opt(10, 30, 15) .unwrap(); - reconfigure_rtc(|rtc| rtc.set_datetime(now.into()).expect("datetime not set")); + let (rtc, _time_provider) = Rtc::new(p.RTC); + + critical_section::with(|cs| { + rtc.borrow_mut(cs).set_datetime(now.into()).expect("datetime not set"); + }); spawner.spawn(task_1().unwrap()); spawner.spawn(task_2().unwrap()); -- cgit