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
//! Delay abstraction trait
//!
//! Provides a unified [`DelayUs`] trait implemented by
//! [`crate::systick::SysTick`], [`crate::systick::Delay`],
//! [`crate::timer::Timer0`] and [`crate::timer::Timer1`], so upper-layer code
//! stays agnostic to the delay source.
//!
//! # Example
//!
//! ```ignore
//! use rp2350::delay::DelayUs;
//! use rp2350::timer::Timer0;
//! use rp2350_pac::TIMER0;
//!
//! fn blink(delay: &impl DelayUs) {
//! delay.delay_ms(500);
//! }
//!
//! let timer = Timer0::init(unsafe { TIMER0::steal() });
//! blink(&timer);
//! ```
/// Unified microsecond delay trait
///
/// Implemented by [`crate::systick::SysTick`], [`crate::systick::Delay`],
/// [`crate::timer::Timer0`] and [`crate::timer::Timer1`].
///
/// Upper-layer code can decouple from a concrete delay implementation via
/// generics or dynamic dispatch (`&dyn DelayUs`):
///
/// ```ignore
/// fn wait(delay: &impl DelayUs) {
/// delay.delay_ms(100);
/// }
/// ```
pub trait DelayUs {
/// Delay for the given number of microseconds (busy-wait)
fn delay_us(&self, us: u32);
/// Delay for the given number of milliseconds
///
/// The default implementation calls `delay_us(ms * 1_000)`; override it
/// for better efficiency if needed.
#[inline]
fn delay_ms(&self, ms: u32) {
self.delay_us(ms.saturating_mul(1_000));
}
/// Delay for the given number of seconds
///
/// The default implementation loops calling `delay_ms(1_000)` to avoid
/// overflow of `ms * 1_000`.
#[inline]
fn delay_secs(&self, secs: u32) {
for _ in 0..secs {
self.delay_ms(1_000);
}
}
}
impl<T: DelayUs> DelayUs for crate::Steal<T> {
#[inline]
fn delay_us(&self, us: u32) {
self.0.delay_us(us)
}
}