mirror of
https://github.com/embassy-rs/embassy.git
synced 2025-10-02 14:44:32 +00:00
stm32: read microsecond from RTC
This commit is contained in:
parent
27fb1f4dd0
commit
610804f138
@ -21,6 +21,8 @@ pub enum Error {
|
|||||||
InvalidMinute,
|
InvalidMinute,
|
||||||
/// The [DateTime] contains an invalid second value. Must be between `0..=59`.
|
/// The [DateTime] contains an invalid second value. Must be between `0..=59`.
|
||||||
InvalidSecond,
|
InvalidSecond,
|
||||||
|
/// The [DateTime] contains an invalid microsecond value. Must be between `0..=999_999`.
|
||||||
|
InvalidMicrosecond,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Structure containing date and time information
|
/// Structure containing date and time information
|
||||||
@ -39,6 +41,8 @@ pub struct DateTime {
|
|||||||
minute: u8,
|
minute: u8,
|
||||||
/// 0..59
|
/// 0..59
|
||||||
second: u8,
|
second: u8,
|
||||||
|
/// 0..999_999
|
||||||
|
usecond: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DateTime {
|
impl DateTime {
|
||||||
@ -77,6 +81,11 @@ impl DateTime {
|
|||||||
self.second
|
self.second
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the microsecond (0..=999_999)
|
||||||
|
pub const fn microsecond(&self) -> u32 {
|
||||||
|
self.usecond
|
||||||
|
}
|
||||||
|
|
||||||
/// Create a new DateTime with the given information.
|
/// Create a new DateTime with the given information.
|
||||||
pub fn from(
|
pub fn from(
|
||||||
year: u16,
|
year: u16,
|
||||||
@ -86,6 +95,7 @@ impl DateTime {
|
|||||||
hour: u8,
|
hour: u8,
|
||||||
minute: u8,
|
minute: u8,
|
||||||
second: u8,
|
second: u8,
|
||||||
|
usecond: u32,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self, Error> {
|
||||||
if year > 4095 {
|
if year > 4095 {
|
||||||
Err(Error::InvalidYear)
|
Err(Error::InvalidYear)
|
||||||
@ -99,6 +109,8 @@ impl DateTime {
|
|||||||
Err(Error::InvalidMinute)
|
Err(Error::InvalidMinute)
|
||||||
} else if second > 59 {
|
} else if second > 59 {
|
||||||
Err(Error::InvalidSecond)
|
Err(Error::InvalidSecond)
|
||||||
|
} else if usecond > 999_999 {
|
||||||
|
Err(Error::InvalidMicrosecond)
|
||||||
} else {
|
} else {
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
year,
|
year,
|
||||||
@ -108,6 +120,7 @@ impl DateTime {
|
|||||||
hour,
|
hour,
|
||||||
minute,
|
minute,
|
||||||
second,
|
second,
|
||||||
|
usecond,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,6 +137,7 @@ impl From<chrono::NaiveDateTime> for DateTime {
|
|||||||
hour: date_time.hour() as u8,
|
hour: date_time.hour() as u8,
|
||||||
minute: date_time.minute() as u8,
|
minute: date_time.minute() as u8,
|
||||||
second: date_time.second() as u8,
|
second: date_time.second() as u8,
|
||||||
|
usecond: date_time.and_utc().timestamp_subsec_micros(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -133,7 +147,12 @@ impl From<DateTime> for chrono::NaiveDateTime {
|
|||||||
fn from(date_time: DateTime) -> Self {
|
fn from(date_time: DateTime) -> Self {
|
||||||
NaiveDate::from_ymd_opt(date_time.year as i32, date_time.month as u32, date_time.day as u32)
|
NaiveDate::from_ymd_opt(date_time.year as i32, date_time.month as u32, date_time.day as u32)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.and_hms_opt(date_time.hour as u32, date_time.minute as u32, date_time.second as u32)
|
.and_hms_micro_opt(
|
||||||
|
date_time.hour as u32,
|
||||||
|
date_time.minute as u32,
|
||||||
|
date_time.second as u32,
|
||||||
|
date_time.usecond,
|
||||||
|
)
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ impl RtcTimeProvider {
|
|||||||
///
|
///
|
||||||
/// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`].
|
/// Will return an `RtcError::InvalidDateTime` if the stored value in the system is not a valid [`DayOfWeek`].
|
||||||
pub fn now(&self) -> Result<DateTime, RtcError> {
|
pub fn now(&self) -> Result<DateTime, RtcError> {
|
||||||
self.read(|dr, tr, _| {
|
self.read(|dr, tr, _ss| {
|
||||||
let second = bcd2_to_byte((tr.st(), tr.su()));
|
let second = bcd2_to_byte((tr.st(), tr.su()));
|
||||||
let minute = bcd2_to_byte((tr.mnt(), tr.mnu()));
|
let minute = bcd2_to_byte((tr.mnt(), tr.mnu()));
|
||||||
let hour = bcd2_to_byte((tr.ht(), tr.hu()));
|
let hour = bcd2_to_byte((tr.ht(), tr.hu()));
|
||||||
@ -69,7 +69,17 @@ impl RtcTimeProvider {
|
|||||||
let month = bcd2_to_byte((dr.mt() as u8, dr.mu()));
|
let month = bcd2_to_byte((dr.mt() as u8, dr.mu()));
|
||||||
let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 2000_u16;
|
let year = bcd2_to_byte((dr.yt(), dr.yu())) as u16 + 2000_u16;
|
||||||
|
|
||||||
DateTime::from(year, month, day, weekday, hour, minute, second).map_err(RtcError::InvalidDateTime)
|
// Calculate second fraction and multiply to microseconds
|
||||||
|
// Formula from RM0410
|
||||||
|
#[cfg(not(rtc_v2f2))]
|
||||||
|
let us = {
|
||||||
|
let prediv = RTC::regs().prer().read().prediv_s() as f32;
|
||||||
|
(((prediv - _ss as f32) / (prediv + 1.0)) * 1e6).min(999_999.0) as u32
|
||||||
|
};
|
||||||
|
#[cfg(rtc_v2f2)]
|
||||||
|
let us = 0;
|
||||||
|
|
||||||
|
DateTime::from(year, month, day, weekday, hour, minute, second, us).map_err(RtcError::InvalidDateTime)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ async fn main(_spawner: Spawner) {
|
|||||||
|
|
||||||
info!("Hello World!");
|
info!("Hello World!");
|
||||||
|
|
||||||
let now = DateTime::from(2023, 6, 14, DayOfWeek::Friday, 15, 59, 10);
|
let now = DateTime::from(2023, 6, 14, DayOfWeek::Friday, 15, 59, 10, 0);
|
||||||
|
|
||||||
let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
|
let mut rtc = Rtc::new(p.RTC, RtcConfig::default());
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user