aboutsummaryrefslogtreecommitdiff
path: root/tests/rp/src/bin/gpio_multicore.rs
blob: 6abcba590bc088e1a01b992cb594af076e2e33c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#![no_std]
#![no_main]
#[cfg(feature = "rp2040")]
teleprobe_meta::target!(b"rpi-pico");
#[cfg(feature = "rp235xb")]
teleprobe_meta::target!(b"pimoroni-pico-plus-2");

use defmt::{info, unwrap};
use embassy_executor::Executor;
use embassy_rp::Peri;
use embassy_rp::gpio::{Input, Level, Output, Pull};
use embassy_rp::multicore::{Stack, spawn_core1};
use embassy_rp::peripherals::{PIN_0, PIN_1};
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
use embassy_sync::channel::Channel;
use static_cell::StaticCell;
use {defmt_rtt as _, panic_probe as _};

static mut CORE1_STACK: Stack<1024> = Stack::new();
static EXECUTOR0: StaticCell<Executor> = StaticCell::new();
static EXECUTOR1: StaticCell<Executor> = StaticCell::new();
static CHANNEL0: Channel<CriticalSectionRawMutex, (), 1> = Channel::new();
static CHANNEL1: Channel<CriticalSectionRawMutex, (), 1> = Channel::new();

#[cortex_m_rt::entry]
fn main() -> ! {
    let p = embassy_rp::init(Default::default());
    spawn_core1(
        p.CORE1,
        unsafe { &mut *core::ptr::addr_of_mut!(CORE1_STACK) },
        move || {
            let executor1 = EXECUTOR1.init(Executor::new());
            executor1.run(|spawner| spawner.spawn(unwrap!(core1_task(p.PIN_1))));
        },
    );
    let executor0 = EXECUTOR0.init(Executor::new());
    executor0.run(|spawner| spawner.spawn(unwrap!(core0_task(p.PIN_0))));
}

#[embassy_executor::task]
async fn core0_task(p: Peri<'static, PIN_0>) {
    info!("CORE0 is running");

    let mut pin = Output::new(p, Level::Low);

    CHANNEL0.send(()).await;
    CHANNEL1.receive().await;

    pin.set_high();

    CHANNEL1.receive().await;

    info!("Test OK");
    cortex_m::asm::bkpt();
}

#[embassy_executor::task]
async fn core1_task(p: Peri<'static, PIN_1>) {
    info!("CORE1 is running");

    CHANNEL0.receive().await;

    let mut pin = Input::new(p, Pull::None);
    let wait = pin.wait_for_rising_edge();

    CHANNEL1.send(()).await;

    wait.await;

    CHANNEL1.send(()).await;
}