aboutsummaryrefslogtreecommitdiff
path: root/examples/rp/src/bin/assign_resources.rs
diff options
context:
space:
mode:
authorrafael <rafael>2024-07-08 22:07:38 +0200
committerrafael <rafael>2024-07-08 22:07:38 +0200
commit376f65e1d388a7eff83e0c12deeb4d556daefea5 (patch)
treea809ec5430ff9927ded949f2a84139848abde472 /examples/rp/src/bin/assign_resources.rs
parentd8bd5907ca9dc1f239ca0e603b51ec723e1fef1b (diff)
add assign_resources example
Diffstat (limited to 'examples/rp/src/bin/assign_resources.rs')
-rw-r--r--examples/rp/src/bin/assign_resources.rs82
1 files changed, 82 insertions, 0 deletions
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}