2021-09-22 13:22:45 +02:00
|
|
|
# Monotonic & spawn_{at/after}
|
|
|
|
|
|
|
|
The understanding of time is an important concept in embedded systems, and to be able to run tasks
|
2022-02-20 19:21:25 +01:00
|
|
|
based on time is essential. The framework provides the static methods
|
2021-09-22 13:22:45 +02:00
|
|
|
`task::spawn_after(/* duration */)` and `task::spawn_at(/* specific time instant */)`.
|
2021-12-16 21:34:46 +01:00
|
|
|
`spawn_after` is more commonly used, but in cases where it's needed to have spawns happen
|
|
|
|
without drift or to a fixed baseline `spawn_at` is available.
|
2021-09-22 13:22:45 +02:00
|
|
|
|
2021-12-16 21:34:46 +01:00
|
|
|
The `#[monotonic]` attribute, applied to a type alias definition, exists to support this.
|
2021-09-22 13:22:45 +02:00
|
|
|
This type alias must point to a type which implements the [`rtic_monotonic::Monotonic`] trait.
|
2021-12-16 21:34:46 +01:00
|
|
|
This is generally some timer which handles the timing of the system.
|
|
|
|
One or more monotonics can coexist in the same system, for example a slow timer that wakes the
|
2021-12-21 19:43:45 +01:00
|
|
|
system from sleep and another which purpose is for fine grained scheduling while the
|
2021-12-16 21:34:46 +01:00
|
|
|
system is awake.
|
2021-09-22 13:22:45 +02:00
|
|
|
|
|
|
|
[`rtic_monotonic::Monotonic`]: https://docs.rs/rtic-monotonic
|
|
|
|
|
|
|
|
The attribute has one required parameter and two optional parameters, `binds`, `default` and
|
2021-12-16 21:34:46 +01:00
|
|
|
`priority` respectively.
|
|
|
|
The required parameter, `binds = InterruptName`, associates an interrupt vector to the timer's
|
|
|
|
interrupt, while `default = true` enables a shorthand API when spawning and accessing
|
|
|
|
time (`monotonics::now()` vs `monotonics::MyMono::now()`), and `priority` sets the priority
|
|
|
|
of the interrupt vector.
|
2021-09-22 13:22:45 +02:00
|
|
|
|
2021-12-16 21:34:46 +01:00
|
|
|
> The default `priority` is the **maximum priority** of the system.
|
|
|
|
> If your system has a high priority task with tight scheduling requirements,
|
|
|
|
> it might be desirable to demote the `monotonic` task to a lower priority
|
|
|
|
> to reduce scheduling jitter for the high priority task.
|
|
|
|
> This however might introduce jitter and delays into scheduling via the `monotonic`,
|
|
|
|
> making it a trade-off.
|
2021-09-22 13:22:45 +02:00
|
|
|
|
2021-12-16 21:34:46 +01:00
|
|
|
The monotonics are initialized in `#[init]` and returned within the `init::Monotonic( ... )` tuple.
|
|
|
|
This activates the monotonics making it possible to use them.
|
2021-09-22 13:22:45 +02:00
|
|
|
|
2021-12-16 21:34:46 +01:00
|
|
|
See the following example:
|
2021-09-22 13:22:45 +02:00
|
|
|
|
2023-04-23 15:33:56 +02:00
|
|
|
``` rust,noplayground
|
2021-09-22 13:22:45 +02:00
|
|
|
{{#include ../../../../examples/schedule.rs}}
|
|
|
|
```
|
|
|
|
|
|
|
|
``` console
|
2024-04-04 00:01:46 +02:00
|
|
|
$ cargo xtask qemu --verbose --example schedule
|
2021-09-22 13:22:45 +02:00
|
|
|
{{#include ../../../../ci/expected/schedule.run}}
|
|
|
|
```
|
|
|
|
|
2022-02-20 19:21:25 +01:00
|
|
|
A key requirement of a Monotonic is that it must deal gracefully with
|
|
|
|
hardware timer overruns.
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
## Canceling or rescheduling a scheduled task
|
|
|
|
|
2021-12-16 21:34:46 +01:00
|
|
|
Tasks spawned using `task::spawn_after` and `task::spawn_at` returns a `SpawnHandle`,
|
|
|
|
which allows canceling or rescheduling of the task scheduled to run in the future.
|
2022-02-20 19:21:25 +01:00
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
If `cancel` or `reschedule_at`/`reschedule_after` returns an `Err` it means that the operation was
|
|
|
|
too late and that the task is already sent for execution. The following example shows this in action:
|
|
|
|
|
2023-04-23 15:33:56 +02:00
|
|
|
``` rust,noplayground
|
2021-09-22 13:22:45 +02:00
|
|
|
{{#include ../../../../examples/cancel-reschedule.rs}}
|
|
|
|
```
|
|
|
|
|
|
|
|
``` console
|
2024-04-04 00:01:46 +02:00
|
|
|
$ cargo xtask qemu --verbose --example cancel-reschedule
|
2021-09-22 13:22:45 +02:00
|
|
|
{{#include ../../../../ci/expected/cancel-reschedule.run}}
|
|
|
|
```
|