1#![allow(non_snake_case)]
31
32use core::ops;
33use cortex_m::peripheral::{DCB, DWT};
34
35use crate::prelude::*;
36use crate::rcc::{self, Rcc};
37
38#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Debug)]
40pub struct Bps(pub u32);
41
42pub use fugit::{
43 Duration, HertzU32, KilohertzU32, MegahertzU32, MicrosDurationU32, MillisDurationU32,
44 RateExtU32,
45};
46
47pub trait U32Ext {
49 fn bps(self) -> Bps;
51}
52
53impl U32Ext for u32 {
54 fn bps(self) -> Bps {
55 Bps(self)
56 }
57}
58
59pub const fn Hz(val: u32) -> HertzU32 {
60 HertzU32::from_raw(val)
61}
62
63pub const fn kHz(val: u32) -> KilohertzU32 {
64 KilohertzU32::from_raw(val)
65}
66
67pub const fn MHz(val: u32) -> MegahertzU32 {
68 MegahertzU32::from_raw(val)
69}
70
71pub const fn ms(val: u32) -> MillisDurationU32 {
72 MillisDurationU32::from_ticks(val)
73}
74
75pub const fn us(val: u32) -> MicrosDurationU32 {
76 MicrosDurationU32::from_ticks(val)
77}
78
79macro_rules! impl_arithmetic {
82 ($wrapper:ty, $wrapped:ty) => {
83 impl ops::Mul<$wrapped> for $wrapper {
84 type Output = Self;
85 fn mul(self, rhs: $wrapped) -> Self {
86 Self(self.0 * rhs)
87 }
88 }
89
90 impl ops::MulAssign<$wrapped> for $wrapper {
91 fn mul_assign(&mut self, rhs: $wrapped) {
92 self.0 *= rhs;
93 }
94 }
95
96 impl ops::Div<$wrapped> for $wrapper {
97 type Output = Self;
98 fn div(self, rhs: $wrapped) -> Self {
99 Self(self.0 / rhs)
100 }
101 }
102
103 impl ops::Div<$wrapper> for $wrapper {
104 type Output = $wrapped;
105 fn div(self, rhs: $wrapper) -> $wrapped {
106 self.0 / rhs.0
107 }
108 }
109
110 impl ops::DivAssign<$wrapped> for $wrapper {
111 fn div_assign(&mut self, rhs: $wrapped) {
112 self.0 /= rhs;
113 }
114 }
115 };
116}
117
118impl_arithmetic!(Bps, u32);
119
120#[derive(Clone, Copy)]
127pub struct MonoTimer {
128 frequency: HertzU32,
129}
130
131impl MonoTimer {
132 pub fn new(mut dwt: DWT, mut dcb: DCB, rcc: &Rcc) -> Self {
134 dcb.enable_trace();
135 dwt.enable_cycle_counter();
136 let frequency = rcc.clocks().hclk();
137 MonoTimer { frequency }
140 }
141
142 pub fn frequency(self) -> HertzU32 {
144 self.frequency
145 }
146
147 pub fn now(self) -> Instant {
149 Instant {
150 now: DWT::cycle_count(),
151 }
152 }
153}
154
155#[derive(Clone, Copy)]
157pub struct Instant {
158 now: u32,
159}
160
161impl Instant {
162 pub fn elapsed(self) -> u32 {
164 DWT::cycle_count().wrapping_sub(self.now)
165 }
166}
167
168#[derive(Copy, Clone)]
172pub struct DwtInstant {
173 tick: u32,
174}
175impl TickInstant for DwtInstant {
176 fn frequency() -> KilohertzU32 {
177 rcc::get_clocks().hclk().convert()
178 }
179
180 #[inline(always)]
181 fn now() -> Self {
182 Self {
183 tick: DWT::cycle_count(),
184 }
185 }
186
187 #[inline(always)]
188 fn tick_since(self, earlier: Self) -> u32 {
189 if self.tick >= earlier.tick {
190 self.tick - earlier.tick
191 } else {
192 self.tick + (u32::MAX - earlier.tick + 1)
193 }
194 }
195}