From 50581ea1fe9bacb3953bd51a6b8029998d269559 Mon Sep 17 00:00:00 2001 From: Per Lindgren Date: Sat, 13 Mar 2021 23:23:14 +0100 Subject: [PATCH] more examples updated --- examples/hardware.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++ examples/late.rs | 2 +- examples/message.rs | 55 +++++++++++++++++++++++++++++++++++++++ examples/not-sync.rs | 43 +++++++++++++++++++++++++++++++ 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 examples/hardware.rs create mode 100644 examples/message.rs create mode 100644 examples/not-sync.rs diff --git a/examples/hardware.rs b/examples/hardware.rs new file mode 100644 index 0000000000..b80600219a --- /dev/null +++ b/examples/hardware.rs @@ -0,0 +1,61 @@ +//! examples/hardware.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 { + #[task_local] + #[init(0)] + times: u32, + } + + #[init] + fn init(_: init::Context) -> (init::LateResources, init::Monotonics) { + // Pends the UART0 interrupt but its handler won't run until *after* + // `init` returns because interrupts are disabled + rtic::pend(Interrupt::UART0); // equivalent to NVIC::pend + + hprintln!("init").unwrap(); + + (init::LateResources {}, init::Monotonics()) + } + + #[idle] + fn idle(_: idle::Context) -> ! { + // interrupts are enabled again; the `UART0` handler runs at this point + + hprintln!("idle").unwrap(); + + rtic::pend(Interrupt::UART0); + + debug::exit(debug::EXIT_SUCCESS); + + loop { + cortex_m::asm::nop(); + } + } + + #[task(binds = UART0, resources = [times])] + fn uart0(cx: uart0::Context) { + let times = cx.resources.times; + // Safe access to local `static mut` variable + *times += 1; + + hprintln!( + "UART0 called {} time{}", + *times, + if *times > 1 { "s" } else { "" } + ) + .unwrap(); + } +} diff --git a/examples/late.rs b/examples/late.rs index b53daac814..5c10b66fcb 100644 --- a/examples/late.rs +++ b/examples/late.rs @@ -1,7 +1,7 @@ //! examples/late.rs #![deny(unsafe_code)] -// #![deny(warnings)] +#![deny(warnings)] #![no_main] #![no_std] diff --git a/examples/message.rs b/examples/message.rs new file mode 100644 index 0000000000..871058d8da --- /dev/null +++ b/examples/message.rs @@ -0,0 +1,55 @@ +//! examples/message.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965, dispatchers = [SSI0])] +mod app { + use cortex_m_semihosting::{debug, hprintln}; + + #[resources] + struct Resources { + #[task_local] + #[init(0)] + times: u32, + } + + #[init] + fn init(_: init::Context) -> (init::LateResources, init::Monotonics) { + foo::spawn(/* no message */).unwrap(); + + (init::LateResources {}, init::Monotonics()) + } + + #[task(resources = [times])] + fn foo(cx: foo::Context) { + let times = cx.resources.times; + + hprintln!("foo").unwrap(); + + bar::spawn(*times).unwrap(); + *times += 1; + } + + #[task] + fn bar(_: bar::Context, x: u32) { + hprintln!("bar({})", x).unwrap(); + + baz::spawn(x + 1, x + 2).unwrap(); + } + + #[task] + fn baz(_: baz::Context, x: u32, y: u32) { + hprintln!("baz({}, {})", x, y).unwrap(); + + if x + y > 4 { + debug::exit(debug::EXIT_SUCCESS); + } + + foo::spawn().unwrap(); + } +} diff --git a/examples/not-sync.rs b/examples/not-sync.rs new file mode 100644 index 0000000000..f01d40432a --- /dev/null +++ b/examples/not-sync.rs @@ -0,0 +1,43 @@ +//! `examples/not-sync.rs` + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use core::marker::PhantomData; +use panic_semihosting as _; + +pub struct NotSync { + _0: PhantomData<*const ()>, +} + +#[rtic::app(device = lm3s6965, dispatchers = [SSI0])] +mod app { + use super::NotSync; + use core::marker::PhantomData; + use cortex_m_semihosting::debug; + + #[resources] + struct Resources { + #[init(NotSync { _0: PhantomData })] + shared: NotSync, + } + + #[init] + fn init(_: init::Context) -> (init::LateResources, init::Monotonics) { + debug::exit(debug::EXIT_SUCCESS); + + (init::LateResources {}, init::Monotonics()) + } + + #[task(resources = [&shared])] + fn foo(c: foo::Context) { + let _: &NotSync = c.resources.shared; + } + + #[task(resources = [&shared])] + fn bar(c: bar::Context) { + let _: &NotSync = c.resources.shared; + } +}