diff options
| -rw-r--r-- | embassy-rp/Cargo.toml | 2 | ||||
| -rw-r--r-- | embassy-rp/src/interrupt.rs | 6 | ||||
| -rw-r--r-- | examples/rp/Cargo.toml | 2 | ||||
| -rw-r--r-- | examples/rp/src/bin/multiprio.rs | 152 |
4 files changed, 160 insertions, 2 deletions
diff --git a/embassy-rp/Cargo.toml b/embassy-rp/Cargo.toml index 59d0bf338..ea8f54f36 100644 --- a/embassy-rp/Cargo.toml +++ b/embassy-rp/Cargo.toml | |||
| @@ -61,7 +61,7 @@ embedded-storage = { version = "0.3" } | |||
| 61 | rand_core = "0.6.4" | 61 | rand_core = "0.6.4" |
| 62 | fixed = "1.23.1" | 62 | fixed = "1.23.1" |
| 63 | 63 | ||
| 64 | rp-pac = { version = "2", features = ["rt"] } | 64 | rp-pac = { version = "2", features = ["rt"], path = "../../rp-pac" } |
| 65 | 65 | ||
| 66 | embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } | 66 | embedded-hal-02 = { package = "embedded-hal", version = "0.2.6", features = ["unproven"] } |
| 67 | embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10", optional = true} | 67 | embedded-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); | |||
| 34 | declare!(I2C0_IRQ); | 34 | declare!(I2C0_IRQ); |
| 35 | declare!(I2C1_IRQ); | 35 | declare!(I2C1_IRQ); |
| 36 | declare!(RTC_IRQ); | 36 | declare!(RTC_IRQ); |
| 37 | declare!(SWI_IRQ_0); | ||
| 38 | declare!(SWI_IRQ_1); | ||
| 39 | declare!(SWI_IRQ_2); | ||
| 40 | declare!(SWI_IRQ_3); | ||
| 41 | declare!(SWI_IRQ_4); | ||
| 42 | declare!(SWI_IRQ_5); | ||
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index 57f6f5c67..3f74129a4 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] |
| 9 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] } | 9 | embassy-embedded-hal = { version = "0.1.0", path = "../../embassy-embedded-hal", features = ["defmt"] } |
| 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } | 10 | embassy-sync = { version = "0.2.0", path = "../../embassy-sync", features = ["defmt"] } |
| 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "defmt", "integrated-timers"] } | 11 | embassy-executor = { version = "0.2.0", path = "../../embassy-executor", features = ["arch-cortex-m", "executor-thread", "executor-interrupt", "defmt", "integrated-timers"] } |
| 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime"] } | 12 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["nightly", "unstable-traits", "defmt", "defmt-timestamp-uptime"] } |
| 13 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] } | 13 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "critical-section-impl"] } |
| 14 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 14 | embassy-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 | |||
| 60 | use core::mem; | ||
| 61 | |||
| 62 | use cortex_m::peripheral::NVIC; | ||
| 63 | use cortex_m_rt::entry; | ||
| 64 | use defmt::{info, unwrap}; | ||
| 65 | use embassy_rp::executor::{Executor, InterruptExecutor}; | ||
| 66 | use embassy_rp::interrupt; | ||
| 67 | use embassy_rp::pac::Interrupt; | ||
| 68 | use embassy_time::{Duration, Instant, Timer, TICK_HZ}; | ||
| 69 | use static_cell::StaticCell; | ||
| 70 | use {defmt_rtt as _, panic_probe as _}; | ||
| 71 | |||
| 72 | #[embassy_executor::task] | ||
| 73 | async fn run_high() { | ||
| 74 | loop { | ||
| 75 | info!(" [high] tick!"); | ||
| 76 | Timer::after(Duration::from_ticks(673740)).await; | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | #[embassy_executor::task] | ||
| 81 | async 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] | ||
| 98 | async 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 | |||
| 114 | static EXECUTOR_HIGH: InterruptExecutor = InterruptExecutor::new(); | ||
| 115 | static EXECUTOR_MED: InterruptExecutor = InterruptExecutor::new(); | ||
| 116 | static EXECUTOR_LOW: StaticCell<Executor> = StaticCell::new(); | ||
| 117 | |||
| 118 | #[interrupt] | ||
| 119 | unsafe fn SWI_IRQ_1() { | ||
| 120 | EXECUTOR_HIGH.on_interrupt() | ||
| 121 | } | ||
| 122 | |||
| 123 | #[interrupt] | ||
| 124 | unsafe fn SWI_IRQ_0() { | ||
| 125 | EXECUTOR_MED.on_interrupt() | ||
| 126 | } | ||
| 127 | |||
| 128 | #[entry] | ||
| 129 | fn 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 | } | ||
