aboutsummaryrefslogtreecommitdiff
path: root/examples
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 /examples
parent8c166272d88e2bbcc5b62dccc9c5800b5f50cca3 (diff)
parent781e33023e3755bddecba23fb3c2dab10e16ccc6 (diff)
Merge pull request #2417 from vasilNnikolov/mutex_rp_example
Add example of pin sharing between tasks
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/src/bin/blinky_two_tasks.rs60
1 files changed, 60 insertions, 0 deletions
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}