diff options
| author | rafael <rafael> | 2024-07-08 22:07:38 +0200 |
|---|---|---|
| committer | rafael <rafael> | 2024-07-08 22:07:38 +0200 |
| commit | 376f65e1d388a7eff83e0c12deeb4d556daefea5 (patch) | |
| tree | a809ec5430ff9927ded949f2a84139848abde472 /examples | |
| parent | d8bd5907ca9dc1f239ca0e603b51ec723e1fef1b (diff) | |
add assign_resources example
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/rp/src/bin/assign_resources.rs | 82 |
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",]} | |||
| 29 | serde = { version = "1.0.203", default-features = false, features = ["derive"] } | 29 | serde = { version = "1.0.203", default-features = false, features = ["derive"] } |
| 30 | serde-json-core = "0.5.1" | 30 | serde-json-core = "0.5.1" |
| 31 | 31 | ||
| 32 | # for assign resources example | ||
| 33 | assign-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"] } |
| 33 | cortex-m = { version = "0.7.6", features = ["inline-asm"] } | 36 | cortex-m = { version = "0.7.6", features = ["inline-asm"] } |
| 34 | cortex-m-rt = "0.7.0" | 37 | cortex-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 | |||
| 14 | use assign_resources::assign_resources; | ||
| 15 | use defmt::*; | ||
| 16 | use embassy_executor::Spawner; | ||
| 17 | use embassy_rp::{ | ||
| 18 | gpio, | ||
| 19 | peripherals::{self, PIN_20, PIN_21}, | ||
| 20 | }; | ||
| 21 | use embassy_time::Timer; | ||
| 22 | use gpio::{Level, Output}; | ||
| 23 | use {defmt_rtt as _, panic_probe as _}; | ||
| 24 | |||
| 25 | #[embassy_executor::main] | ||
| 26 | async 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] | ||
| 44 | async 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 | ||
| 60 | assign_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] | ||
| 72 | async 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 | } | ||
