py32_hal/
time.rs

1//! Time units
2
3// The following code is modified from embassy-stm32
4// https://github.com/embassy-rs/embassy/tree/main/embassy-stm32
5// Special thanks to the Embassy Project and its contributors for their work!
6
7use core::ops::{Div, Mul};
8
9/// Hertz
10#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug)]
11#[cfg_attr(feature = "defmt", derive(defmt::Format))]
12pub struct Hertz(pub u32);
13
14impl Hertz {
15    /// Create a `Hertz` from the given hertz.
16    pub const fn hz(hertz: u32) -> Self {
17        Self(hertz)
18    }
19
20    /// Create a `Hertz` from the given kilohertz.
21    pub const fn khz(kilohertz: u32) -> Self {
22        Self(kilohertz * 1_000)
23    }
24
25    /// Create a `Hertz` from the given megahertz.
26    pub const fn mhz(megahertz: u32) -> Self {
27        Self(megahertz * 1_000_000)
28    }
29}
30
31/// This is a convenience shortcut for [`Hertz::hz`]
32pub const fn hz(hertz: u32) -> Hertz {
33    Hertz::hz(hertz)
34}
35
36/// This is a convenience shortcut for [`Hertz::khz`]
37pub const fn khz(kilohertz: u32) -> Hertz {
38    Hertz::khz(kilohertz)
39}
40
41/// This is a convenience shortcut for [`Hertz::mhz`]
42pub const fn mhz(megahertz: u32) -> Hertz {
43    Hertz::mhz(megahertz)
44}
45
46impl Mul<u32> for Hertz {
47    type Output = Hertz;
48    fn mul(self, rhs: u32) -> Self::Output {
49        Hertz(self.0 * rhs)
50    }
51}
52
53impl Div<u32> for Hertz {
54    type Output = Hertz;
55    fn div(self, rhs: u32) -> Self::Output {
56        Hertz(self.0 / rhs)
57    }
58}
59
60impl Mul<u16> for Hertz {
61    type Output = Hertz;
62    fn mul(self, rhs: u16) -> Self::Output {
63        self * (rhs as u32)
64    }
65}
66
67impl Div<u16> for Hertz {
68    type Output = Hertz;
69    fn div(self, rhs: u16) -> Self::Output {
70        self / (rhs as u32)
71    }
72}
73
74impl Mul<u8> for Hertz {
75    type Output = Hertz;
76    fn mul(self, rhs: u8) -> Self::Output {
77        self * (rhs as u32)
78    }
79}
80
81impl Div<u8> for Hertz {
82    type Output = Hertz;
83    fn div(self, rhs: u8) -> Self::Output {
84        self / (rhs as u32)
85    }
86}
87
88impl Div<Hertz> for Hertz {
89    type Output = u32;
90    fn div(self, rhs: Hertz) -> Self::Output {
91        self.0 / rhs.0
92    }
93}
94
95#[repr(C)]
96#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug, Default)]
97#[cfg_attr(feature = "defmt", derive(defmt::Format))]
98/// A variant on [Hertz] that acts as an `Option<Hertz>` that is smaller and repr C.
99///
100/// An `Option<Hertz>` can be `.into()`'d into this type and back.
101/// The only restriction is that that [Hertz] cannot have the value 0 since that's
102/// seen as the `None` variant.
103pub struct MaybeHertz(u32);
104
105impl MaybeHertz {
106    /// Same as calling the `.into()` function, but without type inference.
107    pub fn to_hertz(self) -> Option<Hertz> {
108        self.into()
109    }
110}
111
112impl From<Option<Hertz>> for MaybeHertz {
113    fn from(value: Option<Hertz>) -> Self {
114        match value {
115            Some(Hertz(0)) => panic!("Hertz cannot be 0"),
116            Some(Hertz(val)) => Self(val),
117            None => Self(0),
118        }
119    }
120}
121
122impl From<MaybeHertz> for Option<Hertz> {
123    fn from(value: MaybeHertz) -> Self {
124        match value {
125            MaybeHertz(0) => None,
126            MaybeHertz(val) => Some(Hertz(val)),
127        }
128    }
129}