rtic/examples/multilock.rs

58 lines
1.2 KiB
Rust
Raw Permalink Normal View History

2020-11-14 16:02:36 +01:00
//! examples/mutlilock.rs
#![deny(unsafe_code)]
#![deny(warnings)]
2023-01-21 23:10:43 +01:00
#![deny(missing_docs)]
2020-11-14 16:02:36 +01:00
#![no_main]
#![no_std]
use panic_semihosting as _;
2021-09-22 13:22:45 +02:00
#[rtic::app(device = lm3s6965, dispatchers = [GPIOA])]
2020-11-14 16:02:36 +01:00
mod app {
use cortex_m_semihosting::{debug, hprintln};
2021-07-07 22:50:59 +02:00
#[shared]
struct Shared {
2020-11-14 16:02:36 +01:00
shared1: u32,
shared2: u32,
shared3: u32,
}
2021-07-07 22:50:59 +02:00
#[local]
struct Local {}
2020-11-14 16:02:36 +01:00
#[init]
2021-07-07 22:50:59 +02:00
fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {
2021-09-22 13:22:45 +02:00
locks::spawn().unwrap();
2020-11-14 16:02:36 +01:00
2021-07-07 22:50:59 +02:00
(
Shared {
shared1: 0,
shared2: 0,
shared3: 0,
},
Local {},
init::Monotonics(),
)
2020-11-14 16:02:36 +01:00
}
// when omitted priority is assumed to be `1`
2021-09-22 13:22:45 +02:00
#[task(shared = [shared1, shared2, shared3])]
2020-11-14 16:02:36 +01:00
fn locks(c: locks::Context) {
2021-09-22 13:22:45 +02:00
let s1 = c.shared.shared1;
let s2 = c.shared.shared2;
let s3 = c.shared.shared3;
2020-11-14 16:02:36 +01:00
(s1, s2, s3).lock(|s1, s2, s3| {
*s1 += 1;
*s2 += 1;
*s3 += 1;
hprintln!("Multiple locks, s1: {}, s2: {}, s3: {}", *s1, *s2, *s3);
2020-11-14 16:02:36 +01:00
});
2021-09-22 13:22:45 +02:00
debug::exit(debug::EXIT_SUCCESS); // Exit QEMU simulator
2020-11-14 16:02:36 +01:00
}
}