Skip to main content

hal_mik32/
clock.rs

1use core::ops::{Div, Mul};
2
3/// Hertz
4#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug)]
5pub struct Hertz(pub u32);
6
7impl Hertz {
8    /// Create a `Hertz` from the given hertz.
9    pub const fn hz(hertz: u32) -> Self {
10        Self(hertz)
11    }
12
13    /// Create a `Hertz` from the given kilohertz.
14    pub const fn khz(kilohertz: u32) -> Self {
15        Self(kilohertz * 1_000)
16    }
17
18    /// Create a `Hertz` from the given megahertz.
19    pub const fn mhz(megahertz: u32) -> Self {
20        Self(megahertz * 1_000_000)
21    }
22}
23
24impl From<Hertz> for u32 {
25    fn from(hz: Hertz) -> Self {
26        hz.0
27    }
28}
29
30/// This is a convenience shortcut for [`Hertz::hz`]
31pub const fn hz(hertz: u32) -> Hertz {
32    Hertz::hz(hertz)
33}
34
35/// This is a convenience shortcut for [`Hertz::khz`]
36pub const fn khz(kilohertz: u32) -> Hertz {
37    Hertz::khz(kilohertz)
38}
39
40/// This is a convenience shortcut for [`Hertz::mhz`]
41pub const fn mhz(megahertz: u32) -> Hertz {
42    Hertz::mhz(megahertz)
43}
44
45impl Mul<u32> for Hertz {
46    type Output = Hertz;
47    fn mul(self, rhs: u32) -> Self::Output {
48        Hertz(self.0 * rhs)
49    }
50}
51
52impl Div<u32> for Hertz {
53    type Output = Hertz;
54    fn div(self, rhs: u32) -> Self::Output {
55        Hertz(self.0 / rhs)
56    }
57}
58
59impl Mul<u16> for Hertz {
60    type Output = Hertz;
61    fn mul(self, rhs: u16) -> Self::Output {
62        self * (rhs as u32)
63    }
64}
65
66impl Div<u16> for Hertz {
67    type Output = Hertz;
68    fn div(self, rhs: u16) -> Self::Output {
69        self / (rhs as u32)
70    }
71}
72
73impl Mul<u8> for Hertz {
74    type Output = Hertz;
75    fn mul(self, rhs: u8) -> Self::Output {
76        self * (rhs as u32)
77    }
78}
79
80impl Div<u8> for Hertz {
81    type Output = Hertz;
82    fn div(self, rhs: u8) -> Self::Output {
83        self / (rhs as u32)
84    }
85}
86
87impl Div<Hertz> for Hertz {
88    type Output = u32;
89    fn div(self, rhs: Hertz) -> Self::Output {
90        self.0 / rhs.0
91    }
92}