Skip to main content

systick_timer/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2
3#![cfg_attr(not(test), no_std)]
4
5//! Provides a SysTick based 64-bit timer implementation.
6//!
7//! In addition, optionally wraps this into a basic Embassy time
8//! driver.
9//! With the `embedded-hal` feature enabled, [`Timer`] also implements
10//! [`embedded_hal::delay::DelayNs`].
11//! The blocking delay implementation requires the underlying SysTick timer
12//! to have been started with [`Timer::start`] and to be actively advancing;
13//! otherwise delay calls will wait forever.
14//!
15//! The timer is a standalone implementation that can be used from
16//! any Cortex-M0/M3/M4/M7 code.
17//!
18//! The timer does not use critical sections or locking, only atomic
19//! operations.
20//!
21//! Usage:
22//! ```ignore
23//! // Set up timer with 1ms resolution, reload at 100us, 8MHz clock
24//! static INSTANCE : Timer = Timer::new(1_000, 799, 8_000_000);
25//!
26//! #[cortex_m_rt::entry]
27//! fn main() -> ! {
28//!     // Configure and start SYST
29//!     INSTANCE.start(&mut cortex_m::Peripherals::take().unwrap().SYST);
30//!     // Get the current time in milliseconds
31//!      let now = timer.now();
32//! }
33//! ```
34//! Call the timer from your Systick handler:
35//! ```ignore
36//! #[exception]
37//! fn SysTick() {
38//!     INSTANCE.systick_handler();
39//! }
40//! ```
41//!
42//! To reduce the frequency of overflow interrupts,
43//! you can use the maximum reload value:
44//! ```ignore
45//! let timer = Timer::new(1_000, 16_777_215, 48_000_000);
46//! ```
47//! This generates an interrupt and reloads the timer every ~350ms, but
48//! the resolution is still 1ms
49//!
50//! ----------------------------------------------------------------
51//!
52//! To use the Embassy driver, the setup needs to look as follows. First,
53//! create a static instance of the timer, passing in SysTick frequency
54//! and reload value. The constant <4> determines the number of concurrent
55//! wait tasks supported.
56//!
57//! ```ignore
58//! embassy_time_driver::time_driver_impl!(static DRIVER: SystickDriver<4>
59//!   = SystickDriver::new(8_000_000, 7999));
60//! ```
61//!
62//! Next, you must have a SysTick interrupt handler that calls the driver's
63//! `systick_interrupt()` method on its static instance.
64//!
65//! ```ignore
66//! #[exception]
67//! fn SysTick() {
68//!     DRIVER.systick_interrupt();
69//! }
70//! ```
71//!
72//! And in main, before using any timer calls, initialize the driver with
73//! the actual SysTick peripheral:
74//!
75//! ```ignore
76//! #[embassy_executor::main]
77//! async fn main(_s: embassy_executor::Spawner) {
78//!   let mut periph = Peripherals::take().unwrap();
79//!   DRIVER.start(&mut periph.SYST);
80//!   // .. can use Timer::now() etc.
81//! }
82//! ```
83
84pub mod timer;
85pub use timer::Timer;
86
87#[cfg(feature = "embassy-time-driver")]
88mod embassy_driver;
89#[cfg(feature = "embassy-time-driver")]
90pub use embassy_driver::SystickDriver;