more examples

This commit is contained in:
Per Lindgren 2021-03-12 02:08:46 +01:00
parent 2e028abc1c
commit e85baee30b
4 changed files with 162 additions and 10 deletions

39
examples/callback.rs Normal file
View file

@ -0,0 +1,39 @@
//! examples/callback.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m_semihosting::{debug, hprintln};
use panic_semihosting as _;
#[rtic::app(device = lm3s6965, dispatchers = [SSI0])]
mod app {
use super::*;
#[init()]
fn init(c: init::Context) -> (init::LateResources, init::Monotonics) {
hprintln!("init").unwrap();
driver(&bar::spawn);
foo::spawn(123).unwrap();
(init::LateResources {}, init::Monotonics())
}
#[task()]
fn foo(c: foo::Context, data: u32) {
hprintln!("foo {}", data).unwrap();
bar::spawn().unwrap();
}
#[task()]
fn bar(_c: bar::Context) {
hprintln!("bar").unwrap();
debug::exit(debug::EXIT_SUCCESS);
}
}
// some external code (e.g. driver)
fn driver<E>(_callback: &dyn Fn() -> Result<(), E>) {
hprintln!("driver").unwrap();
}

View file

@ -48,11 +48,6 @@ mod app {
log::spawn(_cx.resources.count.lock(|count| *count)).unwrap(); log::spawn(_cx.resources.count.lock(|count| *count)).unwrap();
} }
// this wouldn't compile in `release` mode
// *_cx.resources.count += 1;
// ..
} }
// The whole task should disappear, // The whole task should disappear,
@ -66,11 +61,6 @@ mod app {
log::spawn(_cx.resources.count.lock(|count| *count)).unwrap(); log::spawn(_cx.resources.count.lock(|count| *count)).unwrap();
} }
// this wouldn't compile in `release` mode
// *_cx.resources.count += 1;
// ..
} }
#[cfg(debug_assertions)] #[cfg(debug_assertions)]

59
examples/destructure.rs Normal file
View file

@ -0,0 +1,59 @@
//! examples/destructure.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use panic_semihosting as _;
#[rtic::app(device = lm3s6965)]
mod app {
use cortex_m_semihosting::{debug, hprintln};
use lm3s6965::Interrupt;
#[resources]
struct Resources {
// Some resources to work with
#[init(0)]
a: u32,
#[init(0)]
b: u32,
#[init(0)]
c: u32,
}
#[init]
fn init(_: init::Context) -> (init::LateResources, init::Monotonics) {
rtic::pend(Interrupt::UART0);
rtic::pend(Interrupt::UART1);
(init::LateResources {}, init::Monotonics())
}
#[idle()]
fn idle(_cx: idle::Context) -> ! {
debug::exit(debug::EXIT_SUCCESS);
loop {
cortex_m::asm::nop();
}
}
// Direct destructure
#[task(binds = UART0, resources = [&a, &b, &c])]
fn uart0(cx: uart0::Context) {
let a = cx.resources.a;
let b = cx.resources.b;
let c = cx.resources.c;
hprintln!("UART0: a = {}, b = {}, c = {}", a, b, c).unwrap();
}
// De-structure-ing syntax
#[task(binds = UART1, resources = [&a, &b, &c])]
fn uart1(cx: uart1::Context) {
let uart1::Resources { a, b, c } = cx.resources;
hprintln!("UART0: a = {}, b = {}, c = {}", a, b, c).unwrap();
}
}

64
examples/generics.rs Normal file
View file

@ -0,0 +1,64 @@
//! examples/generics.rs
#![deny(unsafe_code)]
#![deny(warnings)]
#![no_main]
#![no_std]
use cortex_m_semihosting::hprintln;
use panic_semihosting as _;
use rtic::Mutex;
#[rtic::app(device = lm3s6965)]
mod app {
use cortex_m_semihosting::{debug, hprintln};
use lm3s6965::Interrupt;
#[resources]
struct Resources {
#[init(0)]
shared: u32,
}
#[init]
fn init(_: init::Context) -> (init::LateResources, init::Monotonics) {
rtic::pend(Interrupt::UART0);
rtic::pend(Interrupt::UART1);
(init::LateResources {}, init::Monotonics())
}
#[task(binds = UART0, resources = [shared])]
fn uart0(c: uart0::Context) {
let mut state = 0;
// second argument has type `resources::shared`
super::advance(&mut state, c.resources.shared);
hprintln!("UART0, state {}", state).unwrap();
rtic::pend(Interrupt::UART1);
debug::exit(debug::EXIT_SUCCESS);
}
#[task(binds = UART1, priority = 2, resources = [shared])]
fn uart1(c: uart1::Context) {
let mut state = 1;
// second argument has type `resources::shared`
super::advance(&mut state, c.resources.shared);
hprintln!("UART1, state {}", state).unwrap();
}
}
// the second parameter is generic: it can be any type that implements the `Mutex` trait
fn advance(state: &mut u32, mut shared: impl Mutex<T = u32>) {
*state += 1;
let (old, new) = shared.lock(|shared: &mut u32| {
let old = *shared;
*shared += *state;
(old, *shared)
});
hprintln!("shared: {} -> {}", old, new).unwrap();
}