diff options
| author | xoviat <[email protected]> | 2023-10-25 19:50:30 -0500 |
|---|---|---|
| committer | xoviat <[email protected]> | 2023-10-25 19:50:30 -0500 |
| commit | 0beb84768e4ce42c1dedbcbb7b76d5309fce1171 (patch) | |
| tree | 8bb68d4888032682affb5ec0f2d0c257f7b7615d | |
| parent | 0cc3e18db65dd2d6ae173e94014973741f14e2ee (diff) | |
stm32/rtc: more rtc cleanup
| -rw-r--r-- | embassy-stm32/src/rtc/datetime.rs | 191 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/mod.rs | 89 | ||||
| -rw-r--r-- | embassy-stm32/src/rtc/v2.rs | 13 |
3 files changed, 173 insertions, 120 deletions
diff --git a/embassy-stm32/src/rtc/datetime.rs b/embassy-stm32/src/rtc/datetime.rs index 3efe9be5d..a1943cf3a 100644 --- a/embassy-stm32/src/rtc/datetime.rs +++ b/embassy-stm32/src/rtc/datetime.rs | |||
| @@ -4,8 +4,60 @@ use core::convert::From; | |||
| 4 | #[cfg(feature = "chrono")] | 4 | #[cfg(feature = "chrono")] |
| 5 | use chrono::{self, Datelike, NaiveDate, Timelike, Weekday}; | 5 | use chrono::{self, Datelike, NaiveDate, Timelike, Weekday}; |
| 6 | 6 | ||
| 7 | use super::byte_to_bcd2; | 7 | #[cfg(any(feature = "defmt", feature = "time"))] |
| 8 | use crate::pac::rtc::Rtc; | 8 | use crate::peripherals::RTC; |
| 9 | #[cfg(any(feature = "defmt", feature = "time"))] | ||
| 10 | use crate::rtc::sealed::Instance; | ||
| 11 | |||
| 12 | /// Represents an instant in time that can be substracted to compute a duration | ||
| 13 | pub struct RtcInstant { | ||
| 14 | /// 0..59 | ||
| 15 | pub second: u8, | ||
| 16 | /// 0..256 | ||
| 17 | pub subsecond: u16, | ||
| 18 | } | ||
| 19 | |||
| 20 | impl RtcInstant { | ||
| 21 | #[allow(dead_code)] | ||
| 22 | pub(super) fn from(second: u8, subsecond: u16) -> Result<Self, super::RtcError> { | ||
| 23 | Ok(Self { second, subsecond }) | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | #[cfg(feature = "defmt")] | ||
| 28 | impl defmt::Format for RtcInstant { | ||
| 29 | fn format(&self, fmt: defmt::Formatter) { | ||
| 30 | defmt::write!( | ||
| 31 | fmt, | ||
| 32 | "{}:{}", | ||
| 33 | self.second, | ||
| 34 | RTC::regs().prer().read().prediv_s() - self.subsecond, | ||
| 35 | ) | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | #[cfg(feature = "time")] | ||
| 40 | impl core::ops::Sub for RtcInstant { | ||
| 41 | type Output = embassy_time::Duration; | ||
| 42 | |||
| 43 | fn sub(self, rhs: Self) -> Self::Output { | ||
| 44 | use embassy_time::{Duration, TICK_HZ}; | ||
| 45 | |||
| 46 | let second = if self.second < rhs.second { | ||
| 47 | self.second + 60 | ||
| 48 | } else { | ||
| 49 | self.second | ||
| 50 | }; | ||
| 51 | |||
| 52 | let psc = RTC::regs().prer().read().prediv_s() as u32; | ||
| 53 | |||
| 54 | let self_ticks = second as u32 * (psc + 1) + (psc - self.subsecond as u32); | ||
| 55 | let other_ticks = rhs.second as u32 * (psc + 1) + (psc - rhs.subsecond as u32); | ||
| 56 | let rtc_ticks = self_ticks - other_ticks; | ||
| 57 | |||
| 58 | Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / (psc + 1)) as u64) | ||
| 59 | } | ||
| 60 | } | ||
| 9 | 61 | ||
| 10 | /// Errors regarding the [`DateTime`] struct. | 62 | /// Errors regarding the [`DateTime`] struct. |
| 11 | #[derive(Clone, Debug, PartialEq, Eq)] | 63 | #[derive(Clone, Debug, PartialEq, Eq)] |
| @@ -32,19 +84,85 @@ pub enum Error { | |||
| 32 | /// Structure containing date and time information | 84 | /// Structure containing date and time information |
| 33 | pub struct DateTime { | 85 | pub struct DateTime { |
| 34 | /// 0..4095 | 86 | /// 0..4095 |
| 35 | pub year: u16, | 87 | year: u16, |
| 36 | /// 1..12, 1 is January | 88 | /// 1..12, 1 is January |
| 37 | pub month: u8, | 89 | month: u8, |
| 38 | /// 1..28,29,30,31 depending on month | 90 | /// 1..28,29,30,31 depending on month |
| 39 | pub day: u8, | 91 | day: u8, |
| 40 | /// | 92 | /// |
| 41 | pub day_of_week: DayOfWeek, | 93 | day_of_week: DayOfWeek, |
| 42 | /// 0..23 | 94 | /// 0..23 |
| 43 | pub hour: u8, | 95 | hour: u8, |
| 44 | /// 0..59 | 96 | /// 0..59 |
| 45 | pub minute: u8, | 97 | minute: u8, |
| 46 | /// 0..59 | 98 | /// 0..59 |
| 47 | pub second: u8, | 99 | second: u8, |
| 100 | } | ||
| 101 | |||
| 102 | impl DateTime { | ||
| 103 | pub const fn year(&self) -> u16 { | ||
| 104 | self.year | ||
| 105 | } | ||
| 106 | |||
| 107 | pub const fn month(&self) -> u8 { | ||
| 108 | self.month | ||
| 109 | } | ||
| 110 | |||
| 111 | pub const fn day(&self) -> u8 { | ||
| 112 | self.day | ||
| 113 | } | ||
| 114 | |||
| 115 | pub const fn day_of_week(&self) -> DayOfWeek { | ||
| 116 | self.day_of_week | ||
| 117 | } | ||
| 118 | |||
| 119 | pub const fn hour(&self) -> u8 { | ||
| 120 | self.hour | ||
| 121 | } | ||
| 122 | |||
| 123 | pub const fn minute(&self) -> u8 { | ||
| 124 | self.minute | ||
| 125 | } | ||
| 126 | |||
| 127 | pub const fn second(&self) -> u8 { | ||
| 128 | self.second | ||
| 129 | } | ||
| 130 | |||
| 131 | pub fn from( | ||
| 132 | year: u16, | ||
| 133 | month: u8, | ||
| 134 | day: u8, | ||
| 135 | day_of_week: u8, | ||
| 136 | hour: u8, | ||
| 137 | minute: u8, | ||
| 138 | second: u8, | ||
| 139 | ) -> Result<Self, Error> { | ||
| 140 | let day_of_week = day_of_week_from_u8(day_of_week)?; | ||
| 141 | |||
| 142 | if year > 4095 { | ||
| 143 | Err(Error::InvalidYear) | ||
| 144 | } else if month < 1 || month > 12 { | ||
| 145 | Err(Error::InvalidMonth) | ||
| 146 | } else if day < 1 || day > 31 { | ||
| 147 | Err(Error::InvalidDay) | ||
| 148 | } else if hour > 23 { | ||
| 149 | Err(Error::InvalidHour) | ||
| 150 | } else if minute > 59 { | ||
| 151 | Err(Error::InvalidMinute) | ||
| 152 | } else if second > 59 { | ||
| 153 | Err(Error::InvalidSecond) | ||
| 154 | } else { | ||
| 155 | Ok(Self { | ||
| 156 | year, | ||
| 157 | month, | ||
| 158 | day, | ||
| 159 | day_of_week, | ||
| 160 | hour, | ||
| 161 | minute, | ||
| 162 | second, | ||
| 163 | }) | ||
| 164 | } | ||
| 165 | } | ||
| 48 | } | 166 | } |
| 49 | 167 | ||
| 50 | #[cfg(feature = "chrono")] | 168 | #[cfg(feature = "chrono")] |
| @@ -142,58 +260,3 @@ pub(super) fn validate_datetime(dt: &DateTime) -> Result<(), Error> { | |||
| 142 | Ok(()) | 260 | Ok(()) |
| 143 | } | 261 | } |
| 144 | } | 262 | } |
| 145 | |||
| 146 | pub(super) fn write_date_time(rtc: &Rtc, t: DateTime) { | ||
| 147 | let (ht, hu) = byte_to_bcd2(t.hour as u8); | ||
| 148 | let (mnt, mnu) = byte_to_bcd2(t.minute as u8); | ||
| 149 | let (st, su) = byte_to_bcd2(t.second as u8); | ||
| 150 | |||
| 151 | let (dt, du) = byte_to_bcd2(t.day as u8); | ||
| 152 | let (mt, mu) = byte_to_bcd2(t.month as u8); | ||
| 153 | let yr = t.year as u16; | ||
| 154 | let yr_offset = (yr - 1970_u16) as u8; | ||
| 155 | let (yt, yu) = byte_to_bcd2(yr_offset); | ||
| 156 | |||
| 157 | use crate::pac::rtc::vals::Ampm; | ||
| 158 | |||
| 159 | rtc.tr().write(|w| { | ||
| 160 | w.set_ht(ht); | ||
| 161 | w.set_hu(hu); | ||
| 162 | w.set_mnt(mnt); | ||
| 163 | w.set_mnu(mnu); | ||
| 164 | w.set_st(st); | ||
| 165 | w.set_su(su); | ||
| 166 | w.set_pm(Ampm::AM); | ||
| 167 | }); | ||
| 168 | |||
| 169 | rtc.dr().write(|w| { | ||
| 170 | w.set_dt(dt); | ||
| 171 | w.set_du(du); | ||
| 172 | w.set_mt(mt > 0); | ||
| 173 | w.set_mu(mu); | ||
| 174 | w.set_yt(yt); | ||
| 175 | w.set_yu(yu); | ||
| 176 | w.set_wdu(day_of_week_to_u8(t.day_of_week)); | ||
| 177 | }); | ||
| 178 | } | ||
| 179 | |||
| 180 | pub(super) fn datetime( | ||
| 181 | year: u16, | ||
| 182 | month: u8, | ||
| 183 | day: u8, | ||
| 184 | day_of_week: u8, | ||
| 185 | hour: u8, | ||
| 186 | minute: u8, | ||
| 187 | second: u8, | ||
| 188 | ) -> Result<DateTime, Error> { | ||
| 189 | let day_of_week = day_of_week_from_u8(day_of_week)?; | ||
| 190 | Ok(DateTime { | ||
| 191 | year, | ||
| 192 | month, | ||
| 193 | day, | ||
| 194 | day_of_week, | ||
| 195 | hour, | ||
| 196 | minute, | ||
| 197 | second, | ||
| 198 | }) | ||
| 199 | } | ||
diff --git a/embassy-stm32/src/rtc/mod.rs b/embassy-stm32/src/rtc/mod.rs index d77443a9c..da79c3926 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; | |||
| 9 | #[cfg(feature = "low-power")] | 9 | #[cfg(feature = "low-power")] |
| 10 | use embassy_sync::blocking_mutex::Mutex; | 10 | use embassy_sync::blocking_mutex::Mutex; |
| 11 | 11 | ||
| 12 | pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError}; | 12 | pub use self::datetime::{DateTime, DayOfWeek, Error as DateTimeError, RtcInstant}; |
| 13 | use crate::rtc::datetime::day_of_week_to_u8; | ||
| 13 | use crate::time::Hertz; | 14 | use crate::time::Hertz; |
| 14 | 15 | ||
| 15 | /// refer to AN4759 to compare features of RTC2 and RTC3 | 16 | /// refer to AN4759 to compare features of RTC2 and RTC3 |
| @@ -39,48 +40,6 @@ pub enum RtcError { | |||
| 39 | NotRunning, | 40 | NotRunning, |
| 40 | } | 41 | } |
| 41 | 42 | ||
| 42 | #[cfg(feature = "low-power")] | ||
| 43 | /// Represents an instant in time that can be substracted to compute a duration | ||
| 44 | struct RtcInstant { | ||
| 45 | second: u8, | ||
| 46 | subsecond: u16, | ||
| 47 | } | ||
| 48 | |||
| 49 | #[cfg(all(feature = "low-power", feature = "defmt"))] | ||
| 50 | impl defmt::Format for RtcInstant { | ||
| 51 | fn format(&self, fmt: defmt::Formatter) { | ||
| 52 | defmt::write!( | ||
| 53 | fmt, | ||
| 54 | "{}:{}", | ||
| 55 | self.second, | ||
| 56 | RTC::regs().prer().read().prediv_s() - self.subsecond, | ||
| 57 | ) | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | #[cfg(feature = "low-power")] | ||
| 62 | impl core::ops::Sub for RtcInstant { | ||
| 63 | type Output = embassy_time::Duration; | ||
| 64 | |||
| 65 | fn sub(self, rhs: Self) -> Self::Output { | ||
| 66 | use embassy_time::{Duration, TICK_HZ}; | ||
| 67 | |||
| 68 | let second = if self.second < rhs.second { | ||
| 69 | self.second + 60 | ||
| 70 | } else { | ||
| 71 | self.second | ||
| 72 | }; | ||
| 73 | |||
| 74 | let psc = RTC::regs().prer().read().prediv_s() as u32; | ||
| 75 | |||
| 76 | let self_ticks = second as u32 * (psc + 1) + (psc - self.subsecond as u32); | ||
| 77 | let other_ticks = rhs.second as u32 * (psc + 1) + (psc - rhs.subsecond as u32); | ||
| 78 | let rtc_ticks = self_ticks - other_ticks; | ||
| 79 | |||
| 80 | Duration::from_ticks(((rtc_ticks * TICK_HZ as u32) / (psc + 1)) as u64) | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | pub struct RtcTimeProvider { | 43 | pub struct RtcTimeProvider { |
| 85 | _private: (), | 44 | _private: (), |
| 86 | } | 45 | } |
| @@ -113,7 +72,7 @@ impl RtcTimeProvider { | |||
| 113 | let month = bcd2_to_byte((dr.mt() as u8, dr.mu())); | 72 | let month = bcd2_to_byte((dr.mt() as u8, dr.mu())); |
| 114 | let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16; | 73 | let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16; |
| 115 | 74 | ||
| 116 | return self::datetime::datetime(year, month, day, weekday, hour, minute, second) | 75 | return DateTime::from(year, month, day, weekday, hour, minute, second) |
| 117 | .map_err(RtcError::InvalidDateTime); | 76 | .map_err(RtcError::InvalidDateTime); |
| 118 | } | 77 | } |
| 119 | } | 78 | } |
| @@ -134,7 +93,7 @@ impl RtcTimeProvider { | |||
| 134 | let month = bcd2_to_byte((dr.mt() as u8, dr.mu())); | 93 | let month = bcd2_to_byte((dr.mt() as u8, dr.mu())); |
| 135 | let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16; | 94 | let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 1970_u16; |
| 136 | 95 | ||
| 137 | self::datetime::datetime(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime) | 96 | DateTime::from(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime) |
| 138 | } | 97 | } |
| 139 | } | 98 | } |
| 140 | } | 99 | } |
| @@ -223,14 +182,46 @@ impl Rtc { | |||
| 223 | /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range. | 182 | /// Will return `RtcError::InvalidDateTime` if the datetime is not a valid range. |
| 224 | pub fn set_datetime(&mut self, t: DateTime) -> Result<(), RtcError> { | 183 | pub fn set_datetime(&mut self, t: DateTime) -> Result<(), RtcError> { |
| 225 | self::datetime::validate_datetime(&t).map_err(RtcError::InvalidDateTime)?; | 184 | self::datetime::validate_datetime(&t).map_err(RtcError::InvalidDateTime)?; |
| 226 | self.write(true, |rtc| self::datetime::write_date_time(rtc, t)); | 185 | self.write(true, |rtc| { |
| 186 | let (ht, hu) = byte_to_bcd2(t.hour() as u8); | ||
| 187 | let (mnt, mnu) = byte_to_bcd2(t.minute() as u8); | ||
| 188 | let (st, su) = byte_to_bcd2(t.second() as u8); | ||
| 189 | |||
| 190 | let (dt, du) = byte_to_bcd2(t.day() as u8); | ||
| 191 | let (mt, mu) = byte_to_bcd2(t.month() as u8); | ||
| 192 | let yr = t.year() as u16; | ||
| 193 | let yr_offset = (yr - 1970_u16) as u8; | ||
| 194 | let (yt, yu) = byte_to_bcd2(yr_offset); | ||
| 195 | |||
| 196 | use crate::pac::rtc::vals::Ampm; | ||
| 197 | |||
| 198 | rtc.tr().write(|w| { | ||
| 199 | w.set_ht(ht); | ||
| 200 | w.set_hu(hu); | ||
| 201 | w.set_mnt(mnt); | ||
| 202 | w.set_mnu(mnu); | ||
| 203 | w.set_st(st); | ||
| 204 | w.set_su(su); | ||
| 205 | w.set_pm(Ampm::AM); | ||
| 206 | }); | ||
| 207 | |||
| 208 | rtc.dr().write(|w| { | ||
| 209 | w.set_dt(dt); | ||
| 210 | w.set_du(du); | ||
| 211 | w.set_mt(mt > 0); | ||
| 212 | w.set_mu(mu); | ||
| 213 | w.set_yt(yt); | ||
| 214 | w.set_yu(yu); | ||
| 215 | w.set_wdu(day_of_week_to_u8(t.day_of_week())); | ||
| 216 | }); | ||
| 217 | }); | ||
| 227 | 218 | ||
| 228 | Ok(()) | 219 | Ok(()) |
| 229 | } | 220 | } |
| 230 | 221 | ||
| 231 | #[cfg(feature = "low-power")] | 222 | #[cfg(not(rtc_v2f2))] |
| 232 | /// Return the current instant. | 223 | /// Return the current instant. |
| 233 | fn instant(&self) -> RtcInstant { | 224 | pub fn instant(&self) -> Result<RtcInstant, RtcError> { |
| 234 | let r = RTC::regs(); | 225 | let r = RTC::regs(); |
| 235 | let tr = r.tr().read(); | 226 | let tr = r.tr().read(); |
| 236 | let subsecond = r.ssr().read().ss(); | 227 | let subsecond = r.ssr().read().ss(); |
| @@ -239,7 +230,7 @@ impl Rtc { | |||
| 239 | // Unlock the registers | 230 | // Unlock the registers |
| 240 | r.dr().read(); | 231 | r.dr().read(); |
| 241 | 232 | ||
| 242 | RtcInstant { second, subsecond } | 233 | RtcInstant::from(second, subsecond.try_into().unwrap()) |
| 243 | } | 234 | } |
| 244 | 235 | ||
| 245 | /// Return the current datetime. | 236 | /// Return the current datetime. |
diff --git a/embassy-stm32/src/rtc/v2.rs b/embassy-stm32/src/rtc/v2.rs index eeb23e1f1..b6ab9b209 100644 --- a/embassy-stm32/src/rtc/v2.rs +++ b/embassy-stm32/src/rtc/v2.rs | |||
| @@ -95,15 +95,16 @@ impl super::Rtc { | |||
| 95 | regs.cr().modify(|w| w.set_wutie(true)); | 95 | regs.cr().modify(|w| w.set_wutie(true)); |
| 96 | }); | 96 | }); |
| 97 | 97 | ||
| 98 | let instant = self.instant().unwrap(); | ||
| 98 | trace!( | 99 | trace!( |
| 99 | "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}", | 100 | "rtc: start wakeup alarm for {} ms (psc: {}, ticks: {}) at {}", |
| 100 | Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(), | 101 | Duration::from_ticks(rtc_ticks as u64 * TICK_HZ * prescaler as u64 / rtc_hz).as_millis(), |
| 101 | prescaler as u32, | 102 | prescaler as u32, |
| 102 | rtc_ticks, | 103 | rtc_ticks, |
| 103 | self.instant(), | 104 | instant, |
| 104 | ); | 105 | ); |
| 105 | 106 | ||
| 106 | assert!(self.stop_time.borrow(cs).replace(Some(self.instant())).is_none()) | 107 | assert!(self.stop_time.borrow(cs).replace(Some(instant)).is_none()) |
| 107 | } | 108 | } |
| 108 | 109 | ||
| 109 | #[cfg(feature = "low-power")] | 110 | #[cfg(feature = "low-power")] |
| @@ -112,8 +113,9 @@ impl super::Rtc { | |||
| 112 | pub(crate) fn stop_wakeup_alarm(&self, cs: critical_section::CriticalSection) -> Option<embassy_time::Duration> { | 113 | pub(crate) fn stop_wakeup_alarm(&self, cs: critical_section::CriticalSection) -> Option<embassy_time::Duration> { |
| 113 | use crate::interrupt::typelevel::Interrupt; | 114 | use crate::interrupt::typelevel::Interrupt; |
| 114 | 115 | ||
| 116 | let instant = self.instant().unwrap(); | ||
| 115 | if RTC::regs().cr().read().wute() { | 117 | if RTC::regs().cr().read().wute() { |
| 116 | trace!("rtc: stop wakeup alarm at {}", self.instant()); | 118 | trace!("rtc: stop wakeup alarm at {}", instant); |
| 117 | 119 | ||
| 118 | self.write(false, |regs| { | 120 | self.write(false, |regs| { |
| 119 | regs.cr().modify(|w| w.set_wutie(false)); | 121 | regs.cr().modify(|w| w.set_wutie(false)); |
| @@ -128,10 +130,7 @@ impl super::Rtc { | |||
| 128 | }); | 130 | }); |
| 129 | } | 131 | } |
| 130 | 132 | ||
| 131 | self.stop_time | 133 | self.stop_time.borrow(cs).take().map(|stop_time| instant - stop_time) |
| 132 | .borrow(cs) | ||
| 133 | .take() | ||
| 134 | .map(|stop_time| self.instant() - stop_time) | ||
| 135 | } | 134 | } |
| 136 | 135 | ||
| 137 | #[cfg(feature = "low-power")] | 136 | #[cfg(feature = "low-power")] |
