aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--examples/rp/Cargo.toml3
-rw-r--r--examples/rp/src/bin/assign_resources.rs82
2 files changed, 85 insertions, 0 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index 9bd403f02..d06ab5e5a 100644
--- a/examples/rp/Cargo.toml
+++ b/examples/rp/Cargo.toml
@@ -29,6 +29,9 @@ reqwless = { version = "0.12.0", features = ["defmt",]}
29serde = { version = "1.0.203", default-features = false, features = ["derive"] } 29serde = { version = "1.0.203", default-features = false, features = ["derive"] }
30serde-json-core = "0.5.1" 30serde-json-core = "0.5.1"
31 31
32# for assign resources example
33assign-resources = { git = "https://github.com/adamgreig/assign-resources", rev = "94ad10e2729afdf0fd5a77cd12e68409a982f58a" }
34
32#cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } 35#cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
33cortex-m = { version = "0.7.6", features = ["inline-asm"] } 36cortex-m = { version = "0.7.6", features = ["inline-asm"] }
34cortex-m-rt = "0.7.0" 37cortex-m-rt = "0.7.0"
diff --git a/examples/rp/src/bin/assign_resources.rs b/examples/rp/src/bin/assign_resources.rs
new file mode 100644
index 000000000..38e730498
--- /dev/null
+++ b/examples/rp/src/bin/assign_resources.rs
@@ -0,0 +1,82 @@
1//! This example demonstrates how to assign resources to multiple tasks by splitting up the peripherals.
2//! It is not about sharing the same resources between tasks, see sharing.rs for that or head to https://embassy.dev/book/#_sharing_peripherals_between_tasks)
3//! Of course splitting up resources and sharing resources can be combined, yet this example is only about splitting up resources.
4//!
5//! There are basically two ways we demonstrate here:
6//! 1) Assigning resources to a task by passing parts of the peripherals
7//! 2) Assigning resources to a task by passing a struct with the split up peripherals, using the assign-resources macro
8//!
9//! using four LEDs on Pins 10, 11, 20 and 21
10
11#![no_std]
12#![no_main]
13
14use assign_resources::assign_resources;
15use defmt::*;
16use embassy_executor::Spawner;
17use embassy_rp::{
18 gpio,
19 peripherals::{self, PIN_20, PIN_21},
20};
21use embassy_time::Timer;
22use gpio::{Level, Output};
23use {defmt_rtt as _, panic_probe as _};
24
25#[embassy_executor::main]
26async fn main(spawner: Spawner) {
27 // initialize the peripherals
28 let p = embassy_rp::init(Default::default());
29
30 // 1) Assigning a resource to a task by passing parts of the peripherals.
31 spawner
32 .spawn(double_blinky_manually_assigned(spawner, p.PIN_20, p.PIN_21))
33 .unwrap();
34
35 // 2) Using the assign-resources macro to assign resources to a task.
36 // we perform the split, see further below for the definition of the resources struct
37 let r = split_resources!(p);
38 // and then we can use them
39 spawner.spawn(double_blinky_macro_assigned(spawner, r.leds)).unwrap();
40}
41
42// 1) Assigning a resource to a task by passing parts of the peripherals.
43#[embassy_executor::task]
44async fn double_blinky_manually_assigned(_spawner: Spawner, pin_20: PIN_20, pin_21: PIN_21) {
45 let mut led_20 = Output::new(pin_20, Level::Low);
46 let mut led_21 = Output::new(pin_21, Level::High);
47
48 loop {
49 info!("toggling leds");
50 led_20.toggle();
51 led_21.toggle();
52 Timer::after_secs(1).await;
53 }
54}
55
56// 2) Using the assign-resources macro to assign resources to a task.
57// first we define the resources we want to assign to the task using the assign_resources! macro
58// basically this will split up the peripherals struct into smaller structs, that we define here
59// naming is up to you, make sure your future self understands what you did here
60assign_resources! {
61 leds: Leds{
62 led_10: PIN_10,
63 led_11: PIN_11,
64 }
65 // add more resources to more structs if needed, for example defining one struct for each task
66}
67// this could be done in another file and imported here, but for the sake of simplicity we do it here
68// see https://github.com/adamgreig/assign-resources for more information
69
70// 2) Using the split resources in a task
71#[embassy_executor::task]
72async fn double_blinky_macro_assigned(_spawner: Spawner, r: Leds) {
73 let mut led_10 = Output::new(r.led_10, Level::Low);
74 let mut led_11 = Output::new(r.led_11, Level::High);
75
76 loop {
77 info!("toggling leds");
78 led_10.toggle();
79 led_11.toggle();
80 Timer::after_secs(1).await;
81 }
82}