diff options
| author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2022-12-13 14:55:34 +0000 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-12-13 14:55:34 +0000 |
| commit | 47747d3b73f392e53ead8ff49cd09fd017df3215 (patch) | |
| tree | 9fb66fa2bb865d224ba2d98c5f1fa3bdf9e34b26 /examples | |
| parent | 36639e5262a79df6a3c5acf5b083d51943ca5f97 (diff) | |
| parent | c4d8f3579e4927446421730d52a409d724f2800c (diff) | |
Merge #1105
1105: embassy-rp: Add multicore support r=Dirbaio a=kalkyl
This PR adds multicore support + critical-section impl using hardware spinlocks.
Based on the rp2040-hal implementation.
Co-authored-by: kalkyl <[email protected]>
Co-authored-by: Henrik Alsér <[email protected]>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/rp/Cargo.toml | 5 | ||||
| -rw-r--r-- | examples/rp/src/bin/multicore.rs | 60 |
2 files changed, 63 insertions, 2 deletions
diff --git a/examples/rp/Cargo.toml b/examples/rp/Cargo.toml index b07c471af..dfbc26426 100644 --- a/examples/rp/Cargo.toml +++ b/examples/rp/Cargo.toml | |||
| @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0" | |||
| 9 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] } | 9 | embassy-sync = { version = "0.1.0", path = "../../embassy-sync", features = ["defmt"] } |
| 10 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } | 10 | embassy-executor = { version = "0.1.0", path = "../../embassy-executor", features = ["defmt", "integrated-timers"] } |
| 11 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } | 11 | embassy-time = { version = "0.1.0", path = "../../embassy-time", features = ["defmt", "defmt-timestamp-uptime"] } |
| 12 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio"] } | 12 | embassy-rp = { version = "0.1.0", path = "../../embassy-rp", features = ["defmt", "unstable-traits", "nightly", "unstable-pac", "time-driver", "pio", "critical-section-impl"] } |
| 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } | 13 | embassy-usb = { version = "0.1.0", path = "../../embassy-usb", features = ["defmt"] } |
| 14 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } | 14 | embassy-net = { version = "0.1.0", path = "../../embassy-net", features = ["defmt", "nightly", "tcp", "dhcpv4", "medium-ethernet", "pool-16"] } |
| 15 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } | 15 | embassy-futures = { version = "0.1.0", path = "../../embassy-futures" } |
| @@ -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..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 | } | ||
