diff options
| author | Ulf Lilleengen <[email protected]> | 2024-05-18 10:17:03 +0200 |
|---|---|---|
| committer | Ulf Lilleengen <[email protected]> | 2024-05-21 10:05:21 +0200 |
| commit | 739e5861c2e47db251725163fcd91cd822cf97b7 (patch) | |
| tree | 947dc7961ca7c42ec216056fc2adf616ab812b10 /docs/pages/time_keeping.adoc | |
| parent | 51d553092550059afb22b2620cea14bbed21abff (diff) | |
convert from antora to asciidoctor
Diffstat (limited to 'docs/pages/time_keeping.adoc')
| -rw-r--r-- | docs/pages/time_keeping.adoc | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/docs/pages/time_keeping.adoc b/docs/pages/time_keeping.adoc new file mode 100644 index 000000000..17492a884 --- /dev/null +++ b/docs/pages/time_keeping.adoc | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | = Time-keeping | ||
| 2 | |||
| 3 | In an embedded program, delaying a task is one of the most common actions taken. In an event loop, delays will need to be inserted to ensure | ||
| 4 | that other tasks have a chance to run before the next iteration of the loop is called, if no other I/O is performed. Embassy provides abstractions | ||
| 5 | to delay the current task for a specified interval of time. | ||
| 6 | |||
| 7 | The interface for time-keeping in Embassy is handled by the link:https://crates.io/crates/embassy-time[embassy-time] crate. The types can be used with the internal | ||
| 8 | timer queue in link:https://crates.io/crates/embassy-executor[embassy-executor] or a custom timer queue implementation. | ||
| 9 | |||
| 10 | == Timer | ||
| 11 | |||
| 12 | The `embassy::time::Timer` type provides two timing methods. | ||
| 13 | |||
| 14 | `Timer::at` creates a future that completes at the specified `Instant`, relative to the system boot time. | ||
| 15 | `Timer::after` creates a future that completes after the specified `Duration`, relative to when the future was created. | ||
| 16 | |||
| 17 | An example of a delay is provided as follows: | ||
| 18 | |||
| 19 | TIP: Dependencies needed to run this example link:/book/dev/basic_application.html#_the_cargo_toml[can be found here]. | ||
| 20 | [,rust] | ||
| 21 | ---- | ||
| 22 | use embassy::executor::{task, Executor}; | ||
| 23 | use embassy::time::{Duration, Timer}; | ||
| 24 | |||
| 25 | #[task] | ||
| 26 | /// Task that ticks periodically | ||
| 27 | async fn tick_periodic() -> ! { | ||
| 28 | loop { | ||
| 29 | rprintln!("tick!"); | ||
| 30 | // async sleep primitive, suspends the task for 500ms. | ||
| 31 | Timer::after(Duration::from_millis(500)).await; | ||
| 32 | } | ||
| 33 | } | ||
| 34 | ---- | ||
| 35 | |||
| 36 | == Delay | ||
| 37 | |||
| 38 | The `embassy::time::Delay` type provides an implementation of the link:https://docs.rs/embedded-hal/1.0.0/embedded_hal/delay/index.html[embedded-hal] and | ||
| 39 | link:https://docs.rs/embedded-hal-async/latest/embedded_hal_async/delay/index.html[embedded-hal-async] traits. This can be used for drivers | ||
| 40 | that expect a generic delay implementation to be provided. | ||
| 41 | |||
| 42 | An example of how this can be used: | ||
| 43 | |||
| 44 | TIP: Dependencies needed to run this example link:/book/dev/basic_application.html#_the_cargo_toml[can be found here]. | ||
| 45 | [,rust] | ||
| 46 | ---- | ||
| 47 | use embassy::executor::{task, Executor}; | ||
| 48 | |||
| 49 | #[task] | ||
| 50 | /// Task that ticks periodically | ||
| 51 | async fn tick_periodic() -> ! { | ||
| 52 | loop { | ||
| 53 | rprintln!("tick!"); | ||
| 54 | // async sleep primitive, suspends the task for 500ms. | ||
| 55 | generic_delay(embassy::time::Delay).await | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | async fn generic_delay<D: embedded_hal_async::delay::DelayNs>(delay: D) { | ||
| 60 | delay.delay_ms(500).await; | ||
| 61 | } | ||
| 62 | ---- | ||
