aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorUlf Lilleengen <[email protected]>2024-01-10 09:48:09 +0100
committerUlf Lilleengen <[email protected]>2024-01-10 09:52:46 +0100
commitff5f5021fb4f469f72b3645760238ebe1c4d99af (patch)
tree9ecdc1b4876a3d566a8135aa88ae3de95a78f650 /examples
parentb8672458947b4d48b5d5c950a60edd677d87e29e (diff)
cleanup docs and add channel synchronization example
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/.cargo/config.toml2
-rw-r--r--examples/rp/src/bin/blinky_two_channels.rs47
2 files changed, 48 insertions, 1 deletions
diff --git a/examples/rp/.cargo/config.toml b/examples/rp/.cargo/config.toml
index 3d7d61740..04490c789 100644
--- a/examples/rp/.cargo/config.toml
+++ b/examples/rp/.cargo/config.toml
@@ -1,5 +1,5 @@
1[target.'cfg(all(target_arch = "arm", target_os = "none"))'] 1[target.'cfg(all(target_arch = "arm", target_os = "none"))']
2runner = "probe-rs run --chip RP2040" 2runner = "probe-rs run --chip RP2040 --probe 1209:4853:0e0039001450563641333620"
3 3
4[build] 4[build]
5target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ 5target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
diff --git a/examples/rp/src/bin/blinky_two_channels.rs b/examples/rp/src/bin/blinky_two_channels.rs
new file mode 100644
index 000000000..6179dc260
--- /dev/null
+++ b/examples/rp/src/bin/blinky_two_channels.rs
@@ -0,0 +1,47 @@
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::channel::{Channel, Sender};
13use embassy_time::{Duration, Ticker};
14use gpio::{AnyPin, Level, Output};
15use {defmt_rtt as _, panic_probe as _};
16
17enum LedState {
18 Toggle,
19}
20static CHANNEL: Channel<ThreadModeRawMutex, LedState, 64> = Channel::new();
21
22#[embassy_executor::main]
23async fn main(spawner: Spawner) {
24 let p = embassy_rp::init(Default::default());
25 let mut led = Output::new(AnyPin::from(p.PIN_25), Level::High);
26
27 let dt = 100 * 1_000_000;
28 let k = 1.003;
29
30 unwrap!(spawner.spawn(toggle_led(CHANNEL.sender(), Duration::from_nanos(dt))));
31 unwrap!(spawner.spawn(toggle_led(CHANNEL.sender(), Duration::from_nanos((dt as f64 * k) as u64))));
32
33 loop {
34 match CHANNEL.receive().await {
35 LedState::Toggle => led.toggle(),
36 }
37 }
38}
39
40#[embassy_executor::task(pool_size = 2)]
41async fn toggle_led(control: Sender<'static, ThreadModeRawMutex, LedState, 64>, delay: Duration) {
42 let mut ticker = Ticker::every(delay);
43 loop {
44 control.send(LedState::Toggle).await;
45 ticker.next().await;
46 }
47}