stm32g0xx_hal/
time.rs

1pub use fugit::{
2    ExtU32, HertzU32 as Hertz, HoursDurationU32 as Hour, MicrosDurationU32 as MicroSecond,
3    MinutesDurationU32 as Minute, RateExtU32, SecsDurationU32 as Second,
4};
5
6/// Baudrate
7#[derive(Debug, Eq, PartialEq, PartialOrd, Clone, Copy)]
8pub struct Bps(pub u32);
9
10/// A measurement of a monotonically nondecreasing clock
11pub type Instant = fugit::TimerInstantU32<1_000_000>;
12
13/// WeekDay (1-7)
14#[derive(Clone, Copy, Debug, PartialEq, Eq)]
15pub struct WeekDay(pub u32);
16
17/// Date (1-31)
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct MonthDay(pub u32);
20
21/// Week (1-52)
22#[derive(Clone, Copy, Debug, PartialEq, Eq)]
23pub struct Week(pub u32);
24
25/// Month (1-12)
26#[derive(Clone, Copy, Debug, PartialEq, Eq)]
27pub struct Month(pub u32);
28
29/// Year
30#[derive(Clone, Copy, Debug, PartialEq, Eq)]
31pub struct Year(pub u32);
32
33#[derive(Clone, Copy, Debug, PartialEq, Eq)]
34pub struct Time {
35    pub hours: u32,
36    pub minutes: u32,
37    pub seconds: u32,
38    pub daylight_savings: bool,
39}
40
41impl Time {
42    pub fn new(hours: Hour, minutes: Minute, seconds: Second, daylight_savings: bool) -> Self {
43        Self {
44            hours: hours.ticks(),
45            minutes: minutes.ticks(),
46            seconds: seconds.ticks(),
47            daylight_savings,
48        }
49    }
50}
51
52#[derive(Clone, Copy, Debug, PartialEq, Eq)]
53pub struct Date {
54    pub day: u32,
55    pub month: u32,
56    pub year: u32,
57}
58
59impl Date {
60    pub fn new(year: Year, month: Month, day: MonthDay) -> Self {
61        Self {
62            day: day.0,
63            month: month.0,
64            year: year.0,
65        }
66    }
67}
68
69pub trait U32Ext {
70    /// Wrap in `Bps`
71    fn bps(self) -> Bps;
72
73    /// Day in month
74    fn day(self) -> MonthDay;
75
76    /// Month
77    fn month(self) -> Month;
78
79    /// Year
80    fn year(self) -> Year;
81}
82
83impl U32Ext for u32 {
84    fn bps(self) -> Bps {
85        assert!(self > 0);
86        Bps(self)
87    }
88    fn day(self) -> MonthDay {
89        MonthDay(self)
90    }
91
92    fn month(self) -> Month {
93        Month(self)
94    }
95
96    fn year(self) -> Year {
97        Year(self)
98    }
99}
100
101pub fn duration(hz: Hertz, cycles: u32) -> MicroSecond {
102    let cycles = cycles as u64;
103    let clk = hz.raw() as u64;
104    let us = cycles.saturating_mul(1_000_000_u64) / clk;
105    MicroSecond::from_ticks(us as u32)
106}
107
108pub fn cycles(ms: MicroSecond, clk: Hertz) -> u32 {
109    assert!(ms.ticks() > 0);
110    let clk = clk.raw() as u64;
111    let period = ms.ticks() as u64;
112    let cycles = clk.saturating_mul(period) / 1_000_000_u64;
113    cycles as u32
114}