rtic/examples/complex.rs

129 lines
3.5 KiB
Rust
Raw Normal View History

//! examples/complex.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965)]
mod app {
use cortex_m_semihosting::{debug, hprintln};
use lm3s6965::Interrupt;
#[shared]
struct Shared {
s2: u32, // shared with ceiling 2
s3: u32, // shared with ceiling 3
s4: u32, // shared with ceiling 4
}
#[local]
struct Local {}
#[init]
2023-01-07 17:59:39 +01:00
fn init(_: init::Context) -> (Shared, Local) {
2023-01-08 19:40:31 +01:00
hprintln!("init");
(
Shared {
s2: 0,
s3: 0,
s4: 0,
},
Local {},
)
}
#[idle(shared = [s2, s3])]
fn idle(mut cx: idle::Context) -> ! {
2023-01-08 19:40:31 +01:00
hprintln!("idle p0 started");
rtic::pend(Interrupt::GPIOC);
cx.shared.s3.lock(|s| {
2023-01-08 19:40:31 +01:00
hprintln!("idle enter lock s3 {}", s);
hprintln!("idle pend t0");
rtic::pend(Interrupt::GPIOA); // t0 p2, with shared ceiling 3
2023-01-08 19:40:31 +01:00
hprintln!("idle pend t1");
rtic::pend(Interrupt::GPIOB); // t1 p3, with shared ceiling 3
2023-01-08 19:40:31 +01:00
hprintln!("idle pend t2");
rtic::pend(Interrupt::GPIOC); // t2 p4, no sharing
2023-01-08 19:40:31 +01:00
hprintln!("idle still in lock s3 {}", s);
});
2023-01-08 19:40:31 +01:00
hprintln!("\nback in idle");
cx.shared.s2.lock(|s| {
2023-01-08 19:40:31 +01:00
hprintln!("enter lock s2 {}", s);
hprintln!("idle pend t0");
rtic::pend(Interrupt::GPIOA); // t0 p2, with shared ceiling 2
2023-01-08 19:40:31 +01:00
hprintln!("idle pend t1");
rtic::pend(Interrupt::GPIOB); // t1 p3, no sharing
2023-01-08 19:40:31 +01:00
hprintln!("idle pend t2");
rtic::pend(Interrupt::GPIOC); // t2 p4, no sharing
2023-01-08 19:40:31 +01:00
hprintln!("idle still in lock s2 {}", s);
});
2023-01-08 19:40:31 +01:00
hprintln!("\nidle exit");
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
loop {
cortex_m::asm::nop();
}
}
#[task(binds = GPIOA, priority = 2, local = [times: u32 = 0], shared = [s2, s3])]
fn t0(cx: t0::Context) {
// Safe access to local `static mut` variable
*cx.local.times += 1;
hprintln!(
"t0 p2 called {} time{}",
*cx.local.times,
if *cx.local.times > 1 { "s" } else { "" }
2023-01-08 19:40:31 +01:00
);
hprintln!("t0 p2 exit");
}
#[task(binds = GPIOB, priority = 3, local = [times: u32 = 0], shared = [s3, s4])]
fn t1(mut cx: t1::Context) {
// Safe access to local `static mut` variable
*cx.local.times += 1;
hprintln!(
"t1 p3 called {} time{}",
*cx.local.times,
if *cx.local.times > 1 { "s" } else { "" }
2023-01-08 19:40:31 +01:00
);
cx.shared.s4.lock(|s| {
2023-01-08 19:40:31 +01:00
hprintln!("t1 enter lock s4 {}", s);
hprintln!("t1 pend t0");
rtic::pend(Interrupt::GPIOA); // t0 p2, with shared ceiling 2
2023-01-08 19:40:31 +01:00
hprintln!("t1 pend t2");
rtic::pend(Interrupt::GPIOC); // t2 p4, no sharing
2023-01-08 19:40:31 +01:00
hprintln!("t1 still in lock s4 {}", s);
});
2023-01-08 19:40:31 +01:00
hprintln!("t1 p3 exit");
}
#[task(binds = GPIOC, priority = 4, local = [times: u32 = 0], shared = [s4])]
fn t2(mut cx: t2::Context) {
// Safe access to local `static mut` variable
*cx.local.times += 1;
hprintln!(
"t2 p4 called {} time{}",
*cx.local.times,
if *cx.local.times > 1 { "s" } else { "" }
2023-01-08 19:40:31 +01:00
);
cx.shared.s4.lock(|s| {
2023-01-08 19:40:31 +01:00
hprintln!("enter lock s4 {}", s);
*s += 1;
});
2023-01-08 19:40:31 +01:00
hprintln!("t3 p4 exit");
}
}