diff options
Diffstat (limited to 'examples/rp/src')
| -rw-r--r-- | examples/rp/src/bin/multicore.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/examples/rp/src/bin/multicore.rs b/examples/rp/src/bin/multicore.rs new file mode 100644 index 000000000..376b2b61e --- /dev/null +++ b/examples/rp/src/bin/multicore.rs | |||
| @@ -0,0 +1,60 @@ | |||
| 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::multicore::{spawn_core1, Stack}; | ||
| 10 | use embassy_rp::peripherals::PIN_25; | ||
| 11 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 12 | use embassy_sync::channel::Channel; | ||
| 13 | use embassy_time::{Duration, Timer}; | ||
| 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 | spawn_core1(p.CORE1, unsafe { &mut CORE1_STACK }, move || { | ||
| 32 | let executor1 = EXECUTOR1.init(Executor::new()); | ||
| 33 | executor1.run(|spawner| unwrap!(spawner.spawn(core1_task(led)))); | ||
| 34 | }); | ||
| 35 | |||
| 36 | let executor0 = EXECUTOR0.init(Executor::new()); | ||
| 37 | executor0.run(|spawner| unwrap!(spawner.spawn(core0_task()))); | ||
| 38 | } | ||
| 39 | |||
| 40 | #[embassy_executor::task] | ||
| 41 | async fn core0_task() { | ||
| 42 | info!("Hello from core 0"); | ||
| 43 | loop { | ||
| 44 | CHANNEL.send(LedState::On).await; | ||
| 45 | Timer::after(Duration::from_millis(100)).await; | ||
| 46 | CHANNEL.send(LedState::Off).await; | ||
| 47 | Timer::after(Duration::from_millis(400)).await; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | #[embassy_executor::task] | ||
| 52 | async fn core1_task(mut led: Output<'static, PIN_25>) { | ||
| 53 | info!("Hello from core 1"); | ||
| 54 | loop { | ||
| 55 | match CHANNEL.recv().await { | ||
| 56 | LedState::On => led.set_high(), | ||
| 57 | LedState::Off => led.set_low(), | ||
| 58 | } | ||
| 59 | } | ||
| 60 | } | ||
