diff options
Diffstat (limited to 'examples')
39 files changed, 180 insertions, 86 deletions
diff --git a/examples/baseline.rs b/examples/baseline.rs index f46b273d..e517bf08 100644 --- a/examples/baseline.rs +++ b/examples/baseline.rs @@ -11,7 +11,7 @@ use panic_semihosting as _; // NOTE: does NOT properly work on QEMU #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { +mod app { #[init(spawn = [foo])] fn init(cx: init::Context) { // omitted: initialization of `CYCCNT` @@ -51,4 +51,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/binds.rs b/examples/binds.rs index 82bf8964..9c73433b 100644 --- a/examples/binds.rs +++ b/examples/binds.rs @@ -11,7 +11,7 @@ use panic_semihosting as _; // `examples/interrupt.rs` rewritten to use `binds` #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] fn init(_: init::Context) { rtic::pend(Interrupt::UART0); @@ -45,4 +45,4 @@ const APP: () = { ) .unwrap(); } -}; +} diff --git a/examples/capacity.rs b/examples/capacity.rs index 00cec344..7ccb086a 100644 --- a/examples/capacity.rs +++ b/examples/capacity.rs @@ -10,7 +10,7 @@ use lm3s6965::Interrupt; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] fn init(_: init::Context) { rtic::pend(Interrupt::UART0); @@ -44,4 +44,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/cfg.rs b/examples/cfg.rs index 8eeeb2a9..f4848302 100644 --- a/examples/cfg.rs +++ b/examples/cfg.rs @@ -11,7 +11,8 @@ use cortex_m_semihosting::hprintln; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + #[resources] struct Resources { #[cfg(debug_assertions)] // <- `true` when using the `dev` profile #[init(0)] @@ -66,4 +67,4 @@ const APP: () = { fn SSI0(); fn QEI0(); } -}; +} diff --git a/examples/destructure.rs b/examples/destructure.rs index 1756bd9e..45d73195 100644 --- a/examples/destructure.rs +++ b/examples/destructure.rs @@ -10,7 +10,8 @@ use lm3s6965::Interrupt; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + #[resources] struct Resources { // Some resources to work with #[init(0)] @@ -44,4 +45,4 @@ const APP: () = { hprintln!("UART0: a = {}, b = {}, c = {}", a, b, c).unwrap(); } -}; +} diff --git a/examples/double_schedule.rs b/examples/double_schedule.rs index 6b3aec8f..b1b78b80 100644 --- a/examples/double_schedule.rs +++ b/examples/double_schedule.rs @@ -9,7 +9,9 @@ use panic_semihosting as _; use rtic::cyccnt::U32Ext; #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { +mod app { + + #[resources] struct Resources { nothing: (), } @@ -34,4 +36,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/generics.rs b/examples/generics.rs index 40ab81ac..c65e6518 100644 --- a/examples/generics.rs +++ b/examples/generics.rs @@ -11,7 +11,8 @@ use panic_semihosting as _; use rtic::{Exclusive, Mutex}; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + #[resources] struct Resources { #[init(0)] shared: u32, @@ -49,7 +50,7 @@ const APP: () = { // second argument has type `Exclusive<u32>` advance(STATE, Exclusive(c.resources.shared)); } -}; +} // 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>) { diff --git a/examples/hardware.rs b/examples/hardware.rs index 8105a742..831b029c 100644 --- a/examples/hardware.rs +++ b/examples/hardware.rs @@ -10,7 +10,7 @@ use lm3s6965::Interrupt; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] fn init(_: init::Context) { // Pends the UART0 interrupt but its handler won't run until *after* @@ -49,4 +49,4 @@ const APP: () = { ) .unwrap(); } -}; +} diff --git a/examples/idle.rs b/examples/idle.rs index 3d28dac8..0db05459 100644 --- a/examples/idle.rs +++ b/examples/idle.rs @@ -9,7 +9,7 @@ use cortex_m_semihosting::{debug, hprintln}; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] fn init(_: init::Context) { hprintln!("init").unwrap(); @@ -30,4 +30,4 @@ const APP: () = { cortex_m::asm::nop(); } } -}; +} diff --git a/examples/init.rs b/examples/init.rs index 1623ca7c..ea543338 100644 --- a/examples/init.rs +++ b/examples/init.rs @@ -9,7 +9,7 @@ use cortex_m_semihosting::{debug, hprintln}; use panic_semihosting as _; #[rtic::app(device = lm3s6965, peripherals = true)] -const APP: () = { +mod app { #[init] fn init(cx: init::Context) { static mut X: u32 = 0; @@ -31,4 +31,4 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); } -}; +} diff --git a/examples/late.rs b/examples/late.rs index 60b9be00..761c68f5 100644 --- a/examples/late.rs +++ b/examples/late.rs @@ -15,8 +15,13 @@ use lm3s6965::Interrupt; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + use heapless::{ + consts::*, + spsc::{Consumer, Producer}, + }; // Late resources + #[resources] struct Resources { p: Producer<'static, u32, U4>, c: Consumer<'static, u32, U4>, @@ -49,4 +54,4 @@ const APP: () = { fn uart0(c: uart0::Context) { c.resources.p.enqueue(42).unwrap(); } -}; +} diff --git a/examples/lock.rs b/examples/lock.rs index 5e3bce25..6ce61dc6 100644 --- a/examples/lock.rs +++ b/examples/lock.rs @@ -10,7 +10,8 @@ use lm3s6965::Interrupt; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + #[resources] struct Resources { #[init(0)] shared: u32, @@ -59,4 +60,4 @@ const APP: () = { fn gpioc(_: gpioc::Context) { hprintln!("C").unwrap(); } -}; +} diff --git a/examples/message.rs b/examples/message.rs index 596f2449..3f14a5a4 100644 --- a/examples/message.rs +++ b/examples/message.rs @@ -9,7 +9,7 @@ use cortex_m_semihosting::{debug, hprintln}; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init(spawn = [foo])] fn init(c: init::Context) { c.spawn.foo(/* no message */).unwrap(); @@ -49,4 +49,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/not-send.rs b/examples/not-send.rs index 16a874dc..45f7e4e7 100644 --- a/examples/not-send.rs +++ b/examples/not-send.rs @@ -16,7 +16,10 @@ pub struct NotSend { } #[app(device = lm3s6965)] -const APP: () = { +mod app { + use super::NotSend; + + #[resources] struct Resources { #[init(None)] shared: Option<NotSend>, @@ -60,4 +63,4 @@ const APP: () = { fn SSI0(); fn QEI0(); } -}; +} diff --git a/examples/not-sync.rs b/examples/not-sync.rs index a7eaac8e..75816424 100644 --- a/examples/not-sync.rs +++ b/examples/not-sync.rs @@ -15,7 +15,11 @@ pub struct NotSync { } #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + use super::NotSync; + use core::marker::PhantomData; + + #[resources] struct Resources { #[init(NotSync { _0: PhantomData })] shared: NotSync, @@ -42,4 +46,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/only-shared-access.rs b/examples/only-shared-access.rs index c022b037..91d0b7ad 100644 --- a/examples/only-shared-access.rs +++ b/examples/only-shared-access.rs @@ -10,7 +10,8 @@ use lm3s6965::Interrupt; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + #[resources] struct Resources { key: u32, } @@ -35,4 +36,4 @@ const APP: () = { fn uart1(cx: uart1::Context) { hprintln!("UART1(key = {:#x})", cx.resources.key).unwrap(); } -}; +} diff --git a/examples/periodic.rs b/examples/periodic.rs index 405346e3..2d4c73b5 100644 --- a/examples/periodic.rs +++ b/examples/periodic.rs @@ -13,7 +13,8 @@ const PERIOD: u32 = 8_000_000; // NOTE: does NOT work on QEMU! #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { +mod app { + #[init(schedule = [foo])] fn init(cx: init::Context) { // omitted: initialization of `CYCCNT` @@ -35,4 +36,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/peripherals-taken.rs b/examples/peripherals-taken.rs index cd4ba0f0..10bc2603 100644 --- a/examples/peripherals-taken.rs +++ b/examples/peripherals-taken.rs @@ -7,10 +7,10 @@ use cortex_m_semihosting::debug; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] - fn main(_: main::Context) { + fn taskmain(_: taskmain::Context) { assert!(cortex_m::Peripherals::take().is_none()); debug::exit(debug::EXIT_SUCCESS); } -}; +} diff --git a/examples/pool.rs b/examples/pool.rs index 824d5bd8..27408d55 100644 --- a/examples/pool.rs +++ b/examples/pool.rs @@ -18,7 +18,12 @@ use rtic::app; pool!(P: [u8; 128]); #[app(device = lm3s6965)] -const APP: () = { +mod app { + use crate::Box; + + // Import the memory pool into scope + use super::P; + #[init] fn init(_: init::Context) { static mut MEMORY: [u8; 512] = [0; 512]; @@ -66,4 +71,4 @@ const APP: () = { fn SSI0(); fn QEI0(); } -}; +} diff --git a/examples/preempt.rs b/examples/preempt.rs index 3cb11029..02193011 100644 --- a/examples/preempt.rs +++ b/examples/preempt.rs @@ -9,7 +9,7 @@ use panic_semihosting as _; use rtic::app; #[app(device = lm3s6965)] -const APP: () = { +mod app { #[init] fn init(_: init::Context) { rtic::pend(Interrupt::GPIOA); @@ -34,4 +34,4 @@ const APP: () = { rtic::pend(Interrupt::GPIOB); hprintln!(" GPIOC - end").unwrap(); } -}; +} diff --git a/examples/ramfunc.rs b/examples/ramfunc.rs index 1f95d496..789d7871 100644 --- a/examples/ramfunc.rs +++ b/examples/ramfunc.rs @@ -9,7 +9,7 @@ use cortex_m_semihosting::{debug, hprintln}; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init(spawn = [bar])] fn init(c: init::Context) { c.spawn.bar().unwrap(); @@ -38,4 +38,4 @@ const APP: () = { #[link_section = ".data.UART1"] fn UART1(); } -}; +} diff --git a/examples/resource-user-struct.rs b/examples/resource-user-struct.rs new file mode 100644 index 00000000..dbda9a3f --- /dev/null +++ b/examples/resource-user-struct.rs @@ -0,0 +1,61 @@ +//! examples/resource.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtic::app(device = lm3s6965)] +mod app { + #[resources] + struct Resources { + // A resource + #[init(0)] + shared: u32, + } + + // Should not collide with the struct above + #[allow(dead_code)] + struct Resources2 { + // A resource + shared: u32, + } + + #[init] + fn init(_: init::Context) { + rtic::pend(Interrupt::UART0); + rtic::pend(Interrupt::UART1); + } + + // `shared` cannot be accessed from this context + #[idle] + fn idle(_cx: idle::Context) -> ! { + debug::exit(debug::EXIT_SUCCESS); + + // error: no `resources` field in `idle::Context` + // _cx.resources.shared += 1; + + loop {} + } + + // `shared` can be accessed from this context + #[task(binds = UART0, resources = [shared])] + fn uart0(cx: uart0::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; + + hprintln!("UART0: shared = {}", shared).unwrap(); + } + + // `shared` can be accessed from this context + #[task(binds = UART1, resources = [shared])] + fn uart1(cx: uart1::Context) { + *cx.resources.shared += 1; + + hprintln!("UART1: shared = {}", cx.resources.shared).unwrap(); + } +} diff --git a/examples/resource.rs b/examples/resource.rs index 2361fd00..4cd0f4ca 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -10,7 +10,8 @@ use lm3s6965::Interrupt; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + #[resources] struct Resources { // A resource #[init(0)] @@ -52,4 +53,4 @@ const APP: () = { hprintln!("UART1: shared = {}", cx.resources.shared).unwrap(); } -}; +} diff --git a/examples/schedule.rs b/examples/schedule.rs index 70a7a5e3..d5547b67 100644 --- a/examples/schedule.rs +++ b/examples/schedule.rs @@ -12,7 +12,7 @@ use rtic::cyccnt::{Instant, U32Ext as _}; // NOTE: does NOT work on QEMU! #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { +mod app { #[init(schedule = [foo, bar])] fn init(mut cx: init::Context) { // Initialize (enable) the monotonic timer (CYCCNT) @@ -50,4 +50,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/shared-with-init.rs b/examples/shared-with-init.rs index bd55f7ef..9f7e26aa 100644 --- a/examples/shared-with-init.rs +++ b/examples/shared-with-init.rs @@ -13,7 +13,10 @@ use rtic::app; pub struct MustBeSend; #[app(device = lm3s6965)] -const APP: () = { +mod app { + use super::MustBeSend; + + #[resources] struct Resources { #[init(None)] shared: Option<MustBeSend>, @@ -37,4 +40,4 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); } } -}; +} diff --git a/examples/smallest.rs b/examples/smallest.rs index ec3fa970..b8cbf87e 100644 --- a/examples/smallest.rs +++ b/examples/smallest.rs @@ -7,4 +7,4 @@ use panic_semihosting as _; // panic handler use rtic::app; #[app(device = lm3s6965)] -const APP: () = {}; +mod app {} diff --git a/examples/t-binds.rs b/examples/t-binds.rs index 588ac46f..7d7bd7d2 100644 --- a/examples/t-binds.rs +++ b/examples/t-binds.rs @@ -8,7 +8,7 @@ use panic_halt as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] fn init(_: init::Context) {} @@ -23,7 +23,7 @@ const APP: () = { fn bar(c: bar::Context) { bar_trampoline(c) } -}; +} #[allow(dead_code)] fn foo_trampoline(_: foo::Context) {} diff --git a/examples/t-cfg-resources.rs b/examples/t-cfg-resources.rs index 4f7fd635..61eb4c7b 100644 --- a/examples/t-cfg-resources.rs +++ b/examples/t-cfg-resources.rs @@ -6,19 +6,17 @@ use panic_halt as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + #[resources] struct Resources { // A resource #[init(0)] shared: u32, - // A conditionally compiled resource behind feature_x #[cfg(feature = "feature_x")] x: u32, - - dummy: (), + dummy: (), // dummy such that we have at least one late resource } - #[init] fn init(_: init::Context) -> init::LateResources { init::LateResources { @@ -35,4 +33,4 @@ const APP: () = { cortex_m::asm::nop(); } } -}; +} diff --git a/examples/t-cfg.rs b/examples/t-cfg.rs index b6c9e472..3deb107c 100644 --- a/examples/t-cfg.rs +++ b/examples/t-cfg.rs @@ -6,7 +6,8 @@ use panic_halt as _; #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { +mod app { + #[resources] struct Resources { #[cfg(never)] #[init(0)] @@ -52,4 +53,4 @@ const APP: () = { fn SSI0(); fn QEI0(); } -}; +} diff --git a/examples/t-htask-main.rs b/examples/t-htask-main.rs index c4bebf94..998252e1 100644 --- a/examples/t-htask-main.rs +++ b/examples/t-htask-main.rs @@ -7,14 +7,14 @@ use cortex_m_semihosting::debug; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] fn init(_: init::Context) { rtic::pend(lm3s6965::Interrupt::UART0) } #[task(binds = UART0)] - fn main(_: main::Context) { + fn taskmain(_: taskmain::Context) { debug::exit(debug::EXIT_SUCCESS); } -}; +} diff --git a/examples/t-idle-main.rs b/examples/t-idle-main.rs index 051a9ee8..03a52cb2 100644 --- a/examples/t-idle-main.rs +++ b/examples/t-idle-main.rs @@ -7,15 +7,15 @@ use cortex_m_semihosting::debug; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] fn init(_: init::Context) {} #[idle] - fn main(_: main::Context) -> ! { + fn taskmain(_: taskmain::Context) -> ! { debug::exit(debug::EXIT_SUCCESS); loop { cortex_m::asm::nop(); } } -}; +} diff --git a/examples/t-init-main.rs b/examples/t-init-main.rs index 6a6cd991..d0814877 100644 --- a/examples/t-init-main.rs +++ b/examples/t-init-main.rs @@ -7,9 +7,9 @@ use cortex_m_semihosting::debug; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init] - fn main(_: main::Context) { + fn taskmain(_: taskmain::Context) { debug::exit(debug::EXIT_SUCCESS); } -}; +} diff --git a/examples/t-late-not-send.rs b/examples/t-late-not-send.rs index d2a9b63c..345d9aef 100644 --- a/examples/t-late-not-send.rs +++ b/examples/t-late-not-send.rs @@ -12,7 +12,10 @@ pub struct NotSend { } #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + use super::NotSend; + + #[resources] struct Resources { x: NotSend, #[init(None)] @@ -35,4 +38,4 @@ const APP: () = { cortex_m::asm::nop(); } } -}; +} diff --git a/examples/t-resource.rs b/examples/t-resource.rs index 81ba1856..94b527fa 100644 --- a/examples/t-resource.rs +++ b/examples/t-resource.rs @@ -8,7 +8,8 @@ use panic_halt as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { + #[resources] struct Resources { #[init(0)] o1: u32, // init @@ -86,4 +87,4 @@ const APP: () = { // no `Mutex` proxy when co-owned by cooperative (same priority) tasks let _: &mut u32 = c.resources.s2; } -}; +} diff --git a/examples/t-schedule.rs b/examples/t-schedule.rs index 3854aad3..ef2eb080 100644 --- a/examples/t-schedule.rs +++ b/examples/t-schedule.rs @@ -9,7 +9,7 @@ use panic_halt as _; use rtic::cyccnt::{Instant, U32Ext as _}; #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { +mod app { #[init(schedule = [foo, bar, baz])] fn init(c: init::Context) { let _: Result<(), ()> = c.schedule.foo(c.start + 10.cycles()); @@ -61,4 +61,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/t-spawn.rs b/examples/t-spawn.rs index 35831ccf..72143c5b 100644 --- a/examples/t-spawn.rs +++ b/examples/t-spawn.rs @@ -8,7 +8,7 @@ use panic_halt as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init(spawn = [foo, bar, baz])] fn init(c: init::Context) { let _: Result<(), ()> = c.spawn.foo(); @@ -60,4 +60,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/t-stask-main.rs b/examples/t-stask-main.rs index f2709404..3e650f60 100644 --- a/examples/t-stask-main.rs +++ b/examples/t-stask-main.rs @@ -7,14 +7,14 @@ use cortex_m_semihosting::debug; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { - #[init(spawn = [main])] +mod app { + #[init(spawn = [taskmain])] fn init(cx: init::Context) { - cx.spawn.main().ok(); + cx.spawn.taskmain().ok(); } #[task] - fn main(_: main::Context) { + fn taskmain(_: taskmain::Context) { debug::exit(debug::EXIT_SUCCESS); } @@ -24,4 +24,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} diff --git a/examples/task.rs b/examples/task.rs index 12c4ac83..f510df74 100644 --- a/examples/task.rs +++ b/examples/task.rs @@ -9,7 +9,7 @@ use cortex_m_semihosting::{debug, hprintln}; use panic_semihosting as _; #[rtic::app(device = lm3s6965)] -const APP: () = { +mod app { #[init(spawn = [foo])] fn init(c: init::Context) { c.spawn.foo().unwrap(); @@ -52,4 +52,4 @@ const APP: () = { fn SSI0(); fn QEI0(); } -}; +} diff --git a/examples/types.rs b/examples/types.rs index 5233f868..cd7e8a2f 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -10,7 +10,8 @@ use panic_semihosting as _; use rtic::cyccnt; #[rtic::app(device = lm3s6965, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)] -const APP: () = { +mod app { + #[resources] struct Resources { #[init(0)] shared: u32, @@ -60,4 +61,4 @@ const APP: () = { extern "C" { fn SSI0(); } -}; +} |