diff options
Diffstat (limited to 'embassy-stm32/src/rtc/datetime.rs')
| -rw-r--r-- | embassy-stm32/src/rtc/datetime.rs | 191 |
1 files changed, 127 insertions, 64 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 | } | ||
