rtic/examples/static.rs

62 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)]
2023-01-19 13:56:59 +01:00
#![deny(missing_docs)]
#![no_main]
#![no_std]
2023-01-07 17:59:39 +01:00
#![feature(type_alias_impl_trait)]
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()])]
2023-01-07 17:59:39 +01:00
fn init(cx: init::Context) -> (Shared, Local) {
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();
2023-01-07 17:59:39 +01:00
(Shared {}, Local { p, c })
}
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() {
2023-01-08 19:40:31 +01:00
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])]
2023-01-07 17:59:39 +01:00
async fn foo(c: foo::Context) {
2021-09-22 13:22:45 +02:00
*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
}