rtic/examples/resource-user-struct.rs

73 lines
1.6 KiB
Rust
Raw Normal View History

//! examples/resource.rs
#![deny(unsafe_code)]
#![deny(warnings)]
2023-01-21 23:10:43 +01:00
#![deny(missing_docs)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965)]
mod app {
2020-10-15 18:50:17 +02:00
use cortex_m_semihosting::{debug, hprintln};
use lm3s6965::Interrupt;
2021-07-07 22:50:59 +02:00
#[shared]
struct Shared {
// A resource
shared: u32,
}
// Should not collide with the struct above
2020-06-04 18:06:18 +02:00
#[allow(dead_code)]
2021-07-07 22:50:59 +02:00
struct Shared2 {
// A resource
shared: u32,
}
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) {
rtic::pend(Interrupt::UART0);
rtic::pend(Interrupt::UART1);
2021-07-07 22:50:59 +02:00
(Shared { shared: 0 }, Local {}, init::Monotonics())
}
// `shared` cannot be accessed from this context
#[idle]
fn idle(_cx: idle::Context) -> ! {
2021-09-22 13:22:45 +02:00
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
2021-07-07 22:50:59 +02:00
// error: no `shared` field in `idle::Context`
// _cx.shared.shared += 1;
loop {}
}
// `shared` can be accessed from this context
2021-07-07 22:50:59 +02:00
#[task(binds = UART0, shared = [shared])]
fn uart0(mut cx: uart0::Context) {
2021-07-07 22:50:59 +02:00
let shared = cx.shared.shared.lock(|shared| {
*shared += 1;
*shared
});
hprintln!("UART0: shared = {}", shared);
}
// `shared` can be accessed from this context
2021-07-07 22:50:59 +02:00
#[task(binds = UART1, shared = [shared])]
fn uart1(mut cx: uart1::Context) {
2021-07-07 22:50:59 +02:00
let shared = cx.shared.shared.lock(|shared| {
*shared += 1;
*shared
});
hprintln!("UART1: shared = {}", shared);
}
}