diff --git a/examples/callback.rs b/examples/callback.rs new file mode 100644 index 0000000000..299c94105a --- /dev/null +++ b/examples/callback.rs @@ -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(_callback: &dyn Fn() -> Result<(), E>) { + hprintln!("driver").unwrap(); +} diff --git a/examples/cfg-whole-task.rs b/examples/cfg-whole-task.rs index 47c3530e17..ac64676dc0 100644 --- a/examples/cfg-whole-task.rs +++ b/examples/cfg-whole-task.rs @@ -48,11 +48,6 @@ mod app { 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, @@ -66,11 +61,6 @@ mod app { log::spawn(_cx.resources.count.lock(|count| *count)).unwrap(); } - - // this wouldn't compile in `release` mode - // *_cx.resources.count += 1; - - // .. } #[cfg(debug_assertions)] diff --git a/examples/destructure.rs b/examples/destructure.rs new file mode 100644 index 0000000000..9ad695293c --- /dev/null +++ b/examples/destructure.rs @@ -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(); + } +} diff --git a/examples/generics.rs b/examples/generics.rs new file mode 100644 index 0000000000..97ddfe65a7 --- /dev/null +++ b/examples/generics.rs @@ -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) { + *state += 1; + + let (old, new) = shared.lock(|shared: &mut u32| { + let old = *shared; + *shared += *state; + (old, *shared) + }); + + hprintln!("shared: {} -> {}", old, new).unwrap(); +}