From 825b2c2c3a565920a31a87328caa82442a44d209 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 22 Apr 2023 18:34:30 +0200 Subject: Update tips on Monotonic implemenations --- book/en/src/by-example/tips_monotonic_impl.md | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/tips_monotonic_impl.md b/book/en/src/by-example/tips_monotonic_impl.md index 57b0a01..85b5709 100644 --- a/book/en/src/by-example/tips_monotonic_impl.md +++ b/book/en/src/by-example/tips_monotonic_impl.md @@ -1,14 +1,23 @@ # Implementing a `Monotonic` timer for scheduling -The framework is flexible because it can use any timer which has compare-match and optionally supporting overflow interrupts for scheduling. The single requirement to make a timer usable with RTIC is implementing the [`rtic-time::Monotonic`] trait. +The framework is flexible because it can use any timer which has compare-match and optionally supporting overflow interrupts for scheduling. The single requirement to make a timer usable with RTIC is implementing the `rtic-time::Monotonic` trait. -For RTIC 1.0 and 2.0 we instead assume the user has a time library, e.g. [`fugit`] or [`embedded_time`], as the basis for all time-based operations when implementing `Monotonic`. These libraries make it much easier to correctly implement the `Monotonic` trait, allowing the use of -almost any timer in the system for scheduling. +For RTIC 1.0 and 2.0 we instead assume the user has a time library, e.g. [`fugit`] or [`embedded_time`], as the basis for all time-based operations when implementing `Monotonic`. These libraries make it much easier to correctly implement the `Monotonic` trait, allowing the use of almost any timer in the system for scheduling. -The trait documents the requirements for each method, and for inspiration -there is a reference implementation based on the `SysTick` timer available on all ARM Cortex M MCUs. +The trait documents the requirements for each method. There are reference implementations available in [`rtic-monotonics`](https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics/src) that can be used for inspriation. - [`Systick based`], runs at a fixed interrupt (tick) rate - with some overhead but simple and provides support for large time spans +- [`RP2040 Timer`], a "proper" implementation with support for waiting for long periods without interrupts. Clearly demonstrates how to use the `TimerQueue` to handle scheduling. +- [`nRF52 timers`] implements monotonic & Timer Queue for the RTC and normal timers in nRF52's + +## Contributing + +Contributing new implementations of `Monotonic` can be done in multiple ways: +* Implement the trait behind a feature flag in [`rtic-monotonics`], and create a PR for them to be included in the main RTIC repository. This way, the implementations of are in-tree, and RTIC can guarantee their correctness, and can update them in the case of a new release. +* Implement the changes in an external repository. + + +# V1.0.x Here is a list of `Monotonic` implementations for RTIC 1.0: @@ -27,3 +36,6 @@ If you know of more implementations feel free to add them to this list. [`Nordic nRF52 series RTC`]: https://gist.github.com/korken89/fe94a475726414dd1bce031c76adc3dd [`Systick based`]: https://github.com/rtic-monotonics [`DWT and Systick based`]: https://github.com/rtic-rs/dwt-systick-monotonic +[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics +[`RP2040 Timer`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/rp2040.rs +[`nRF52 timers`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/nrf.rs \ No newline at end of file -- cgit v1.2.3 From 4437f12b30e011a229c49d62538284d7ae61324e Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 22 Apr 2023 19:18:43 +0200 Subject: Remove link to inactive `embedded_time` --- book/en/src/by-example/tips_monotonic_impl.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/tips_monotonic_impl.md b/book/en/src/by-example/tips_monotonic_impl.md index 85b5709..5dc4dd7 100644 --- a/book/en/src/by-example/tips_monotonic_impl.md +++ b/book/en/src/by-example/tips_monotonic_impl.md @@ -2,7 +2,7 @@ The framework is flexible because it can use any timer which has compare-match and optionally supporting overflow interrupts for scheduling. The single requirement to make a timer usable with RTIC is implementing the `rtic-time::Monotonic` trait. -For RTIC 1.0 and 2.0 we instead assume the user has a time library, e.g. [`fugit`] or [`embedded_time`], as the basis for all time-based operations when implementing `Monotonic`. These libraries make it much easier to correctly implement the `Monotonic` trait, allowing the use of almost any timer in the system for scheduling. +For RTIC 1.0 and 2.0 we instead assume the user has a time library, e.g. [`fugit`], as the basis for all time-based operations when implementing `Monotonic`. These libraries make it much easier to correctly implement the `Monotonic` trait, allowing the use of almost any timer in the system for scheduling. The trait documents the requirements for each method. There are reference implementations available in [`rtic-monotonics`](https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics/src) that can be used for inspriation. @@ -30,7 +30,6 @@ If you know of more implementations feel free to add them to this list. [`rtic_time::Monotonic`]: https://docs.rs/rtic_time/ [`fugit`]: https://docs.rs/fugit/ -[`embedded_time`]: https://docs.rs/embedded_time/ [`STM32F411 series`]: https://github.com/kalkyl/f411-rtic/blob/a696fce7d6d19fda2356c37642c4d53547982cca/src/mono.rs [`Nordic nRF52 series Timer`]: https://github.com/kalkyl/nrf-play/blob/47f4410d4e39374c18ff58dc17c25159085fb526/src/mono.rs [`Nordic nRF52 series RTC`]: https://gist.github.com/korken89/fe94a475726414dd1bce031c76adc3dd -- cgit v1.2.3 From d22faec870b57274b1b2a74c1fe947e969c13379 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 22 Apr 2023 19:55:44 +0200 Subject: Fix run-on sentence --- book/en/src/by-example/message_passing.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/message_passing.md b/book/en/src/by-example/message_passing.md index 0dc8f85..5665c36 100644 --- a/book/en/src/by-example/message_passing.md +++ b/book/en/src/by-example/message_passing.md @@ -3,7 +3,7 @@ Software tasks support message passing, this means that software tasks can be spawned with an argument: `foo::spawn(1)` which will run the task `foo` with the argument `1`. -Capacity sets the size of the spawn queue for the task, if not specified capacity defaults to 1. +Capacity sets the size of the spawn queue for the task. If it is not specified, the capacity defaults to 1. In the example below, the capacity of task `foo` is `3`, allowing three simultaneous pending spawns of `foo`. Exceeding this capacity is an `Error`. -- cgit v1.2.3 From ed465b0c3b8b4c588f5cc7945af79a504f928cc8 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 22 Apr 2023 19:54:42 +0200 Subject: Fix #699 --- book/en/src/by-example/channel.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/channel.md b/book/en/src/by-example/channel.md index c020870..50c3278 100644 --- a/book/en/src/by-example/channel.md +++ b/book/en/src/by-example/channel.md @@ -1,6 +1,6 @@ # Communication over channels. -Channels can be used to communicate data between running *software* tasks. The channel is essentially a wait queue, allowing tasks with multiple producers and a single receiver. A channel is constructed in the `init` task and backed by statically allocated memory. Send and receive endpoints are distributed to *software* tasks: +Channels can be used to communicate data between running tasks. The channel is essentially a wait queue, allowing tasks with multiple producers and a single receiver. A channel is constructed in the `init` task and backed by statically allocated memory. Send and receive endpoints are distributed to *software* tasks: ``` rust ... @@ -16,6 +16,8 @@ const CAPACITY: usize = 5; In this case the channel holds data of `u32` type with a capacity of 5 elements. +Channels can also be used from *hardware* tasks, but only in a non-`async` manner using the [Try API](#try-api). + ## Sending data The `send` method post a message on the channel as shown below: @@ -107,11 +109,11 @@ $ cargo run --target thumbv7m-none-eabi --example async-channel-no-receiver --fe {{#include ../../../../rtic/ci/expected/async-channel-no-receiver.run}} ``` - - ## Try API -In cases you wish the sender to proceed even in case the channel is full. To that end, a `try_send` API is provided. +Using the Try API, you can send or receive data from or to a channel without requiring that the operation succeeds, and in non-`async` contexts. + +This API is exposed through `Receiver::try_recv` and `Sender::try_send`. ``` rust {{#include ../../../../rtic/examples/async-channel-try.rs}} -- cgit v1.2.3 From 3d98102a54c660d54ddfb36ad02cba0f25783750 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 22 Apr 2023 21:27:50 +0200 Subject: Refer to rtic-rs/rtic/examples instead --- book/en/src/by-example/starting_a_project.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/starting_a_project.md b/book/en/src/by-example/starting_a_project.md index 86d7e71..ae979f4 100644 --- a/book/en/src/by-example/starting_a_project.md +++ b/book/en/src/by-example/starting_a_project.md @@ -10,12 +10,8 @@ If you are targeting ARMv6-M or ARMv8-M-base architecture, check out the section This will give you an RTIC application with support for RTT logging with [`defmt`] and stack overflow protection using [`flip-link`]. There is also a multitude of examples provided by the community: -For inspiration, you may look at the below resources. For now, they cover RTIC v1.x, but will be updated with RTIC v2.x examples over time. - -- [`rtic-examples`] - Multiple projects -- [https://github.com/kalkyl/f411-rtic](https://github.com/kalkyl/f411-rtic) -- ... More to come +For inspiration, you may look at the [rtic examples]. [`defmt`]: https://github.com/knurling-rs/defmt/ [`flip-link`]: https://github.com/knurling-rs/flip-link/ -[`rtic-examples`]: https://github.com/rtic-rs/rtic-examples +[rtic examples]: https://github.com/rtic-rs/rtic/tree/master/examples -- cgit v1.2.3 From a14d24007ae6391121dc493782b900d6edb7d992 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 22 Apr 2023 22:47:07 +0200 Subject: Update delay.md file to be a bit easier to read, and add spoiler tags for the walls of code --- book/en/src/by-example/delay.md | 58 +++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 22 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/delay.md b/book/en/src/by-example/delay.md index f286363..893ead9 100644 --- a/book/en/src/by-example/delay.md +++ b/book/en/src/by-example/delay.md @@ -1,18 +1,20 @@ # Tasks with delay -A convenient way to express *miniminal* timing requirements is by means of delaying progression. +A convenient way to express miniminal timing requirements is by delaying progression. -This can be achieved by instantiating a monotonic timer: +This can be achieved by instantiating a monotonic timer (for implementations, see [`rtic-monotonics`]): + +[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics +[`rtic-time`]: https://github.com/rtic-rs/rtic/tree/master/rtic-time ``` rust ... -rtic_monotonics::make_systick_handler!(); - #[init] fn init(cx: init::Context) -> (Shared, Local) { hprintln!("init"); - Systick::start(cx.core.SYST, 12_000_000); + let token = rtic_monotonics::create_systick_token!(); + Systick::start(cx.core.SYST, 12_000_000, token); ... ``` @@ -28,11 +30,14 @@ async fn foo(_cx: foo::Context) { ``` -Technically, the timer queue is implemented as a list based priority queue, where list-nodes are statically allocated as part of the underlying task `Future`. Thus, the timer queue is infallible at run-time (its size and allocation is determined at compile time). + + +Technically, the timer queue is implemented as a list based priority queue, where list-nodes are statically allocated as part of the underlying task `Future`. Thus, the timer queue is infallible at run-time (its size and allocation are determined at compile time). Similarly the channels implementation, the timer-queue implementation relies on a global *Critical Section* (CS) for race protection. For the examples a CS implementation is provided by adding `--features test-critical-section` to the build options. -For a complete example: +
+A complete example ``` rust {{#include ../../../../rtic/examples/async-delay.rs}} @@ -46,11 +51,15 @@ $ cargo run --target thumbv7m-none-eabi --example async-delay --features test-cr {{#include ../../../../rtic/ci/expected/async-delay.run}} ``` +
+ ## Timeout -Rust `Futures` (underlying Rust `async`/`await`) are composable. This makes it possible to `select` in between `Futures` that have completed. +Rust [`Future`]s (underlying Rust `async`/`await`) are composable. This makes it possible to `select` in between `Futures` that have completed. -A common use case is transactions with associated timeout. In the examples shown below, we introduce a fake HAL device which performs some transaction. We have modelled the time it takes based on the input parameter (`n`) as `350ms + n * 100ms)`. +[`Future`]: https://doc.rust-lang.org/std/future/trait.Future.html + +A common use case is transactions with an associated timeout. In the examples shown below, we introduce a fake HAL device that performs some transaction. We have modelled the time it takes based on the input parameter (`n`) as `350ms + n * 100ms`. Using the `select_biased` macro from the `futures` crate it may look like this: @@ -62,19 +71,13 @@ select_biased! { } ``` -Assuming the `hal_get` will take 450ms to finish, a short timeout of 200ms will expire. +Assuming the `hal_get` will take 450ms to finish, a short timeout of 200ms will expire before `hal_get` can complete. -``` rust -// Call hal with long relative timeout using `select_biased` -select_biased! { - v = hal_get(1).fuse() => hprintln!("hal returned {}", v), // hal finish first - _ = Systick::delay(1000.millis()).fuse() => hprintln!("timeout", ), -} -``` +Extending the timeout to 1000ms would cause `hal_get` will to complete first. -By extending the timeout to 1000ms, the `hal_get` will finish first. +Using `select_biased` any number of futures can be combined, so its very powerful. However, as the timeout pattern is frequently used, more ergonomic support is baked into RTIC, provided by the [`rtic-monotonics`] and [`rtic-time`] crates. -Using `select_biased` any number of futures can be combined, so its very powerful. However, as the timeout pattern is frequently used, it is directly supported by the RTIC [rtc-monotonics] and [rtic-time] crates. The second example from above using `timeout_after`: +Rewriting the second example from above using `timeout_after` gives: ``` rust // Call hal with long relative timeout using monotonic `timeout_after` @@ -84,7 +87,7 @@ match Systick::timeout_after(1000.millis(), hal_get(1)).await { } ``` -In cases you want exact control over time without drift. For this purpose we can use exact points in time using `Instance`, and spans of time using `Duration`. Operations on the `Instance` and `Duration` types are given by the [fugit] crate. +In cases where you want exact control over time without drift we can use exact points in time using `Instant`, and spans of time using `Duration`. Operations on the `Instant` and `Duration` types come from the [`fugit`] crate. [fugit]: https://crates.io/crates/fugit @@ -109,10 +112,20 @@ for n in 0..3 { } ``` -`instant = Systick::now()` gives the baseline (i.e., the absolute current point in time). We want to call `hal_get` after 1000ms relative to this absolute point in time. This can be accomplished by `Systick::delay_until(instant).await;`. We define the absolute point in time for the `timeout`, and call `Systick::timeout_at(timeout, hal_get(n)).await`. For the first loop iteration `n == 0`, and the `hal_get` will take 350ms (and finishes before the timeout). For the second iteration `n == 1`, and `hal_get` will take 450ms (and again succeeds to finish before the timeout). For the third iteration `n == 2` (`hal_get` will take 5500ms to finish). In this case we will run into a timeout. +`let mut instant = Systick::now()` sets the starting time of execution. + +We want to call `hal_get` after 1000ms relative to this starting time. This can be accomplished by using `Systick::delay_until(instant).await`. + +Then, we define a point in time called `timeout`, and call `Systick::timeout_at(timeout, hal_get(n)).await`. + +For the first iteration of the loop, with `n == 0`, the `hal_get` will take 350ms (and finishes before the timeout). + +For the second iteration, with `n == 1`, the `hal_get` will take 450ms (and again succeeds to finish before the timeout). +For the third iteration, with `n == 2`, `hal_get` will take 550ms to finish, in which case we will run into a timeout. -The complete example: +
+A complete example ``` rust {{#include ../../../../rtic/examples/async-timeout.rs}} @@ -125,3 +138,4 @@ $ cargo run --target thumbv7m-none-eabi --example async-timeout --features test- ``` console {{#include ../../../../rtic/ci/expected/async-timeout.run}} ``` +
-- cgit v1.2.3 From 552ecd4458e24369932bc4a3dd641c4c4e3e59fc Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 22 Apr 2023 22:53:45 +0200 Subject: Promote starting a new project to it's own chapter --- book/en/src/by-example/starting_a_project.md | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 book/en/src/by-example/starting_a_project.md (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/starting_a_project.md b/book/en/src/by-example/starting_a_project.md deleted file mode 100644 index ae979f4..0000000 --- a/book/en/src/by-example/starting_a_project.md +++ /dev/null @@ -1,17 +0,0 @@ -# Starting a new project - -A recommendation when starting a RTIC project from scratch is to -follow RTIC's [`defmt-app-template`]. - -If you are targeting ARMv6-M or ARMv8-M-base architecture, check out the section [Target Architecture](../internals/targets.md) for more information on hardware limitations to be aware of. - -[`defmt-app-template`]: https://github.com/rtic-rs/defmt-app-template - -This will give you an RTIC application with support for RTT logging with [`defmt`] and stack overflow -protection using [`flip-link`]. There is also a multitude of examples provided by the community: - -For inspiration, you may look at the [rtic examples]. - -[`defmt`]: https://github.com/knurling-rs/defmt/ -[`flip-link`]: https://github.com/knurling-rs/flip-link/ -[rtic examples]: https://github.com/rtic-rs/rtic/tree/master/examples -- cgit v1.2.3 From a76f4cd85fd8eef258c6bf1b54211a96e626931e Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 22 Apr 2023 22:57:16 +0200 Subject: monotonic.md is now deprecated --- book/en/src/by-example/monotonic.md | 64 ------------------------------------- 1 file changed, 64 deletions(-) delete mode 100644 book/en/src/by-example/monotonic.md (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/monotonic.md b/book/en/src/by-example/monotonic.md deleted file mode 100644 index 3a23681..0000000 --- a/book/en/src/by-example/monotonic.md +++ /dev/null @@ -1,64 +0,0 @@ -# Monotonic & spawn_{at/after} - -The understanding of time is an important concept in embedded systems, and to be able to run tasks -based on time is essential. The framework provides the static methods -`task::spawn_after(/* duration */)` and `task::spawn_at(/* specific time instant */)`. -`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. - -The `#[monotonic]` attribute, applied to a type alias definition, exists to support this. -This type alias must point to a type which implements the [`rtic_monotonic::Monotonic`] trait. -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 -system from sleep and another which purpose is for fine grained scheduling while the -system is awake. - -[`rtic_monotonic::Monotonic`]: https://docs.rs/rtic-monotonic - -The attribute has one required parameter and two optional parameters, `binds`, `default` and -`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. - -> 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. - -The monotonics are initialized in `#[init]` and returned within the `init::Monotonic( ... )` tuple. -This activates the monotonics making it possible to use them. - -See the following example: - -``` rust -{{#include ../../../../examples/schedule.rs}} -``` - -``` console -$ cargo run --target thumbv7m-none-eabi --example schedule -{{#include ../../../../ci/expected/schedule.run}} -``` - -A key requirement of a Monotonic is that it must deal gracefully with -hardware timer overruns. - -## Canceling or rescheduling a scheduled task - -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. - -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: - -``` rust -{{#include ../../../../examples/cancel-reschedule.rs}} -``` - -``` console -$ cargo run --target thumbv7m-none-eabi --example cancel-reschedule -{{#include ../../../../ci/expected/cancel-reschedule.run}} -``` -- cgit v1.2.3 From cb0ceea472f33ed7a8b17fe7e0b98f24927d9185 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 23 Apr 2023 13:17:39 +0200 Subject: Remove v1 reference here --- book/en/src/by-example/tips_monotonic_impl.md | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/tips_monotonic_impl.md b/book/en/src/by-example/tips_monotonic_impl.md index 5dc4dd7..9f88c19 100644 --- a/book/en/src/by-example/tips_monotonic_impl.md +++ b/book/en/src/by-example/tips_monotonic_impl.md @@ -4,7 +4,7 @@ The framework is flexible because it can use any timer which has compare-match a For RTIC 1.0 and 2.0 we instead assume the user has a time library, e.g. [`fugit`], as the basis for all time-based operations when implementing `Monotonic`. These libraries make it much easier to correctly implement the `Monotonic` trait, allowing the use of almost any timer in the system for scheduling. -The trait documents the requirements for each method. There are reference implementations available in [`rtic-monotonics`](https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics/src) that can be used for inspriation. +The trait documents the requirements for each method. There are reference implementations available in [`rtic-monotonics`] that can be used for inspriation. - [`Systick based`], runs at a fixed interrupt (tick) rate - with some overhead but simple and provides support for large time spans - [`RP2040 Timer`], a "proper" implementation with support for waiting for long periods without interrupts. Clearly demonstrates how to use the `TimerQueue` to handle scheduling. @@ -16,25 +16,10 @@ Contributing new implementations of `Monotonic` can be done in multiple ways: * Implement the trait behind a feature flag in [`rtic-monotonics`], and create a PR for them to be included in the main RTIC repository. This way, the implementations of are in-tree, and RTIC can guarantee their correctness, and can update them in the case of a new release. * Implement the changes in an external repository. - -# V1.0.x - -Here is a list of `Monotonic` implementations for RTIC 1.0: - -- [`STM32F411 series`], implemented for the 32-bit timers -- [`Nordic nRF52 series Timer`], implemented for the 32-bit timers -- [`Nordic nRF52 series RTC`], implemented for the RTCs -- [`DWT and Systick based`], a more efficient (tickless) implementation - requires both `SysTick` and `DWT`, supports both high resolution and large time spans - -If you know of more implementations feel free to add them to this list. - +[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics/ [`rtic_time::Monotonic`]: https://docs.rs/rtic_time/ [`fugit`]: https://docs.rs/fugit/ -[`STM32F411 series`]: https://github.com/kalkyl/f411-rtic/blob/a696fce7d6d19fda2356c37642c4d53547982cca/src/mono.rs -[`Nordic nRF52 series Timer`]: https://github.com/kalkyl/nrf-play/blob/47f4410d4e39374c18ff58dc17c25159085fb526/src/mono.rs -[`Nordic nRF52 series RTC`]: https://gist.github.com/korken89/fe94a475726414dd1bce031c76adc3dd [`Systick based`]: https://github.com/rtic-monotonics -[`DWT and Systick based`]: https://github.com/rtic-rs/dwt-systick-monotonic [`rtic-monotonics`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics [`RP2040 Timer`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/rp2040.rs [`nRF52 timers`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/nrf.rs \ No newline at end of file -- cgit v1.2.3 From e51146a98cc7c83ea574d13b4b5d8e7ceeeb004b Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 23 Apr 2023 13:22:35 +0200 Subject: Move tips into their own subdir --- book/en/src/by-example/tips.md | 3 -- book/en/src/by-example/tips/destructureing.md | 15 ++++++++ book/en/src/by-example/tips/from_ram.md | 45 +++++++++++++++++++++++ book/en/src/by-example/tips/index.md | 3 ++ book/en/src/by-example/tips/indirection.md | 26 ++++++++++++++ book/en/src/by-example/tips/monotonic_impl.md | 25 +++++++++++++ book/en/src/by-example/tips/static_lifetimes.md | 23 ++++++++++++ book/en/src/by-example/tips/view_code.md | 47 +++++++++++++++++++++++++ book/en/src/by-example/tips_destructureing.md | 15 -------- book/en/src/by-example/tips_from_ram.md | 45 ----------------------- book/en/src/by-example/tips_indirection.md | 26 -------------- book/en/src/by-example/tips_monotonic_impl.md | 25 ------------- book/en/src/by-example/tips_static_lifetimes.md | 23 ------------ book/en/src/by-example/tips_view_code.md | 47 ------------------------- 14 files changed, 184 insertions(+), 184 deletions(-) delete mode 100644 book/en/src/by-example/tips.md create mode 100644 book/en/src/by-example/tips/destructureing.md create mode 100644 book/en/src/by-example/tips/from_ram.md create mode 100644 book/en/src/by-example/tips/index.md create mode 100644 book/en/src/by-example/tips/indirection.md create mode 100644 book/en/src/by-example/tips/monotonic_impl.md create mode 100644 book/en/src/by-example/tips/static_lifetimes.md create mode 100644 book/en/src/by-example/tips/view_code.md delete mode 100644 book/en/src/by-example/tips_destructureing.md delete mode 100644 book/en/src/by-example/tips_from_ram.md delete mode 100644 book/en/src/by-example/tips_indirection.md delete mode 100644 book/en/src/by-example/tips_monotonic_impl.md delete mode 100644 book/en/src/by-example/tips_static_lifetimes.md delete mode 100644 book/en/src/by-example/tips_view_code.md (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/tips.md b/book/en/src/by-example/tips.md deleted file mode 100644 index 18d5991..0000000 --- a/book/en/src/by-example/tips.md +++ /dev/null @@ -1,3 +0,0 @@ -# Tips & tricks - -In this section we will explore common tips & tricks related to using RTIC. diff --git a/book/en/src/by-example/tips/destructureing.md b/book/en/src/by-example/tips/destructureing.md new file mode 100644 index 0000000..6e1d796 --- /dev/null +++ b/book/en/src/by-example/tips/destructureing.md @@ -0,0 +1,15 @@ +# Resource de-structure-ing + +Destructuring task resources might help readability if a task takes multiple +resources. Here are two examples on how to split up the resource struct: + +``` rust +{{#include ../../../../../rtic/examples/destructure.rs}} +``` + +``` console +$ cargo run --target thumbv7m-none-eabi --example destructure +``` +``` console +{{#include ../../../../../rtic/ci/expected/destructure.run}} +``` diff --git a/book/en/src/by-example/tips/from_ram.md b/book/en/src/by-example/tips/from_ram.md new file mode 100644 index 0000000..7306e12 --- /dev/null +++ b/book/en/src/by-example/tips/from_ram.md @@ -0,0 +1,45 @@ +# Running tasks from RAM + +The main goal of moving the specification of RTIC applications to attributes in RTIC v0.4.0 was to allow inter-operation with other attributes. For example, the `link_section` attribute can be applied to tasks to place them in RAM; this can +improve performance in some cases. + +> **IMPORTANT**: In general, the `link_section`, `export_name` and `no_mangle` attributes are powerful but also easy to misuse. Incorrectly using any of these attributes can cause undefined behavior; you should always prefer to use safe, higher level attributes around them like `cortex-m-rt`'s `interrupt` and `exception` attributes. +> +> In the particular case of RAM functions there's no safe abstraction for it in `cortex-m-rt` v0.6.5 but there's an [RFC] for adding a `ramfunc` attribute in a future release. + +[RFC]: https://github.com/rust-embedded/cortex-m-rt/pull/100 + +The example below shows how to place the higher priority task, `bar`, in RAM. + +``` rust +{{#include ../../../../../rtic/examples/ramfunc.rs}} +``` + +Running this program produces the expected output. + +``` console +$ cargo run --target thumbv7m-none-eabi --example ramfunc +``` + +``` console +{{#include ../../../../../rtic/ci/expected/ramfunc.run}} +``` + +One can look at the output of `cargo-nm` to confirm that `bar` ended in RAM +(`0x2000_0000`), whereas `foo` ended in Flash (`0x0000_0000`). + +``` console +$ cargo nm --example ramfunc --release | grep ' foo::' +``` + +``` console +{{#include ../../../../../rtic/ci/expected/ramfunc.run.grep.foo}} +``` + +``` console +$ cargo nm --example ramfunc --target thumbv7m-none-eabi --release | grep '*bar::' +``` + +``` console +{{#include ../../../../../rtic/ci/expected/ramfunc.run.grep.bar}} +``` diff --git a/book/en/src/by-example/tips/index.md b/book/en/src/by-example/tips/index.md new file mode 100644 index 0000000..18d5991 --- /dev/null +++ b/book/en/src/by-example/tips/index.md @@ -0,0 +1,3 @@ +# Tips & tricks + +In this section we will explore common tips & tricks related to using RTIC. diff --git a/book/en/src/by-example/tips/indirection.md b/book/en/src/by-example/tips/indirection.md new file mode 100644 index 0000000..eef0d8e --- /dev/null +++ b/book/en/src/by-example/tips/indirection.md @@ -0,0 +1,26 @@ +# Using indirection for faster message passing + +Message passing always involves copying the payload from the sender into a static variable and then from the static variable into the receiver. Thus sending a large buffer, like a `[u8; 128]`, as a message involves two expensive +`memcpy`s. + +Indirection can minimize message passing overhead: instead of sending the buffer by value, one can send an owning pointer into the buffer. + +One can use a global memory allocator to achieve indirection (`alloc::Box`, `alloc::Rc`, etc.), which requires using the nightly channel as of Rust v1.37.0, or one can use a statically allocated memory pool like [`heapless::Pool`]. + +[`heapless::Pool`]: https://docs.rs/heapless/0.5.0/heapless/pool/index.html + +As this example of approach goes completely outside of RTIC resource model with shared and local the program would rely on the correctness of the memory allocator, in this case `heapless::pool`. + +Here's an example where `heapless::Pool` is used to "box" buffers of 128 bytes. + +``` rust +{{#include ../../../../../rtic/examples/pool.rs}} +``` + +``` console +$ cargo run --target thumbv7m-none-eabi --example pool +``` + +``` console +{{#include ../../../../../rtic/ci/expected/pool.run}} +``` diff --git a/book/en/src/by-example/tips/monotonic_impl.md b/book/en/src/by-example/tips/monotonic_impl.md new file mode 100644 index 0000000..9f88c19 --- /dev/null +++ b/book/en/src/by-example/tips/monotonic_impl.md @@ -0,0 +1,25 @@ +# Implementing a `Monotonic` timer for scheduling + +The framework is flexible because it can use any timer which has compare-match and optionally supporting overflow interrupts for scheduling. The single requirement to make a timer usable with RTIC is implementing the `rtic-time::Monotonic` trait. + +For RTIC 1.0 and 2.0 we instead assume the user has a time library, e.g. [`fugit`], as the basis for all time-based operations when implementing `Monotonic`. These libraries make it much easier to correctly implement the `Monotonic` trait, allowing the use of almost any timer in the system for scheduling. + +The trait documents the requirements for each method. There are reference implementations available in [`rtic-monotonics`] that can be used for inspriation. + +- [`Systick based`], runs at a fixed interrupt (tick) rate - with some overhead but simple and provides support for large time spans +- [`RP2040 Timer`], a "proper" implementation with support for waiting for long periods without interrupts. Clearly demonstrates how to use the `TimerQueue` to handle scheduling. +- [`nRF52 timers`] implements monotonic & Timer Queue for the RTC and normal timers in nRF52's + +## Contributing + +Contributing new implementations of `Monotonic` can be done in multiple ways: +* Implement the trait behind a feature flag in [`rtic-monotonics`], and create a PR for them to be included in the main RTIC repository. This way, the implementations of are in-tree, and RTIC can guarantee their correctness, and can update them in the case of a new release. +* Implement the changes in an external repository. + +[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics/ +[`rtic_time::Monotonic`]: https://docs.rs/rtic_time/ +[`fugit`]: https://docs.rs/fugit/ +[`Systick based`]: https://github.com/rtic-monotonics +[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics +[`RP2040 Timer`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/rp2040.rs +[`nRF52 timers`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/nrf.rs \ No newline at end of file diff --git a/book/en/src/by-example/tips/static_lifetimes.md b/book/en/src/by-example/tips/static_lifetimes.md new file mode 100644 index 0000000..b1fd606 --- /dev/null +++ b/book/en/src/by-example/tips/static_lifetimes.md @@ -0,0 +1,23 @@ +# 'static super-powers + +In `#[init]` and `#[idle]` `local` resources have `'static` lifetime. + +Useful when pre-allocating and/or splitting resources between tasks, drivers or some other object. This comes in handy when drivers, such as USB drivers, need to allocate memory and when using splittable data structures such as [`heapless::spsc::Queue`]. + +In the following example two different tasks share a [`heapless::spsc::Queue`] for lock-free access to the shared queue. + +[`heapless::spsc::Queue`]: https://docs.rs/heapless/0.7.5/heapless/spsc/struct.Queue.html + +``` rust +{{#include ../../../../../rtic/examples/static.rs}} +``` + +Running this program produces the expected output. + +``` console +$ cargo run --target thumbv7m-none-eabi --example static +``` + +``` console +{{#include ../../../../../rtic/ci/expected/static.run}} +``` diff --git a/book/en/src/by-example/tips/view_code.md b/book/en/src/by-example/tips/view_code.md new file mode 100644 index 0000000..b4a9066 --- /dev/null +++ b/book/en/src/by-example/tips/view_code.md @@ -0,0 +1,47 @@ +# Inspecting generated code + +`#[rtic::app]` is a procedural macro that produces support code. If for some reason you need to inspect the code generated by this macro you have two options: + +You can inspect the file `rtic-expansion.rs` inside the `target` directory. This file contains the expansion of the `#[rtic::app]` item (not your whole program!) of the *last built* (via `cargo build` or `cargo check`) RTIC application. The expanded code is not pretty printed by default, so you'll want to run `rustfmt` on it before you read it. + +``` console +$ cargo build --example smallest --target thumbv7m-none-eabi +``` + +``` console +$ rustfmt target/rtic-expansion.rs +``` + +``` console +$ tail target/rtic-expansion.rs +``` + +``` rust +#[doc = r" Implementation details"] +mod app { + #[doc = r" Always include the device crate which contains the vector table"] + use lm3s6965 as _; + #[no_mangle] + unsafe extern "C" fn main() -> ! { + rtic::export::interrupt::disable(); + let mut core: rtic::export::Peripherals = core::mem::transmute(()); + core.SCB.scr.modify(|r| r | 1 << 1); + rtic::export::interrupt::enable(); + loop { + rtic::export::wfi() + } + } +} +``` + +Or, you can use the [`cargo-expand`] sub-command. This sub-command will expand *all* the macros, including the `#[rtic::app]` attribute, and modules in your crate and print the output to the console. + +[`cargo-expand`]: https://crates.io/crates/cargo-expand + +``` console +# produces the same output as before +``` + +``` console +cargo expand --example smallest | tail +``` diff --git a/book/en/src/by-example/tips_destructureing.md b/book/en/src/by-example/tips_destructureing.md deleted file mode 100644 index ab27987..0000000 --- a/book/en/src/by-example/tips_destructureing.md +++ /dev/null @@ -1,15 +0,0 @@ -# Resource de-structure-ing - -Destructuring task resources might help readability if a task takes multiple -resources. Here are two examples on how to split up the resource struct: - -``` rust -{{#include ../../../../rtic/examples/destructure.rs}} -``` - -``` console -$ cargo run --target thumbv7m-none-eabi --example destructure -``` -``` console -{{#include ../../../../rtic/ci/expected/destructure.run}} -``` diff --git a/book/en/src/by-example/tips_from_ram.md b/book/en/src/by-example/tips_from_ram.md deleted file mode 100644 index f6b2173..0000000 --- a/book/en/src/by-example/tips_from_ram.md +++ /dev/null @@ -1,45 +0,0 @@ -# Running tasks from RAM - -The main goal of moving the specification of RTIC applications to attributes in RTIC v0.4.0 was to allow inter-operation with other attributes. For example, the `link_section` attribute can be applied to tasks to place them in RAM; this can -improve performance in some cases. - -> **IMPORTANT**: In general, the `link_section`, `export_name` and `no_mangle` attributes are powerful but also easy to misuse. Incorrectly using any of these attributes can cause undefined behavior; you should always prefer to use safe, higher level attributes around them like `cortex-m-rt`'s `interrupt` and `exception` attributes. -> -> In the particular case of RAM functions there's no safe abstraction for it in `cortex-m-rt` v0.6.5 but there's an [RFC] for adding a `ramfunc` attribute in a future release. - -[RFC]: https://github.com/rust-embedded/cortex-m-rt/pull/100 - -The example below shows how to place the higher priority task, `bar`, in RAM. - -``` rust -{{#include ../../../../rtic/examples/ramfunc.rs}} -``` - -Running this program produces the expected output. - -``` console -$ cargo run --target thumbv7m-none-eabi --example ramfunc -``` - -``` console -{{#include ../../../../rtic/ci/expected/ramfunc.run}} -``` - -One can look at the output of `cargo-nm` to confirm that `bar` ended in RAM -(`0x2000_0000`), whereas `foo` ended in Flash (`0x0000_0000`). - -``` console -$ cargo nm --example ramfunc --release | grep ' foo::' -``` - -``` console -{{#include ../../../../rtic/ci/expected/ramfunc.run.grep.foo}} -``` - -``` console -$ cargo nm --example ramfunc --target thumbv7m-none-eabi --release | grep '*bar::' -``` - -``` console -{{#include ../../../../rtic/ci/expected/ramfunc.run.grep.bar}} -``` diff --git a/book/en/src/by-example/tips_indirection.md b/book/en/src/by-example/tips_indirection.md deleted file mode 100644 index 0de14a6..0000000 --- a/book/en/src/by-example/tips_indirection.md +++ /dev/null @@ -1,26 +0,0 @@ -# Using indirection for faster message passing - -Message passing always involves copying the payload from the sender into a static variable and then from the static variable into the receiver. Thus sending a large buffer, like a `[u8; 128]`, as a message involves two expensive -`memcpy`s. - -Indirection can minimize message passing overhead: instead of sending the buffer by value, one can send an owning pointer into the buffer. - -One can use a global memory allocator to achieve indirection (`alloc::Box`, `alloc::Rc`, etc.), which requires using the nightly channel as of Rust v1.37.0, or one can use a statically allocated memory pool like [`heapless::Pool`]. - -[`heapless::Pool`]: https://docs.rs/heapless/0.5.0/heapless/pool/index.html - -As this example of approach goes completely outside of RTIC resource model with shared and local the program would rely on the correctness of the memory allocator, in this case `heapless::pool`. - -Here's an example where `heapless::Pool` is used to "box" buffers of 128 bytes. - -``` rust -{{#include ../../../../rtic/examples/pool.rs}} -``` - -``` console -$ cargo run --target thumbv7m-none-eabi --example pool -``` - -``` console -{{#include ../../../../rtic/ci/expected/pool.run}} -``` diff --git a/book/en/src/by-example/tips_monotonic_impl.md b/book/en/src/by-example/tips_monotonic_impl.md deleted file mode 100644 index 9f88c19..0000000 --- a/book/en/src/by-example/tips_monotonic_impl.md +++ /dev/null @@ -1,25 +0,0 @@ -# Implementing a `Monotonic` timer for scheduling - -The framework is flexible because it can use any timer which has compare-match and optionally supporting overflow interrupts for scheduling. The single requirement to make a timer usable with RTIC is implementing the `rtic-time::Monotonic` trait. - -For RTIC 1.0 and 2.0 we instead assume the user has a time library, e.g. [`fugit`], as the basis for all time-based operations when implementing `Monotonic`. These libraries make it much easier to correctly implement the `Monotonic` trait, allowing the use of almost any timer in the system for scheduling. - -The trait documents the requirements for each method. There are reference implementations available in [`rtic-monotonics`] that can be used for inspriation. - -- [`Systick based`], runs at a fixed interrupt (tick) rate - with some overhead but simple and provides support for large time spans -- [`RP2040 Timer`], a "proper" implementation with support for waiting for long periods without interrupts. Clearly demonstrates how to use the `TimerQueue` to handle scheduling. -- [`nRF52 timers`] implements monotonic & Timer Queue for the RTC and normal timers in nRF52's - -## Contributing - -Contributing new implementations of `Monotonic` can be done in multiple ways: -* Implement the trait behind a feature flag in [`rtic-monotonics`], and create a PR for them to be included in the main RTIC repository. This way, the implementations of are in-tree, and RTIC can guarantee their correctness, and can update them in the case of a new release. -* Implement the changes in an external repository. - -[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics/ -[`rtic_time::Monotonic`]: https://docs.rs/rtic_time/ -[`fugit`]: https://docs.rs/fugit/ -[`Systick based`]: https://github.com/rtic-monotonics -[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics -[`RP2040 Timer`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/rp2040.rs -[`nRF52 timers`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/nrf.rs \ No newline at end of file diff --git a/book/en/src/by-example/tips_static_lifetimes.md b/book/en/src/by-example/tips_static_lifetimes.md deleted file mode 100644 index 0eaa59f..0000000 --- a/book/en/src/by-example/tips_static_lifetimes.md +++ /dev/null @@ -1,23 +0,0 @@ -# 'static super-powers - -In `#[init]` and `#[idle]` `local` resources have `'static` lifetime. - -Useful when pre-allocating and/or splitting resources between tasks, drivers or some other object. This comes in handy when drivers, such as USB drivers, need to allocate memory and when using splittable data structures such as [`heapless::spsc::Queue`]. - -In the following example two different tasks share a [`heapless::spsc::Queue`] for lock-free access to the shared queue. - -[`heapless::spsc::Queue`]: https://docs.rs/heapless/0.7.5/heapless/spsc/struct.Queue.html - -``` rust -{{#include ../../../../rtic/examples/static.rs}} -``` - -Running this program produces the expected output. - -``` console -$ cargo run --target thumbv7m-none-eabi --example static -``` - -``` console -{{#include ../../../../rtic/ci/expected/static.run}} -``` diff --git a/book/en/src/by-example/tips_view_code.md b/book/en/src/by-example/tips_view_code.md deleted file mode 100644 index b4a9066..0000000 --- a/book/en/src/by-example/tips_view_code.md +++ /dev/null @@ -1,47 +0,0 @@ -# Inspecting generated code - -`#[rtic::app]` is a procedural macro that produces support code. If for some reason you need to inspect the code generated by this macro you have two options: - -You can inspect the file `rtic-expansion.rs` inside the `target` directory. This file contains the expansion of the `#[rtic::app]` item (not your whole program!) of the *last built* (via `cargo build` or `cargo check`) RTIC application. The expanded code is not pretty printed by default, so you'll want to run `rustfmt` on it before you read it. - -``` console -$ cargo build --example smallest --target thumbv7m-none-eabi -``` - -``` console -$ rustfmt target/rtic-expansion.rs -``` - -``` console -$ tail target/rtic-expansion.rs -``` - -``` rust -#[doc = r" Implementation details"] -mod app { - #[doc = r" Always include the device crate which contains the vector table"] - use lm3s6965 as _; - #[no_mangle] - unsafe extern "C" fn main() -> ! { - rtic::export::interrupt::disable(); - let mut core: rtic::export::Peripherals = core::mem::transmute(()); - core.SCB.scr.modify(|r| r | 1 << 1); - rtic::export::interrupt::enable(); - loop { - rtic::export::wfi() - } - } -} -``` - -Or, you can use the [`cargo-expand`] sub-command. This sub-command will expand *all* the macros, including the `#[rtic::app]` attribute, and modules in your crate and print the output to the console. - -[`cargo-expand`]: https://crates.io/crates/cargo-expand - -``` console -# produces the same output as before -``` - -``` console -cargo expand --example smallest | tail -``` -- cgit v1.2.3 From 0807aa548c865d12746ce17aa1c17f7968b139a9 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 23 Apr 2023 15:33:29 +0200 Subject: Include this code as blocks instead --- book/en/src/by-example/delay.md | 44 ++++++++--------------------------------- 1 file changed, 8 insertions(+), 36 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/delay.md b/book/en/src/by-example/delay.md index 893ead9..479fd42 100644 --- a/book/en/src/by-example/delay.md +++ b/book/en/src/by-example/delay.md @@ -9,13 +9,8 @@ This can be achieved by instantiating a monotonic timer (for implementations, se ``` rust ... -#[init] -fn init(cx: init::Context) -> (Shared, Local) { - hprintln!("init"); - - let token = rtic_monotonics::create_systick_token!(); - Systick::start(cx.core.SYST, 12_000_000, token); - ... +{{#include ../../../../rtic/examples/async-timeout.rs:init}} + ... ``` A *software* task can `await` the delay to expire: @@ -63,12 +58,8 @@ A common use case is transactions with an associated timeout. In the examples sh Using the `select_biased` macro from the `futures` crate it may look like this: -``` rust -// Call hal with short relative timeout using `select_biased` -select_biased! { - v = hal_get(1).fuse() => hprintln!("hal returned {}", v), - _ = Systick::delay(200.millis()).fuse() => hprintln!("timeout", ), // this will finish first -} +``` rust,noplayground +{{#include ../../../../rtic/examples/async-timeout.rs:select_biased}} ``` Assuming the `hal_get` will take 450ms to finish, a short timeout of 200ms will expire before `hal_get` can complete. @@ -80,11 +71,7 @@ Using `select_biased` any number of futures can be combined, so its very powerfu Rewriting the second example from above using `timeout_after` gives: ``` rust -// Call hal with long relative timeout using monotonic `timeout_after` -match Systick::timeout_after(1000.millis(), hal_get(1)).await { - Ok(v) => hprintln!("hal returned {}", v), - _ => hprintln!("timeout"), -} +{{#include ../../../../rtic/examples/async-timeout.rs:timeout_at_basic}} ``` In cases where you want exact control over time without drift we can use exact points in time using `Instant`, and spans of time using `Duration`. Operations on the `Instant` and `Duration` types come from the [`fugit`] crate. @@ -92,24 +79,9 @@ In cases where you want exact control over time without drift we can use exact p [fugit]: https://crates.io/crates/fugit ``` rust -// get the current time instance -let mut instant = Systick::now(); - -// do this 3 times -for n in 0..3 { - // absolute point in time without drift - instant += 1000.millis(); - Systick::delay_until(instant).await; - - // absolute point it time for timeout - let timeout = instant + 500.millis(); - hprintln!("now is {:?}, timeout at {:?}", Systick::now(), timeout); - - match Systick::timeout_at(timeout, hal_get(n)).await { - Ok(v) => hprintln!("hal returned {} at time {:?}", v, Systick::now()), - _ => hprintln!("timeout"), - } -} + +{{#include ../../../../rtic/examples/async-timeout.rs:timeout_at}} + ``` `let mut instant = Systick::now()` sets the starting time of execution. -- cgit v1.2.3 From a66540efa014b3716d252612bfc7f8f17ed765c4 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sun, 23 Apr 2023 15:33:56 +0200 Subject: Disable the playground on all of these --- book/en/src/by-example/app.md | 2 +- book/en/src/by-example/app_idle.md | 4 ++-- book/en/src/by-example/app_init.md | 2 +- book/en/src/by-example/app_minimal.md | 2 +- book/en/src/by-example/app_priorities.md | 2 +- book/en/src/by-example/channel.md | 16 ++++++++-------- book/en/src/by-example/delay.md | 14 +++++++------- book/en/src/by-example/hardware_tasks.md | 2 +- book/en/src/by-example/message_passing.md | 2 +- book/en/src/by-example/resources.md | 12 ++++++------ book/en/src/by-example/software_tasks.md | 10 +++++----- book/en/src/by-example/tips/destructureing.md | 2 +- book/en/src/by-example/tips/from_ram.md | 2 +- book/en/src/by-example/tips/indirection.md | 2 +- book/en/src/by-example/tips/static_lifetimes.md | 2 +- book/en/src/by-example/tips/view_code.md | 2 +- 16 files changed, 39 insertions(+), 39 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/app.md b/book/en/src/by-example/app.md index 0d977a1..8450bb9 100644 --- a/book/en/src/by-example/app.md +++ b/book/en/src/by-example/app.md @@ -27,6 +27,6 @@ Overall, the generated code infers no additional overhead in comparison to a han To give a flavour of RTIC, the following example contains commonly used features. In the following sections we will go through each feature in detail. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/common.rs}} ``` diff --git a/book/en/src/by-example/app_idle.md b/book/en/src/by-example/app_idle.md index cbfd7ba..c0b4139 100644 --- a/book/en/src/by-example/app_idle.md +++ b/book/en/src/by-example/app_idle.md @@ -11,7 +11,7 @@ Like in `init`, locally declared resources will have `'static` lifetimes that ar The example below shows that `idle` runs after `init`. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/idle.rs}} ``` @@ -38,7 +38,7 @@ The following example shows how to enable sleep by setting the [WFI]: https://developer.arm.com/documentation/dui0662/b/The-Cortex-M0--Instruction-Set/Miscellaneous-instructions/WFI [NOP]: https://developer.arm.com/documentation/dui0662/b/The-Cortex-M0--Instruction-Set/Miscellaneous-instructions/NOP -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/idle-wfi.rs}} ``` diff --git a/book/en/src/by-example/app_init.md b/book/en/src/by-example/app_init.md index fb37387..e581ef5 100644 --- a/book/en/src/by-example/app_init.md +++ b/book/en/src/by-example/app_init.md @@ -16,7 +16,7 @@ The example below shows the types of the `core`, `device` and `cs` fields, and s The `device` field is only available when the `peripherals` argument is set to the default value `true`. In the rare case you want to implement an ultra-slim application you can explicitly set `peripherals` to `false`. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/init.rs}} ``` diff --git a/book/en/src/by-example/app_minimal.md b/book/en/src/by-example/app_minimal.md index 714f543..2c6f218 100644 --- a/book/en/src/by-example/app_minimal.md +++ b/book/en/src/by-example/app_minimal.md @@ -2,7 +2,7 @@ This is the smallest possible RTIC application: -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/smallest.rs}} ``` diff --git a/book/en/src/by-example/app_priorities.md b/book/en/src/by-example/app_priorities.md index 9d27658..86ff985 100644 --- a/book/en/src/by-example/app_priorities.md +++ b/book/en/src/by-example/app_priorities.md @@ -33,7 +33,7 @@ Task Priority The following example showcases the priority based scheduling of tasks: -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/preempt.rs}} ``` diff --git a/book/en/src/by-example/channel.md b/book/en/src/by-example/channel.md index 50c3278..75ecbfd 100644 --- a/book/en/src/by-example/channel.md +++ b/book/en/src/by-example/channel.md @@ -2,7 +2,7 @@ Channels can be used to communicate data between running tasks. The channel is essentially a wait queue, allowing tasks with multiple producers and a single receiver. A channel is constructed in the `init` task and backed by statically allocated memory. Send and receive endpoints are distributed to *software* tasks: -``` rust +``` rust,noplayground ... const CAPACITY: usize = 5; #[init] @@ -22,7 +22,7 @@ Channels can also be used from *hardware* tasks, but only in a non-`async` manne The `send` method post a message on the channel as shown below: -``` rust +``` rust,noplayground #[task] async fn sender1(_c: sender1::Context, mut sender: Sender<'static, u32, CAPACITY>) { hprintln!("Sender 1 sending: 1"); @@ -34,7 +34,7 @@ async fn sender1(_c: sender1::Context, mut sender: Sender<'static, u32, CAPACITY The receiver can `await` incoming messages: -``` rust +``` rust,noplayground #[task] async fn receiver(_c: receiver::Context, mut receiver: Receiver<'static, u32, CAPACITY>) { while let Ok(val) = receiver.recv().await { @@ -48,7 +48,7 @@ Channels are implemented using a small (global) *Critical Section* (CS) for prot For a complete example: -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-channel.rs}} ``` @@ -64,7 +64,7 @@ Also sender endpoint can be awaited. In case the channel capacity has not yet be In the following example the `CAPACITY` has been reduced to 1, forcing sender tasks to wait until the data in the channel has been received. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-channel-done.rs}} ``` @@ -81,7 +81,7 @@ $ cargo run --target thumbv7m-none-eabi --example async-channel-done --features In case all senders have been dropped `await`-ing on an empty receiver channel results in an error. This allows to gracefully implement different types of shutdown operations. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-channel-no-sender.rs}} ``` @@ -97,7 +97,7 @@ Similarly, `await`-ing on a send channel results in an error in case the receive The resulting error returns the data back to the sender, allowing the sender to take appropriate action (e.g., storing the data to later retry sending it). -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-channel-no-receiver.rs}} ``` @@ -115,7 +115,7 @@ Using the Try API, you can send or receive data from or to a channel without req This API is exposed through `Receiver::try_recv` and `Sender::try_send`. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-channel-try.rs}} ``` diff --git a/book/en/src/by-example/delay.md b/book/en/src/by-example/delay.md index 479fd42..a6ad0e0 100644 --- a/book/en/src/by-example/delay.md +++ b/book/en/src/by-example/delay.md @@ -7,7 +7,7 @@ This can be achieved by instantiating a monotonic timer (for implementations, se [`rtic-monotonics`]: https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics [`rtic-time`]: https://github.com/rtic-rs/rtic/tree/master/rtic-time -``` rust +``` rust,noplayground ... {{#include ../../../../rtic/examples/async-timeout.rs:init}} ... @@ -15,7 +15,7 @@ This can be achieved by instantiating a monotonic timer (for implementations, se A *software* task can `await` the delay to expire: -``` rust +``` rust,noplayground #[task] async fn foo(_cx: foo::Context) { ... @@ -34,7 +34,7 @@ Similarly the channels implementation, the timer-queue implementation relies on
A complete example -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-delay.rs}} ``` @@ -58,7 +58,7 @@ A common use case is transactions with an associated timeout. In the examples sh Using the `select_biased` macro from the `futures` crate it may look like this: -``` rust,noplayground +``` rust,noplayground,noplayground {{#include ../../../../rtic/examples/async-timeout.rs:select_biased}} ``` @@ -70,7 +70,7 @@ Using `select_biased` any number of futures can be combined, so its very powerfu Rewriting the second example from above using `timeout_after` gives: -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-timeout.rs:timeout_at_basic}} ``` @@ -78,7 +78,7 @@ In cases where you want exact control over time without drift we can use exact p [fugit]: https://crates.io/crates/fugit -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-timeout.rs:timeout_at}} @@ -99,7 +99,7 @@ For the third iteration, with `n == 2`, `hal_get` will take 550ms to finish, in
A complete example -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/async-timeout.rs}} ``` diff --git a/book/en/src/by-example/hardware_tasks.md b/book/en/src/by-example/hardware_tasks.md index 75dd1a4..bf00dc4 100644 --- a/book/en/src/by-example/hardware_tasks.md +++ b/book/en/src/by-example/hardware_tasks.md @@ -19,7 +19,7 @@ Beware of using interrupt vectors that are used internally by hardware features; The example below demonstrates the use of the `#[task(binds = InterruptName)]` attribute to declare a hardware task bound to an interrupt handler. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/hardware.rs}} ``` diff --git a/book/en/src/by-example/message_passing.md b/book/en/src/by-example/message_passing.md index 5665c36..02fd298 100644 --- a/book/en/src/by-example/message_passing.md +++ b/book/en/src/by-example/message_passing.md @@ -10,7 +10,7 @@ pending spawns of `foo`. Exceeding this capacity is an `Error`. The number of arguments to a task is not limited: -``` rust +``` rust,noplayground {{#include ../../../../examples/message_passing.rs}} ``` diff --git a/book/en/src/by-example/resources.md b/book/en/src/by-example/resources.md index 0bf5d11..c2472bc 100644 --- a/book/en/src/by-example/resources.md +++ b/book/en/src/by-example/resources.md @@ -25,7 +25,7 @@ Types of `#[local]` resources must implement a [`Send`] trait as they are being The example application shown below contains three tasks `foo`, `bar` and `idle`, each having access to its own `#[local]` resource. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/locals.rs}} ``` @@ -51,7 +51,7 @@ Types of `#[task(local = [..])]` resources have to be neither [`Send`] nor [`Syn In the example below the different uses and lifetimes are shown: -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/declared_locals.rs}} ``` @@ -76,7 +76,7 @@ The critical section created by the `lock` API is based on dynamic priorities: i In the example below we have three interrupt handlers with priorities ranging from one to three. The two handlers with the lower priorities contend for a `shared` resource and need to succeed in locking the resource in order to access its data. The highest priority handler, which does not access the `shared` resource, is free to preempt a critical section created by the lowest priority handler. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/lock.rs}} ``` @@ -94,7 +94,7 @@ Types of `#[shared]` resources have to be [`Send`]. As an extension to `lock`, and to reduce rightward drift, locks can be taken as tuples. The following examples show this in use: -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/multilock.rs}} ``` @@ -116,7 +116,7 @@ Note that in this release of RTIC it is not possible to request both exclusive a In the example below a key (e.g. a cryptographic key) is loaded (or created) at runtime (returned by `init`) and then used from two tasks that run at different priorities without any kind of lock. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/only-shared-access.rs}} ``` @@ -142,7 +142,7 @@ To adhere to the Rust [aliasing] rule, a resource may be either accessed through Using `#[lock_free]` on resources shared by tasks running at different priorities will result in a *compile-time* error -- not using the `lock` API would violate the aforementioned alias rule. Similarly, for each priority there can be only a single *software* task accessing a shared resource (as an `async` task may yield execution to other *software* or *hardware* tasks running at the same priority). However, under this single-task restriction, we make the observation that the resource is in effect no longer `shared` but rather `local`. Thus, using a `#[lock_free]` shared resource will result in a *compile-time* error -- where applicable, use a `#[local]` resource instead. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/lock-free.rs}} ``` diff --git a/book/en/src/by-example/software_tasks.md b/book/en/src/by-example/software_tasks.md index 0efc57b..ddf88fd 100644 --- a/book/en/src/by-example/software_tasks.md +++ b/book/en/src/by-example/software_tasks.md @@ -23,7 +23,7 @@ The framework will give a compilation error if there are not enough dispatchers See the following example: -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/spawn.rs}} ``` @@ -40,7 +40,7 @@ In the below example, we `spawn` the *software* task `foo` from the `idle` task. Technically the async executor will `poll` the `foo` *future* which in this case leaves the *future* in a *completed* state. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/spawn_loop.rs}} ``` @@ -56,7 +56,7 @@ An attempt to `spawn` an already spawned task (running) task will result in an e Technically, a `spawn` to a *future* that is not in *completed* state is considered an error. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/spawn_err.rs}} ``` @@ -71,7 +71,7 @@ $ cargo run --target thumbv7m-none-eabi --example spawn_err ## Passing arguments You can also pass arguments at spawn as follows. -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/spawn_arguments.rs}} ``` @@ -92,7 +92,7 @@ Conceptually, one can see such tasks as running in the `main` thread of the appl [Send]: https://doc.rust-lang.org/nomicon/send-and-sync.html -``` rust +``` rust,noplayground {{#include ../../../../rtic/examples/zero-prio-task.rs}} ``` diff --git a/book/en/src/by-example/tips/destructureing.md b/book/en/src/by-example/tips/destructureing.md index 6e1d796..752311d 100644 --- a/book/en/src/by-example/tips/destructureing.md +++ b/book/en/src/by-example/tips/destructureing.md @@ -3,7 +3,7 @@ Destructuring task resources might help readability if a task takes multiple resources. Here are two examples on how to split up the resource struct: -``` rust +``` rust,noplayground {{#include ../../../../../rtic/examples/destructure.rs}} ``` diff --git a/book/en/src/by-example/tips/from_ram.md b/book/en/src/by-example/tips/from_ram.md index 7306e12..a153139 100644 --- a/book/en/src/by-example/tips/from_ram.md +++ b/book/en/src/by-example/tips/from_ram.md @@ -11,7 +11,7 @@ improve performance in some cases. The example below shows how to place the higher priority task, `bar`, in RAM. -``` rust +``` rust,noplayground {{#include ../../../../../rtic/examples/ramfunc.rs}} ``` diff --git a/book/en/src/by-example/tips/indirection.md b/book/en/src/by-example/tips/indirection.md index eef0d8e..58b3bde 100644 --- a/book/en/src/by-example/tips/indirection.md +++ b/book/en/src/by-example/tips/indirection.md @@ -13,7 +13,7 @@ As this example of approach goes completely outside of RTIC resource model with Here's an example where `heapless::Pool` is used to "box" buffers of 128 bytes. -``` rust +``` rust,noplayground {{#include ../../../../../rtic/examples/pool.rs}} ``` diff --git a/book/en/src/by-example/tips/static_lifetimes.md b/book/en/src/by-example/tips/static_lifetimes.md index b1fd606..f4e4829 100644 --- a/book/en/src/by-example/tips/static_lifetimes.md +++ b/book/en/src/by-example/tips/static_lifetimes.md @@ -8,7 +8,7 @@ In the following example two different tasks share a [`heapless::spsc::Queue`] f [`heapless::spsc::Queue`]: https://docs.rs/heapless/0.7.5/heapless/spsc/struct.Queue.html -``` rust +``` rust,noplayground {{#include ../../../../../rtic/examples/static.rs}} ``` diff --git a/book/en/src/by-example/tips/view_code.md b/book/en/src/by-example/tips/view_code.md index b4a9066..64af7ad 100644 --- a/book/en/src/by-example/tips/view_code.md +++ b/book/en/src/by-example/tips/view_code.md @@ -16,7 +16,7 @@ $ rustfmt target/rtic-expansion.rs $ tail target/rtic-expansion.rs ``` -``` rust +``` rust,noplayground #[doc = r" Implementation details"] mod app { #[doc = r" Always include the device crate which contains the vector table"] -- cgit v1.2.3 From f2a57de5c172ee8f20ee449b593b05e18b0314b8 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Fri, 5 May 2023 19:06:34 +0200 Subject: taste the rainbow! --- book/en/src/by-example/app.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/app.md b/book/en/src/by-example/app.md index 8450bb9..fae13cb 100644 --- a/book/en/src/by-example/app.md +++ b/book/en/src/by-example/app.md @@ -24,7 +24,7 @@ Overall, the generated code infers no additional overhead in comparison to a han ## An RTIC application example -To give a flavour of RTIC, the following example contains commonly used features. +To give a taste of RTIC, the following example contains commonly used features. In the following sections we will go through each feature in detail. ``` rust,noplayground -- cgit v1.2.3 From 03b16a3a2d83af9b520fa5b5bad3ba76155f594d Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Fri, 5 May 2023 19:20:57 +0200 Subject: Archive app_task.md --- book/en/src/by-example/app_task.md | 23 ----------------------- 1 file changed, 23 deletions(-) delete mode 100644 book/en/src/by-example/app_task.md (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/app_task.md b/book/en/src/by-example/app_task.md deleted file mode 100644 index b2731f6..0000000 --- a/book/en/src/by-example/app_task.md +++ /dev/null @@ -1,23 +0,0 @@ - - -# Defining tasks with `#[task]` - -Tasks, defined with `#[task]`, are the main mechanism of getting work done in RTIC. - -Tasks can - -* Be spawned (now or in the future, also by themselves) -* Receive messages (passing messages between tasks) -* Be prioritized, allowing preemptive multitasking -* Optionally bind to a hardware interrupt - -RTIC makes a distinction between “software tasks” and “hardware tasks”. - -*Hardware tasks* are tasks that are bound to a specific interrupt vector in the MCU while software tasks are not. - -This means that if a hardware task is bound to, lets say, a UART RX interrupt, the task will be run every -time that interrupt triggers, usually when a character is received. - -*Software tasks* are explicitly spawned in a task, either immediately or using the Monotonic timer mechanism. - -In the coming pages we will explore both tasks and the different options available. -- cgit v1.2.3 From 5c6483f71b1622e518847006147f2360c7563aa6 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Fri, 5 May 2023 19:21:02 +0200 Subject: Update these --- book/en/src/by-example/app.md | 18 +++++++++++++----- book/en/src/by-example/hardware_tasks.md | 6 +++--- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/app.md b/book/en/src/by-example/app.md index fae13cb..b5815fc 100644 --- a/book/en/src/by-example/app.md +++ b/book/en/src/by-example/app.md @@ -2,7 +2,9 @@ ## Requirements on the `app` attribute -All RTIC applications use the [`app`] attribute (`#[app(..)]`). This attribute only applies to a `mod`-item containing the RTIC application. The `app` attribute has a mandatory `device` argument that takes a *path* as a value. This must be a full path pointing to a *peripheral access crate* (PAC) generated using [`svd2rust`] **v0.14.x** or newer. +All RTIC applications use the [`app`] attribute (`#[app(..)]`). This attribute only applies to a `mod`-item containing the RTIC application. + +The `app` attribute has a mandatory `device` argument that takes a *path* as a value. This must be a full path pointing to a *peripheral access crate* (PAC) generated using [`svd2rust`] **v0.14.x** or newer. The `app` attribute will expand into a suitable entry point and thus replaces the use of the [`cortex_m_rt::entry`] attribute. @@ -12,13 +14,19 @@ The `app` attribute will expand into a suitable entry point and thus replaces th ## Structure and zero-cost concurrency -An RTIC `app` is an executable system model for single-core applications, declaring a set of `local` and `shared` resources operated on by a set of `init`, `idle`, *hardware* and *software* tasks. In short the `init` task runs before any other task returning the set of `local` and `shared` resources. Tasks run preemptively based on their associated static priority, `idle` has the lowest priority (and can be used for background work, and/or to put the system to sleep until woken by some event). Hardware tasks are bound to underlying hardware interrupts, while software tasks are scheduled by asynchronous executors (one for each software task priority). +An RTIC `app` is an executable system model for single-core applications, declaring a set of `local` and `shared` resources operated on by a set of `init`, `idle`, *hardware* and *software* tasks. + +* `init` runs before any other task, and returns the `local` and `shared` resources. +* Tasks (both hardware and software) run preemptively based on their associated static priority. +* Hardware tasks are bound to underlying hardware interrupts. +* Software tasks are schedulied by an set of asynchronous executors, one for each software task priority. +* `idle` has the lowest priority, and can be used for background work, and/or to put the system to sleep until it is woken by some event. At compile time the task/resource model is analyzed under the Stack Resource Policy (SRP) and executable code generated with the following outstanding properties: -- guaranteed race-free resource access and deadlock-free execution on a single-shared stack - - hardware task scheduling is performed directly by the hardware, and - - software task scheduling is performed by auto generated async executors tailored to the application. +- Guaranteed race-free resource access and deadlock-free execution on a single-shared stack. +- Hardware task scheduling is performed directly by the hardware. +- Software task scheduling is performed by auto generated async executors tailored to the application. Overall, the generated code infers no additional overhead in comparison to a hand-written implementation, thus in Rust terms RTIC offers a zero-cost abstraction to concurrency. diff --git a/book/en/src/by-example/hardware_tasks.md b/book/en/src/by-example/hardware_tasks.md index bf00dc4..ded488c 100644 --- a/book/en/src/by-example/hardware_tasks.md +++ b/book/en/src/by-example/hardware_tasks.md @@ -1,8 +1,6 @@ # Hardware tasks -At its core RTIC is using a hardware interrupt controller ([ARM NVIC on cortex-m][NVIC]) to schedule and start execution of tasks. All tasks except `pre-init`, `#[init]` and `#[idle]` run as interrupt handlers. - -Hardware tasks are explicitly bound to interrupt handlers. +At its core RTIC is using a hardware interrupt controller ([ARM NVIC on cortex-m][NVIC]) to schedule and start execution of tasks. All tasks except `pre-init` (a hidden "task"), `#[init]` and `#[idle]` run as interrupt handlers. To bind a task to an interrupt, use the `#[task]` attribute argument `binds = InterruptName`. This task then becomes the interrupt handler for this hardware interrupt vector. @@ -17,6 +15,8 @@ Beware of using interrupt vectors that are used internally by hardware features; [pacorhal]: https://docs.rust-embedded.org/book/start/registers.html [NVIC]: https://developer.arm.com/documentation/100166/0001/Nested-Vectored-Interrupt-Controller/NVIC-functional-description/NVIC-interrupts +## Example + The example below demonstrates the use of the `#[task(binds = InterruptName)]` attribute to declare a hardware task bound to an interrupt handler. ``` rust,noplayground -- cgit v1.2.3 From ab17bbf9f37e81b9aab88694e73d23f54664fa01 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Fri, 5 May 2023 19:31:25 +0200 Subject: Demarcate a bit more --- book/en/src/by-example/app.md | 6 ++++++ book/en/src/by-example/software_tasks.md | 9 ++++----- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/app.md b/book/en/src/by-example/app.md index b5815fc..0aeed5b 100644 --- a/book/en/src/by-example/app.md +++ b/book/en/src/by-example/app.md @@ -30,6 +30,12 @@ At compile time the task/resource model is analyzed under the Stack Resource Pol Overall, the generated code infers no additional overhead in comparison to a hand-written implementation, thus in Rust terms RTIC offers a zero-cost abstraction to concurrency. +## Priority + +Priorities in RTIC are specified using the `priority = N` (where N is a positive number) argument passed to the `#[task]` attribute. All `#[task]`s can have a priority. If the priority of a task is not specified, it is set to the default value of 1. + +Priorities in RTIC follow a higher value = more important scheme. For examples, a task with priority 2 will preempt a task with priority 1. + ## An RTIC application example To give a taste of RTIC, the following example contains commonly used features. diff --git a/book/en/src/by-example/software_tasks.md b/book/en/src/by-example/software_tasks.md index ddf88fd..444f4a6 100644 --- a/book/en/src/by-example/software_tasks.md +++ b/book/en/src/by-example/software_tasks.md @@ -1,7 +1,6 @@ # Software tasks & spawn -The RTIC concept of a software task shares a lot with that of [hardware tasks](./hardware_tasks.md) with the core difference that a software task is not explicitly bound to a specific -interrupt vector, but rather bound to a “dispatcher” interrupt vector running at the intended priority of the software task (see below). +The RTIC concept of a software task shares a lot with that of [hardware tasks](./hardware_tasks.md). The core difference is that a software task is not explicitly bound to a specific interrupt vector, but rather bound to a “dispatcher” interrupt vector running at the intended priority of the software task (see below). Similarly to *hardware* tasks, the `#[task]` attribute used on a function declare it as a task. The absence of a `binds = InterruptName` argument to the attribute declares the function as a *software task*. @@ -9,11 +8,11 @@ The static method `task_name::spawn()` spawns (starts) a software task and given The *software* task itself is given as an `async` Rust function, which allows the user to optionally `await` future events. This allows to blend reactive programming (by means of *hardware* tasks) with sequential programming (by means of *software* tasks). -Whereas, *hardware* tasks are assumed to run-to-completion (and return), *software* tasks may be started (`spawned`) once and run forever, with the side condition that any loop (execution path) is broken by at least one `await` (yielding operation). +While *hardware* tasks are assumed to run-to-completion (and return), *software* tasks may be started (`spawned`) once and run forever, on the condition that any loop (execution path) is broken by at least one `await` (yielding operation). -All *software* tasks at the same priority level shares an interrupt handler acting as an async executor dispatching the software tasks. +## Dispatchers -This list of dispatchers, `dispatchers = [FreeInterrupt1, FreeInterrupt2, ...]` is an argument to the `#[app]` attribute, where you define the set of free and usable interrupts. +All *software* tasks at the same priority level share an interrupt handler acting as an async executor dispatching the software tasks. This list of dispatchers, `dispatchers = [FreeInterrupt1, FreeInterrupt2, ...]` is an argument to the `#[app]` attribute, where you define the set of free and usable interrupts. Each interrupt vector acting as dispatcher gets assigned to one priority level meaning that the list of dispatchers need to cover all priority levels used by software tasks. -- cgit v1.2.3 From 4b3bf59215d682e9473ca66545a1f7c2acbccbfe Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 20 May 2023 11:24:03 +0200 Subject: Move some more stuff to the archive, update this link --- book/en/src/by-example/tips/from_ram.md | 45 ------------------------------ book/en/src/by-example/tips/indirection.md | 2 +- 2 files changed, 1 insertion(+), 46 deletions(-) delete mode 100644 book/en/src/by-example/tips/from_ram.md (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/tips/from_ram.md b/book/en/src/by-example/tips/from_ram.md deleted file mode 100644 index a153139..0000000 --- a/book/en/src/by-example/tips/from_ram.md +++ /dev/null @@ -1,45 +0,0 @@ -# Running tasks from RAM - -The main goal of moving the specification of RTIC applications to attributes in RTIC v0.4.0 was to allow inter-operation with other attributes. For example, the `link_section` attribute can be applied to tasks to place them in RAM; this can -improve performance in some cases. - -> **IMPORTANT**: In general, the `link_section`, `export_name` and `no_mangle` attributes are powerful but also easy to misuse. Incorrectly using any of these attributes can cause undefined behavior; you should always prefer to use safe, higher level attributes around them like `cortex-m-rt`'s `interrupt` and `exception` attributes. -> -> In the particular case of RAM functions there's no safe abstraction for it in `cortex-m-rt` v0.6.5 but there's an [RFC] for adding a `ramfunc` attribute in a future release. - -[RFC]: https://github.com/rust-embedded/cortex-m-rt/pull/100 - -The example below shows how to place the higher priority task, `bar`, in RAM. - -``` rust,noplayground -{{#include ../../../../../rtic/examples/ramfunc.rs}} -``` - -Running this program produces the expected output. - -``` console -$ cargo run --target thumbv7m-none-eabi --example ramfunc -``` - -``` console -{{#include ../../../../../rtic/ci/expected/ramfunc.run}} -``` - -One can look at the output of `cargo-nm` to confirm that `bar` ended in RAM -(`0x2000_0000`), whereas `foo` ended in Flash (`0x0000_0000`). - -``` console -$ cargo nm --example ramfunc --release | grep ' foo::' -``` - -``` console -{{#include ../../../../../rtic/ci/expected/ramfunc.run.grep.foo}} -``` - -``` console -$ cargo nm --example ramfunc --target thumbv7m-none-eabi --release | grep '*bar::' -``` - -``` console -{{#include ../../../../../rtic/ci/expected/ramfunc.run.grep.bar}} -``` diff --git a/book/en/src/by-example/tips/indirection.md b/book/en/src/by-example/tips/indirection.md index 58b3bde..aa68190 100644 --- a/book/en/src/by-example/tips/indirection.md +++ b/book/en/src/by-example/tips/indirection.md @@ -7,7 +7,7 @@ Indirection can minimize message passing overhead: instead of sending the buffer One can use a global memory allocator to achieve indirection (`alloc::Box`, `alloc::Rc`, etc.), which requires using the nightly channel as of Rust v1.37.0, or one can use a statically allocated memory pool like [`heapless::Pool`]. -[`heapless::Pool`]: https://docs.rs/heapless/0.5.0/heapless/pool/index.html +[`heapless::Pool`]: https://docs.rs/heapless/latest/heapless/pool/index.html As this example of approach goes completely outside of RTIC resource model with shared and local the program would rely on the correctness of the memory allocator, in this case `heapless::pool`. -- cgit v1.2.3 From 311291b95adbd24ecd037f5e555e4c30138f4339 Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 20 May 2023 12:02:51 +0200 Subject: Make Monotonic implementation more obvious --- book/en/src/by-example/delay.md | 11 +++++------ book/en/src/by-example/tips/monotonic_impl.md | 25 ------------------------- 2 files changed, 5 insertions(+), 31 deletions(-) delete mode 100644 book/en/src/by-example/tips/monotonic_impl.md (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/delay.md b/book/en/src/by-example/delay.md index a6ad0e0..09091fc 100644 --- a/book/en/src/by-example/delay.md +++ b/book/en/src/by-example/delay.md @@ -6,6 +6,8 @@ This can be achieved by instantiating a monotonic timer (for implementations, se [`rtic-monotonics`]: https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics [`rtic-time`]: https://github.com/rtic-rs/rtic/tree/master/rtic-time +[`Monotonic`]: https://docs.rs/rtic-time/latest/rtic_time/trait.Monotonic.html +[Implementing a `Monotonic`]: ../../monotonic_impl.md ``` rust,noplayground ... @@ -25,12 +27,6 @@ async fn foo(_cx: foo::Context) { ``` - - -Technically, the timer queue is implemented as a list based priority queue, where list-nodes are statically allocated as part of the underlying task `Future`. Thus, the timer queue is infallible at run-time (its size and allocation are determined at compile time). - -Similarly the channels implementation, the timer-queue implementation relies on a global *Critical Section* (CS) for race protection. For the examples a CS implementation is provided by adding `--features test-critical-section` to the build options. -
A complete example @@ -48,6 +44,9 @@ $ cargo run --target thumbv7m-none-eabi --example async-delay --features test-cr
+> Interested in contributing new implementations of [`Monotonic`], or more information about the inner workings of monotonics? +> Check out the [Implementing a `Monotonic`] chapter! + ## Timeout Rust [`Future`]s (underlying Rust `async`/`await`) are composable. This makes it possible to `select` in between `Futures` that have completed. diff --git a/book/en/src/by-example/tips/monotonic_impl.md b/book/en/src/by-example/tips/monotonic_impl.md deleted file mode 100644 index 9f88c19..0000000 --- a/book/en/src/by-example/tips/monotonic_impl.md +++ /dev/null @@ -1,25 +0,0 @@ -# Implementing a `Monotonic` timer for scheduling - -The framework is flexible because it can use any timer which has compare-match and optionally supporting overflow interrupts for scheduling. The single requirement to make a timer usable with RTIC is implementing the `rtic-time::Monotonic` trait. - -For RTIC 1.0 and 2.0 we instead assume the user has a time library, e.g. [`fugit`], as the basis for all time-based operations when implementing `Monotonic`. These libraries make it much easier to correctly implement the `Monotonic` trait, allowing the use of almost any timer in the system for scheduling. - -The trait documents the requirements for each method. There are reference implementations available in [`rtic-monotonics`] that can be used for inspriation. - -- [`Systick based`], runs at a fixed interrupt (tick) rate - with some overhead but simple and provides support for large time spans -- [`RP2040 Timer`], a "proper" implementation with support for waiting for long periods without interrupts. Clearly demonstrates how to use the `TimerQueue` to handle scheduling. -- [`nRF52 timers`] implements monotonic & Timer Queue for the RTC and normal timers in nRF52's - -## Contributing - -Contributing new implementations of `Monotonic` can be done in multiple ways: -* Implement the trait behind a feature flag in [`rtic-monotonics`], and create a PR for them to be included in the main RTIC repository. This way, the implementations of are in-tree, and RTIC can guarantee their correctness, and can update them in the case of a new release. -* Implement the changes in an external repository. - -[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics/ -[`rtic_time::Monotonic`]: https://docs.rs/rtic_time/ -[`fugit`]: https://docs.rs/fugit/ -[`Systick based`]: https://github.com/rtic-monotonics -[`rtic-monotonics`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics -[`RP2040 Timer`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/rp2040.rs -[`nRF52 timers`]: https://github.com/rtic-rs/rtic/blob/master/rtic-monotonics/src/nrf.rs \ No newline at end of file -- cgit v1.2.3 From 9fa073f7936782bddf5d02b7b1949032e84de1bd Mon Sep 17 00:00:00 2001 From: datdenkikniet Date: Sat, 20 May 2023 13:08:44 +0200 Subject: Fix link --- book/en/src/by-example/delay.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'book/en/src/by-example') diff --git a/book/en/src/by-example/delay.md b/book/en/src/by-example/delay.md index 09091fc..81f855f 100644 --- a/book/en/src/by-example/delay.md +++ b/book/en/src/by-example/delay.md @@ -7,7 +7,7 @@ This can be achieved by instantiating a monotonic timer (for implementations, se [`rtic-monotonics`]: https://github.com/rtic-rs/rtic/tree/master/rtic-monotonics [`rtic-time`]: https://github.com/rtic-rs/rtic/tree/master/rtic-time [`Monotonic`]: https://docs.rs/rtic-time/latest/rtic_time/trait.Monotonic.html -[Implementing a `Monotonic`]: ../../monotonic_impl.md +[Implementing a `Monotonic`]: ../monotonic_impl.md ``` rust,noplayground ... -- cgit v1.2.3