|
27 | 27 | //! assert_eq!(freq_khz, freq_mhz);
|
28 | 28 | //! ```
|
29 | 29 |
|
| 30 | +use core::ops; |
30 | 31 | use cortex_m::peripheral::DWT;
|
31 | 32 |
|
32 | 33 | use crate::rcc::Clocks;
|
@@ -181,6 +182,50 @@ impl Into<Hertz> for MicroSeconds {
|
181 | 182 | }
|
182 | 183 | }
|
183 | 184 |
|
| 185 | +/// Macro to implement arithmetic operations (e.g. multiplication, division) |
| 186 | +/// for wrapper types. |
| 187 | +macro_rules! impl_arithmetic { |
| 188 | + ($wrapper:ty, $wrapped:ty) => { |
| 189 | + impl ops::Mul<$wrapped> for $wrapper { |
| 190 | + type Output = Self; |
| 191 | + fn mul(self, rhs: $wrapped) -> Self { |
| 192 | + Self(self.0 * rhs) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + impl ops::MulAssign<$wrapped> for $wrapper { |
| 197 | + fn mul_assign(&mut self, rhs: $wrapped) { |
| 198 | + self.0 *= rhs; |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + impl ops::Div<$wrapped> for $wrapper { |
| 203 | + type Output = Self; |
| 204 | + fn div(self, rhs: $wrapped) -> Self { |
| 205 | + Self(self.0 / rhs) |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + impl ops::Div<$wrapper> for $wrapper { |
| 210 | + type Output = $wrapped; |
| 211 | + fn div(self, rhs: $wrapper) -> $wrapped { |
| 212 | + self.0 / rhs.0 |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + impl ops::DivAssign<$wrapped> for $wrapper { |
| 217 | + fn div_assign(&mut self, rhs: $wrapped) { |
| 218 | + self.0 /= rhs; |
| 219 | + } |
| 220 | + } |
| 221 | + } |
| 222 | +} |
| 223 | + |
| 224 | +impl_arithmetic!(Hertz, u32); |
| 225 | +impl_arithmetic!(KiloHertz, u32); |
| 226 | +impl_arithmetic!(MegaHertz, u32); |
| 227 | +impl_arithmetic!(Bps, u32); |
| 228 | + |
184 | 229 | /// A monotonic non-decreasing timer
|
185 | 230 | #[derive(Clone, Copy)]
|
186 | 231 | pub struct MonoTimer {
|
|
0 commit comments