diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/.cargo/config.toml | 2 | ||||
| -rw-r--r-- | examples/rp/src/bin/blinky_two_channels.rs | 50 | ||||
| -rw-r--r-- | examples/rp/src/bin/blinky_two_tasks.rs | 17 |
3 files changed, 54 insertions, 15 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"))'] |
| 2 | runner = "probe-rs run --chip RP2040" | 2 | runner = "probe-rs run --chip RP2040 --probe 1209:4853:0e0039001450563641333620" |
| 3 | 3 | ||
| 4 | [build] | 4 | [build] |
| 5 | target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+ | 5 | target = "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..b2eec2a21 --- /dev/null +++ b/examples/rp/src/bin/blinky_two_channels.rs | |||
| @@ -0,0 +1,50 @@ | |||
| 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) | ||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Spawner; | ||
| 10 | use embassy_rp::gpio; | ||
| 11 | use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex; | ||
| 12 | use embassy_sync::channel::{Channel, Sender}; | ||
| 13 | use embassy_time::{Duration, Ticker}; | ||
| 14 | use gpio::{AnyPin, Level, Output}; | ||
| 15 | use {defmt_rtt as _, panic_probe as _}; | ||
| 16 | |||
| 17 | enum LedState { | ||
| 18 | Toggle, | ||
| 19 | } | ||
| 20 | static CHANNEL: Channel<ThreadModeRawMutex, LedState, 64> = Channel::new(); | ||
| 21 | |||
| 22 | #[embassy_executor::main] | ||
| 23 | async 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( | ||
| 32 | CHANNEL.sender(), | ||
| 33 | Duration::from_nanos((dt as f64 * k) as u64) | ||
| 34 | ))); | ||
| 35 | |||
| 36 | loop { | ||
| 37 | match CHANNEL.receive().await { | ||
| 38 | LedState::Toggle => led.toggle(), | ||
| 39 | } | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | #[embassy_executor::task(pool_size = 2)] | ||
| 44 | async fn toggle_led(control: Sender<'static, ThreadModeRawMutex, LedState, 64>, delay: Duration) { | ||
| 45 | let mut ticker = Ticker::every(delay); | ||
| 46 | loop { | ||
| 47 | control.send(LedState::Toggle).await; | ||
| 48 | ticker.next().await; | ||
| 49 | } | ||
| 50 | } | ||
diff --git a/examples/rp/src/bin/blinky_two_tasks.rs b/examples/rp/src/bin/blinky_two_tasks.rs index 7e0b531e1..a03f3a592 100644 --- a/examples/rp/src/bin/blinky_two_tasks.rs +++ b/examples/rp/src/bin/blinky_two_tasks.rs | |||
| @@ -30,13 +30,11 @@ async fn main(spawner: Spawner) { | |||
| 30 | let dt = 100 * 1_000_000; | 30 | let dt = 100 * 1_000_000; |
| 31 | let k = 1.003; | 31 | let k = 1.003; |
| 32 | 32 | ||
| 33 | unwrap!(spawner.spawn(toggle(&LED, Duration::from_nanos(dt)))); | 33 | unwrap!(spawner.spawn(toggle_led(&LED, Duration::from_nanos(dt)))); |
| 34 | unwrap!(spawner.spawn(toggle_slightly_slower( | 34 | unwrap!(spawner.spawn(toggle_led(&LED, Duration::from_nanos((dt as f64 * k) as u64)))); |
| 35 | &LED, | ||
| 36 | Duration::from_nanos((dt as f64 * k) as u64) | ||
| 37 | ))); | ||
| 38 | } | 35 | } |
| 39 | 36 | ||
| 37 | #[embassy_executor::task(pool_size = 2)] | ||
| 40 | async fn toggle_led(led: &'static LedType, delay: Duration) { | 38 | async fn toggle_led(led: &'static LedType, delay: Duration) { |
| 41 | let mut ticker = Ticker::every(delay); | 39 | let mut ticker = Ticker::every(delay); |
| 42 | loop { | 40 | loop { |
| @@ -49,12 +47,3 @@ async fn toggle_led(led: &'static LedType, delay: Duration) { | |||
| 49 | ticker.next().await; | 47 | ticker.next().await; |
| 50 | } | 48 | } |
| 51 | } | 49 | } |
| 52 | #[embassy_executor::task] | ||
| 53 | async fn toggle(led: &'static LedType, delay: Duration) { | ||
| 54 | toggle_led(led, delay).await | ||
| 55 | } | ||
| 56 | |||
| 57 | #[embassy_executor::task] | ||
| 58 | async fn toggle_slightly_slower(led: &'static LedType, delay: Duration) { | ||
| 59 | toggle_led(led, delay).await | ||
| 60 | } | ||
