Skip to content

Commit

Permalink
feat(mbx): add demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
andelf committed Jun 26, 2024
1 parent 78268e8 commit 8b1e29f
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
61 changes: 61 additions & 0 deletions examples/embassy_mbx_fifo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#![no_main]
#![no_std]
#![feature(type_alias_impl_trait)]
#![feature(abi_riscv_interrupt)]

use embassy_executor::Spawner;
use embassy_time::Timer;
use futures_util::StreamExt;
use hal::gpio::{Level, Output};
use hal::mbx::Mbx;
use hal::{bind_interrupts, peripherals};
use {defmt_rtt as _, hpm_hal as hal};

bind_interrupts!(struct Irqs {
MBX0A => hal::mbx::InterruptHandler<peripherals::MBX0A>;
MBX0B => hal::mbx::InterruptHandler<peripherals::MBX0B>;
});

#[embassy_executor::task]
async fn mailbox(mbx: Mbx<'static>) {
let mut mbx = mbx;
let mut i = 1;
loop {
defmt::info!("[task0] sending {}", i);
mbx.send_fifo(i).await;
i += 1;

Timer::after_millis(100).await;
}
}

#[embassy_executor::main(entry = "hpm_hal::entry")]
async fn main(spawner: Spawner) -> ! {
let p = hal::init(Default::default());

defmt::info!("Board init!");

let mut led = Output::new(p.PA10, Level::Low, Default::default());

let inbox = Mbx::new(p.MBX0A, Irqs);
let mut outbox = Mbx::new(p.MBX0B, Irqs);

spawner.spawn(mailbox(inbox)).unwrap();
defmt::info!("Mailbox task spawned!");

loop {
led.toggle();

while let Some(val) = outbox.next().await {
defmt::info!("[main] receive: {}", val);
Timer::after_millis(1000).await; // slower
}
}
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
defmt::info!("Panic!");

loop {}
}
58 changes: 58 additions & 0 deletions examples/embassy_mbx_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#![no_main]
#![no_std]
#![feature(type_alias_impl_trait)]
#![feature(abi_riscv_interrupt)]

use embassy_executor::Spawner;
use embassy_time::Timer;
use hal::gpio::{Level, Output};
use hal::mbx::Mbx;
use hal::{bind_interrupts, peripherals};
use {defmt_rtt as _, hpm_hal as hal};

bind_interrupts!(struct Irqs {
MBX0A => hal::mbx::InterruptHandler<peripherals::MBX0A>;
MBX0B => hal::mbx::InterruptHandler<peripherals::MBX0B>;
});

#[embassy_executor::task]
async fn mailbox(mbx: Mbx<'static>) {
let mut mbx = mbx;
let mut i = 114514;
loop {
defmt::info!("[task0] sending {}", i);
mbx.send(i).await;
i += 1;

Timer::after_millis(100).await;
}
}

#[embassy_executor::main(entry = "hpm_hal::entry")]
async fn main(spawner: Spawner) -> ! {
let p = hal::init(Default::default());

defmt::info!("Board init!");

let mut led = Output::new(p.PA10, Level::Low, Default::default());

let inbox = Mbx::new(p.MBX0A, Irqs);
let mut outbox = Mbx::new(p.MBX0B, Irqs);

spawner.spawn(mailbox(inbox)).unwrap();
defmt::info!("Mailbox task spawned!");

loop {
led.toggle();

let val = outbox.recv().await;
defmt::info!("[main] receive: {}", val);
}
}

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
defmt::info!("Panic!");

loop {}
}

0 comments on commit 8b1e29f

Please sign in to comment.