aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorCaleb Jamison <[email protected]>2023-05-09 17:55:27 -0400
committerGitHub <[email protected]>2023-05-09 17:55:27 -0400
commit9d971e5b150e2ebe51570040ea59e3ccdbef7b17 (patch)
tree153a70e9123bbbd876f3a4b08659181d83ccec89 /examples
parent5cfe1a1fb40470dfaf256fc87989fd67884113f1 (diff)
parent856b944eaf20bbd5f1625226415af210a28af89a (diff)
Merge branch 'master' into master
Diffstat (limited to 'examples')
-rw-r--r--examples/nrf52840/src/bin/pubsub.rs4
-rw-r--r--examples/nrf52840/src/bin/usb_serial.rs2
-rw-r--r--examples/nrf52840/src/bin/usb_serial_multitask.rs2
-rw-r--r--examples/nrf52840/src/bin/usb_serial_winusb.rs2
-rw-r--r--examples/rp/Cargo.toml2
-rw-r--r--examples/rp/src/bin/multiprio.rs152
-rw-r--r--examples/rp/src/bin/usb_serial.rs2
-rw-r--r--examples/stm32f0/src/bin/button_controlled_blink.rs4
-rw-r--r--examples/stm32f4/src/bin/usb_serial.rs2
-rw-r--r--examples/stm32f7/src/bin/usb_serial.rs2
-rw-r--r--examples/stm32h5/src/bin/usb_serial.rs2
-rw-r--r--examples/stm32h7/src/bin/usb_serial.rs2
-rw-r--r--examples/stm32l4/src/bin/usb_serial.rs2
-rw-r--r--examples/stm32u5/src/bin/usb_serial.rs2
-rw-r--r--examples/stm32wl/src/bin/uart_async.rs2
15 files changed, 168 insertions, 16 deletions
diff --git a/examples/nrf52840/src/bin/pubsub.rs b/examples/nrf52840/src/bin/pubsub.rs
index 688e6d075..cca60ebc9 100644
--- a/examples/nrf52840/src/bin/pubsub.rs
+++ b/examples/nrf52840/src/bin/pubsub.rs
@@ -74,9 +74,9 @@ async fn fast_logger(mut messages: Subscriber<'static, ThreadModeRawMutex, Messa
74} 74}
75 75
76/// A logger task that awaits the messages, but also does some other work. 76/// A logger task that awaits the messages, but also does some other work.
77/// Because of this, depeding on how the messages were published, the subscriber might miss some messages 77/// Because of this, depending on how the messages were published, the subscriber might miss some messages.
78/// 78///
79/// This takes the dynamic `DynSubscriber`. This is not as performant as the generic version, but let's you ignore some of the generics 79/// This takes the dynamic `DynSubscriber`. This is not as performant as the generic version, but let's you ignore some of the generics.
80#[embassy_executor::task] 80#[embassy_executor::task]
81async fn slow_logger(mut messages: DynSubscriber<'static, Message>) { 81async fn slow_logger(mut messages: DynSubscriber<'static, Message>) {
82 loop { 82 loop {
diff --git a/examples/nrf52840/src/bin/usb_serial.rs b/examples/nrf52840/src/bin/usb_serial.rs
index 9727a4f57..dc95cde84 100644
--- a/examples/nrf52840/src/bin/usb_serial.rs
+++ b/examples/nrf52840/src/bin/usb_serial.rs
@@ -40,7 +40,7 @@ async fn main(_spawner: Spawner) {
40 config.max_power = 100; 40 config.max_power = 100;
41 config.max_packet_size_0 = 64; 41 config.max_packet_size_0 = 64;
42 42
43 // Required for windows compatiblity. 43 // Required for windows compatibility.
44 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 44 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
45 config.device_class = 0xEF; 45 config.device_class = 0xEF;
46 config.device_sub_class = 0x02; 46 config.device_sub_class = 0x02;
diff --git a/examples/nrf52840/src/bin/usb_serial_multitask.rs b/examples/nrf52840/src/bin/usb_serial_multitask.rs
index 6da2c2a2f..ac22d9499 100644
--- a/examples/nrf52840/src/bin/usb_serial_multitask.rs
+++ b/examples/nrf52840/src/bin/usb_serial_multitask.rs
@@ -66,7 +66,7 @@ async fn main(spawner: Spawner) {
66 config.max_power = 100; 66 config.max_power = 100;
67 config.max_packet_size_0 = 64; 67 config.max_packet_size_0 = 64;
68 68
69 // Required for windows compatiblity. 69 // Required for windows compatibility.
70 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 70 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
71 config.device_class = 0xEF; 71 config.device_class = 0xEF;
72 config.device_sub_class = 0x02; 72 config.device_sub_class = 0x02;
diff --git a/examples/nrf52840/src/bin/usb_serial_winusb.rs b/examples/nrf52840/src/bin/usb_serial_winusb.rs
index 6e4f71a48..1d39d3841 100644
--- a/examples/nrf52840/src/bin/usb_serial_winusb.rs
+++ b/examples/nrf52840/src/bin/usb_serial_winusb.rs
@@ -45,7 +45,7 @@ async fn main(_spawner: Spawner) {
45 config.max_power = 100; 45 config.max_power = 100;
46 config.max_packet_size_0 = 64; 46 config.max_packet_size_0 = 64;
47 47
48 // Required for windows compatiblity. 48 // Required for windows compatibility.
49 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 49 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
50 config.device_class = 0xEF; 50 config.device_class = 0xEF;
51 config.device_sub_class = 0x02; 51 config.device_sub_class = 0x02;
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}
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;
diff --git a/examples/stm32f0/src/bin/button_controlled_blink.rs b/examples/stm32f0/src/bin/button_controlled_blink.rs
index e1f223433..f362c53f5 100644
--- a/examples/stm32f0/src/bin/button_controlled_blink.rs
+++ b/examples/stm32f0/src/bin/button_controlled_blink.rs
@@ -17,8 +17,8 @@ static BLINK_MS: AtomicU32 = AtomicU32::new(0);
17 17
18#[embassy_executor::task] 18#[embassy_executor::task]
19async fn led_task(led: AnyPin) { 19async fn led_task(led: AnyPin) {
20 // Configure the LED pin as a push pull ouput and obtain handler. 20 // Configure the LED pin as a push pull output and obtain handler.
21 // On the Nucleo F091RC theres an on-board LED connected to pin PA5. 21 // On the Nucleo F091RC there's an on-board LED connected to pin PA5.
22 let mut led = Output::new(led, Level::Low, Speed::Low); 22 let mut led = Output::new(led, Level::Low, Speed::Low);
23 23
24 loop { 24 loop {
diff --git a/examples/stm32f4/src/bin/usb_serial.rs b/examples/stm32f4/src/bin/usb_serial.rs
index baabc1a2d..d2b1dca43 100644
--- a/examples/stm32f4/src/bin/usb_serial.rs
+++ b/examples/stm32f4/src/bin/usb_serial.rs
@@ -34,7 +34,7 @@ async fn main(_spawner: Spawner) {
34 config.product = Some("USB-serial example"); 34 config.product = Some("USB-serial example");
35 config.serial_number = Some("12345678"); 35 config.serial_number = Some("12345678");
36 36
37 // Required for windows compatiblity. 37 // Required for windows compatibility.
38 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 38 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
39 config.device_class = 0xEF; 39 config.device_class = 0xEF;
40 config.device_sub_class = 0x02; 40 config.device_sub_class = 0x02;
diff --git a/examples/stm32f7/src/bin/usb_serial.rs b/examples/stm32f7/src/bin/usb_serial.rs
index 5fd9d2ec9..dca90d9cb 100644
--- a/examples/stm32f7/src/bin/usb_serial.rs
+++ b/examples/stm32f7/src/bin/usb_serial.rs
@@ -35,7 +35,7 @@ async fn main(_spawner: Spawner) {
35 config.product = Some("USB-serial example"); 35 config.product = Some("USB-serial example");
36 config.serial_number = Some("12345678"); 36 config.serial_number = Some("12345678");
37 37
38 // Required for windows compatiblity. 38 // Required for windows compatibility.
39 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 39 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
40 config.device_class = 0xEF; 40 config.device_class = 0xEF;
41 config.device_sub_class = 0x02; 41 config.device_sub_class = 0x02;
diff --git a/examples/stm32h5/src/bin/usb_serial.rs b/examples/stm32h5/src/bin/usb_serial.rs
index 6af269c1d..4f987cbd1 100644
--- a/examples/stm32h5/src/bin/usb_serial.rs
+++ b/examples/stm32h5/src/bin/usb_serial.rs
@@ -57,7 +57,7 @@ async fn main(_spawner: Spawner) {
57 config.product = Some("USB-serial example"); 57 config.product = Some("USB-serial example");
58 config.serial_number = Some("12345678"); 58 config.serial_number = Some("12345678");
59 59
60 // Required for windows compatiblity. 60 // Required for windows compatibility.
61 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 61 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
62 config.device_class = 0xEF; 62 config.device_class = 0xEF;
63 config.device_sub_class = 0x02; 63 config.device_sub_class = 0x02;
diff --git a/examples/stm32h7/src/bin/usb_serial.rs b/examples/stm32h7/src/bin/usb_serial.rs
index 9ef520ae2..475af116d 100644
--- a/examples/stm32h7/src/bin/usb_serial.rs
+++ b/examples/stm32h7/src/bin/usb_serial.rs
@@ -34,7 +34,7 @@ async fn main(_spawner: Spawner) {
34 config.product = Some("USB-serial example"); 34 config.product = Some("USB-serial example");
35 config.serial_number = Some("12345678"); 35 config.serial_number = Some("12345678");
36 36
37 // Required for windows compatiblity. 37 // Required for windows compatibility.
38 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 38 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
39 config.device_class = 0xEF; 39 config.device_class = 0xEF;
40 config.device_sub_class = 0x02; 40 config.device_sub_class = 0x02;
diff --git a/examples/stm32l4/src/bin/usb_serial.rs b/examples/stm32l4/src/bin/usb_serial.rs
index 663f60d52..bdb290e63 100644
--- a/examples/stm32l4/src/bin/usb_serial.rs
+++ b/examples/stm32l4/src/bin/usb_serial.rs
@@ -36,7 +36,7 @@ async fn main(_spawner: Spawner) {
36 config.product = Some("USB-serial example"); 36 config.product = Some("USB-serial example");
37 config.serial_number = Some("12345678"); 37 config.serial_number = Some("12345678");
38 38
39 // Required for windows compatiblity. 39 // Required for windows compatibility.
40 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 40 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
41 config.device_class = 0xEF; 41 config.device_class = 0xEF;
42 config.device_sub_class = 0x02; 42 config.device_sub_class = 0x02;
diff --git a/examples/stm32u5/src/bin/usb_serial.rs b/examples/stm32u5/src/bin/usb_serial.rs
index 8cd3bf2f4..4882cd2e0 100644
--- a/examples/stm32u5/src/bin/usb_serial.rs
+++ b/examples/stm32u5/src/bin/usb_serial.rs
@@ -36,7 +36,7 @@ async fn main(_spawner: Spawner) {
36 config.product = Some("USB-serial example"); 36 config.product = Some("USB-serial example");
37 config.serial_number = Some("12345678"); 37 config.serial_number = Some("12345678");
38 38
39 // Required for windows compatiblity. 39 // Required for windows compatibility.
40 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help 40 // https://developer.nordicsemi.com/nRF_Connect_SDK/doc/1.9.1/kconfig/CONFIG_CDC_ACM_IAD.html#help
41 config.device_class = 0xEF; 41 config.device_class = 0xEF;
42 config.device_sub_class = 0x02; 42 config.device_sub_class = 0x02;
diff --git a/examples/stm32wl/src/bin/uart_async.rs b/examples/stm32wl/src/bin/uart_async.rs
index f12fec4c8..ac8766af6 100644
--- a/examples/stm32wl/src/bin/uart_async.rs
+++ b/examples/stm32wl/src/bin/uart_async.rs
@@ -48,7 +48,7 @@ async fn main(_spawner: Spawner) {
48 //Write suc. 48 //Write suc.
49 } 49 }
50 Err(..) => { 50 Err(..) => {
51 //Wasnt able to write 51 //Wasn't able to write
52 } 52 }
53 } 53 }
54 } 54 }