embedded-time 0.1.2

A library for abstracting clocks and handling time in embedded systems
Documentation

embedded-time

Check Test Clippy

crates.io

embedded-time provides a comprehensive library for implementing abstractions over hardware and work with instants and durations in an intuitive way.

  • Clock trait allowing abstraction of hardware timers/counters for timekeeping.
  • Work with time using milliseconds, seconds, etc. rather than cycles or ticks.
  • Includes examples for the nRF52_DK development kit as bare-metal as well as using rtfm (with patches)

Example Usage

struct SomeClock;

impl Clock for SomeClock {
    type Rep = i64;

    fn now() -> Instant<Self> {
        // read the count of the clock
        // ...
        Instant::new(count as Self::Rep)
    }
}

impl Period for SomeClock {
    // this clock is counting at 16 MHz
    const PERIOD: Ratio<i32> = Ratio::<i32>::new_raw(1, 16_000_000);
}

fn main() {
    // read from a Clock
    let instant1 = SomeClock::now();
    
    // ... some time passes
    
    let instant2 = SomeClock::now();
    assert!(instant1 < instant2);    // instant1 is *before* instant2
    
    // duration is the difference between the instances
    let duration: Option<Microseconds<i64>> = instant2.elapsed_since(&instant1);    
    
    // add some duration to an instant
    let future_instant = instant2 + Milliseconds(23);
    // or
    let future_instant = instant2 + 23.milliseconds();
    
    assert(future_instant > instant2);
}

Full documentation

License

This project is licensed under either of

at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in time by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.