diff options
| author | Curly <[email protected]> | 2025-02-23 07:33:58 -0800 |
|---|---|---|
| committer | Curly <[email protected]> | 2025-02-23 07:33:58 -0800 |
| commit | 3932835998802fc3abf7cce4f736e072858ebfd1 (patch) | |
| tree | 5dd714b99bc74a03556c58809237c88691c293bb /examples/rp235x/src/bin/multicore.rs | |
| parent | c3c67db93e627a4fafe5e1a1123e5cbb4abafe47 (diff) | |
rename `rp23` (?) folder to `rp235x`; fix `ci.sh` to use `rp235x` folder
Diffstat (limited to 'examples/rp235x/src/bin/multicore.rs')
| -rw-r--r-- | examples/rp235x/src/bin/multicore.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/rp235x/src/bin/multicore.rs b/examples/rp235x/src/bin/multicore.rs new file mode 100644 index 000000000..7cb546c91 --- /dev/null +++ b/examples/rp235x/src/bin/multicore.rs | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | //! This example shows how to send messages between the two cores in the RP2040 chip. | ||
| 2 | //! | ||
| 3 | //! The LED on the RP Pico W board is connected differently. See wifi_blinky.rs. | ||
| 4 | |||
| 5 | #![no_std] | ||
| 6 | #![no_main] | ||
| 7 | |||
| 8 | use defmt::*; | ||
| 9 | use embassy_executor::Executor; | ||
| 10 | use embassy_rp::gpio::{Level, Output}; | ||
| 11 | use embassy_rp::multicore::{spawn_core1, Stack}; | ||
| 12 | use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; | ||
| 13 | use embassy_sync::channel::Channel; | ||
| 14 | use embassy_time::Timer; | ||
| 15 | use static_cell::StaticCell; | ||
| 16 | use {defmt_rtt as _, panic_probe as _}; | ||
| 17 | |||
| 18 | static mut CORE1_STACK: Stack<4096> = Stack::new(); | ||
| 19 | static EXECUTOR0: StaticCell<Executor> = StaticCell::new(); | ||
| 20 | static EXECUTOR1: StaticCell<Executor> = StaticCell::new(); | ||
| 21 | static CHANNEL: Channel<CriticalSectionRawMutex, LedState, 1> = Channel::new(); | ||
| 22 | |||
| 23 | enum LedState { | ||
| 24 | On, | ||
| 25 | Off, | ||
| 26 | } | ||
| 27 | |||
| 28 | #[cortex_m_rt::entry] | ||
| 29 | fn main() -> ! { | ||
| 30 | let p = embassy_rp::init(Default::default()); | ||
| 31 | let led = Output::new(p.PIN_25, Level::Low); | ||
| 32 | |||
| 33 | spawn_core1( | ||
| 34 | p.CORE1, | ||
| 35 | unsafe { &mut *core::ptr::addr_of_mut!(CORE1_STACK) }, | ||
| 36 | move || { | ||
| 37 | let executor1 = EXECUTOR1.init(Executor::new()); | ||
| 38 | executor1.run(|spawner| unwrap!(spawner.spawn(core1_task(led)))); | ||
| 39 | }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | let executor0 = EXECUTOR0.init(Executor::new()); | ||
| 43 | executor0.run(|spawner| unwrap!(spawner.spawn(core0_task()))); | ||
| 44 | } | ||
| 45 | |||
| 46 | #[embassy_executor::task] | ||
| 47 | async fn core0_task() { | ||
| 48 | info!("Hello from core 0"); | ||
| 49 | loop { | ||
| 50 | CHANNEL.send(LedState::On).await; | ||
| 51 | Timer::after_millis(100).await; | ||
| 52 | CHANNEL.send(LedState::Off).await; | ||
| 53 | Timer::after_millis(400).await; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | #[embassy_executor::task] | ||
| 58 | async fn core1_task(mut led: Output<'static>) { | ||
| 59 | info!("Hello from core 1"); | ||
| 60 | loop { | ||
| 61 | match CHANNEL.receive().await { | ||
| 62 | LedState::On => led.set_high(), | ||
| 63 | LedState::Off => led.set_low(), | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
