rtic/examples/multilock.rs

57 lines
1.2 KiB
Rust
Raw Normal View History

2020-11-14 16:02:36 +01:00
//! examples/mutlilock.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
2023-01-07 14:27:57 +01:00
#![feature(type_alias_impl_trait)]
2020-11-14 16:02:36 +01:00
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]
2023-01-07 14:27:57 +01:00
fn init(_: init::Context) -> (Shared, Local) {
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 {},
)
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])]
2023-01-07 14:27:57 +01:00
async 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;
2023-01-02 14:34:05 +01:00
hprintln!("Multiple locks, s1: {}, s2: {}, s3: {}", *s1, *s2, *s3).unwrap();
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
}
}