rtic/examples/t-resource.rs

88 lines
2.3 KiB
Rust
Raw Permalink Normal View History

//! [compile-pass] Check code generation of resources
2018-11-03 17:02:41 +01:00
2019-04-21 20:13:15 +02:00
#![deny(unsafe_code)]
#![deny(warnings)]
2018-11-03 17:02:41 +01:00
#![no_main]
#![no_std]
use panic_halt as _;
2018-11-03 17:02:41 +01:00
2020-06-11 19:18:29 +02:00
#[rtic::app(device = lm3s6965)]
2018-11-03 17:02:41 +01:00
const APP: () = {
2019-07-10 22:42:44 +02:00
struct Resources {
#[init(0)]
o1: u32, // init
#[init(0)]
o2: u32, // idle
#[init(0)]
o3: u32, // EXTI0
#[init(0)]
o4: u32, // idle
#[init(0)]
o5: u32, // EXTI1
#[init(0)]
o6: u32, // init
#[init(0)]
s1: u32, // idle & uart0
#[init(0)]
s2: u32, // uart0 & uart1
#[init(0)]
s3: u32, // idle & uart0
}
#[init(resources = [o1, o4, o5, o6, s3])]
2019-04-21 20:13:15 +02:00
fn init(c: init::Context) {
2018-11-03 17:02:41 +01:00
// owned by `init` == `&'static mut`
2019-07-10 22:42:44 +02:00
let _: &'static mut u32 = c.resources.o1;
2018-11-03 17:02:41 +01:00
// owned by `init` == `&'static` if read-only
2019-07-10 22:42:44 +02:00
let _: &'static u32 = c.resources.o6;
2018-11-03 17:02:41 +01:00
// `init` has exclusive access to all resources
2019-07-10 22:42:44 +02:00
let _: &mut u32 = c.resources.o4;
let _: &mut u32 = c.resources.o5;
let _: &mut u32 = c.resources.s3;
2018-11-03 17:02:41 +01:00
}
2019-07-10 22:42:44 +02:00
#[idle(resources = [o2, &o4, s1, &s3])]
2019-04-21 20:13:15 +02:00
fn idle(mut c: idle::Context) -> ! {
2018-11-03 17:02:41 +01:00
// owned by `idle` == `&'static mut`
2019-07-10 22:42:44 +02:00
let _: &'static mut u32 = c.resources.o2;
2018-11-03 17:02:41 +01:00
// owned by `idle` == `&'static` if read-only
2019-07-10 22:42:44 +02:00
let _: &'static u32 = c.resources.o4;
2018-11-03 17:02:41 +01:00
// shared with `idle` == `Mutex`
2019-07-10 22:42:44 +02:00
c.resources.s1.lock(|_| {});
2018-11-03 17:02:41 +01:00
// `&` if read-only
2019-07-10 22:42:44 +02:00
let _: &u32 = c.resources.s3;
2018-11-03 17:02:41 +01:00
loop {}
}
2019-07-10 22:42:44 +02:00
#[task(binds = UART0, resources = [o3, s1, s2, &s3])]
2019-06-20 06:19:59 +02:00
fn uart0(c: uart0::Context) {
2018-11-03 17:02:41 +01:00
// owned by interrupt == `&mut`
2019-07-10 22:42:44 +02:00
let _: &mut u32 = c.resources.o3;
2018-11-03 17:02:41 +01:00
2018-11-04 19:46:49 +01:00
// no `Mutex` proxy when access from highest priority task
2019-07-10 22:42:44 +02:00
let _: &mut u32 = c.resources.s1;
2018-11-03 17:02:41 +01:00
2018-11-04 19:46:49 +01:00
// no `Mutex` proxy when co-owned by cooperative (same priority) tasks
2019-07-10 22:42:44 +02:00
let _: &mut u32 = c.resources.s2;
2018-11-03 17:02:41 +01:00
// `&` if read-only
2019-07-10 22:42:44 +02:00
let _: &u32 = c.resources.s3;
2018-11-03 17:02:41 +01:00
}
2019-07-10 22:42:44 +02:00
#[task(binds = UART1, resources = [s2, &o5])]
2019-06-20 06:19:59 +02:00
fn uart1(c: uart1::Context) {
2018-11-03 17:02:41 +01:00
// owned by interrupt == `&` if read-only
2019-07-10 22:42:44 +02:00
let _: &u32 = c.resources.o5;
2018-11-03 17:02:41 +01:00
2018-11-04 19:46:49 +01:00
// no `Mutex` proxy when co-owned by cooperative (same priority) tasks
2019-07-10 22:42:44 +02:00
let _: &mut u32 = c.resources.s2;
2018-11-03 17:02:41 +01:00
}
};