aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2024-01-10 07:25:47 +0000
committerGitHub <[email protected]>2024-01-10 07:25:47 +0000
commitbe3c70d455e87422aff5e439401860a9ec85bf16 (patch)
tree3d8662b57dc758b0cf223f239d79a57682b3f904
parent8c166272d88e2bbcc5b62dccc9c5800b5f50cca3 (diff)
parent781e33023e3755bddecba23fb3c2dab10e16ccc6 (diff)
Merge pull request #2417 from vasilNnikolov/mutex_rp_example
Add example of pin sharing between tasks
-rw-r--r--docs/modules/ROOT/nav.adoc1
-rw-r--r--docs/modules/ROOT/pages/sharing_peripherals.adoc78
-rw-r--r--examples/rp/src/bin/blinky_two_tasks.rs60
3 files changed, 139 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.
diff --git a/examples/rp/src/bin/blinky_two_tasks.rs b/examples/rp/src/bin/blinky_two_tasks.rs
new file mode 100644
index 000000000..7e0b531e1
--- /dev/null
+++ b/examples/rp/src/bin/blinky_two_tasks.rs
@@ -0,0 +1,60 @@
1#![no_std]
2#![no_main]
3/// This example demonstrates how to access a given pin from more than one embassy task
4/// The on-board LED is toggled by two tasks with slightly different periods, leading to the
5/// apparent duty cycle of the LED increasing, then decreasing, linearly. The phenomenon is similar
6/// to interference and the 'beats' you can hear if you play two frequencies close to one another
7/// [Link explaining it](https://www.physicsclassroom.com/class/sound/Lesson-3/Interference-and-Beats)
8use defmt::*;
9use embassy_executor::Spawner;
10use embassy_rp::gpio;
11use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
12use embassy_sync::mutex::Mutex;
13use embassy_time::{Duration, Ticker};
14use gpio::{AnyPin, Level, Output};
15use {defmt_rtt as _, panic_probe as _};
16
17type LedType = Mutex<ThreadModeRawMutex, Option<Output<'static, AnyPin>>>;
18static LED: LedType = Mutex::new(None);
19
20#[embassy_executor::main]
21async fn main(spawner: Spawner) {
22 let p = embassy_rp::init(Default::default());
23 // set the content of the global LED reference to the real LED pin
24 let led = Output::new(AnyPin::from(p.PIN_25), Level::High);
25 // inner scope is so that once the mutex is written to, the MutexGuard is dropped, thus the
26 // Mutex is released
27 {
28 *(LED.lock().await) = Some(led);
29 }
30 let dt = 100 * 1_000_000;
31 let k = 1.003;
32
33 unwrap!(spawner.spawn(toggle(&LED, Duration::from_nanos(dt))));
34 unwrap!(spawner.spawn(toggle_slightly_slower(
35 &LED,
36 Duration::from_nanos((dt as f64 * k) as u64)
37 )));
38}
39
40async fn toggle_led(led: &'static LedType, delay: Duration) {
41 let mut ticker = Ticker::every(delay);
42 loop {
43 {
44 let mut led_unlocked = led.lock().await;
45 if let Some(pin_ref) = led_unlocked.as_mut() {
46 pin_ref.toggle();
47 }
48 }
49 ticker.next().await;
50 }
51}
52#[embassy_executor::task]
53async fn toggle(led: &'static LedType, delay: Duration) {
54 toggle_led(led, delay).await
55}
56
57#[embassy_executor::task]
58async fn toggle_slightly_slower(led: &'static LedType, delay: Duration) {
59 toggle_led(led, delay).await
60}