Skip to main content

pic32_hal/
time.rs

1//! Types for time units and frequency
2//! TODO: replace with fugit
3// Based on https://github.com/stm32-rs/stm32f4xx-hal/blob/master/src/time.rs
4
5#![allow(clippy::from_over_into)]
6
7/// Bits per second
8#[derive(PartialEq, PartialOrd, Clone, Copy)]
9pub struct Bps(pub u32);
10
11#[derive(PartialEq, PartialOrd, Clone, Copy)]
12pub struct Hertz(pub u32);
13
14#[derive(PartialEq, PartialOrd, Clone, Copy)]
15pub struct KiloHertz(pub u32);
16
17#[derive(PartialEq, PartialOrd, Clone, Copy)]
18pub struct MegaHertz(pub u32);
19
20/// Extension trait that adds convenience methods to the `u32` type
21pub trait U32Ext {
22    /// Wrap in `Bps`
23    fn bps(self) -> Bps;
24
25    /// Wrap in `Hertz`
26    fn hz(self) -> Hertz;
27
28    /// Wrap in `KiloHertz`
29    fn khz(self) -> KiloHertz;
30
31    /// Wrap in `MegaHertz`
32    fn mhz(self) -> MegaHertz;
33
34    /// Wrap in `MilliSeconds`
35    fn ms(self) -> MilliSeconds;
36}
37
38impl U32Ext for u32 {
39    fn bps(self) -> Bps {
40        Bps(self)
41    }
42
43    fn hz(self) -> Hertz {
44        Hertz(self)
45    }
46
47    fn khz(self) -> KiloHertz {
48        KiloHertz(self)
49    }
50
51    fn mhz(self) -> MegaHertz {
52        MegaHertz(self)
53    }
54
55    fn ms(self) -> MilliSeconds {
56        MilliSeconds(self)
57    }
58}
59
60impl Into<Hertz> for KiloHertz {
61    fn into(self) -> Hertz {
62        Hertz(self.0 * 1_000)
63    }
64}
65
66impl Into<Hertz> for MegaHertz {
67    fn into(self) -> Hertz {
68        Hertz(self.0 * 1_000_000)
69    }
70}
71
72impl Into<KiloHertz> for MegaHertz {
73    fn into(self) -> KiloHertz {
74        KiloHertz(self.0 * 1_000)
75    }
76}
77
78/// Time unit
79#[derive(PartialEq, PartialOrd, Clone, Copy)]
80pub struct MilliSeconds(pub u32);