aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorkalkyl <[email protected]>2022-12-10 08:26:35 +0100
committerkalkyl <[email protected]>2022-12-10 08:26:35 +0100
commit1ee58492fbc58b721dc5ed9037c6787af257cbeb (patch)
treece840a88cd6dec6b21005943a547f6418e9a1f1b /examples
parent5d4f09156af094732edc5c01332af8b13db10e0f (diff)
embassy-rp: Add multicore support
Diffstat (limited to 'examples')
-rw-r--r--examples/rp/Cargo.toml3
-rw-r--r--examples/rp/src/bin/multicore.rs62
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" }
18defmt = "0.3" 18defmt = "0.3"
19defmt-rtt = "0.4" 19defmt-rtt = "0.4"
20 20
21cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] } 21#cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
22cortex-m = { version = "0.7.6" }
22cortex-m-rt = "0.7.0" 23cortex-m-rt = "0.7.0"
23panic-probe = { version = "0.3", features = ["print-defmt"] } 24panic-probe = { version = "0.3", features = ["print-defmt"] }
24futures = { version = "0.3.17", default-features = false, features = ["async-await", "cfg-target-has-atomic", "unstable"] } 25futures = { 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
5use defmt::*;
6use embassy_executor::Executor;
7use embassy_executor::_export::StaticCell;
8use embassy_rp::gpio::{Level, Output};
9use embassy_rp::peripherals::PIN_25;
10use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
11use embassy_sync::channel::Channel;
12use embassy_time::{Duration, Timer};
13use embassy_rp::multicore::{Multicore, Stack};
14use {defmt_rtt as _, panic_probe as _};
15
16static mut CORE1_STACK: Stack<4096> = Stack::new();
17static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
18static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
19static CHANNEL: Channel<CriticalSectionRawMutex, LedState, 1> = Channel::new();
20
21enum LedState {
22 On,
23 Off,
24}
25
26#[cortex_m_rt::entry]
27fn 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]
43async 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]
54async 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