Skip to main content

ch32_hal/
time.rs

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