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
// SPDX-License-Identifier: Apache-2.0
//! Provides a SysTick based 64-bit timer implementation.
//!
//! In addition, optionally wraps this into a basic Embassy time
//! driver.
//! With the `embedded-hal` feature enabled, [`Timer`] also implements
//! [`embedded_hal::delay::DelayNs`].
//! The blocking delay implementation requires the underlying SysTick timer
//! to have been started with [`Timer::start`] and to be actively advancing;
//! otherwise delay calls will wait forever.
//!
//! The timer is a standalone implementation that can be used from
//! any Cortex-M0/M3/M4/M7 code.
//!
//! The timer does not use critical sections or locking, only atomic
//! operations.
//!
//! Usage:
//! ```ignore
//! // Set up timer with 1ms resolution, reload at 100us, 8MHz clock
//! static INSTANCE : Timer = Timer::new(1_000, 799, 8_000_000);
//!
//! #[cortex_m_rt::entry]
//! fn main() -> ! {
//! // Configure and start SYST
//! INSTANCE.start(&mut cortex_m::Peripherals::take().unwrap().SYST);
//! // Get the current time in milliseconds
//! let now = timer.now();
//! }
//! ```
//! Call the timer from your Systick handler:
//! ```ignore
//! #[exception]
//! fn SysTick() {
//! INSTANCE.systick_handler();
//! }
//! ```
//!
//! To reduce the frequency of overflow interrupts,
//! you can use the maximum reload value:
//! ```ignore
//! let timer = Timer::new(1_000, 16_777_215, 48_000_000);
//! ```
//! This generates an interrupt and reloads the timer every ~350ms, but
//! the resolution is still 1ms
//!
//! ----------------------------------------------------------------
//!
//! To use the Embassy driver, the setup needs to look as follows. First,
//! create a static instance of the timer, passing in SysTick frequency
//! and reload value. The constant <4> determines the number of concurrent
//! wait tasks supported.
//!
//! ```ignore
//! embassy_time_driver::time_driver_impl!(static DRIVER: SystickDriver<4>
//! = SystickDriver::new(8_000_000, 7999));
//! ```
//!
//! Next, you must have a SysTick interrupt handler that calls the driver's
//! `systick_interrupt()` method on its static instance.
//!
//! ```ignore
//! #[exception]
//! fn SysTick() {
//! DRIVER.systick_interrupt();
//! }
//! ```
//!
//! And in main, before using any timer calls, initialize the driver with
//! the actual SysTick peripheral:
//!
//! ```ignore
//! #[embassy_executor::main]
//! async fn main(_s: embassy_executor::Spawner) {
//! let mut periph = Peripherals::take().unwrap();
//! DRIVER.start(&mut periph.SYST);
//! // .. can use Timer::now() etc.
//! }
//! ```
pub use Timer;
pub use SystickDriver;