mirror of
https://github.com/rtic-rs/rtic.git
synced 2025-12-16 21:05:35 +01:00
impl Mutex on all shared resources
document how to write generic code that operates on resources
This commit is contained in:
parent
16d473a9b6
commit
37a0692a0f
11 changed files with 322 additions and 104 deletions
|
|
@ -23,7 +23,7 @@ macro_rules! println {
|
|||
|
||||
#[app(device = lm3s6965)]
|
||||
const APP: () = {
|
||||
#[init(spawn = [foo])]
|
||||
#[init]
|
||||
fn init() {
|
||||
rtfm::pend(Interrupt::UART0);
|
||||
}
|
||||
|
|
|
|||
74
examples/generics.rs
Normal file
74
examples/generics.rs
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
//! examples/generics.rs
|
||||
|
||||
#![deny(unsafe_code)]
|
||||
#![deny(warnings)]
|
||||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
extern crate panic_semihosting;
|
||||
|
||||
use cortex_m_semihosting::debug;
|
||||
use lm3s6965::Interrupt;
|
||||
use rtfm::{app, Mutex};
|
||||
|
||||
// NOTE: This convenience macro will appear in all the other examples and
|
||||
// will always look the same
|
||||
macro_rules! println {
|
||||
($($tt:tt)*) => {
|
||||
if let Ok(mut stdout) = cortex_m_semihosting::hio::hstdout() {
|
||||
use core::fmt::Write;
|
||||
|
||||
writeln!(stdout, $($tt)*).ok();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[app(device = lm3s6965)]
|
||||
const APP: () = {
|
||||
static mut SHARED: u32 = 0;
|
||||
|
||||
#[init]
|
||||
fn init() {
|
||||
rtfm::pend(Interrupt::UART0);
|
||||
rtfm::pend(Interrupt::UART1);
|
||||
}
|
||||
|
||||
#[interrupt(resources = [SHARED])]
|
||||
fn UART0() {
|
||||
static mut STATE: u32 = 0;
|
||||
|
||||
println!("UART0(STATE = {})", *STATE);
|
||||
|
||||
advance(STATE, resources.SHARED);
|
||||
|
||||
rtfm::pend(Interrupt::UART1);
|
||||
|
||||
debug::exit(debug::EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
#[interrupt(priority = 2, resources = [SHARED])]
|
||||
fn UART1() {
|
||||
static mut STATE: u32 = 0;
|
||||
|
||||
println!("UART1(STATE = {})", *STATE);
|
||||
|
||||
// just to show that `SHARED` can be accessed directly and ..
|
||||
*resources.SHARED += 0;
|
||||
// .. also through a (no-op) `lock`
|
||||
resources.SHARED.lock(|shared| *shared += 0);
|
||||
|
||||
advance(STATE, resources.SHARED);
|
||||
}
|
||||
};
|
||||
|
||||
fn advance(state: &mut u32, mut shared: impl Mutex<T = u32>) {
|
||||
*state += 1;
|
||||
|
||||
let (old, new) = shared.lock(|shared| {
|
||||
let old = *shared;
|
||||
*shared += *state;
|
||||
(old, *shared)
|
||||
});
|
||||
|
||||
println!("SHARED: {} -> {}", old, new);
|
||||
}
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
extern crate panic_semihosting;
|
||||
|
||||
use cortex_m_semihosting::debug;
|
||||
use rtfm::{app, Instant};
|
||||
use rtfm::{app, Exclusive, Instant};
|
||||
|
||||
#[app(device = lm3s6965)]
|
||||
const APP: () = {
|
||||
|
|
@ -43,6 +43,7 @@ const APP: () = {
|
|||
#[task(priority = 2, resources = [SHARED], schedule = [foo], spawn = [foo])]
|
||||
fn foo() {
|
||||
let _: Instant = scheduled;
|
||||
let _: Exclusive<u32> = resources.SHARED;
|
||||
let _: foo::Resources = resources;
|
||||
let _: foo::Schedule = schedule;
|
||||
let _: foo::Spawn = spawn;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue