diff --git a/embassy-stm32/src/rtc/datetime.rs b/embassy-stm32/src/rtc/datetime.rs index 3efe9be5..a1943cf3 100644 --- a/embassy-stm32/src/rtc/datetime.rs +++ b/embassy-stm32/src/rtc/datetime.rs @@ -4,8 +4,60 @@ use core::convert::From; #[cfg(feature = "chrono")] use chrono::{self, Datelike, NaiveDate, Timelike, Weekday}; -use super::byte_to_bcd2; -use crate::pac::rtc::Rtc; +#[cfg(any(feature = "defmt", feature = "time"))] +use crate::peripherals::RTC; +#[cfg(any(feature = "defmt", feature = "time"))] +use crate::rtc::sealed::Instance; + +/// Represents an instant in time that can be substracted to compute a duration +pub struct RtcInstant { + /// 0..59 + pub second: u8, + /// 0..256 + pub subsecond: u16, +} + +impl RtcInstant { + #[allow(dead_code)] + pub(super) fn from(second: u8, subsecond: u16) -> Result { + Ok(Self { second, subsecond }) + } +} + +#[cfg(feature = "defmt")] +impl defmt::Format for RtcInstant { + fn format(&self, fmt: defmt::Formatter) { + defmt::write!( + fmt, + "{}:{}", + self.second, + RTC::regs().prer().read().prediv_s() - self.subsecond, + ) + } +} + +#[cfg(feature = "time")] +impl core::ops::Sub for RtcInstant { + type Output = embassy_time::Duration; + + fn sub(self, rhs: Self) -> Self::Output { + use embassy_time::{Duration, TICK_HZ}; + + let second = if self.second < rhs.second { + self.second + 60 + } else { + self.second + }; + + let psc = RTC::regs().prer().read().prediv_s() as u32; + + let self_ticks = second as u32 * (psc + 1) + (psc - self.subsecond as u32); + let other_ticks = rhs.second as u32 * (psc + 1) + (psc - rhs.subsecond as u32); + let rtc_ticks = self_ticks - other_ticks; + + Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / (psc + 1)) as u64) + } +} /// Errors regarding the [`DateTime`] struct. #[derive(Clone, Debug, PartialEq, Eq)] @@ -32,19 +84,85 @@ pub enum Error { /// Structure containing date and time information pub struct DateTime { /// 0..4095 - pub year: u16, + year: u16, /// 1..12, 1 is January - pub month: u8, + month: u8, /// 1..28,29,30,31 depending on month - pub day: u8, + day: u8, /// - pub day_of_week: DayOfWeek, + day_of_week: DayOfWeek, /// 0..23 - pub hour: u8, + hour: u8, /// 0..59 - pub minute: u8, + minute: u8, /// 0..59 - pub second: u8, + second: u8, +} + +impl DateTime { + pub const fn year(&self) -> u16 { + self.year + } + + pub const fn month(&self) -> u8 { + self.month + } + + pub const fn day(&self) -> u8 { + self.day + } + + pub const fn day_of_week(&self) -> DayOfWeek { + self.day_of_week + } + + pub const fn hour(&self) -> u8 { + self.hour + } + + pub const fn minute(&self) -> u8 { + self.minute + } + + pub const fn second(&self) -> u8 { + self.second + } + + pub fn from( + year: u16, + month: u8, + day: u8, + day_of_week: u8, + hour: u8, + minute: u8, + second: u8, + ) -> Result { + let day_of_week = day_of_week_from_u8(day_of_week)?; + + if year > 4095 { + Err(Error::InvalidYear) + } else if month < 1 || month > 12 { + Err(Error::InvalidMonth) + } else if day < 1 || day > 31 { + Err(Error::InvalidDay) + } else if hour > 23 { + Err(Error::InvalidHour) + } else if minute > 59 { + Err(Error::InvalidMinute) + } else if second > 59 { + Err(Error::InvalidSecond) + } else { + Ok(Self { + year, + month, + day, + day_of_week, + hour, + minute, + second, + }) + } + } } #[cfg(feature = "chrono")] @@ -142,58 +260,3 @@ pub(super) fn validate_datetime(dt: &DateTime) -> Result<(), Error> { Ok(()) } } - -pub(super) fn write_date_time(rtc: &Rtc, t: DateTime) { - let (ht, hu) = byte_to_bcd2(t.hour as u8); - let (mnt, mnu) = byte_to_bcd2(t.minute as u8); - let (st, su) = byte_to_bcd2(t.second as u8); - - let (dt, du) = byte_to_bcd2(t.day as u8); - let (mt, mu) = byte_to_bcd2(t.month as u8); - let yr = t.year as u16; - let yr_offset = (yr - 1970_u16) as u8; - let (yt, yu) = byte_to_bcd2(yr_offset); - - use crate::pac::rtc::vals::Ampm; - - rtc.tr().write(|w| { - w.set_ht(ht); - w.set_hu(hu); - w.set_mnt(mnt); - w.set_mnu(mnu); - w.set_st(st); - w.set_su(su); - w.set_pm(Ampm::AM); - }); - - rtc.dr().write(|w| { - w.set_dt(dt); - w.set_du(du); - w.set_mt(mt > 0); - w.set_mu(mu); - w.set_yt(yt); - w.set_yu(yu); - w.set_wdu(day_of_week_to_u8(t.day_of_week)); - }); -} - -pub(super) fn datetime( - year: u16, - month: u8, - day: u8, - day_of_week: u8, - hour: u8, - minute: u8, - second: u8, -) -> Result { - let day_of_week = day_of_week_from_u8(day_of_week)?; - Ok(DateTime { - year, - month, - day, - day_of_week, - hour, - minute, - second, - }) -} diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index d77443a9..da79c392 100644 --- a/embassy-stm32/src/rtc/mod.rs +++ b/embassy-stm32/src/rtc/mod.rs @@ -9,7 +9,8 @@ use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; #[cfg(feature = "low-power")] use embassy_sync::blocking_mutex::Mutex; -pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; +pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError, RtcInstant}; +use crate::rtc::datetime::day_of_week_to_u8; use crate::time::Hertz; /// refer to AN4759 to compare features of RTC2 and RTC3 @@ -39,48 +40,6 @@ pub enum RtcError { NotRunning, } -#[cfg(feature = "low-power")] -/// Represents an instant in time that can be substracted to compute a duration -struct RtcInstant { - second: u8, - subsecond: u16, -} - -#[cfg(all(feature = "low-power", feature = "defmt"))] -impl defmt::Format for RtcInstant { - fn format(&self, fmt: defmt::Formatter) { - defmt::write!( - fmt, - "{}:{}", - self.second, - RTC::regs().prer().read().prediv_s() - self.subsecond, - ) - } -} - -#[cfg(feature = "low-power")] -impl core::ops::Sub for RtcInstant { - type Output = embassy_time::Duration; - - fn sub(self, rhs: Self) -> Self::Output { - use embassy_time::{Duration, TICK_HZ}; - - let second = if self.second < rhs.second { - self.second + 60 - } else { - self.second - }; - - let psc = RTC::regs().prer().read().prediv_s() as u32; - - let self_ticks = second as u32 * (psc + 1) + (psc - self.subsecond as u32); - let other_ticks = rhs.second as u32 * (psc + 1) + (psc - rhs.subsecond as u32); - let rtc_ticks = self_ticks - other_ticks; - - Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / (psc + 1)) as u64) - } -} - pub struct RtcTimeProvider { _private: (), } @@ -113,7 +72,7 @@ impl RtcTimeProvider { let month = bcd2_to_byte((dr.mt() as u8, dr.mu())); let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16; - return self::datetime::datetime(year, month, day, weekday, hour, minute, second) + return DateTime::from(year, month, day, weekday, hour, minute, second) .map_err(RtcError::InvalidDateTime); } } @@ -134,7 +93,7 @@ impl RtcTimeProvider { let month = bcd2_to_byte((dr.mt() as u8, dr.mu())); let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16; - self::datetime::datetime(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime) + DateTime::from(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime) } } } @@ -223,14 +182,46 @@ impl Rtc { /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range. pub fn set_datetime(&mut self, t: DateTime) -> Result<(), RtcError> { self::datetime::validate_datetime(&t).map_err(RtcError::InvalidDateTime)?; - self.write(true, |rtc| self::datetime::write_date_time(rtc, t)); + self.write(true, |rtc| { + let (ht, hu) = byte_to_bcd2(t.hour() as u8); + let (mnt, mnu) = byte_to_bcd2(t.minute() as u8); + let (st, su) = byte_to_bcd2(t.second() as u8); + + let (dt, du) = byte_to_bcd2(t.day() as u8); + let (mt, mu) = byte_to_bcd2(t.month() as u8); + let yr = t.year() as u16; + let yr_offset = (yr - 1970_u16) as u8; + let (yt, yu) = byte_to_bcd2(yr_offset); + + use crate::pac::rtc::vals::Ampm; + + rtc.tr().write(|w| { + w.set_ht(ht); + w.set_hu(hu); + w.set_mnt(mnt); + w.set_mnu(mnu); + w.set_st(st); + w.set_su(su); + w.set_pm(Ampm::AM); + }); + + rtc.dr().write(|w| { + w.set_dt(dt); + w.set_du(du); + w.set_mt(mt > 0); + w.set_mu(mu); + w.set_yt(yt); + w.set_yu(yu); + w.set_wdu(day_of_week_to_u8(t.day_of_week())); + }); + }); Ok(()) } - #[cfg(feature = "low-power")] + #[cfg(not(rtc_v2f2))] /// Return the current instant. - fn instant(&self) -> RtcInstant { + pub fn instant(&self) -> Result { let r = RTC::regs(); let tr = r.tr().read(); let subsecond = r.ssr().read().ss(); @@ -239,7 +230,7 @@ impl Rtc { // Unlock the registers r.dr().read(); - RtcInstant { second, subsecond } + RtcInstant::from(second, subsecond.try_into().unwrap()) } /// Return the current datetime. diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs index eeb23e1f..b6ab9b20 100644 --- a/embassy-stm32/src/rtc/v2.rs +++ b/embassy-stm32/src/rtc/v2.rs @@ -95,15 +95,16 @@ impl super::Rtc { regs.cr().modify(|w| w.set_wutie(true)); }); + let instant = self.instant().unwrap(); trace!( "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}", Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(), prescaler as u32, rtc_ticks, - self.instant(), + instant, ); - assert!(self.stop_time.borrow(cs).replace(Some(self.instant())).is_none()) + assert!(self.stop_time.borrow(cs).replace(Some(instant)).is_none()) } #[cfg(feature = "low-power")] @@ -112,8 +113,9 @@ impl super::Rtc { pub(crate) fn stop_wakeup_alarm(&self, cs: critical_section::CriticalSection) -> Option { use crate::interrupt::typelevel::Interrupt; + let instant = self.instant().unwrap(); if RTC::regs().cr().read().wute() { - trace!("rtc: stop wakeup alarm at {}", self.instant()); + trace!("rtc: stop wakeup alarm at {}", instant); self.write(false, |regs| { regs.cr().modify(|w| w.set_wutie(false)); @@ -128,10 +130,7 @@ impl super::Rtc { }); } - self.stop_time - .borrow(cs) - .take() - .map(|stop_time| self.instant() - stop_time) + self.stop_time.borrow(cs).take().map(|stop_time| instant - stop_time) } #[cfg(feature = "low-power")]