rtic/examples/t-resource.rs

86 lines
2.1 KiB
Rust
Raw Normal View History

2021-07-07 22:50:59 +02:00
//! [compile-pass] Check code generation of shared 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]
2021-03-03 08:53:03 +01:00
use panic_semihosting as _;
2018-11-03 17:02:41 +01:00
2020-06-11 19:18:29 +02:00
#[rtic::app(device = lm3s6965)]
2020-05-19 20:00:13 +02:00
mod app {
2021-07-07 22:50:59 +02:00
#[shared]
struct Shared {
2019-07-10 22:42:44 +02:00
o2: u32, // idle
o3: u32, // EXTI0
o4: u32, // idle
o5: u32, // EXTI1
s1: u32, // idle & uart0
s2: u32, // uart0 & uart1
s3: u32, // idle & uart0
}
2021-07-07 22:50:59 +02:00
#[local]
struct Local {}
#[init]
2021-07-07 22:50:59 +02:00
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
(
Shared {
o2: 0,
o3: 0,
o4: 0,
o5: 0,
s1: 0,
s2: 0,
s3: 0,
},
Local {},
init::Monotonics(),
)
2018-11-03 17:02:41 +01:00
}
2021-07-07 22:50:59 +02:00
#[idle(shared = [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`
2021-07-07 22:50:59 +02:00
let _: shared_resources::o2 = c.shared.o2;
2018-11-03 17:02:41 +01:00
// owned by `idle` == `&'static` if read-only
2021-07-07 22:50:59 +02:00
let _: &u32 = c.shared.o4;
2018-11-03 17:02:41 +01:00
// shared with `idle` == `Mutex`
2021-07-07 22:50:59 +02:00
c.shared.s1.lock(|_| {});
2018-11-03 17:02:41 +01:00
// `&` if read-only
2021-07-07 22:50:59 +02:00
let _: &u32 = c.shared.s3;
2018-11-03 17:02:41 +01:00
loop {
cortex_m::asm::nop();
}
2018-11-03 17:02:41 +01:00
}
2021-07-07 22:50:59 +02:00
#[task(binds = UART0, shared = [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`
2021-07-07 22:50:59 +02:00
let _: shared_resources::o3 = c.shared.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
2021-07-07 22:50:59 +02:00
let _: shared_resources::s1 = c.shared.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
2021-07-07 22:50:59 +02:00
let _: shared_resources::s2 = c.shared.s2;
2018-11-03 17:02:41 +01:00
// `&` if read-only
2021-07-07 22:50:59 +02:00
let _: &u32 = c.shared.s3;
2018-11-03 17:02:41 +01:00
}
2021-07-07 22:50:59 +02:00
#[task(binds = UART1, shared = [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
2021-07-07 22:50:59 +02:00
let _: &u32 = c.shared.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
2021-07-07 22:50:59 +02:00
let _: shared_resources::s2 = c.shared.s2;
2018-11-03 17:02:41 +01:00
}
2020-04-22 12:58:14 +02:00
}