aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
Diffstat (limited to 'docs')
-rw-r--r--docs/modules/ROOT/nav.adoc1
-rw-r--r--docs/modules/ROOT/pages/sharing_peripherals.adoc78
2 files changed, 79 insertions, 0 deletions
diff --git a/docs/modules/ROOT/nav.adoc b/docs/modules/ROOT/nav.adoc
index fabb80e31..b692c44e1 100644
--- a/docs/modules/ROOT/nav.adoc
+++ b/docs/modules/ROOT/nav.adoc
@@ -6,6 +6,7 @@
6* xref:layer_by_layer.adoc[Bare metal to async] 6* xref:layer_by_layer.adoc[Bare metal to async]
7* xref:runtime.adoc[Executor] 7* xref:runtime.adoc[Executor]
8* xref:delaying_a_task.adoc[Delaying a Task] 8* xref:delaying_a_task.adoc[Delaying a Task]
9* xref:sharing_peripherals.adoc[Sharing peripherals between tasks]
9* xref:hal.adoc[HAL] 10* xref:hal.adoc[HAL]
10** xref:nrf.adoc[nRF] 11** xref:nrf.adoc[nRF]
11** xref:stm32.adoc[STM32] 12** xref:stm32.adoc[STM32]
diff --git a/docs/modules/ROOT/pages/sharing_peripherals.adoc b/docs/modules/ROOT/pages/sharing_peripherals.adoc
new file mode 100644
index 000000000..41f467942
--- /dev/null
+++ b/docs/modules/ROOT/pages/sharing_peripherals.adoc
@@ -0,0 +1,78 @@
1= Sharing peripherals between tasks
2
3Often times, more than one task needs access to the same resource (pin, communication interface, etc.). The following example shows how to use the on-board LED on a Raspberry Pi Pico board by two tasks simultaneously.
4
5[,rust]
6----
7use defmt::*;
8use embassy_executor::Spawner;
9use embassy_rp::gpio;
10use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
11use embassy_sync::mutex::Mutex;
12use embassy_time::{Duration, Ticker};
13use gpio::{AnyPin, Level, Output};
14use {defmt_rtt as _, panic_probe as _};
15
16type LedType = Mutex<ThreadModeRawMutex, Option<Output<'static, AnyPin>>>;
17static LED: LedType = Mutex::new(None);
18
19#[embassy_executor::main]
20async fn main(spawner: Spawner) {
21 let p = embassy_rp::init(Default::default());
22 // set the content of the global LED reference to the real LED pin
23 let led = Output::new(AnyPin::from(p.PIN_25), Level::High);
24 // inner scope is so that once the mutex is written to, the MutexGuard is dropped, thus the
25 // Mutex is released
26 {
27 *(LED.lock().await) = Some(led);
28 }
29 let dt = 100 * 1_000_000;
30 let k = 1.003;
31
32 unwrap!(spawner.spawn(toggle(&LED, Duration::from_nanos(dt))));
33 unwrap!(spawner.spawn(toggle_slightly_slower(
34 &LED,
35 Duration::from_nanos((dt as f64 * k) as u64)
36 )));
37}
38
39async fn toggle_led(led: &'static LedType, delay: Duration) {
40 let mut ticker = Ticker::every(delay);
41 loop {
42 {
43 let mut led_unlocked = led.lock().await;
44 if let Some(pin_ref) = led_unlocked.as_mut() {
45 pin_ref.toggle();
46 }
47 }
48 ticker.next().await;
49 }
50}
51#[embassy_executor::task]
52async fn toggle(led: &'static LedType, delay: Duration) {
53 toggle_led(led, delay).await
54}
55
56#[embassy_executor::task]
57async fn toggle_slightly_slower(led: &'static LedType, delay: Duration) {
58 toggle_led(led, delay).await
59}
60----
61
62The structure facilitating access to the resource is the defined `LedType`.
63
64== Why so complicated
65
66Unwrapping the layers gives insight into why each one is needed.
67
68=== `Mutex<RawMutexType, T>`
69
70The mutex is there so if one task gets the resource first and begins modifying it, all other tasks wanting to write will have to wait (the `led.lock().await` will return immediately if no task has locked the mutex, and will block if it is accessed somewhere else).
71
72=== `Option<T>`
73
74The `LED` variable needs to be defined outside the main task as references accepted by tasks need to be `'static`. However, if it is outside the main task, it cannot be initialised to point to any pin, as the pins themselves are not initialised. Thus, it is set to `None`.
75
76=== `Output<AnyPin>`
77
78To indicate that the pin will be set to an Output. The `AnyPin` could have been `embassy_rp::peripherals::PIN_25`, however this option lets the `toggle_led` function be more generic.