aboutsummaryrefslogtreecommitdiff
path: root/embassy-stm32/src/time.rs
diff options
context:
space:
mode:
authorDion Dokter <[email protected]>2024-07-08 16:54:06 +0200
committerDion Dokter <[email protected]>2024-07-08 16:54:06 +0200
commit203297b56912c05d2dd6a009ffeb433fb2ffbea6 (patch)
treefa1708215925ad861d68dc8454069c11ae5c861c /embassy-stm32/src/time.rs
parentb1ea90a87e5ce6b16bbc155ad30d6d3473a998bb (diff)
Make clocks repr C.
Add shared data. Modify freq functions to use shared data. Modify examples to use new init/
Diffstat (limited to 'embassy-stm32/src/time.rs')
-rw-r--r--embassy-stm32/src/time.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/embassy-stm32/src/time.rs b/embassy-stm32/src/time.rs
index 17690aefc..d7337191d 100644
--- a/embassy-stm32/src/time.rs
+++ b/embassy-stm32/src/time.rs
@@ -87,3 +87,39 @@ impl Div<Hertz> for Hertz {
87 self.0 / rhs.0 87 self.0 / rhs.0
88 } 88 }
89} 89}
90
91#[repr(C)]
92#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Debug, Default)]
93#[cfg_attr(feature = "defmt", derive(defmt::Format))]
94/// A variant on [Hertz] that acts as an `Option<Hertz>` that is smaller and repr C.
95///
96/// An `Option<Hertz>` can be `.into()`'d into this type and back.
97/// The only restriction is that that [Hertz] cannot have the value 0 since that's
98/// seen as the `None` variant.
99pub struct MaybeHertz(u32);
100
101impl MaybeHertz {
102 /// Same as calling the `.into()` function, but without type inference.
103 pub fn to_hertz(self) -> Option<Hertz> {
104 self.into()
105 }
106}
107
108impl From<Option<Hertz>> for MaybeHertz {
109 fn from(value: Option<Hertz>) -> Self {
110 match value {
111 Some(Hertz(0)) => panic!("Hertz cannot be 0"),
112 Some(Hertz(val)) => Self(val),
113 None => Self(0),
114 }
115 }
116}
117
118impl From<MaybeHertz> for Option<Hertz> {
119 fn from(value: MaybeHertz) -> Self {
120 match value {
121 MaybeHertz(0) => None,
122 MaybeHertz(val) => Some(Hertz(val)),
123 }
124 }
125}