2020-06-04 17:43:16 +02:00
|
|
|
//! examples/resource.rs
|
|
|
|
|
|
|
|
#![deny(unsafe_code)]
|
|
|
|
#![deny(warnings)]
|
|
|
|
#![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 {
|
2020-06-04 17:43:16 +02:00
|
|
|
// 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 {
|
2020-06-04 17:43:16 +02:00
|
|
|
// A resource
|
|
|
|
shared: u32,
|
|
|
|
}
|
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
#[local]
|
|
|
|
struct Local {}
|
|
|
|
|
2020-06-04 17:43:16 +02:00
|
|
|
#[init]
|
2021-07-07 22:50:59 +02:00
|
|
|
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
|
2020-06-04 17:43:16 +02:00
|
|
|
rtic::pend(Interrupt::UART0);
|
|
|
|
rtic::pend(Interrupt::UART1);
|
2020-10-05 18:25:15 +02:00
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
(Shared { shared: 0 }, Local {}, init::Monotonics())
|
2020-06-04 17:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// `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
|
2020-06-04 17:43:16 +02:00
|
|
|
|
2021-07-07 22:50:59 +02:00
|
|
|
// error: no `shared` field in `idle::Context`
|
|
|
|
// _cx.shared.shared += 1;
|
2020-06-04 17:43:16 +02:00
|
|
|
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `shared` can be accessed from this context
|
2021-07-07 22:50:59 +02:00
|
|
|
#[task(binds = UART0, shared = [shared])]
|
2020-10-22 21:36:32 +02:00
|
|
|
fn uart0(mut cx: uart0::Context) {
|
2021-07-07 22:50:59 +02:00
|
|
|
let shared = cx.shared.shared.lock(|shared| {
|
2020-10-22 21:36:32 +02:00
|
|
|
*shared += 1;
|
|
|
|
*shared
|
|
|
|
});
|
2020-06-04 17:43:16 +02:00
|
|
|
|
2023-01-11 21:33:44 +01:00
|
|
|
hprintln!("UART0: shared = {}", shared);
|
2020-06-04 17:43:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// `shared` can be accessed from this context
|
2021-07-07 22:50:59 +02:00
|
|
|
#[task(binds = UART1, shared = [shared])]
|
2020-10-22 21:36:32 +02:00
|
|
|
fn uart1(mut cx: uart1::Context) {
|
2021-07-07 22:50:59 +02:00
|
|
|
let shared = cx.shared.shared.lock(|shared| {
|
2020-10-22 21:36:32 +02:00
|
|
|
*shared += 1;
|
|
|
|
*shared
|
|
|
|
});
|
2020-06-04 17:43:16 +02:00
|
|
|
|
2023-01-11 21:33:44 +01:00
|
|
|
hprintln!("UART1: shared = {}", shared);
|
2020-06-04 17:43:16 +02:00
|
|
|
}
|
|
|
|
}
|