From 8b1e29f94bc89620a24a866607c5eefeb657a2df Mon Sep 17 00:00:00 2001 From: Andelf Date: Wed, 26 Jun 2024 08:09:17 +0800 Subject: [PATCH] feat(mbx): add demo code --- examples/embassy_mbx_fifo.rs | 61 +++++++++++++++++++++++++++++++++ examples/embassy_mbx_message.rs | 58 +++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 examples/embassy_mbx_fifo.rs create mode 100644 examples/embassy_mbx_message.rs diff --git a/examples/embassy_mbx_fifo.rs b/examples/embassy_mbx_fifo.rs new file mode 100644 index 0000000..7071d71 --- /dev/null +++ b/examples/embassy_mbx_fifo.rs @@ -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; + MBX0B => hal::mbx::InterruptHandler; +}); + +#[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 {} +} diff --git a/examples/embassy_mbx_message.rs b/examples/embassy_mbx_message.rs new file mode 100644 index 0000000..a2da23d --- /dev/null +++ b/examples/embassy_mbx_message.rs @@ -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; + MBX0B => hal::mbx::InterruptHandler; +}); + +#[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 {} +}