aboutsummaryrefslogtreecommitdiff
path: root/book/en/src/by-example/channel.md
diff options
context:
space:
mode:
authorPer Lindgren <per.lindgren@ltu.se>2023-01-28 21:57:43 +0100
committerHenrik Tjäder <henrik@tjaders.com>2023-03-01 00:33:39 +0100
commit1f51b10297e9cbb4797aa1ed8be6a2b84c9f2e07 (patch)
treefaab2e5fd8a3432ac5b1f7be3bd9372d8063f8c5 /book/en/src/by-example/channel.md
parentd0c51269608c18a105fd010f070bd9af6f443c60 (diff)
Book: Major rework for RTIC v2
Diffstat (limited to 'book/en/src/by-example/channel.md')
-rw-r--r--book/en/src/by-example/channel.md112
1 files changed, 112 insertions, 0 deletions
diff --git a/book/en/src/by-example/channel.md b/book/en/src/by-example/channel.md
new file mode 100644
index 0000000..99bfedd
--- /dev/null
+++ b/book/en/src/by-example/channel.md
@@ -0,0 +1,112 @@
+# 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:
+
+```rust
+...
+const CAPACITY: usize = 5;
+#[init]
+ fn init(_: init::Context) -> (Shared, Local) {
+ let (s, r) = make_channel!(u32, CAPACITY);
+ receiver::spawn(r).unwrap();
+ sender1::spawn(s.clone()).unwrap();
+ sender2::spawn(s.clone()).unwrap();
+ ...
+```
+
+In this case the channel holds data of `u32` type with a capacity of 5 elements.
+
+## Sending data
+
+The `send` method post a message on the channel as shown below:
+
+```rust
+#[task]
+async fn sender1(_c: sender1::Context, mut sender: Sender<'static, u32, CAPACITY>) {
+ hprintln!("Sender 1 sending: 1");
+ sender.send(1).await.unwrap();
+}
+```
+
+## Receiving data
+
+The receiver can `await` incoming messages:
+
+```rust
+#[task]
+async fn receiver(_c: receiver::Context, mut receiver: Receiver<'static, u32, CAPACITY>) {
+ while let Ok(val) = receiver.recv().await {
+ hprintln!("Receiver got: {}", val);
+ ...
+ }
+}
+```
+
+For a complete example:
+
+``` rust
+{{#include ../../../../rtic/examples/async-channel.rs}}
+```
+
+``` console
+$ cargo run --target thumbv7m-none-eabi --example async-channel --features test-critical-section
+{{#include ../../../../rtic/ci/expected/async-channel.run}}
+```
+
+Also sender endpoint can be awaited. In case there the channel capacity has not been reached, `await` the sender can progress immediately, while in the case the capacity is reached, the sender is blocked until there is free space in the queue. In this way data is never lost.
+
+In the below example the `CAPACITY` has been reduced to 1, forcing sender tasks to wait until the data in the channel has been received.
+
+``` rust
+{{#include ../../../../rtic/examples/async-channel-done.rs}}
+```
+
+Looking at the output, we find that `Sender 2` will wait until the data sent by `Sender 1` as been received.
+
+> **NOTICE** *Software* tasks at the same priority are executed asynchronously to each other, thus **NO** strict order can be assumed. (The presented order here applies only to the current implementation, and may change between RTIC framework releases.)
+
+``` console
+$ cargo run --target thumbv7m-none-eabi --example async-channel-done --features test-critical-section
+{{#include ../../../../rtic/ci/expected/async-channel-done.run}}
+```
+
+## Error handling
+
+In case all senders have been dropped `await` on an empty receiver channel results in an error. This allows to gracefully implement different types of shutdown operations.
+
+``` rust
+{{#include ../../../../rtic/examples/async-channel-no-sender.rs}}
+```
+
+``` console
+$ cargo run --target thumbv7m-none-eabi --example async-channel-no-sender --features test-critical-section
+{{#include ../../../../rtic/ci/expected/async-channel-no-sender.run}}
+```
+
+Similarly, `await` on a send channel results in an error in case the receiver has been dropped. This allows to gracefully implement application level error handling.
+
+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
+{{#include ../../../../rtic/examples/async-channel-no-receiver.rs}}
+```
+
+``` console
+$ cargo run --target thumbv7m-none-eabi --example async-channel-no-receiver --features test-critical-section
+{{#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.
+
+``` rust
+{{#include ../../../../rtic/examples/async-channel-try.rs}}
+```
+
+``` console
+$ cargo run --target thumbv7m-none-eabi --example async-channel-try --features test-critical-section
+{{#include ../../../../rtic/ci/expected/async-channel-try.run}}
+``` \ No newline at end of file