1#![allow(non_snake_case)]
31
32use core::ops;
33use cortex_m::peripheral::{DCB, DWT};
34
35use crate::rcc::Clocks;
36
37#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Debug)]
39pub struct Bps(pub u32);
40
41pub use fugit::{
42 HertzU32 as Hertz, KilohertzU32 as KiloHertz, MegahertzU32 as MegaHertz,
43 MicrosDurationU32 as MicroSeconds, MillisDurationU32 as MilliSeconds,
44};
45
46pub trait U32Ext {
48 fn bps(self) -> Bps;
50}
51
52impl U32Ext for u32 {
53 fn bps(self) -> Bps {
54 Bps(self)
55 }
56}
57
58pub const fn Hz(val: u32) -> Hertz {
59 Hertz::from_raw(val)
60}
61
62pub const fn kHz(val: u32) -> KiloHertz {
63 KiloHertz::from_raw(val)
64}
65
66pub const fn MHz(val: u32) -> MegaHertz {
67 MegaHertz::from_raw(val)
68}
69
70pub const fn ms(val: u32) -> MilliSeconds {
71 MilliSeconds::from_ticks(val)
72}
73
74pub const fn us(val: u32) -> MicroSeconds {
75 MicroSeconds::from_ticks(val)
76}
77
78macro_rules! impl_arithmetic {
81 ($wrapper:ty, $wrapped:ty) => {
82 impl ops::Mul<$wrapped> for $wrapper {
83 type Output = Self;
84 fn mul(self, rhs: $wrapped) -> Self {
85 Self(self.0 * rhs)
86 }
87 }
88
89 impl ops::MulAssign<$wrapped> for $wrapper {
90 fn mul_assign(&mut self, rhs: $wrapped) {
91 self.0 *= rhs;
92 }
93 }
94
95 impl ops::Div<$wrapped> for $wrapper {
96 type Output = Self;
97 fn div(self, rhs: $wrapped) -> Self {
98 Self(self.0 / rhs)
99 }
100 }
101
102 impl ops::Div<$wrapper> for $wrapper {
103 type Output = $wrapped;
104 fn div(self, rhs: $wrapper) -> $wrapped {
105 self.0 / rhs.0
106 }
107 }
108
109 impl ops::DivAssign<$wrapped> for $wrapper {
110 fn div_assign(&mut self, rhs: $wrapped) {
111 self.0 /= rhs;
112 }
113 }
114 };
115}
116
117impl_arithmetic!(Bps, u32);
118
119#[derive(Clone, Copy)]
126pub struct MonoTimer {
127 frequency: Hertz,
128}
129
130impl MonoTimer {
131 pub fn new(mut dwt: DWT, mut dcb: DCB, clocks: &Clocks) -> Self {
133 dcb.enable_trace();
134 dwt.enable_cycle_counter();
135
136 MonoTimer {
139 frequency: clocks.hclk(),
140 }
141 }
142
143 pub fn frequency(self) -> Hertz {
145 self.frequency
146 }
147
148 pub fn now(self) -> Instant {
150 Instant {
151 now: DWT::cycle_count(),
152 }
153 }
154}
155
156#[derive(Clone, Copy)]
158pub struct Instant {
159 now: u32,
160}
161
162impl Instant {
163 pub fn elapsed(self) -> u32 {
165 DWT::cycle_count().wrapping_sub(self.now)
166 }
167}