diff options
Diffstat (limited to 'embassy-time/src')
| -rw-r--r-- | embassy-time/src/fmt.rs | 45 | ||||
| -rw-r--r-- | embassy-time/src/instant.rs | 2 | ||||
| -rw-r--r-- | embassy-time/src/tick.rs | 186 | ||||
| -rw-r--r-- | embassy-time/src/timer.rs | 8 |
4 files changed, 233 insertions, 8 deletions
diff --git a/embassy-time/src/fmt.rs b/embassy-time/src/fmt.rs index 066970813..78e583c1c 100644 --- a/embassy-time/src/fmt.rs +++ b/embassy-time/src/fmt.rs | |||
| @@ -1,6 +1,8 @@ | |||
| 1 | #![macro_use] | 1 | #![macro_use] |
| 2 | #![allow(unused_macros)] | 2 | #![allow(unused_macros)] |
| 3 | 3 | ||
| 4 | use core::fmt::{Debug, Display, LowerHex}; | ||
| 5 | |||
| 4 | #[cfg(all(feature = "defmt", feature = "log"))] | 6 | #[cfg(all(feature = "defmt", feature = "log"))] |
| 5 | compile_error!("You may not enable both `defmt` and `log` features."); | 7 | compile_error!("You may not enable both `defmt` and `log` features."); |
| 6 | 8 | ||
| @@ -81,14 +83,17 @@ macro_rules! todo { | |||
| 81 | }; | 83 | }; |
| 82 | } | 84 | } |
| 83 | 85 | ||
| 86 | #[cfg(not(feature = "defmt"))] | ||
| 84 | macro_rules! unreachable { | 87 | macro_rules! unreachable { |
| 85 | ($($x:tt)*) => { | 88 | ($($x:tt)*) => { |
| 86 | { | 89 | ::core::unreachable!($($x)*) |
| 87 | #[cfg(not(feature = "defmt"))] | 90 | }; |
| 88 | ::core::unreachable!($($x)*); | 91 | } |
| 89 | #[cfg(feature = "defmt")] | 92 | |
| 90 | ::defmt::unreachable!($($x)*); | 93 | #[cfg(feature = "defmt")] |
| 91 | } | 94 | macro_rules! unreachable { |
| 95 | ($($x:tt)*) => { | ||
| 96 | ::defmt::unreachable!($($x)*) | ||
| 92 | }; | 97 | }; |
| 93 | } | 98 | } |
| 94 | 99 | ||
| @@ -223,3 +228,31 @@ impl<T, E> Try for Result<T, E> { | |||
| 223 | self | 228 | self |
| 224 | } | 229 | } |
| 225 | } | 230 | } |
| 231 | |||
| 232 | #[allow(unused)] | ||
| 233 | pub(crate) struct Bytes<'a>(pub &'a [u8]); | ||
| 234 | |||
| 235 | impl<'a> Debug for Bytes<'a> { | ||
| 236 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| 237 | write!(f, "{:#02x?}", self.0) | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | impl<'a> Display for Bytes<'a> { | ||
| 242 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| 243 | write!(f, "{:#02x?}", self.0) | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | impl<'a> LowerHex for Bytes<'a> { | ||
| 248 | fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
| 249 | write!(f, "{:#02x?}", self.0) | ||
| 250 | } | ||
| 251 | } | ||
| 252 | |||
| 253 | #[cfg(feature = "defmt")] | ||
| 254 | impl<'a> defmt::Format for Bytes<'a> { | ||
| 255 | fn format(&self, fmt: defmt::Formatter) { | ||
| 256 | defmt::write!(fmt, "{:02x}", self.0) | ||
| 257 | } | ||
| 258 | } | ||
diff --git a/embassy-time/src/instant.rs b/embassy-time/src/instant.rs index 44f226f62..5571cdd15 100644 --- a/embassy-time/src/instant.rs +++ b/embassy-time/src/instant.rs | |||
| @@ -71,7 +71,7 @@ impl Instant { | |||
| 71 | /// Panics on over/underflow. | 71 | /// Panics on over/underflow. |
| 72 | pub fn duration_since(&self, earlier: Instant) -> Duration { | 72 | pub fn duration_since(&self, earlier: Instant) -> Duration { |
| 73 | Duration { | 73 | Duration { |
| 74 | ticks: self.ticks.checked_sub(earlier.ticks).unwrap(), | 74 | ticks: unwrap!(self.ticks.checked_sub(earlier.ticks)), |
| 75 | } | 75 | } |
| 76 | } | 76 | } |
| 77 | 77 | ||
diff --git a/embassy-time/src/tick.rs b/embassy-time/src/tick.rs index 608bc44f1..be544181a 100644 --- a/embassy-time/src/tick.rs +++ b/embassy-time/src/tick.rs | |||
| @@ -156,6 +156,130 @@ pub const TICK_HZ: u64 = 512_000_000; | |||
| 156 | pub const TICK_HZ: u64 = 576_000_000; | 156 | pub const TICK_HZ: u64 = 576_000_000; |
| 157 | #[cfg(feature = "tick-hz-768_000_000")] | 157 | #[cfg(feature = "tick-hz-768_000_000")] |
| 158 | pub const TICK_HZ: u64 = 768_000_000; | 158 | pub const TICK_HZ: u64 = 768_000_000; |
| 159 | #[cfg(feature = "tick-hz-20_000_000")] | ||
| 160 | pub const TICK_HZ: u64 = 20_000_000; | ||
| 161 | #[cfg(feature = "tick-hz-30_000_000")] | ||
| 162 | pub const TICK_HZ: u64 = 30_000_000; | ||
| 163 | #[cfg(feature = "tick-hz-40_000_000")] | ||
| 164 | pub const TICK_HZ: u64 = 40_000_000; | ||
| 165 | #[cfg(feature = "tick-hz-50_000_000")] | ||
| 166 | pub const TICK_HZ: u64 = 50_000_000; | ||
| 167 | #[cfg(feature = "tick-hz-60_000_000")] | ||
| 168 | pub const TICK_HZ: u64 = 60_000_000; | ||
| 169 | #[cfg(feature = "tick-hz-70_000_000")] | ||
| 170 | pub const TICK_HZ: u64 = 70_000_000; | ||
| 171 | #[cfg(feature = "tick-hz-80_000_000")] | ||
| 172 | pub const TICK_HZ: u64 = 80_000_000; | ||
| 173 | #[cfg(feature = "tick-hz-90_000_000")] | ||
| 174 | pub const TICK_HZ: u64 = 90_000_000; | ||
| 175 | #[cfg(feature = "tick-hz-110_000_000")] | ||
| 176 | pub const TICK_HZ: u64 = 110_000_000; | ||
| 177 | #[cfg(feature = "tick-hz-120_000_000")] | ||
| 178 | pub const TICK_HZ: u64 = 120_000_000; | ||
| 179 | #[cfg(feature = "tick-hz-130_000_000")] | ||
| 180 | pub const TICK_HZ: u64 = 130_000_000; | ||
| 181 | #[cfg(feature = "tick-hz-140_000_000")] | ||
| 182 | pub const TICK_HZ: u64 = 140_000_000; | ||
| 183 | #[cfg(feature = "tick-hz-150_000_000")] | ||
| 184 | pub const TICK_HZ: u64 = 150_000_000; | ||
| 185 | #[cfg(feature = "tick-hz-160_000_000")] | ||
| 186 | pub const TICK_HZ: u64 = 160_000_000; | ||
| 187 | #[cfg(feature = "tick-hz-170_000_000")] | ||
| 188 | pub const TICK_HZ: u64 = 170_000_000; | ||
| 189 | #[cfg(feature = "tick-hz-180_000_000")] | ||
| 190 | pub const TICK_HZ: u64 = 180_000_000; | ||
| 191 | #[cfg(feature = "tick-hz-190_000_000")] | ||
| 192 | pub const TICK_HZ: u64 = 190_000_000; | ||
| 193 | #[cfg(feature = "tick-hz-200_000_000")] | ||
| 194 | pub const TICK_HZ: u64 = 200_000_000; | ||
| 195 | #[cfg(feature = "tick-hz-210_000_000")] | ||
| 196 | pub const TICK_HZ: u64 = 210_000_000; | ||
| 197 | #[cfg(feature = "tick-hz-220_000_000")] | ||
| 198 | pub const TICK_HZ: u64 = 220_000_000; | ||
| 199 | #[cfg(feature = "tick-hz-230_000_000")] | ||
| 200 | pub const TICK_HZ: u64 = 230_000_000; | ||
| 201 | #[cfg(feature = "tick-hz-240_000_000")] | ||
| 202 | pub const TICK_HZ: u64 = 240_000_000; | ||
| 203 | #[cfg(feature = "tick-hz-250_000_000")] | ||
| 204 | pub const TICK_HZ: u64 = 250_000_000; | ||
| 205 | #[cfg(feature = "tick-hz-260_000_000")] | ||
| 206 | pub const TICK_HZ: u64 = 260_000_000; | ||
| 207 | #[cfg(feature = "tick-hz-270_000_000")] | ||
| 208 | pub const TICK_HZ: u64 = 270_000_000; | ||
| 209 | #[cfg(feature = "tick-hz-280_000_000")] | ||
| 210 | pub const TICK_HZ: u64 = 280_000_000; | ||
| 211 | #[cfg(feature = "tick-hz-290_000_000")] | ||
| 212 | pub const TICK_HZ: u64 = 290_000_000; | ||
| 213 | #[cfg(feature = "tick-hz-300_000_000")] | ||
| 214 | pub const TICK_HZ: u64 = 300_000_000; | ||
| 215 | #[cfg(feature = "tick-hz-320_000_000")] | ||
| 216 | pub const TICK_HZ: u64 = 320_000_000; | ||
| 217 | #[cfg(feature = "tick-hz-340_000_000")] | ||
| 218 | pub const TICK_HZ: u64 = 340_000_000; | ||
| 219 | #[cfg(feature = "tick-hz-360_000_000")] | ||
| 220 | pub const TICK_HZ: u64 = 360_000_000; | ||
| 221 | #[cfg(feature = "tick-hz-380_000_000")] | ||
| 222 | pub const TICK_HZ: u64 = 380_000_000; | ||
| 223 | #[cfg(feature = "tick-hz-400_000_000")] | ||
| 224 | pub const TICK_HZ: u64 = 400_000_000; | ||
| 225 | #[cfg(feature = "tick-hz-420_000_000")] | ||
| 226 | pub const TICK_HZ: u64 = 420_000_000; | ||
| 227 | #[cfg(feature = "tick-hz-440_000_000")] | ||
| 228 | pub const TICK_HZ: u64 = 440_000_000; | ||
| 229 | #[cfg(feature = "tick-hz-460_000_000")] | ||
| 230 | pub const TICK_HZ: u64 = 460_000_000; | ||
| 231 | #[cfg(feature = "tick-hz-480_000_000")] | ||
| 232 | pub const TICK_HZ: u64 = 480_000_000; | ||
| 233 | #[cfg(feature = "tick-hz-500_000_000")] | ||
| 234 | pub const TICK_HZ: u64 = 500_000_000; | ||
| 235 | #[cfg(feature = "tick-hz-520_000_000")] | ||
| 236 | pub const TICK_HZ: u64 = 520_000_000; | ||
| 237 | #[cfg(feature = "tick-hz-540_000_000")] | ||
| 238 | pub const TICK_HZ: u64 = 540_000_000; | ||
| 239 | #[cfg(feature = "tick-hz-560_000_000")] | ||
| 240 | pub const TICK_HZ: u64 = 560_000_000; | ||
| 241 | #[cfg(feature = "tick-hz-580_000_000")] | ||
| 242 | pub const TICK_HZ: u64 = 580_000_000; | ||
| 243 | #[cfg(feature = "tick-hz-600_000_000")] | ||
| 244 | pub const TICK_HZ: u64 = 600_000_000; | ||
| 245 | #[cfg(feature = "tick-hz-620_000_000")] | ||
| 246 | pub const TICK_HZ: u64 = 620_000_000; | ||
| 247 | #[cfg(feature = "tick-hz-640_000_000")] | ||
| 248 | pub const TICK_HZ: u64 = 640_000_000; | ||
| 249 | #[cfg(feature = "tick-hz-660_000_000")] | ||
| 250 | pub const TICK_HZ: u64 = 660_000_000; | ||
| 251 | #[cfg(feature = "tick-hz-680_000_000")] | ||
| 252 | pub const TICK_HZ: u64 = 680_000_000; | ||
| 253 | #[cfg(feature = "tick-hz-700_000_000")] | ||
| 254 | pub const TICK_HZ: u64 = 700_000_000; | ||
| 255 | #[cfg(feature = "tick-hz-720_000_000")] | ||
| 256 | pub const TICK_HZ: u64 = 720_000_000; | ||
| 257 | #[cfg(feature = "tick-hz-740_000_000")] | ||
| 258 | pub const TICK_HZ: u64 = 740_000_000; | ||
| 259 | #[cfg(feature = "tick-hz-760_000_000")] | ||
| 260 | pub const TICK_HZ: u64 = 760_000_000; | ||
| 261 | #[cfg(feature = "tick-hz-780_000_000")] | ||
| 262 | pub const TICK_HZ: u64 = 780_000_000; | ||
| 263 | #[cfg(feature = "tick-hz-800_000_000")] | ||
| 264 | pub const TICK_HZ: u64 = 800_000_000; | ||
| 265 | #[cfg(feature = "tick-hz-820_000_000")] | ||
| 266 | pub const TICK_HZ: u64 = 820_000_000; | ||
| 267 | #[cfg(feature = "tick-hz-840_000_000")] | ||
| 268 | pub const TICK_HZ: u64 = 840_000_000; | ||
| 269 | #[cfg(feature = "tick-hz-860_000_000")] | ||
| 270 | pub const TICK_HZ: u64 = 860_000_000; | ||
| 271 | #[cfg(feature = "tick-hz-880_000_000")] | ||
| 272 | pub const TICK_HZ: u64 = 880_000_000; | ||
| 273 | #[cfg(feature = "tick-hz-900_000_000")] | ||
| 274 | pub const TICK_HZ: u64 = 900_000_000; | ||
| 275 | #[cfg(feature = "tick-hz-920_000_000")] | ||
| 276 | pub const TICK_HZ: u64 = 920_000_000; | ||
| 277 | #[cfg(feature = "tick-hz-940_000_000")] | ||
| 278 | pub const TICK_HZ: u64 = 940_000_000; | ||
| 279 | #[cfg(feature = "tick-hz-960_000_000")] | ||
| 280 | pub const TICK_HZ: u64 = 960_000_000; | ||
| 281 | #[cfg(feature = "tick-hz-980_000_000")] | ||
| 282 | pub const TICK_HZ: u64 = 980_000_000; | ||
| 159 | #[cfg(not(any( | 283 | #[cfg(not(any( |
| 160 | feature = "tick-hz-1", | 284 | feature = "tick-hz-1", |
| 161 | feature = "tick-hz-10", | 285 | feature = "tick-hz-10", |
| @@ -235,5 +359,67 @@ pub const TICK_HZ: u64 = 768_000_000; | |||
| 235 | feature = "tick-hz-512_000_000", | 359 | feature = "tick-hz-512_000_000", |
| 236 | feature = "tick-hz-576_000_000", | 360 | feature = "tick-hz-576_000_000", |
| 237 | feature = "tick-hz-768_000_000", | 361 | feature = "tick-hz-768_000_000", |
| 362 | feature = "tick-hz-20_000_000", | ||
| 363 | feature = "tick-hz-30_000_000", | ||
| 364 | feature = "tick-hz-40_000_000", | ||
| 365 | feature = "tick-hz-50_000_000", | ||
| 366 | feature = "tick-hz-60_000_000", | ||
| 367 | feature = "tick-hz-70_000_000", | ||
| 368 | feature = "tick-hz-80_000_000", | ||
| 369 | feature = "tick-hz-90_000_000", | ||
| 370 | feature = "tick-hz-110_000_000", | ||
| 371 | feature = "tick-hz-120_000_000", | ||
| 372 | feature = "tick-hz-130_000_000", | ||
| 373 | feature = "tick-hz-140_000_000", | ||
| 374 | feature = "tick-hz-150_000_000", | ||
| 375 | feature = "tick-hz-160_000_000", | ||
| 376 | feature = "tick-hz-170_000_000", | ||
| 377 | feature = "tick-hz-180_000_000", | ||
| 378 | feature = "tick-hz-190_000_000", | ||
| 379 | feature = "tick-hz-200_000_000", | ||
| 380 | feature = "tick-hz-210_000_000", | ||
| 381 | feature = "tick-hz-220_000_000", | ||
| 382 | feature = "tick-hz-230_000_000", | ||
| 383 | feature = "tick-hz-240_000_000", | ||
| 384 | feature = "tick-hz-250_000_000", | ||
| 385 | feature = "tick-hz-260_000_000", | ||
| 386 | feature = "tick-hz-270_000_000", | ||
| 387 | feature = "tick-hz-280_000_000", | ||
| 388 | feature = "tick-hz-290_000_000", | ||
| 389 | feature = "tick-hz-300_000_000", | ||
| 390 | feature = "tick-hz-320_000_000", | ||
| 391 | feature = "tick-hz-340_000_000", | ||
| 392 | feature = "tick-hz-360_000_000", | ||
| 393 | feature = "tick-hz-380_000_000", | ||
| 394 | feature = "tick-hz-400_000_000", | ||
| 395 | feature = "tick-hz-420_000_000", | ||
| 396 | feature = "tick-hz-440_000_000", | ||
| 397 | feature = "tick-hz-460_000_000", | ||
| 398 | feature = "tick-hz-480_000_000", | ||
| 399 | feature = "tick-hz-500_000_000", | ||
| 400 | feature = "tick-hz-520_000_000", | ||
| 401 | feature = "tick-hz-540_000_000", | ||
| 402 | feature = "tick-hz-560_000_000", | ||
| 403 | feature = "tick-hz-580_000_000", | ||
| 404 | feature = "tick-hz-600_000_000", | ||
| 405 | feature = "tick-hz-620_000_000", | ||
| 406 | feature = "tick-hz-640_000_000", | ||
| 407 | feature = "tick-hz-660_000_000", | ||
| 408 | feature = "tick-hz-680_000_000", | ||
| 409 | feature = "tick-hz-700_000_000", | ||
| 410 | feature = "tick-hz-720_000_000", | ||
| 411 | feature = "tick-hz-740_000_000", | ||
| 412 | feature = "tick-hz-760_000_000", | ||
| 413 | feature = "tick-hz-780_000_000", | ||
| 414 | feature = "tick-hz-800_000_000", | ||
| 415 | feature = "tick-hz-820_000_000", | ||
| 416 | feature = "tick-hz-840_000_000", | ||
| 417 | feature = "tick-hz-860_000_000", | ||
| 418 | feature = "tick-hz-880_000_000", | ||
| 419 | feature = "tick-hz-900_000_000", | ||
| 420 | feature = "tick-hz-920_000_000", | ||
| 421 | feature = "tick-hz-940_000_000", | ||
| 422 | feature = "tick-hz-960_000_000", | ||
| 423 | feature = "tick-hz-980_000_000", | ||
| 238 | )))] | 424 | )))] |
| 239 | pub const TICK_HZ: u64 = 1_000_000; | 425 | pub const TICK_HZ: u64 = 1_000_000; |
diff --git a/embassy-time/src/timer.rs b/embassy-time/src/timer.rs index ad5026e62..07ddf473f 100644 --- a/embassy-time/src/timer.rs +++ b/embassy-time/src/timer.rs | |||
| @@ -133,7 +133,13 @@ impl Ticker { | |||
| 133 | Self { expires_at, duration } | 133 | Self { expires_at, duration } |
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | /// Waits for the next tick | 136 | /// Resets the ticker back to its original state. |
| 137 | /// This causes the ticker to go back to zero, even if the current tick isn't over yet. | ||
| 138 | pub fn reset(&mut self) { | ||
| 139 | self.expires_at = Instant::now() + self.duration; | ||
| 140 | } | ||
| 141 | |||
| 142 | /// Waits for the next tick. | ||
| 137 | pub fn next(&mut self) -> impl Future<Output = ()> + '_ { | 143 | pub fn next(&mut self) -> impl Future<Output = ()> + '_ { |
| 138 | poll_fn(|cx| { | 144 | poll_fn(|cx| { |
| 139 | if self.expires_at <= Instant::now() { | 145 | if self.expires_at <= Instant::now() { |
