diff options
| author | kalkyl <[email protected]> | 2022-12-10 08:26:35 +0100 |
|---|---|---|
| committer | kalkyl <[email protected]> | 2022-12-10 08:26:35 +0100 |
| commit | 1ee58492fbc58b721dc5ed9037c6787af257cbeb (patch) | |
| tree | ce840a88cd6dec6b21005943a547f6418e9a1f1b /examples | |
| parent | 5d4f09156af094732edc5c01332af8b13db10e0f (diff) | |
embassy-rp: Add multicore support
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/Cargo.toml | 3 | ||||
| -rw-r--r-- | examples/rp/src/bin/multicore.rs | 62 |
2 files changed, 64 insertions, 1 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index 60a8ba94d..bd624a329 100644 --- a/examples/rp/Cargo.toml +++ b/examples/rp/Cargo.toml | |||
| @@ -18,7 +18,8 @@ embassy-usb-logger = { version = "0.1.0", path = "../../embassy-usb-logger" } | |||
| 18 | defmt = "0.3" | 18 | defmt = "0.3" |
| 19 | defmt-rtt = "0.4" | 19 | defmt-rtt = "0.4" |
| 20 | 20 | ||
| 21 | cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } | 21 | #cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } |
| 22 | cortex-m = { version = "0.7.6" } | ||
| 22 | cortex-m-rt = "0.7.0" | 23 | cortex-m-rt = "0.7.0" |
| 23 | panic-probe = { version = "0.3", features = ["print-defmt"] } | 24 | panic-probe = { version = "0.3", features = ["print-defmt"] } |
| 24 | futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] } | 25 | futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] } |
diff --git a/examples/rp/src/bin/multicore.rs b/examples/rp/src/bin/multicore.rs new file mode 100644 index 000000000..46d3cd17c --- /dev/null +++ b/examples/rp/src/bin/multicore.rs | |||
| @@ -0,0 +1,62 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | #![feature(type_alias_impl_trait)] | ||
| 4 | |||
| 5 | use defmt::*; | ||
| 6 | use embassy_executor::Executor; | ||
| 7 | use embassy_executor::_export::StaticCell; | ||
| 8 | use embassy_rp::gpio::{Level, Output}; | ||
| 9 | use embassy_rp::peripherals::PIN_25; | ||
| 10 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 11 | use embassy_sync::channel::Channel; | ||
| 12 | use embassy_time::{Duration, Timer}; | ||
| 13 | use embassy_rp::multicore::{Multicore, Stack}; | ||
| 14 | use {defmt_rtt as _, panic_probe as _}; | ||
| 15 | |||
| 16 | static mut CORE1_STACK: Stack<4096> = Stack::new(); | ||
| 17 | static EXECUTOR0: StaticCell<Executor> = StaticCell::new(); | ||
| 18 | static EXECUTOR1: StaticCell<Executor> = StaticCell::new(); | ||
| 19 | static CHANNEL: Channel<CriticalSectionRawMutex, LedState, 1> = Channel::new(); | ||
| 20 | |||
| 21 | enum LedState { | ||
| 22 | On, | ||
| 23 | Off, | ||
| 24 | } | ||
| 25 | |||
| 26 | #[cortex_m_rt::entry] | ||
| 27 | fn main() -> ! { | ||
| 28 | let p = embassy_rp::init(Default::default()); | ||
| 29 | let led = Output::new(p.PIN_25, Level::Low); | ||
| 30 | |||
| 31 | let mut mc = Multicore::new(); | ||
| 32 | let (_, core1) = mc.cores(); | ||
| 33 | let _ = core1.spawn(unsafe { &mut CORE1_STACK.mem }, move || { | ||
| 34 | let executor1 = EXECUTOR1.init(Executor::new()); | ||
| 35 | executor1.run(|spawner| unwrap!(spawner.spawn(core1_task(led)))); | ||
| 36 | }); | ||
| 37 | |||
| 38 | let executor0 = EXECUTOR0.init(Executor::new()); | ||
| 39 | executor0.run(|spawner| unwrap!(spawner.spawn(core0_task()))); | ||
| 40 | } | ||
| 41 | |||
| 42 | #[embassy_executor::task] | ||
| 43 | async fn core0_task() { | ||
| 44 | info!("Hello from core 0"); | ||
| 45 | loop { | ||
| 46 | CHANNEL.send(LedState::On).await; | ||
| 47 | Timer::after(Duration::from_millis(100)).await; | ||
| 48 | CHANNEL.send(LedState::Off).await; | ||
| 49 | Timer::after(Duration::from_millis(400)).await; | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | #[embassy_executor::task] | ||
| 54 | async fn core1_task(mut led: Output<'static, PIN_25>) { | ||
| 55 | info!("Hello from core 1"); | ||
| 56 | loop { | ||
| 57 | match CHANNEL.recv().await { | ||
| 58 | LedState::On => led.set_high(), | ||
| 59 | LedState::Off => led.set_low(), | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } \ No newline at end of file | ||
