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