rtic/examples/lm3s6965/examples/async-channel.rs

63 lines
1.6 KiB
Rust
Raw Normal View History

//! examples/async-channel.rs
#![no_main]
#![no_std]
2023-02-02 21:45:03 +01:00
#![deny(warnings)]
#![deny(unsafe_code)]
#![deny(missing_docs)]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [SSI0])]
mod app {
use cortex_m_semihosting::{debug, hprintln};
2023-03-01 20:11:00 +01:00
use rtic_sync::{channel::*, make_channel};
#[shared]
struct Shared {}
#[local]
struct Local {}
2023-01-28 21:57:43 +01:00
const CAPACITY: usize = 5;
#[init]
fn init(_: init::Context) -> (Shared, Local) {
2023-01-28 21:57:43 +01:00
let (s, r) = make_channel!(u32, CAPACITY);
receiver::spawn(r).unwrap();
sender1::spawn(s.clone()).unwrap();
sender2::spawn(s.clone()).unwrap();
sender3::spawn(s).unwrap();
(Shared {}, Local {})
}
#[task]
2023-01-28 21:57:43 +01:00
async fn receiver(_c: receiver::Context, mut receiver: Receiver<'static, u32, CAPACITY>) {
while let Ok(val) = receiver.recv().await {
hprintln!("Receiver got: {}", val);
2023-01-28 21:57:43 +01:00
if val == 3 {
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
}
}
}
#[task]
2023-01-28 21:57:43 +01:00
async fn sender1(_c: sender1::Context, mut sender: Sender<'static, u32, CAPACITY>) {
hprintln!("Sender 1 sending: 1");
sender.send(1).await.unwrap();
}
#[task]
2023-01-28 21:57:43 +01:00
async fn sender2(_c: sender2::Context, mut sender: Sender<'static, u32, CAPACITY>) {
hprintln!("Sender 2 sending: 2");
sender.send(2).await.unwrap();
}
#[task]
2023-01-28 21:57:43 +01:00
async fn sender3(_c: sender3::Context, mut sender: Sender<'static, u32, CAPACITY>) {
hprintln!("Sender 3 sending: 3");
2023-01-28 21:57:43 +01:00
sender.send(3).await.unwrap();
}
}