2023-01-27 20:20:14 +01:00
|
|
|
//! examples/async-channel.rs
|
|
|
|
|
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
2023-02-02 21:45:03 +01:00
|
|
|
#![deny(warnings)]
|
|
|
|
#![deny(unsafe_code)]
|
|
|
|
#![deny(missing_docs)]
|
2023-01-27 20:20:14 +01:00
|
|
|
|
|
|
|
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};
|
2023-01-27 20:20:14 +01:00
|
|
|
|
|
|
|
#[shared]
|
|
|
|
struct Shared {}
|
|
|
|
|
|
|
|
#[local]
|
|
|
|
struct Local {}
|
|
|
|
|
2023-01-28 21:57:43 +01:00
|
|
|
const CAPACITY: usize = 5;
|
2023-01-27 20:20:14 +01:00
|
|
|
#[init]
|
|
|
|
fn init(_: init::Context) -> (Shared, Local) {
|
2023-01-28 21:57:43 +01:00
|
|
|
let (s, r) = make_channel!(u32, CAPACITY);
|
2023-01-27 20:20:14 +01:00
|
|
|
|
|
|
|
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>) {
|
2023-01-27 20:20:14 +01:00
|
|
|
while let Ok(val) = receiver.recv().await {
|
|
|
|
hprintln!("Receiver got: {}", val);
|
2023-01-28 21:57:43 +01:00
|
|
|
if val == 3 {
|
2023-01-27 20:20:14 +01:00
|
|
|
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>) {
|
2023-01-27 20:20:14 +01:00
|
|
|
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>) {
|
2023-01-27 20:20:14 +01:00
|
|
|
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>) {
|
2023-01-27 20:20:14 +01:00
|
|
|
hprintln!("Sender 3 sending: 3");
|
2023-01-28 21:57:43 +01:00
|
|
|
sender.send(3).await.unwrap();
|
2023-01-27 20:20:14 +01:00
|
|
|
}
|
|
|
|
}
|