aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2023-05-08 16:59:07 +0000
committerGitHub <[email protected]>2023-05-08 16:59:07 +0000
commitd0703f83dbe0099c3dca0c912d873365a2188018 (patch)
treedbde4918d526075bbafe67ac0041d32c68856670
parent79c60f4a7d17613269f795df867c3c83ca90cbcc (diff)
parent2910b09cba432d79befa42a7f994c60dfe832d40 (diff)
Merge #1435
1435: Added example for multi priority executors rp2040 r=Dirbaio a=fakusb I added an example for multiple priorities of tasks on rp2040 by adjusting [examples/nrf52840/src/bin/multiprio.rs](https://github.com/embassy-rs/embassy/blob/master/examples/nrf52840/src/bin/multiprio.rs) . This needs https://github.com/embassy-rs/rp-pac/pull/2 , and this commit also adds the 6 new interrupt handlers for software interrupts to embassy-rs. We might need to change the git path for rp-pac in [embassy-rp/Cargo.toml](https://github.com/embassy-rs/embassy/compare/master...fakusb:rp2040-multiprio-executor?expand=1#diff-47463ea358745927ecdb686f52feab816fde5d402a9628a136c116f34a802ab0) Closes #1413 Co-authored-by: Fabian Kunze <[email protected]>
-rw-r--r--embassy-rp/Cargo.toml2
-rw-r--r--embassy-rp/src/interrupt.rs6
-rw-r--r--examples/rp/Cargo.toml2
-rw-r--r--examples/rp/src/bin/multiprio.rs152
4 files changed, 160 insertions, 2 deletions
diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml
index 59d0bf338..b2b7fb083 100644
--- a/embassy-rp/Cargo.toml
+++ b/embassy-rp/Cargo.toml
@@ -61,7 +61,7 @@ embedded-storage = { version = "0.3" }
61rand_core = "0.6.4" 61rand_core = "0.6.4"
62fixed = "1.23.1" 62fixed = "1.23.1"
63 63
64rp-pac = { version = "2", features = ["rt"] } 64rp-pac = { version = "3", features = ["rt"] }
65 65
66embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } 66embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] }
67embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10", optional = true} 67embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10", optional = true}
diff --git a/embassy-rp/src/interrupt.rs b/embassy-rp/src/interrupt.rs
index f21a5433b..989f5dc2d 100644
--- a/embassy-rp/src/interrupt.rs
+++ b/embassy-rp/src/interrupt.rs
@@ -34,3 +34,9 @@ declare!(ADC_IRQ_FIFO);
34declare!(I2C0_IRQ); 34declare!(I2C0_IRQ);
35declare!(I2C1_IRQ); 35declare!(I2C1_IRQ);
36declare!(RTC_IRQ); 36declare!(RTC_IRQ);
37declare!(SWI_IRQ_0);
38declare!(SWI_IRQ_1);
39declare!(SWI_IRQ_2);
40declare!(SWI_IRQ_3);
41declare!(SWI_IRQ_4);
42declare!(SWI_IRQ_5);
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml
index d2829df99..ffeb69f15 100644
--- a/examples/rp/Cargo.toml
+++ b/examples/rp/Cargo.toml
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
8[dependencies] 8[dependencies]
9embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] } 9embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] }
10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } 10embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] }
11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } 11embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] }
12embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime"] } 12embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime"] }
13embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] } 13embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] }
14embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } 14embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] }
diff --git a/examples/rp/src/bin/multiprio.rs b/examples/rp/src/bin/multiprio.rs
new file mode 100644
index 000000000..2f79ba49e
--- /dev/null
+++ b/examples/rp/src/bin/multiprio.rs
@@ -0,0 +1,152 @@
1//! This example showcases how to create multiple Executor instances to run tasks at
2//! different priority levels.
3//!
4//! Low priority executor runs in thread mode (not interrupt), and uses `sev` for signaling
5//! there's work in the queue, and `wfe` for waiting for work.
6//!
7//! Medium and high priority executors run in two interrupts with different priorities.
8//! Signaling work is done by pending the interrupt. No "waiting" needs to be done explicitly, since
9//! when there's work the interrupt will trigger and run the executor.
10//!
11//! Sample output below. Note that high priority ticks can interrupt everything else, and
12//! medium priority computations can interrupt low priority computations, making them to appear
13//! to take significantly longer time.
14//!
15//! ```not_rust
16//! [med] Starting long computation
17//! [med] done in 992 ms
18//! [high] tick!
19//! [low] Starting long computation
20//! [med] Starting long computation
21//! [high] tick!
22//! [high] tick!
23//! [med] done in 993 ms
24//! [med] Starting long computation
25//! [high] tick!
26//! [high] tick!
27//! [med] done in 993 ms
28//! [low] done in 3972 ms
29//! [med] Starting long computation
30//! [high] tick!
31//! [high] tick!
32//! [med] done in 993 ms
33//! ```
34//!
35//! For comparison, try changing the code so all 3 tasks get spawned on the low priority executor.
36//! You will get an output like the following. Note that no computation is ever interrupted.
37//!
38//! ```not_rust
39//! [high] tick!
40//! [med] Starting long computation
41//! [med] done in 496 ms
42//! [low] Starting long computation
43//! [low] done in 992 ms
44//! [med] Starting long computation
45//! [med] done in 496 ms
46//! [high] tick!
47//! [low] Starting long computation
48//! [low] done in 992 ms
49//! [high] tick!
50//! [med] Starting long computation
51//! [med] done in 496 ms
52//! [high] tick!
53//! ```
54//!
55
56#![no_std]
57#![no_main]
58#![feature(type_alias_impl_trait)]
59
60use core::mem;
61
62use cortex_m::peripheral::NVIC;
63use cortex_m_rt::entry;
64use defmt::{info, unwrap};
65use embassy_rp::executor::{Executor, InterruptExecutor};
66use embassy_rp::interrupt;
67use embassy_rp::pac::Interrupt;
68use embassy_time::{Duration, Instant, Timer, TICK_HZ};
69use static_cell::StaticCell;
70use {defmt_rtt as _, panic_probe as _};
71
72#[embassy_executor::task]
73async fn run_high() {
74 loop {
75 info!(" [high] tick!");
76 Timer::after(Duration::from_ticks(673740)).await;
77 }
78}
79
80#[embassy_executor::task]
81async fn run_med() {
82 loop {
83 let start = Instant::now();
84 info!(" [med] Starting long computation");
85
86 // Spin-wait to simulate a long CPU computation
87 cortex_m::asm::delay(125_000_000); // ~1 second
88
89 let end = Instant::now();
90 let ms = end.duration_since(start).as_ticks() * 1000 / TICK_HZ;
91 info!(" [med] done in {} ms", ms);
92
93 Timer::after(Duration::from_ticks(53421)).await;
94 }
95}
96
97#[embassy_executor::task]
98async fn run_low() {
99 loop {
100 let start = Instant::now();
101 info!("[low] Starting long computation");
102
103 // Spin-wait to simulate a long CPU computation
104 cortex_m::asm::delay(250_000_000); // ~2 seconds
105
106 let end = Instant::now();
107 let ms = end.duration_since(start).as_ticks() * 1000 / TICK_HZ;
108 info!("[low] done in {} ms", ms);
109
110 Timer::after(Duration::from_ticks(82983)).await;
111 }
112}
113
114static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new();
115static EXECUTOR_MED: InterruptExecutor = InterruptExecutor::new();
116static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new();
117
118#[interrupt]
119unsafe fn SWI_IRQ_1() {
120 EXECUTOR_HIGH.on_interrupt()
121}
122
123#[interrupt]
124unsafe fn SWI_IRQ_0() {
125 EXECUTOR_MED.on_interrupt()
126}
127
128#[entry]
129fn main() -> ! {
130 info!("Hello World!");
131
132 let _p = embassy_rp::init(Default::default());
133 let mut nvic: NVIC = unsafe { mem::transmute(()) };
134
135 // High-priority executor: SWI_IRQ_1, priority level 2
136 unsafe { nvic.set_priority(Interrupt::SWI_IRQ_1, 2 << 6) };
137 info!("bla: {}", NVIC::get_priority(Interrupt::SWI_IRQ_1));
138 let spawner = EXECUTOR_HIGH.start(Interrupt::SWI_IRQ_1);
139 unwrap!(spawner.spawn(run_high()));
140
141 // Medium-priority executor: SWI_IRQ_0, priority level 3
142 unsafe { nvic.set_priority(Interrupt::SWI_IRQ_0, 3 << 6) };
143 info!("bla: {}", NVIC::get_priority(Interrupt::SWI_IRQ_0));
144 let spawner = EXECUTOR_MED.start(Interrupt::SWI_IRQ_0);
145 unwrap!(spawner.spawn(run_med()));
146
147 // Low priority executor: runs in thread mode, using WFE/SEV
148 let executor = EXECUTOR_LOW.init(Executor::new());
149 executor.run(|spawner| {
150 unwrap!(spawner.spawn(run_low()));
151 });
152}