rtic/examples/static.rs

60 lines
1.5 KiB
Rust
Raw Normal View History

2020-09-08 19:51:10 +02:00
//! examples/static.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
2021-09-22 13:22:45 +02:00
#[rtic::app(device = lm3s6965, dispatchers = [UART0])]
2020-09-08 19:51:10 +02:00
mod app {
2020-10-15 18:50:17 +02:00
use cortex_m_semihosting::{debug, hprintln};
2021-04-03 19:30:34 +02:00
use heapless::spsc::{Consumer, Producer, Queue};
2020-09-08 19:51:10 +02:00
2021-07-07 22:50:59 +02:00
#[shared]
2021-09-22 13:22:45 +02:00
struct Shared {}
#[local]
struct Local {
2021-04-03 19:30:34 +02:00
p: Producer<'static, u32, 5>,
c: Consumer<'static, u32, 5>,
}
2021-04-03 19:30:34 +02:00
#[init(local = [q: Queue<u32, 5> = Queue::new()])]
2021-07-07 22:50:59 +02:00
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
2021-09-22 13:22:45 +02:00
// q has 'static life-time so after the split and return of `init`
// it will continue to exist and be allocated
2021-07-07 22:50:59 +02:00
let (p, c) = cx.local.q.split();
2021-09-22 13:22:45 +02:00
foo::spawn().unwrap();
(Shared {}, Local { p, c }, init::Monotonics())
}
2021-09-22 13:22:45 +02:00
#[idle(local = [c])]
fn idle(c: idle::Context) -> ! {
loop {
2021-09-22 13:22:45 +02:00
// Lock-free access to the same underlying queue!
if let Some(data) = c.local.c.dequeue() {
hprintln!("received message: {}", data);
2021-09-22 13:22:45 +02:00
// Run foo until data
if data == 3 {
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
} else {
foo::spawn().unwrap();
}
}
}
}
2021-09-22 13:22:45 +02:00
#[task(local = [p, state: u32 = 0])]
fn foo(c: foo::Context) {
*c.local.state += 1;
// Lock-free access to the same underlying queue!
c.local.p.enqueue(*c.local.state).unwrap();
}
2020-09-08 19:51:10 +02:00
}