diff --git a/embassy-stm32/src/rtc/datetime.rs b/embassy-stm32/src/rtc/datetime.rs index 6274c1e0..0a590c1b 100644 --- a/embassy-stm32/src/rtc/datetime.rs +++ b/embassy-stm32/src/rtc/datetime.rs @@ -51,7 +51,7 @@ pub struct DateTime { impl From for DateTime { fn from(date_time: chrono::NaiveDateTime) -> Self { Self { - year: (date_time.year() - 1970) as u16, + year: date_time.year() as u16, month: date_time.month() as u8, day: date_time.day() as u8, day_of_week: date_time.weekday().into(), @@ -65,14 +65,10 @@ impl From for DateTime { #[cfg(feature = "chrono")] impl From for chrono::NaiveDateTime { fn from(date_time: DateTime) -> Self { - NaiveDate::from_ymd_opt( - (date_time.year + 1970) as i32, - date_time.month as u32, - date_time.day as u32, - ) - .unwrap() - .and_hms_opt(date_time.hour as u32, date_time.minute as u32, date_time.second as u32) - .unwrap() + NaiveDate::from_ymd_opt(date_time.year as i32, date_time.month as u32, date_time.day as u32) + .unwrap() + .and_hms_opt(date_time.hour as u32, date_time.minute as u32, date_time.second as u32) + .unwrap() } } @@ -159,6 +155,8 @@ pub(super) fn write_date_time(rtc: &Rtc, t: DateTime) { let (yt, yu) = byte_to_bcd2(yr_offset); unsafe { + use crate::pac::rtc::vals::Ampm; + rtc.tr().write(|w| { w.set_ht(ht); w.set_hu(hu); @@ -166,7 +164,7 @@ pub(super) fn write_date_time(rtc: &Rtc, t: DateTime) { w.set_mnu(mnu); w.set_st(st); w.set_su(su); - w.set_pm(stm32_metapac::rtc::vals::Ampm::AM); + w.set_pm(Ampm::AM); }); rtc.dr().write(|w| { diff --git a/tests/stm32/Cargo.toml b/tests/stm32/Cargo.toml index 3047c34c..c48d2645 100644 --- a/tests/stm32/Cargo.toml +++ b/tests/stm32/Cargo.toml @@ -6,7 +6,7 @@ license = "MIT OR Apache-2.0" [features] stm32f103c8 = ["embassy-stm32/stm32f103c8"] # Blue Pill -stm32f429zi = ["embassy-stm32/stm32f429zi", "sdmmc"] # Nucleo +stm32f429zi = ["embassy-stm32/stm32f429zi", "sdmmc", "chrono"] # Nucleo stm32g071rb = ["embassy-stm32/stm32g071rb"] # Nucleo stm32c031c6 = ["embassy-stm32/stm32c031c6"] # Nucleo stm32g491re = ["embassy-stm32/stm32g491re"] # Nucleo @@ -16,6 +16,7 @@ stm32h563zi = ["embassy-stm32/stm32h563zi"] # Nucleo stm32u585ai = ["embassy-stm32/stm32u585ai"] # IoT board sdmmc = [] +chrono = ["embassy-stm32/chrono", "dep:chrono"] [dependencies] embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } @@ -33,6 +34,8 @@ embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10" } embedded-hal-async = { version = "=0.2.0-alpha.1" } panic-probe = { version = "0.3.0", features = ["print-defmt"] } +chrono = { version = "^0.4", default-features = false, optional = true} + # BEGIN TESTS # Generated by gen_test.py. DO NOT EDIT. [[bin]] @@ -40,6 +43,11 @@ name = "gpio" path = "src/bin/gpio.rs" required-features = [] +[[bin]] +name = "rtc" +path = "src/bin/rtc.rs" +required-features = [ "chrono",] + [[bin]] name = "sdmmc" path = "src/bin/sdmmc.rs" diff --git a/tests/stm32/src/bin/rtc.rs b/tests/stm32/src/bin/rtc.rs new file mode 100644 index 00000000..ccf2ca60 --- /dev/null +++ b/tests/stm32/src/bin/rtc.rs @@ -0,0 +1,52 @@ +#![no_std] +#![no_main] +#![feature(type_alias_impl_trait)] + +// required-features: chrono + +#[path = "../example_common.rs"] +mod example_common; +use chrono::{NaiveDate, NaiveDateTime}; +use defmt::assert; +use embassy_executor::Spawner; +use embassy_stm32::pac; +use embassy_stm32::rtc::{Rtc, RtcConfig}; +use embassy_time::{Duration, Timer}; +use example_common::*; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = embassy_stm32::init(config()); + info!("Hello World!"); + + let now = NaiveDate::from_ymd_opt(2020, 5, 15) + .unwrap() + .and_hms_opt(10, 30, 15) + .unwrap(); + + info!("Starting LSI"); + + unsafe { + pac::RCC.csr().modify(|w| w.set_lsion(true)); + while !pac::RCC.csr().read().lsirdy() {} + } + + info!("Started LSI"); + + let mut rtc = Rtc::new(p.RTC, RtcConfig::default()); + + rtc.set_datetime(now.into()).expect("datetime not set"); + + info!("Waiting 5 seconds"); + Timer::after(Duration::from_millis(5000)).await; + + let then: NaiveDateTime = rtc.now().unwrap().into(); + let seconds = (then - now).num_seconds(); + + defmt::info!("measured = {}", seconds); + + assert!(seconds > 3 && seconds < 7); + + info!("Test OK"); + cortex_m::asm::bkpt(); +}