aboutsummaryrefslogtreecommitdiff
path: root/examples/rp/src
diff options
context:
space:
mode:
Diffstat (limited to 'examples/rp/src')
-rw-r--r--examples/rp/src/bin/multiprio.rs152
-rw-r--r--examples/rp/src/bin/usb_serial.rs2
2 files changed, 153 insertions, 1 deletions
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}
diff --git a/examples/rp/src/bin/usb_serial.rs b/examples/rp/src/bin/usb_serial.rs
index a991082ee..8160a1875 100644
--- a/examples/rp/src/bin/usb_serial.rs
+++ b/examples/rp/src/bin/usb_serial.rs
@@ -30,7 +30,7 @@ async fn main(_spawner: Spawner) {
30 config.max_power = 100; 30 config.max_power = 100;
31 config.max_packet_size_0 = 64; 31 config.max_packet_size_0 = 64;
32 32
33 // Required for windows compatiblity. 33 // Required for windows compatibility.
34 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 34 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
35 config.device_class = 0xEF; 35 config.device_class = 0xEF;
36 config.device_sub_class = 0x02; 36 config.device_sub_class = 0x02;