From 81275bfa4f41e2066770087f3a33cad4227eab41 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 13 Jun 2019 23:56:59 +0200 Subject: rtfm-syntax refactor + heterogeneous multi-core support --- examples/baseline.rs | 5 ++- examples/binds.rs | 3 +- examples/capacity.rs | 3 +- examples/cfg.rs | 9 +++--- examples/generics.rs | 13 +++----- examples/idle.rs | 3 +- examples/init.rs | 7 ++-- examples/interrupt.rs | 3 +- examples/late.rs | 9 +++--- examples/lock.rs | 5 ++- examples/message.rs | 3 +- examples/not-send.rs | 7 ++-- examples/not-sync.rs | 3 +- examples/periodic.rs | 7 ++-- examples/pool.rs | 3 +- examples/ramfunc.rs | 3 +- examples/resource.rs | 7 ++-- examples/schedule.rs | 7 ++-- examples/shared-with-init.rs | 3 +- examples/smallest.rs | 9 ++---- examples/static.rs | 7 ++-- examples/t-binds.rs | 30 +++++++++++++++++ examples/t-cfg.rs | 47 +++++++++++++++++++++++++++ examples/t-late-not-send.rs | 36 +++++++++++++++++++++ examples/t-resource.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++ examples/t-schedule.rs | 59 +++++++++++++++++++++++++++++++++ examples/t-spawn.rs | 58 +++++++++++++++++++++++++++++++++ examples/task.rs | 3 +- examples/types.rs | 9 +++--- 29 files changed, 360 insertions(+), 78 deletions(-) create mode 100644 examples/t-binds.rs create mode 100644 examples/t-cfg.rs create mode 100644 examples/t-late-not-send.rs create mode 100644 examples/t-resource.rs create mode 100644 examples/t-schedule.rs create mode 100644 examples/t-spawn.rs (limited to 'examples') diff --git a/examples/baseline.rs b/examples/baseline.rs index d743107..cc9b412 100644 --- a/examples/baseline.rs +++ b/examples/baseline.rs @@ -5,13 +5,12 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; +use panic_semihosting as _; // NOTE: does NOT properly work on QEMU -#[rtfm::app(device = lm3s6965)] +#[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { #[init(spawn = [foo])] fn init(c: init::Context) { diff --git a/examples/binds.rs b/examples/binds.rs index 3d2d9b5..1959d75 100644 --- a/examples/binds.rs +++ b/examples/binds.rs @@ -5,10 +5,9 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; +use panic_semihosting as _; // `examples/interrupt.rs` rewritten to use `binds` #[rtfm::app(device = lm3s6965)] diff --git a/examples/capacity.rs b/examples/capacity.rs index 07edd9b..e1a835c 100644 --- a/examples/capacity.rs +++ b/examples/capacity.rs @@ -5,10 +5,9 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { diff --git a/examples/cfg.rs b/examples/cfg.rs index 03f9dbd..b1f65cf 100644 --- a/examples/cfg.rs +++ b/examples/cfg.rs @@ -5,10 +5,9 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - #[cfg(debug_assertions)] use cortex_m_semihosting::hprintln; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { @@ -21,12 +20,12 @@ const APP: () = { } #[task(priority = 3, resources = [COUNT], spawn = [log])] - fn foo(c: foo::Context) { + fn foo(_c: foo::Context) { #[cfg(debug_assertions)] { - *c.resources.COUNT += 1; + *_c.resources.COUNT += 1; - c.spawn.log(*c.resources.COUNT).ok(); + _c.spawn.log(*_c.resources.COUNT).ok(); } // this wouldn't compile in `release` mode diff --git a/examples/generics.rs b/examples/generics.rs index e624da3..a35ba23 100644 --- a/examples/generics.rs +++ b/examples/generics.rs @@ -5,11 +5,10 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; -use rtfm::Mutex; +use panic_semihosting as _; +use rtfm::{Exclusive, Mutex}; #[rtfm::app(device = lm3s6965)] const APP: () = { @@ -35,17 +34,15 @@ const APP: () = { } #[interrupt(priority = 2, resources = [SHARED])] - fn UART1(mut c: UART1::Context) { + fn UART1(c: UART1::Context) { static mut STATE: u32 = 0; hprintln!("UART1(STATE = {})", *STATE).unwrap(); - // just to show that `SHARED` can be accessed directly and .. + // just to show that `SHARED` can be accessed directly *c.resources.SHARED += 0; - // .. also through a (no-op) `lock` - c.resources.SHARED.lock(|shared| *shared += 0); - advance(STATE, c.resources.SHARED); + advance(STATE, Exclusive(c.resources.SHARED)); } }; diff --git a/examples/idle.rs b/examples/idle.rs index d10cc43..c6f676b 100644 --- a/examples/idle.rs +++ b/examples/idle.rs @@ -5,9 +5,8 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { diff --git a/examples/init.rs b/examples/init.rs index df68779..361db4b 100644 --- a/examples/init.rs +++ b/examples/init.rs @@ -5,18 +5,17 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; +use panic_semihosting as _; -#[rtfm::app(device = lm3s6965)] +#[rtfm::app(device = lm3s6965, peripherals = true)] const APP: () = { #[init] fn init(c: init::Context) { static mut X: u32 = 0; // Cortex-M peripherals - let _core: rtfm::Peripherals = c.core; + let _core: cortex_m::Peripherals = c.core; // Device specific peripherals let _device: lm3s6965::Peripherals = c.device; diff --git a/examples/interrupt.rs b/examples/interrupt.rs index dd6efa0..3fe8ff3 100644 --- a/examples/interrupt.rs +++ b/examples/interrupt.rs @@ -5,10 +5,9 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { diff --git a/examples/late.rs b/examples/late.rs index 0074fb3..4d48a6a 100644 --- a/examples/late.rs +++ b/examples/late.rs @@ -5,20 +5,21 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use heapless::{ consts::*, spsc::{Consumer, Producer, Queue}, }; use lm3s6965::Interrupt; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { // Late resources - static mut P: Producer<'static, u32, U4> = (); - static mut C: Consumer<'static, u32, U4> = (); + extern "C" { + static mut P: Producer<'static, u32, U4>; + static mut C: Consumer<'static, u32, U4>; + } #[init] fn init(_: init::Context) -> init::LateResources { diff --git a/examples/lock.rs b/examples/lock.rs index 814c736..b7d36b4 100644 --- a/examples/lock.rs +++ b/examples/lock.rs @@ -5,10 +5,9 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { @@ -46,7 +45,7 @@ const APP: () = { } #[interrupt(priority = 2, resources = [SHARED])] - fn GPIOB(mut c: GPIOB::Context) { + fn GPIOB(c: GPIOB::Context) { // the higher priority task does *not* need a critical section *c.resources.SHARED += 1; diff --git a/examples/message.rs b/examples/message.rs index 1fd3b9d..8bfed52 100644 --- a/examples/message.rs +++ b/examples/message.rs @@ -5,9 +5,8 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { diff --git a/examples/not-send.rs b/examples/not-send.rs index c1b6bcd..f240e51 100644 --- a/examples/not-send.rs +++ b/examples/not-send.rs @@ -5,11 +5,10 @@ #![no_main] #![no_std] -extern crate panic_halt; - use core::marker::PhantomData; use cortex_m_semihosting::debug; +use panic_halt as _; use rtfm::app; pub struct NotSend { @@ -38,13 +37,13 @@ const APP: () = { } #[task(priority = 2, resources = [SHARED])] - fn baz(mut c: baz::Context) { + fn baz(c: baz::Context) { // scenario 2: resource shared between tasks that run at the same priority *c.resources.SHARED = Some(NotSend { _0: PhantomData }); } #[task(priority = 2, resources = [SHARED])] - fn quux(mut c: quux::Context) { + fn quux(c: quux::Context) { // scenario 2 let _not_send = c.resources.SHARED.take().unwrap(); diff --git a/examples/not-sync.rs b/examples/not-sync.rs index bc71406..6b49911 100644 --- a/examples/not-sync.rs +++ b/examples/not-sync.rs @@ -5,11 +5,10 @@ #![no_main] #![no_std] -extern crate panic_halt; - use core::marker::PhantomData; use cortex_m_semihosting::debug; +use panic_halt as _; pub struct NotSync { _0: PhantomData<*const ()>, diff --git a/examples/periodic.rs b/examples/periodic.rs index f784118..b8910db 100644 --- a/examples/periodic.rs +++ b/examples/periodic.rs @@ -5,15 +5,14 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::hprintln; -use rtfm::Instant; +use panic_semihosting as _; +use rtfm::cyccnt::{Instant, U32Ext}; const PERIOD: u32 = 8_000_000; // NOTE: does NOT work on QEMU! -#[rtfm::app(device = lm3s6965)] +#[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { #[init(schedule = [foo])] fn init(c: init::Context) { diff --git a/examples/pool.rs b/examples/pool.rs index 0b594b1..db321b5 100644 --- a/examples/pool.rs +++ b/examples/pool.rs @@ -5,14 +5,13 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use heapless::{ pool, pool::singleton::{Box, Pool}, }; use lm3s6965::Interrupt; +use panic_semihosting as _; use rtfm::app; // Declare a pool of 128-byte memory blocks diff --git a/examples/ramfunc.rs b/examples/ramfunc.rs index 4b0d69c..c38635f 100644 --- a/examples/ramfunc.rs +++ b/examples/ramfunc.rs @@ -5,9 +5,8 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { diff --git a/examples/resource.rs b/examples/resource.rs index 06bdf39..8268950 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -5,10 +5,9 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { @@ -33,7 +32,7 @@ const APP: () = { // `SHARED` can be access from this context #[interrupt(resources = [SHARED])] - fn UART0(mut c: UART0::Context) { + fn UART0(c: UART0::Context) { *c.resources.SHARED += 1; hprintln!("UART0: SHARED = {}", c.resources.SHARED).unwrap(); @@ -41,7 +40,7 @@ const APP: () = { // `SHARED` can be access from this context #[interrupt(resources = [SHARED])] - fn UART1(mut c: UART1::Context) { + fn UART1(c: UART1::Context) { *c.resources.SHARED += 1; hprintln!("UART1: SHARED = {}", c.resources.SHARED).unwrap(); diff --git a/examples/schedule.rs b/examples/schedule.rs index eaafb4c..1cf2b1e 100644 --- a/examples/schedule.rs +++ b/examples/schedule.rs @@ -5,13 +5,12 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::hprintln; -use rtfm::Instant; +use panic_halt as _; +use rtfm::cyccnt::{Instant, U32Ext as _}; // NOTE: does NOT work on QEMU! -#[rtfm::app(device = lm3s6965)] +#[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { #[init(schedule = [foo, bar])] fn init(c: init::Context) { diff --git a/examples/shared-with-init.rs b/examples/shared-with-init.rs index 0fb9191..1640ca9 100644 --- a/examples/shared-with-init.rs +++ b/examples/shared-with-init.rs @@ -5,10 +5,9 @@ #![no_main] #![no_std] -extern crate panic_halt; - use cortex_m_semihosting::debug; use lm3s6965::Interrupt; +use panic_halt as _; use rtfm::app; pub struct MustBeSend; diff --git a/examples/smallest.rs b/examples/smallest.rs index c153716..e422806 100644 --- a/examples/smallest.rs +++ b/examples/smallest.rs @@ -5,13 +5,8 @@ #![no_main] #![no_std] -// panic-handler crate -extern crate panic_semihosting; - +use panic_semihosting as _; // panic handler use rtfm::app; #[app(device = lm3s6965)] -const APP: () = { - #[init] - fn init(_: init::Context) {} -}; +const APP: () = {}; diff --git a/examples/static.rs b/examples/static.rs index 2e3b5b4..eeb522f 100644 --- a/examples/static.rs +++ b/examples/static.rs @@ -5,14 +5,15 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; use lm3s6965::Interrupt; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { - static KEY: u32 = (); + extern "C" { + static KEY: u32; + } #[init] fn init(_: init::Context) -> init::LateResources { diff --git a/examples/t-binds.rs b/examples/t-binds.rs new file mode 100644 index 0000000..b4693a4 --- /dev/null +++ b/examples/t-binds.rs @@ -0,0 +1,30 @@ +//! [compile-pass] Check that `binds` works as advertised + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use panic_halt as _; + +#[rtfm::app(device = lm3s6965)] +const APP: () = { + #[init] + fn init(_: init::Context) {} + + #[exception(binds = SVCall)] + fn foo(c: foo::Context) { + foo_trampoline(c) + } + + #[interrupt(binds = UART0)] + fn bar(c: bar::Context) { + bar_trampoline(c) + } +}; + +#[allow(dead_code)] +fn foo_trampoline(_: foo::Context) {} + +#[allow(dead_code)] +fn bar_trampoline(_: bar::Context) {} diff --git a/examples/t-cfg.rs b/examples/t-cfg.rs new file mode 100644 index 0000000..158eef5 --- /dev/null +++ b/examples/t-cfg.rs @@ -0,0 +1,47 @@ +//! [compile-pass] check that `#[cfg]` attributes are respected + +#![no_main] +#![no_std] + +use panic_halt as _; + +#[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] +const APP: () = { + #[cfg(never)] + static mut FOO: u32 = 0; + + #[init] + fn init(_: init::Context) { + #[cfg(never)] + static mut BAR: u32 = 0; + } + + #[idle] + fn idle(_: idle::Context) -> ! { + #[cfg(never)] + static mut BAR: u32 = 0; + + loop {} + } + + #[task(resources = [FOO], schedule = [quux], spawn = [quux])] + fn foo(_: foo::Context) { + #[cfg(never)] + static mut BAR: u32 = 0; + } + + #[task(priority = 3, resources = [FOO], schedule = [quux], spawn = [quux])] + fn bar(_: bar::Context) { + #[cfg(never)] + static mut BAR: u32 = 0; + } + + #[cfg(never)] + #[task] + fn quux(_: quux::Context) {} + + extern "C" { + fn UART0(); + fn UART1(); + } +}; diff --git a/examples/t-late-not-send.rs b/examples/t-late-not-send.rs new file mode 100644 index 0000000..55a053d --- /dev/null +++ b/examples/t-late-not-send.rs @@ -0,0 +1,36 @@ +//! [compile-pass] late resources don't need to be `Send` if they are owned by `idle` + +#![no_main] +#![no_std] + +use core::marker::PhantomData; + +use panic_halt as _; + +pub struct NotSend { + _0: PhantomData<*const ()>, +} + +#[rtfm::app(device = lm3s6965)] +const APP: () = { + extern "C" { + static mut X: NotSend; + } + + static mut Y: Option = None; + + #[init(resources = [Y])] + fn init(c: init::Context) -> init::LateResources { + // equivalent to late resource initialization + *c.resources.Y = Some(NotSend { _0: PhantomData }); + + init::LateResources { + X: NotSend { _0: PhantomData }, + } + } + + #[idle(resources = [X, Y])] + fn idle(_: idle::Context) -> ! { + loop {} + } +}; diff --git a/examples/t-resource.rs b/examples/t-resource.rs new file mode 100644 index 0000000..40dc2a6 --- /dev/null +++ b/examples/t-resource.rs @@ -0,0 +1,77 @@ +//! [compile-pass] Check code generation of resources + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use panic_halt as _; + +#[rtfm::app(device = lm3s6965)] +const APP: () = { + static mut O1: u32 = 0; // init + static mut O2: u32 = 0; // idle + static mut O3: u32 = 0; // EXTI0 + static O4: u32 = 0; // idle + static O5: u32 = 0; // EXTI1 + static O6: u32 = 0; // init + + static mut S1: u32 = 0; // idle & EXTI0 + static mut S2: u32 = 0; // EXTI0 & EXTI1 + static S3: u32 = 0; + + #[init(resources = [O1, O4, O5, O6, S3])] + fn init(c: init::Context) { + // owned by `init` == `&'static mut` + let _: &'static mut u32 = c.resources.O1; + + // owned by `init` == `&'static` if read-only + let _: &'static u32 = c.resources.O6; + + // `init` has exclusive access to all resources + let _: &mut u32 = c.resources.O4; + let _: &mut u32 = c.resources.O5; + let _: &mut u32 = c.resources.S3; + } + + #[idle(resources = [O2, O4, S1, S3])] + fn idle(mut c: idle::Context) -> ! { + // owned by `idle` == `&'static mut` + let _: &'static mut u32 = c.resources.O2; + + // owned by `idle` == `&'static` if read-only + let _: &'static u32 = c.resources.O4; + + // shared with `idle` == `Mutex` + c.resources.S1.lock(|_| {}); + + // `&` if read-only + let _: &u32 = c.resources.S3; + + loop {} + } + + #[interrupt(resources = [O3, S1, S2, S3])] + fn UART0(c: UART0::Context) { + // owned by interrupt == `&mut` + let _: &mut u32 = c.resources.O3; + + // no `Mutex` proxy when access from highest priority task + let _: &mut u32 = c.resources.S1; + + // no `Mutex` proxy when co-owned by cooperative (same priority) tasks + let _: &mut u32 = c.resources.S2; + + // `&` if read-only + let _: &u32 = c.resources.S3; + } + + #[interrupt(resources = [S2, O5])] + fn UART1(c: UART1::Context) { + // owned by interrupt == `&` if read-only + let _: &u32 = c.resources.O5; + + // 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 new file mode 100644 index 0000000..67ff358 --- /dev/null +++ b/examples/t-schedule.rs @@ -0,0 +1,59 @@ +//! [compile-pass] Check `schedule` code generation + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use panic_halt as _; +use rtfm::cyccnt::{Instant, U32Ext as _}; + +#[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] +const APP: () = { + #[init(schedule = [foo, bar, baz])] + fn init(c: init::Context) { + let _: Result<(), ()> = c.schedule.foo(c.start + 10.cycles()); + let _: Result<(), u32> = c.schedule.bar(c.start + 20.cycles(), 0); + let _: Result<(), (u32, u32)> = c.schedule.baz(c.start + 30.cycles(), 0, 1); + } + + #[idle(schedule = [foo, bar, baz])] + fn idle(c: idle::Context) -> ! { + let _: Result<(), ()> = c.schedule.foo(Instant::now() + 40.cycles()); + let _: Result<(), u32> = c.schedule.bar(Instant::now() + 50.cycles(), 0); + let _: Result<(), (u32, u32)> = c.schedule.baz(Instant::now() + 60.cycles(), 0, 1); + + loop {} + } + + #[exception(schedule = [foo, bar, baz])] + fn SVCall(c: SVCall::Context) { + let _: Result<(), ()> = c.schedule.foo(c.start + 70.cycles()); + let _: Result<(), u32> = c.schedule.bar(c.start + 80.cycles(), 0); + let _: Result<(), (u32, u32)> = c.schedule.baz(c.start + 90.cycles(), 0, 1); + } + + #[interrupt(schedule = [foo, bar, baz])] + fn UART0(c: UART0::Context) { + let _: Result<(), ()> = c.schedule.foo(c.start + 100.cycles()); + let _: Result<(), u32> = c.schedule.bar(c.start + 110.cycles(), 0); + let _: Result<(), (u32, u32)> = c.schedule.baz(c.start + 120.cycles(), 0, 1); + } + + #[task(schedule = [foo, bar, baz])] + fn foo(c: foo::Context) { + let _: Result<(), ()> = c.schedule.foo(c.scheduled + 130.cycles()); + let _: Result<(), u32> = c.schedule.bar(c.scheduled + 140.cycles(), 0); + let _: Result<(), (u32, u32)> = c.schedule.baz(c.scheduled + 150.cycles(), 0, 1); + } + + #[task] + fn bar(_: bar::Context, _x: u32) {} + + #[task] + fn baz(_: baz::Context, _x: u32, _y: u32) {} + + extern "C" { + fn UART1(); + } +}; diff --git a/examples/t-spawn.rs b/examples/t-spawn.rs new file mode 100644 index 0000000..6bb9b31 --- /dev/null +++ b/examples/t-spawn.rs @@ -0,0 +1,58 @@ +//! [compile-pass] Check code generation of `spawn` + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use panic_halt as _; + +#[rtfm::app(device = lm3s6965)] +const APP: () = { + #[init(spawn = [foo, bar, baz])] + fn init(c: init::Context) { + let _: Result<(), ()> = c.spawn.foo(); + let _: Result<(), u32> = c.spawn.bar(0); + let _: Result<(), (u32, u32)> = c.spawn.baz(0, 1); + } + + #[idle(spawn = [foo, bar, baz])] + fn idle(c: idle::Context) -> ! { + let _: Result<(), ()> = c.spawn.foo(); + let _: Result<(), u32> = c.spawn.bar(0); + let _: Result<(), (u32, u32)> = c.spawn.baz(0, 1); + + loop {} + } + + #[exception(spawn = [foo, bar, baz])] + fn SVCall(c: SVCall::Context) { + let _: Result<(), ()> = c.spawn.foo(); + let _: Result<(), u32> = c.spawn.bar(0); + let _: Result<(), (u32, u32)> = c.spawn.baz(0, 1); + } + + #[interrupt(spawn = [foo, bar, baz])] + fn UART0(c: UART0::Context) { + let _: Result<(), ()> = c.spawn.foo(); + let _: Result<(), u32> = c.spawn.bar(0); + let _: Result<(), (u32, u32)> = c.spawn.baz(0, 1); + } + + #[task(spawn = [foo, bar, baz])] + fn foo(c: foo::Context) { + let _: Result<(), ()> = c.spawn.foo(); + let _: Result<(), u32> = c.spawn.bar(0); + let _: Result<(), (u32, u32)> = c.spawn.baz(0, 1); + } + + #[task] + fn bar(_: bar::Context, _x: u32) {} + + #[task] + fn baz(_: baz::Context, _x: u32, _y: u32) {} + + extern "C" { + fn UART1(); + } +}; diff --git a/examples/task.rs b/examples/task.rs index 5bb32ac..43f7e56 100644 --- a/examples/task.rs +++ b/examples/task.rs @@ -5,9 +5,8 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::{debug, hprintln}; +use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { diff --git a/examples/types.rs b/examples/types.rs index c3dd89c..2e72f0a 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -5,12 +5,11 @@ #![no_main] #![no_std] -extern crate panic_semihosting; - use cortex_m_semihosting::debug; -use rtfm::{Exclusive, Instant}; +use panic_semihosting as _; +use rtfm::cyccnt::Instant; -#[rtfm::app(device = lm3s6965)] +#[rtfm::app(device = lm3s6965, peripherals = true, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { static mut SHARED: u32 = 0; @@ -43,7 +42,7 @@ const APP: () = { #[task(priority = 2, resources = [SHARED], schedule = [foo], spawn = [foo])] fn foo(c: foo::Context) { let _: Instant = c.scheduled; - let _: Exclusive = c.resources.SHARED; + let _: &mut u32 = c.resources.SHARED; let _: foo::Resources = c.resources; let _: foo::Schedule = c.schedule; let _: foo::Spawn = c.spawn; -- cgit v1.2.3 From 4e51bb68b976c6bb6a9a989dc560d2a8123a84ca Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 20 Jun 2019 06:19:59 +0200 Subject: RFC #207 --- examples/baseline.rs | 4 ++-- examples/binds.rs | 2 +- examples/capacity.rs | 4 ++-- examples/generics.rs | 8 ++++---- examples/interrupt.rs | 4 ++-- examples/late.rs | 4 ++-- examples/lock.rs | 12 ++++++------ examples/pool.rs | 4 ++-- examples/resource.rs | 8 ++++---- examples/shared-with-init.rs | 4 ++-- examples/static.rs | 8 ++++---- examples/t-binds.rs | 6 ++++-- examples/t-resource.rs | 8 ++++---- examples/t-schedule.rs | 8 ++++---- examples/t-spawn.rs | 8 ++++---- examples/types.rs | 16 ++++++++-------- 16 files changed, 55 insertions(+), 53 deletions(-) (limited to 'examples') diff --git a/examples/baseline.rs b/examples/baseline.rs index cc9b412..3a8ab0e 100644 --- a/examples/baseline.rs +++ b/examples/baseline.rs @@ -35,8 +35,8 @@ const APP: () = { } } - #[interrupt(spawn = [foo])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, spawn = [foo])] + fn uart0(c: uart0::Context) { hprintln!("UART0(baseline = {:?})", c.start).unwrap(); // `foo` inherits the baseline of `UART0`: its `start` time diff --git a/examples/binds.rs b/examples/binds.rs index 1959d75..b10cb43 100644 --- a/examples/binds.rs +++ b/examples/binds.rs @@ -30,7 +30,7 @@ const APP: () = { loop {} } - #[interrupt(binds = UART0)] + #[task(binds = UART0)] fn foo(_: foo::Context) { static mut TIMES: u32 = 0; diff --git a/examples/capacity.rs b/examples/capacity.rs index e1a835c..ebc86b8 100644 --- a/examples/capacity.rs +++ b/examples/capacity.rs @@ -16,8 +16,8 @@ const APP: () = { rtfm::pend(Interrupt::UART0); } - #[interrupt(spawn = [foo, bar])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, spawn = [foo, bar])] + fn uart0(c: uart0::Context) { c.spawn.foo(0).unwrap(); c.spawn.foo(1).unwrap(); c.spawn.foo(2).unwrap(); diff --git a/examples/generics.rs b/examples/generics.rs index a35ba23..562470d 100644 --- a/examples/generics.rs +++ b/examples/generics.rs @@ -20,8 +20,8 @@ const APP: () = { rtfm::pend(Interrupt::UART1); } - #[interrupt(resources = [SHARED])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, resources = [SHARED])] + fn uart0(c: uart0::Context) { static mut STATE: u32 = 0; hprintln!("UART0(STATE = {})", *STATE).unwrap(); @@ -33,8 +33,8 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); } - #[interrupt(priority = 2, resources = [SHARED])] - fn UART1(c: UART1::Context) { + #[task(binds = UART1, priority = 2, resources = [SHARED])] + fn uart1(c: uart1::Context) { static mut STATE: u32 = 0; hprintln!("UART1(STATE = {})", *STATE).unwrap(); diff --git a/examples/interrupt.rs b/examples/interrupt.rs index 3fe8ff3..f0069b8 100644 --- a/examples/interrupt.rs +++ b/examples/interrupt.rs @@ -33,8 +33,8 @@ const APP: () = { loop {} } - #[interrupt] - fn UART0(_: UART0::Context) { + #[task(binds = UART0)] + fn uart0(_: uart0::Context) { static mut TIMES: u32 = 0; // Safe access to local `static mut` variable diff --git a/examples/late.rs b/examples/late.rs index 4d48a6a..19807ff 100644 --- a/examples/late.rs +++ b/examples/late.rs @@ -47,8 +47,8 @@ const APP: () = { } } - #[interrupt(resources = [P])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, resources = [P])] + fn uart0(c: uart0::Context) { c.resources.P.enqueue(42).unwrap(); } }; diff --git a/examples/lock.rs b/examples/lock.rs index b7d36b4..17b6a58 100644 --- a/examples/lock.rs +++ b/examples/lock.rs @@ -19,8 +19,8 @@ const APP: () = { } // when omitted priority is assumed to be `1` - #[interrupt(resources = [SHARED])] - fn GPIOA(mut c: GPIOA::Context) { + #[task(binds = GPIOA, resources = [SHARED])] + fn gpioa(mut c: gpioa::Context) { hprintln!("A").unwrap(); // the lower priority task requires a critical section to access the data @@ -44,16 +44,16 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); } - #[interrupt(priority = 2, resources = [SHARED])] - fn GPIOB(c: GPIOB::Context) { + #[task(binds = GPIOB, priority = 2, resources = [SHARED])] + fn gpiob(c: gpiob::Context) { // the higher priority task does *not* need a critical section *c.resources.SHARED += 1; hprintln!("D - SHARED = {}", *c.resources.SHARED).unwrap(); } - #[interrupt(priority = 3)] - fn GPIOC(_: GPIOC::Context) { + #[task(binds = GPIOC, priority = 3)] + fn gpioc(_: gpioc::Context) { hprintln!("C").unwrap(); } }; diff --git a/examples/pool.rs b/examples/pool.rs index db321b5..8c44cb1 100644 --- a/examples/pool.rs +++ b/examples/pool.rs @@ -29,8 +29,8 @@ const APP: () = { rtfm::pend(Interrupt::I2C0); } - #[interrupt(priority = 2, spawn = [foo, bar])] - fn I2C0(c: I2C0::Context) { + #[task(binds = I2C0, priority = 2, spawn = [foo, bar])] + fn i2c0(c: i2c0::Context) { // claim a memory block, leave it uninitialized and .. let x = P::alloc().unwrap().freeze(); diff --git a/examples/resource.rs b/examples/resource.rs index 8268950..661f8c3 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -31,16 +31,16 @@ const APP: () = { } // `SHARED` can be access from this context - #[interrupt(resources = [SHARED])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, resources = [SHARED])] + fn uart0(c: uart0::Context) { *c.resources.SHARED += 1; hprintln!("UART0: SHARED = {}", c.resources.SHARED).unwrap(); } // `SHARED` can be access from this context - #[interrupt(resources = [SHARED])] - fn UART1(c: UART1::Context) { + #[task(binds = UART1, resources = [SHARED])] + fn uart1(c: uart1::Context) { *c.resources.SHARED += 1; hprintln!("UART1: SHARED = {}", c.resources.SHARED).unwrap(); diff --git a/examples/shared-with-init.rs b/examples/shared-with-init.rs index 1640ca9..ed73c8b 100644 --- a/examples/shared-with-init.rs +++ b/examples/shared-with-init.rs @@ -25,8 +25,8 @@ const APP: () = { rtfm::pend(Interrupt::UART0); } - #[interrupt(resources = [SHARED])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, resources = [SHARED])] + fn uart0(c: uart0::Context) { if let Some(message) = c.resources.SHARED.take() { // `message` has been received drop(message); diff --git a/examples/static.rs b/examples/static.rs index eeb522f..5eb7b19 100644 --- a/examples/static.rs +++ b/examples/static.rs @@ -23,15 +23,15 @@ const APP: () = { init::LateResources { KEY: 0xdeadbeef } } - #[interrupt(resources = [KEY])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, resources = [KEY])] + fn uart0(c: uart0::Context) { hprintln!("UART0(KEY = {:#x})", c.resources.KEY).unwrap(); debug::exit(debug::EXIT_SUCCESS); } - #[interrupt(priority = 2, resources = [KEY])] - fn UART1(c: UART1::Context) { + #[task(binds = UART1, priority = 2, resources = [KEY])] + fn uart1(c: uart1::Context) { hprintln!("UART1(KEY = {:#x})", c.resources.KEY).unwrap(); } }; diff --git a/examples/t-binds.rs b/examples/t-binds.rs index b4693a4..dda8e20 100644 --- a/examples/t-binds.rs +++ b/examples/t-binds.rs @@ -12,12 +12,14 @@ const APP: () = { #[init] fn init(_: init::Context) {} - #[exception(binds = SVCall)] + // Cortex-M exception + #[task(binds = SVCall)] fn foo(c: foo::Context) { foo_trampoline(c) } - #[interrupt(binds = UART0)] + // LM3S6965 interrupt + #[task(binds = UART0)] fn bar(c: bar::Context) { bar_trampoline(c) } diff --git a/examples/t-resource.rs b/examples/t-resource.rs index 40dc2a6..adcc04b 100644 --- a/examples/t-resource.rs +++ b/examples/t-resource.rs @@ -51,8 +51,8 @@ const APP: () = { loop {} } - #[interrupt(resources = [O3, S1, S2, S3])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, resources = [O3, S1, S2, S3])] + fn uart0(c: uart0::Context) { // owned by interrupt == `&mut` let _: &mut u32 = c.resources.O3; @@ -66,8 +66,8 @@ const APP: () = { let _: &u32 = c.resources.S3; } - #[interrupt(resources = [S2, O5])] - fn UART1(c: UART1::Context) { + #[task(binds = UART1, resources = [S2, O5])] + fn uart1(c: uart1::Context) { // owned by interrupt == `&` if read-only let _: &u32 = c.resources.O5; diff --git a/examples/t-schedule.rs b/examples/t-schedule.rs index 67ff358..e6035b3 100644 --- a/examples/t-schedule.rs +++ b/examples/t-schedule.rs @@ -26,15 +26,15 @@ const APP: () = { loop {} } - #[exception(schedule = [foo, bar, baz])] - fn SVCall(c: SVCall::Context) { + #[task(binds = SVCall, schedule = [foo, bar, baz])] + fn svcall(c: svcall::Context) { let _: Result<(), ()> = c.schedule.foo(c.start + 70.cycles()); let _: Result<(), u32> = c.schedule.bar(c.start + 80.cycles(), 0); let _: Result<(), (u32, u32)> = c.schedule.baz(c.start + 90.cycles(), 0, 1); } - #[interrupt(schedule = [foo, bar, baz])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, schedule = [foo, bar, baz])] + fn uart0(c: uart0::Context) { let _: Result<(), ()> = c.schedule.foo(c.start + 100.cycles()); let _: Result<(), u32> = c.schedule.bar(c.start + 110.cycles(), 0); let _: Result<(), (u32, u32)> = c.schedule.baz(c.start + 120.cycles(), 0, 1); diff --git a/examples/t-spawn.rs b/examples/t-spawn.rs index 6bb9b31..682b9b8 100644 --- a/examples/t-spawn.rs +++ b/examples/t-spawn.rs @@ -25,15 +25,15 @@ const APP: () = { loop {} } - #[exception(spawn = [foo, bar, baz])] - fn SVCall(c: SVCall::Context) { + #[task(binds = SVCall, spawn = [foo, bar, baz])] + fn svcall(c: svcall::Context) { let _: Result<(), ()> = c.spawn.foo(); let _: Result<(), u32> = c.spawn.bar(0); let _: Result<(), (u32, u32)> = c.spawn.baz(0, 1); } - #[interrupt(spawn = [foo, bar, baz])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, spawn = [foo, bar, baz])] + fn uart0(c: uart0::Context) { let _: Result<(), ()> = c.spawn.foo(); let _: Result<(), u32> = c.spawn.bar(0); let _: Result<(), (u32, u32)> = c.spawn.baz(0, 1); diff --git a/examples/types.rs b/examples/types.rs index 2e72f0a..3e9c7ea 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -24,19 +24,19 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); } - #[exception(schedule = [foo], spawn = [foo])] - fn SVCall(c: SVCall::Context) { + #[task(binds = SVCall, schedule = [foo], spawn = [foo])] + fn svcall(c: svcall::Context) { let _: Instant = c.start; - let _: SVCall::Schedule = c.schedule; - let _: SVCall::Spawn = c.spawn; + let _: svcall::Schedule = c.schedule; + let _: svcall::Spawn = c.spawn; } - #[interrupt(resources = [SHARED], schedule = [foo], spawn = [foo])] - fn UART0(c: UART0::Context) { + #[task(binds = UART0, resources = [SHARED], schedule = [foo], spawn = [foo])] + fn uart0(c: uart0::Context) { let _: Instant = c.start; let _: resources::SHARED = c.resources.SHARED; - let _: UART0::Schedule = c.schedule; - let _: UART0::Spawn = c.spawn; + let _: uart0::Schedule = c.schedule; + let _: uart0::Spawn = c.spawn; } #[task(priority = 2, resources = [SHARED], schedule = [foo], spawn = [foo])] -- cgit v1.2.3 From 9195038c87703fc94b6e99f6de593886d51c2b19 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 10 Jul 2019 22:42:44 +0200 Subject: implement RFC #212 --- examples/cfg.rs | 15 ++++++---- examples/generics.rs | 19 +++++++----- examples/late.rs | 16 +++++----- examples/lock.rs | 19 +++++++----- examples/not-send.rs | 13 ++++---- examples/not-sync.rs | 13 ++++---- examples/resource.rs | 27 +++++++++-------- examples/shared-with-init.rs | 13 ++++---- examples/static.rs | 14 ++++----- examples/t-cfg.rs | 11 ++++--- examples/t-late-not-send.rs | 16 +++++----- examples/t-resource.rs | 70 +++++++++++++++++++++++++------------------- examples/types.rs | 13 ++++---- 13 files changed, 148 insertions(+), 111 deletions(-) (limited to 'examples') diff --git a/examples/cfg.rs b/examples/cfg.rs index b1f65cf..fb812cb 100644 --- a/examples/cfg.rs +++ b/examples/cfg.rs @@ -11,25 +11,28 @@ use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { - #[cfg(debug_assertions)] // <- `true` when using the `dev` profile - static mut COUNT: u32 = 0; + struct Resources { + #[cfg(debug_assertions)] // <- `true` when using the `dev` profile + #[init(0)] + count: u32, + } #[init] fn init(_: init::Context) { // .. } - #[task(priority = 3, resources = [COUNT], spawn = [log])] + #[task(priority = 3, resources = [count], spawn = [log])] fn foo(_c: foo::Context) { #[cfg(debug_assertions)] { - *_c.resources.COUNT += 1; + *_c.resources.count += 1; - _c.spawn.log(*_c.resources.COUNT).ok(); + _c.spawn.log(*_c.resources.count).ok(); } // this wouldn't compile in `release` mode - // *resources.COUNT += 1; + // *resources.count += 1; // .. } diff --git a/examples/generics.rs b/examples/generics.rs index 562470d..f0632d9 100644 --- a/examples/generics.rs +++ b/examples/generics.rs @@ -12,7 +12,10 @@ use rtfm::{Exclusive, Mutex}; #[rtfm::app(device = lm3s6965)] const APP: () = { - static mut SHARED: u32 = 0; + struct Resources { + #[init(0)] + shared: u32, + } #[init] fn init(_: init::Context) { @@ -20,29 +23,29 @@ const APP: () = { rtfm::pend(Interrupt::UART1); } - #[task(binds = UART0, resources = [SHARED])] + #[task(binds = UART0, resources = [shared])] fn uart0(c: uart0::Context) { static mut STATE: u32 = 0; hprintln!("UART0(STATE = {})", *STATE).unwrap(); - advance(STATE, c.resources.SHARED); + advance(STATE, c.resources.shared); rtfm::pend(Interrupt::UART1); debug::exit(debug::EXIT_SUCCESS); } - #[task(binds = UART1, priority = 2, resources = [SHARED])] + #[task(binds = UART1, priority = 2, resources = [shared])] fn uart1(c: uart1::Context) { static mut STATE: u32 = 0; hprintln!("UART1(STATE = {})", *STATE).unwrap(); - // just to show that `SHARED` can be accessed directly - *c.resources.SHARED += 0; + // just to show that `shared` can be accessed directly + *c.resources.shared += 0; - advance(STATE, Exclusive(c.resources.SHARED)); + advance(STATE, Exclusive(c.resources.shared)); } }; @@ -55,5 +58,5 @@ fn advance(state: &mut u32, mut shared: impl Mutex) { (old, *shared) }); - hprintln!("SHARED: {} -> {}", old, new).unwrap(); + hprintln!("shared: {} -> {}", old, new).unwrap(); } diff --git a/examples/late.rs b/examples/late.rs index 19807ff..536d71a 100644 --- a/examples/late.rs +++ b/examples/late.rs @@ -16,9 +16,9 @@ use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { // Late resources - extern "C" { - static mut P: Producer<'static, u32, U4>; - static mut C: Consumer<'static, u32, U4>; + struct Resources { + p: Producer<'static, u32, U4>, + c: Consumer<'static, u32, U4>, } #[init] @@ -31,13 +31,13 @@ const APP: () = { let (p, c) = Q.as_mut().unwrap().split(); // Initialization of late resources - init::LateResources { P: p, C: c } + init::LateResources { p, c } } - #[idle(resources = [C])] + #[idle(resources = [c])] fn idle(c: idle::Context) -> ! { loop { - if let Some(byte) = c.resources.C.dequeue() { + if let Some(byte) = c.resources.c.dequeue() { hprintln!("received message: {}", byte).unwrap(); debug::exit(debug::EXIT_SUCCESS); @@ -47,8 +47,8 @@ const APP: () = { } } - #[task(binds = UART0, resources = [P])] + #[task(binds = UART0, resources = [p])] fn uart0(c: uart0::Context) { - c.resources.P.enqueue(42).unwrap(); + c.resources.p.enqueue(42).unwrap(); } }; diff --git a/examples/lock.rs b/examples/lock.rs index 17b6a58..f33a60a 100644 --- a/examples/lock.rs +++ b/examples/lock.rs @@ -11,7 +11,10 @@ use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { - static mut SHARED: u32 = 0; + struct Resources { + #[init(0)] + shared: u32, + } #[init] fn init(_: init::Context) { @@ -19,21 +22,21 @@ const APP: () = { } // when omitted priority is assumed to be `1` - #[task(binds = GPIOA, resources = [SHARED])] + #[task(binds = GPIOA, resources = [shared])] fn gpioa(mut c: gpioa::Context) { hprintln!("A").unwrap(); // the lower priority task requires a critical section to access the data - c.resources.SHARED.lock(|shared| { + c.resources.shared.lock(|shared| { // data can only be modified within this critical section (closure) *shared += 1; // GPIOB will *not* run right now due to the critical section rtfm::pend(Interrupt::GPIOB); - hprintln!("B - SHARED = {}", *shared).unwrap(); + hprintln!("B - shared = {}", *shared).unwrap(); - // GPIOC does not contend for `SHARED` so it's allowed to run now + // GPIOC does not contend for `shared` so it's allowed to run now rtfm::pend(Interrupt::GPIOC); }); @@ -44,12 +47,12 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); } - #[task(binds = GPIOB, priority = 2, resources = [SHARED])] + #[task(binds = GPIOB, priority = 2, resources = [shared])] fn gpiob(c: gpiob::Context) { // the higher priority task does *not* need a critical section - *c.resources.SHARED += 1; + *c.resources.shared += 1; - hprintln!("D - SHARED = {}", *c.resources.SHARED).unwrap(); + hprintln!("D - shared = {}", *c.resources.shared).unwrap(); } #[task(binds = GPIOC, priority = 3)] diff --git a/examples/not-send.rs b/examples/not-send.rs index f240e51..d27cc82 100644 --- a/examples/not-send.rs +++ b/examples/not-send.rs @@ -17,7 +17,10 @@ pub struct NotSend { #[app(device = lm3s6965)] const APP: () = { - static mut SHARED: Option = None; + struct Resources { + #[init(None)] + shared: Option, + } #[init(spawn = [baz, quux])] fn init(c: init::Context) { @@ -36,16 +39,16 @@ const APP: () = { // scenario 1 } - #[task(priority = 2, resources = [SHARED])] + #[task(priority = 2, resources = [shared])] fn baz(c: baz::Context) { // scenario 2: resource shared between tasks that run at the same priority - *c.resources.SHARED = Some(NotSend { _0: PhantomData }); + *c.resources.shared = Some(NotSend { _0: PhantomData }); } - #[task(priority = 2, resources = [SHARED])] + #[task(priority = 2, resources = [shared])] fn quux(c: quux::Context) { // scenario 2 - let _not_send = c.resources.SHARED.take().unwrap(); + let _not_send = c.resources.shared.take().unwrap(); debug::exit(debug::EXIT_SUCCESS); } diff --git a/examples/not-sync.rs b/examples/not-sync.rs index 6b49911..f0f6075 100644 --- a/examples/not-sync.rs +++ b/examples/not-sync.rs @@ -16,21 +16,24 @@ pub struct NotSync { #[rtfm::app(device = lm3s6965)] const APP: () = { - static SHARED: NotSync = NotSync { _0: PhantomData }; + struct Resources { + #[init(NotSync { _0: PhantomData })] + shared: NotSync, + } #[init] fn init(_: init::Context) { debug::exit(debug::EXIT_SUCCESS); } - #[task(resources = [SHARED])] + #[task(resources = [shared])] fn foo(c: foo::Context) { - let _: &NotSync = c.resources.SHARED; + let _: &NotSync = c.resources.shared; } - #[task(resources = [SHARED])] + #[task(resources = [shared])] fn bar(c: bar::Context) { - let _: &NotSync = c.resources.SHARED; + let _: &NotSync = c.resources.shared; } extern "C" { diff --git a/examples/resource.rs b/examples/resource.rs index 661f8c3..2506a2c 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -11,8 +11,11 @@ use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { - // A resource - static mut SHARED: u32 = 0; + struct Resources { + // A resource + #[init(0)] + shared: u32, + } #[init] fn init(_: init::Context) { @@ -24,25 +27,25 @@ const APP: () = { fn idle(_: idle::Context) -> ! { debug::exit(debug::EXIT_SUCCESS); - // error: `SHARED` can't be accessed from this context - // SHARED += 1; + // error: `shared` can't be accessed from this context + // shared += 1; loop {} } - // `SHARED` can be access from this context - #[task(binds = UART0, resources = [SHARED])] + // `shared` can be access from this context + #[task(binds = UART0, resources = [shared])] fn uart0(c: uart0::Context) { - *c.resources.SHARED += 1; + *c.resources.shared += 1; - hprintln!("UART0: SHARED = {}", c.resources.SHARED).unwrap(); + hprintln!("UART0: shared = {}", c.resources.shared).unwrap(); } - // `SHARED` can be access from this context - #[task(binds = UART1, resources = [SHARED])] + // `shared` can be access from this context + #[task(binds = UART1, resources = [shared])] fn uart1(c: uart1::Context) { - *c.resources.SHARED += 1; + *c.resources.shared += 1; - hprintln!("UART1: SHARED = {}", c.resources.SHARED).unwrap(); + hprintln!("UART1: shared = {}", c.resources.shared).unwrap(); } }; diff --git a/examples/shared-with-init.rs b/examples/shared-with-init.rs index ed73c8b..14fa54b 100644 --- a/examples/shared-with-init.rs +++ b/examples/shared-with-init.rs @@ -14,20 +14,23 @@ pub struct MustBeSend; #[app(device = lm3s6965)] const APP: () = { - static mut SHARED: Option = None; + struct Resources { + #[init(None)] + shared: Option, + } - #[init(resources = [SHARED])] + #[init(resources = [shared])] fn init(c: init::Context) { // this `message` will be sent to task `UART0` let message = MustBeSend; - *c.resources.SHARED = Some(message); + *c.resources.shared = Some(message); rtfm::pend(Interrupt::UART0); } - #[task(binds = UART0, resources = [SHARED])] + #[task(binds = UART0, resources = [shared])] fn uart0(c: uart0::Context) { - if let Some(message) = c.resources.SHARED.take() { + if let Some(message) = c.resources.shared.take() { // `message` has been received drop(message); diff --git a/examples/static.rs b/examples/static.rs index 5eb7b19..4af7ee6 100644 --- a/examples/static.rs +++ b/examples/static.rs @@ -11,8 +11,8 @@ use panic_semihosting as _; #[rtfm::app(device = lm3s6965)] const APP: () = { - extern "C" { - static KEY: u32; + struct Resources { + key: u32, } #[init] @@ -20,18 +20,18 @@ const APP: () = { rtfm::pend(Interrupt::UART0); rtfm::pend(Interrupt::UART1); - init::LateResources { KEY: 0xdeadbeef } + init::LateResources { key: 0xdeadbeef } } - #[task(binds = UART0, resources = [KEY])] + #[task(binds = UART0, resources = [&key])] fn uart0(c: uart0::Context) { - hprintln!("UART0(KEY = {:#x})", c.resources.KEY).unwrap(); + hprintln!("UART0(key = {:#x})", c.resources.key).unwrap(); debug::exit(debug::EXIT_SUCCESS); } - #[task(binds = UART1, priority = 2, resources = [KEY])] + #[task(binds = UART1, priority = 2, resources = [&key])] fn uart1(c: uart1::Context) { - hprintln!("UART1(KEY = {:#x})", c.resources.KEY).unwrap(); + hprintln!("UART1(key = {:#x})", c.resources.key).unwrap(); } }; diff --git a/examples/t-cfg.rs b/examples/t-cfg.rs index 158eef5..e61ec79 100644 --- a/examples/t-cfg.rs +++ b/examples/t-cfg.rs @@ -7,8 +7,11 @@ use panic_halt as _; #[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { - #[cfg(never)] - static mut FOO: u32 = 0; + struct Resources { + #[cfg(never)] + #[init(0)] + foo: u32, + } #[init] fn init(_: init::Context) { @@ -24,13 +27,13 @@ const APP: () = { loop {} } - #[task(resources = [FOO], schedule = [quux], spawn = [quux])] + #[task(resources = [foo], schedule = [quux], spawn = [quux])] fn foo(_: foo::Context) { #[cfg(never)] static mut BAR: u32 = 0; } - #[task(priority = 3, resources = [FOO], schedule = [quux], spawn = [quux])] + #[task(priority = 3, resources = [foo], schedule = [quux], spawn = [quux])] fn bar(_: bar::Context) { #[cfg(never)] static mut BAR: u32 = 0; diff --git a/examples/t-late-not-send.rs b/examples/t-late-not-send.rs index 55a053d..4fd3504 100644 --- a/examples/t-late-not-send.rs +++ b/examples/t-late-not-send.rs @@ -13,23 +13,23 @@ pub struct NotSend { #[rtfm::app(device = lm3s6965)] const APP: () = { - extern "C" { - static mut X: NotSend; + struct Resources { + x: NotSend, + #[init(None)] + y: Option, } - static mut Y: Option = None; - - #[init(resources = [Y])] + #[init(resources = [y])] fn init(c: init::Context) -> init::LateResources { // equivalent to late resource initialization - *c.resources.Y = Some(NotSend { _0: PhantomData }); + *c.resources.y = Some(NotSend { _0: PhantomData }); init::LateResources { - X: NotSend { _0: PhantomData }, + x: NotSend { _0: PhantomData }, } } - #[idle(resources = [X, Y])] + #[idle(resources = [x, y])] fn idle(_: idle::Context) -> ! { loop {} } diff --git a/examples/t-resource.rs b/examples/t-resource.rs index adcc04b..303340e 100644 --- a/examples/t-resource.rs +++ b/examples/t-resource.rs @@ -9,69 +9,79 @@ use panic_halt as _; #[rtfm::app(device = lm3s6965)] const APP: () = { - static mut O1: u32 = 0; // init - static mut O2: u32 = 0; // idle - static mut O3: u32 = 0; // EXTI0 - static O4: u32 = 0; // idle - static O5: u32 = 0; // EXTI1 - static O6: u32 = 0; // init - - static mut S1: u32 = 0; // idle & EXTI0 - static mut S2: u32 = 0; // EXTI0 & EXTI1 - static S3: u32 = 0; - - #[init(resources = [O1, O4, O5, O6, S3])] + struct Resources { + #[init(0)] + o1: u32, // init + #[init(0)] + o2: u32, // idle + #[init(0)] + o3: u32, // EXTI0 + #[init(0)] + o4: u32, // idle + #[init(0)] + o5: u32, // EXTI1 + #[init(0)] + o6: u32, // init + #[init(0)] + s1: u32, // idle & uart0 + #[init(0)] + s2: u32, // uart0 & uart1 + #[init(0)] + s3: u32, // idle & uart0 + } + + #[init(resources = [o1, o4, o5, o6, s3])] fn init(c: init::Context) { // owned by `init` == `&'static mut` - let _: &'static mut u32 = c.resources.O1; + let _: &'static mut u32 = c.resources.o1; // owned by `init` == `&'static` if read-only - let _: &'static u32 = c.resources.O6; + let _: &'static u32 = c.resources.o6; // `init` has exclusive access to all resources - let _: &mut u32 = c.resources.O4; - let _: &mut u32 = c.resources.O5; - let _: &mut u32 = c.resources.S3; + let _: &mut u32 = c.resources.o4; + let _: &mut u32 = c.resources.o5; + let _: &mut u32 = c.resources.s3; } - #[idle(resources = [O2, O4, S1, S3])] + #[idle(resources = [o2, &o4, s1, &s3])] fn idle(mut c: idle::Context) -> ! { // owned by `idle` == `&'static mut` - let _: &'static mut u32 = c.resources.O2; + let _: &'static mut u32 = c.resources.o2; // owned by `idle` == `&'static` if read-only - let _: &'static u32 = c.resources.O4; + let _: &'static u32 = c.resources.o4; // shared with `idle` == `Mutex` - c.resources.S1.lock(|_| {}); + c.resources.s1.lock(|_| {}); // `&` if read-only - let _: &u32 = c.resources.S3; + let _: &u32 = c.resources.s3; loop {} } - #[task(binds = UART0, resources = [O3, S1, S2, S3])] + #[task(binds = UART0, resources = [o3, s1, s2, &s3])] fn uart0(c: uart0::Context) { // owned by interrupt == `&mut` - let _: &mut u32 = c.resources.O3; + let _: &mut u32 = c.resources.o3; // no `Mutex` proxy when access from highest priority task - let _: &mut u32 = c.resources.S1; + let _: &mut u32 = c.resources.s1; // no `Mutex` proxy when co-owned by cooperative (same priority) tasks - let _: &mut u32 = c.resources.S2; + let _: &mut u32 = c.resources.s2; // `&` if read-only - let _: &u32 = c.resources.S3; + let _: &u32 = c.resources.s3; } - #[task(binds = UART1, resources = [S2, O5])] + #[task(binds = UART1, resources = [s2, &o5])] fn uart1(c: uart1::Context) { // owned by interrupt == `&` if read-only - let _: &u32 = c.resources.O5; + let _: &u32 = c.resources.o5; // no `Mutex` proxy when co-owned by cooperative (same priority) tasks - let _: &mut u32 = c.resources.S2; + let _: &mut u32 = c.resources.s2; } }; diff --git a/examples/types.rs b/examples/types.rs index 3e9c7ea..0c8097f 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -11,7 +11,10 @@ use rtfm::cyccnt::Instant; #[rtfm::app(device = lm3s6965, peripherals = true, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { - static mut SHARED: u32 = 0; + struct Resources { + #[init(0)] + shared: u32, + } #[init(schedule = [foo], spawn = [foo])] fn init(c: init::Context) { @@ -31,18 +34,18 @@ const APP: () = { let _: svcall::Spawn = c.spawn; } - #[task(binds = UART0, resources = [SHARED], schedule = [foo], spawn = [foo])] + #[task(binds = UART0, resources = [shared], schedule = [foo], spawn = [foo])] fn uart0(c: uart0::Context) { let _: Instant = c.start; - let _: resources::SHARED = c.resources.SHARED; + let _: resources::shared = c.resources.shared; let _: uart0::Schedule = c.schedule; let _: uart0::Spawn = c.spawn; } - #[task(priority = 2, resources = [SHARED], schedule = [foo], spawn = [foo])] + #[task(priority = 2, resources = [shared], schedule = [foo], spawn = [foo])] fn foo(c: foo::Context) { let _: Instant = c.scheduled; - let _: &mut u32 = c.resources.SHARED; + let _: &mut u32 = c.resources.shared; let _: foo::Resources = c.resources; let _: foo::Schedule = c.schedule; let _: foo::Spawn = c.spawn; -- cgit v1.2.3 From 07b2b4d83078d0fd260d5f0812e8d5a34d02b793 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 21 Aug 2019 10:17:27 +0200 Subject: doc up --- examples/baseline.rs | 16 +++++++------- examples/cfg.rs | 27 +++++++++++++++-------- examples/generics.rs | 5 ++++- examples/hardware.rs | 50 ++++++++++++++++++++++++++++++++++++++++++ examples/init.rs | 6 ++--- examples/interrupt.rs | 50 ------------------------------------------ examples/late.rs | 8 +++---- examples/not-sync.rs | 4 ++-- examples/only-shared-access.rs | 38 ++++++++++++++++++++++++++++++++ examples/periodic.rs | 10 ++++----- examples/preempt.rs | 37 +++++++++++++++++++++++++++++++ examples/resource.rs | 24 ++++++++++---------- examples/schedule.rs | 6 ++--- examples/smallest.rs | 2 -- examples/static.rs | 37 ------------------------------- examples/task.rs | 6 ++++- examples/types.rs | 47 ++++++++++++++++++++------------------- 17 files changed, 213 insertions(+), 160 deletions(-) create mode 100644 examples/hardware.rs delete mode 100644 examples/interrupt.rs create mode 100644 examples/only-shared-access.rs create mode 100644 examples/preempt.rs delete mode 100644 examples/static.rs (limited to 'examples') diff --git a/examples/baseline.rs b/examples/baseline.rs index 3a8ab0e..b7144dd 100644 --- a/examples/baseline.rs +++ b/examples/baseline.rs @@ -13,18 +13,18 @@ use panic_semihosting as _; #[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { #[init(spawn = [foo])] - fn init(c: init::Context) { - hprintln!("init(baseline = {:?})", c.start).unwrap(); + fn init(cx: init::Context) { + hprintln!("init(baseline = {:?})", cx.start).unwrap(); // `foo` inherits the baseline of `init`: `Instant(0)` - c.spawn.foo().unwrap(); + cx.spawn.foo().unwrap(); } #[task(schedule = [foo])] - fn foo(c: foo::Context) { + fn foo(cx: foo::Context) { static mut ONCE: bool = true; - hprintln!("foo(baseline = {:?})", c.scheduled).unwrap(); + hprintln!("foo(baseline = {:?})", cx.scheduled).unwrap(); if *ONCE { *ONCE = false; @@ -36,11 +36,11 @@ const APP: () = { } #[task(binds = UART0, spawn = [foo])] - fn uart0(c: uart0::Context) { - hprintln!("UART0(baseline = {:?})", c.start).unwrap(); + fn uart0(cx: uart0::Context) { + hprintln!("UART0(baseline = {:?})", cx.start).unwrap(); // `foo` inherits the baseline of `UART0`: its `start` time - c.spawn.foo().unwrap(); + cx.spawn.foo().unwrap(); } extern "C" { diff --git a/examples/cfg.rs b/examples/cfg.rs index fb812cb..2a43b5c 100644 --- a/examples/cfg.rs +++ b/examples/cfg.rs @@ -5,6 +5,7 @@ #![no_main] #![no_std] +use cortex_m_semihosting::debug; #[cfg(debug_assertions)] use cortex_m_semihosting::hprintln; use panic_semihosting as _; @@ -17,28 +18,36 @@ const APP: () = { count: u32, } - #[init] - fn init(_: init::Context) { - // .. + #[init(spawn = [foo])] + fn init(cx: init::Context) { + cx.spawn.foo().unwrap(); + cx.spawn.foo().unwrap(); + } + + #[idle] + fn idle(_: idle::Context) -> ! { + debug::exit(debug::EXIT_SUCCESS); + + loop {} } - #[task(priority = 3, resources = [count], spawn = [log])] - fn foo(_c: foo::Context) { + #[task(capacity = 2, resources = [count], spawn = [log])] + fn foo(_cx: foo::Context) { #[cfg(debug_assertions)] { - *_c.resources.count += 1; + *_cx.resources.count += 1; - _c.spawn.log(*_c.resources.count).ok(); + _cx.spawn.log(*_cx.resources.count).unwrap(); } // this wouldn't compile in `release` mode - // *resources.count += 1; + // *_cx.resources.count += 1; // .. } #[cfg(debug_assertions)] - #[task] + #[task(capacity = 2)] fn log(_: log::Context, n: u32) { hprintln!( "foo has been called {} time{}", diff --git a/examples/generics.rs b/examples/generics.rs index f0632d9..eafc630 100644 --- a/examples/generics.rs +++ b/examples/generics.rs @@ -29,6 +29,7 @@ const APP: () = { hprintln!("UART0(STATE = {})", *STATE).unwrap(); + // second argument has type `resources::shared` advance(STATE, c.resources.shared); rtfm::pend(Interrupt::UART1); @@ -45,14 +46,16 @@ const APP: () = { // just to show that `shared` can be accessed directly *c.resources.shared += 0; + // second argument has type `Exclusive` 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) { *state += 1; - let (old, new) = shared.lock(|shared| { + let (old, new) = shared.lock(|shared: &mut u32| { let old = *shared; *shared += *state; (old, *shared) diff --git a/examples/hardware.rs b/examples/hardware.rs new file mode 100644 index 0000000..77f19d9 --- /dev/null +++ b/examples/hardware.rs @@ -0,0 +1,50 @@ +//! examples/hardware.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtfm::app(device = lm3s6965)] +const APP: () = { + #[init] + fn init(_: init::Context) { + // Pends the UART0 interrupt but its handler won't run until *after* + // `init` returns because interrupts are disabled + rtfm::pend(Interrupt::UART0); // equivalent to NVIC::pend + + hprintln!("init").unwrap(); + } + + #[idle] + fn idle(_: idle::Context) -> ! { + // interrupts are enabled again; the `UART0` handler runs at this point + + hprintln!("idle").unwrap(); + + rtfm::pend(Interrupt::UART0); + + debug::exit(debug::EXIT_SUCCESS); + + loop {} + } + + #[task(binds = UART0)] + fn uart0(_: uart0::Context) { + static mut TIMES: u32 = 0; + + // Safe access to local `static mut` variable + *TIMES += 1; + + hprintln!( + "UART0 called {} time{}", + *TIMES, + if *TIMES > 1 { "s" } else { "" } + ) + .unwrap(); + } +}; diff --git a/examples/init.rs b/examples/init.rs index 361db4b..194e3ec 100644 --- a/examples/init.rs +++ b/examples/init.rs @@ -11,14 +11,14 @@ use panic_semihosting as _; #[rtfm::app(device = lm3s6965, peripherals = true)] const APP: () = { #[init] - fn init(c: init::Context) { + fn init(cx: init::Context) { static mut X: u32 = 0; // Cortex-M peripherals - let _core: cortex_m::Peripherals = c.core; + let _core: cortex_m::Peripherals = cx.core; // Device specific peripherals - let _device: lm3s6965::Peripherals = c.device; + let _device: lm3s6965::Peripherals = cx.device; // Safe access to local `static mut` variable let _x: &'static mut u32 = X; diff --git a/examples/interrupt.rs b/examples/interrupt.rs deleted file mode 100644 index f0069b8..0000000 --- a/examples/interrupt.rs +++ /dev/null @@ -1,50 +0,0 @@ -//! examples/interrupt.rs - -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -use cortex_m_semihosting::{debug, hprintln}; -use lm3s6965::Interrupt; -use panic_semihosting as _; - -#[rtfm::app(device = lm3s6965)] -const APP: () = { - #[init] - fn init(_: init::Context) { - // Pends the UART0 interrupt but its handler won't run until *after* - // `init` returns because interrupts are disabled - rtfm::pend(Interrupt::UART0); - - hprintln!("init").unwrap(); - } - - #[idle] - fn idle(_: idle::Context) -> ! { - // interrupts are enabled again; the `UART0` handler runs at this point - - hprintln!("idle").unwrap(); - - rtfm::pend(Interrupt::UART0); - - debug::exit(debug::EXIT_SUCCESS); - - loop {} - } - - #[task(binds = UART0)] - fn uart0(_: uart0::Context) { - static mut TIMES: u32 = 0; - - // 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 536d71a..2eb12d6 100644 --- a/examples/late.rs +++ b/examples/late.rs @@ -8,6 +8,7 @@ use cortex_m_semihosting::{debug, hprintln}; use heapless::{ consts::*, + i, spsc::{Consumer, Producer, Queue}, }; use lm3s6965::Interrupt; @@ -23,12 +24,9 @@ const APP: () = { #[init] fn init(_: init::Context) -> init::LateResources { - // NOTE: we use `Option` here to work around the lack of - // a stable `const` constructor - static mut Q: Option> = None; + static mut Q: Queue = Queue(i::Queue::new()); - *Q = Some(Queue::new()); - let (p, c) = Q.as_mut().unwrap().split(); + let (p, c) = Q.split(); // Initialization of late resources init::LateResources { p, c } diff --git a/examples/not-sync.rs b/examples/not-sync.rs index f0f6075..7ce2a82 100644 --- a/examples/not-sync.rs +++ b/examples/not-sync.rs @@ -26,12 +26,12 @@ const APP: () = { debug::exit(debug::EXIT_SUCCESS); } - #[task(resources = [shared])] + #[task(resources = [&shared])] fn foo(c: foo::Context) { let _: &NotSync = c.resources.shared; } - #[task(resources = [shared])] + #[task(resources = [&shared])] fn bar(c: bar::Context) { let _: &NotSync = c.resources.shared; } diff --git a/examples/only-shared-access.rs b/examples/only-shared-access.rs new file mode 100644 index 0000000..c7060b1 --- /dev/null +++ b/examples/only-shared-access.rs @@ -0,0 +1,38 @@ +//! examples/static.rs + +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use panic_semihosting as _; + +#[rtfm::app(device = lm3s6965)] +const APP: () = { + struct Resources { + key: u32, + } + + #[init] + fn init(_: init::Context) -> init::LateResources { + rtfm::pend(Interrupt::UART0); + rtfm::pend(Interrupt::UART1); + + init::LateResources { key: 0xdeadbeef } + } + + #[task(binds = UART0, resources = [&key])] + fn uart0(cx: uart0::Context) { + let key: &u32 = cx.resources.key; + hprintln!("UART0(key = {:#x})", key).unwrap(); + + debug::exit(debug::EXIT_SUCCESS); + } + + #[task(binds = UART1, priority = 2, resources = [&key])] + fn uart1(cx: uart1::Context) { + hprintln!("UART1(key = {:#x})", cx.resources.key).unwrap(); + } +}; diff --git a/examples/periodic.rs b/examples/periodic.rs index b8910db..ec110e1 100644 --- a/examples/periodic.rs +++ b/examples/periodic.rs @@ -15,16 +15,16 @@ const PERIOD: u32 = 8_000_000; #[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { #[init(schedule = [foo])] - fn init(c: init::Context) { - c.schedule.foo(Instant::now() + PERIOD.cycles()).unwrap(); + fn init(cx: init::Context) { + cx.schedule.foo(Instant::now() + PERIOD.cycles()).unwrap(); } #[task(schedule = [foo])] - fn foo(c: foo::Context) { + fn foo(cx: foo::Context) { let now = Instant::now(); - hprintln!("foo(scheduled = {:?}, now = {:?})", c.scheduled, now).unwrap(); + hprintln!("foo(scheduled = {:?}, now = {:?})", cx.scheduled, now).unwrap(); - c.schedule.foo(c.scheduled + PERIOD.cycles()).unwrap(); + cx.schedule.foo(cx.scheduled + PERIOD.cycles()).unwrap(); } extern "C" { diff --git a/examples/preempt.rs b/examples/preempt.rs new file mode 100644 index 0000000..9f1b2a5 --- /dev/null +++ b/examples/preempt.rs @@ -0,0 +1,37 @@ +//! examples/preempt.rs + +#![no_main] +#![no_std] + +use cortex_m_semihosting::{debug, hprintln}; +use lm3s6965::Interrupt; +use panic_semihosting as _; +use rtfm::app; + +#[app(device = lm3s6965)] +const APP: () = { + #[init] + fn init(_: init::Context) { + rtfm::pend(Interrupt::UART0); + } + + #[task(binds = UART0, priority = 1)] + fn uart0(_: uart0::Context) { + hprintln!("UART0 - start").unwrap(); + rtfm::pend(Interrupt::UART2); + hprintln!("UART0 - end").unwrap(); + debug::exit(debug::EXIT_SUCCESS); + } + + #[task(binds = UART1, priority = 2)] + fn uart1(_: uart1::Context) { + hprintln!(" UART1").unwrap(); + } + + #[task(binds = UART2, priority = 2)] + fn uart2(_: uart2::Context) { + hprintln!(" UART2 - start").unwrap(); + rtfm::pend(Interrupt::UART1); + hprintln!(" UART2 - end").unwrap(); + } +}; diff --git a/examples/resource.rs b/examples/resource.rs index 2506a2c..8632525 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -23,29 +23,31 @@ const APP: () = { rtfm::pend(Interrupt::UART1); } + // `shared` cannot be accessed from this context #[idle] - fn idle(_: idle::Context) -> ! { + fn idle(_cx: idle::Context) -> ! { debug::exit(debug::EXIT_SUCCESS); - // error: `shared` can't be accessed from this context - // shared += 1; + // error: no `resources` field in `idle::Context` + // _cx.resources.shared += 1; loop {} } - // `shared` can be access from this context + // `shared` can be accessed from this context #[task(binds = UART0, resources = [shared])] - fn uart0(c: uart0::Context) { - *c.resources.shared += 1; + fn uart0(cx: uart0::Context) { + let shared: &mut u32 = cx.resources.shared; + *shared += 1; - hprintln!("UART0: shared = {}", c.resources.shared).unwrap(); + hprintln!("UART0: shared = {}", shared).unwrap(); } - // `shared` can be access from this context + // `shared` can be accessed from this context #[task(binds = UART1, resources = [shared])] - fn uart1(c: uart1::Context) { - *c.resources.shared += 1; + fn uart1(cx: uart1::Context) { + *cx.resources.shared += 1; - hprintln!("UART1: shared = {}", c.resources.shared).unwrap(); + hprintln!("UART1: shared = {}", cx.resources.shared).unwrap(); } }; diff --git a/examples/schedule.rs b/examples/schedule.rs index 1cf2b1e..27d3bd1 100644 --- a/examples/schedule.rs +++ b/examples/schedule.rs @@ -13,16 +13,16 @@ use rtfm::cyccnt::{Instant, U32Ext as _}; #[rtfm::app(device = lm3s6965, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { #[init(schedule = [foo, bar])] - fn init(c: init::Context) { + fn init(cx: init::Context) { let now = Instant::now(); hprintln!("init @ {:?}", now).unwrap(); // Schedule `foo` to run 8e6 cycles (clock cycles) in the future - c.schedule.foo(now + 8_000_000.cycles()).unwrap(); + cx.schedule.foo(now + 8_000_000.cycles()).unwrap(); // Schedule `bar` to run 4e6 cycles in the future - c.schedule.bar(now + 4_000_000.cycles()).unwrap(); + cx.schedule.bar(now + 4_000_000.cycles()).unwrap(); } #[task] diff --git a/examples/smallest.rs b/examples/smallest.rs index e422806..7b26a85 100644 --- a/examples/smallest.rs +++ b/examples/smallest.rs @@ -1,7 +1,5 @@ //! examples/smallest.rs -#![deny(unsafe_code)] -#![deny(warnings)] #![no_main] #![no_std] diff --git a/examples/static.rs b/examples/static.rs deleted file mode 100644 index 4af7ee6..0000000 --- a/examples/static.rs +++ /dev/null @@ -1,37 +0,0 @@ -//! examples/static.rs - -#![deny(unsafe_code)] -#![deny(warnings)] -#![no_main] -#![no_std] - -use cortex_m_semihosting::{debug, hprintln}; -use lm3s6965::Interrupt; -use panic_semihosting as _; - -#[rtfm::app(device = lm3s6965)] -const APP: () = { - struct Resources { - key: u32, - } - - #[init] - fn init(_: init::Context) -> init::LateResources { - rtfm::pend(Interrupt::UART0); - rtfm::pend(Interrupt::UART1); - - init::LateResources { key: 0xdeadbeef } - } - - #[task(binds = UART0, resources = [&key])] - fn uart0(c: uart0::Context) { - hprintln!("UART0(key = {:#x})", c.resources.key).unwrap(); - - debug::exit(debug::EXIT_SUCCESS); - } - - #[task(binds = UART1, priority = 2, resources = [&key])] - fn uart1(c: uart1::Context) { - hprintln!("UART1(key = {:#x})", c.resources.key).unwrap(); - } -}; diff --git a/examples/task.rs b/examples/task.rs index 43f7e56..9e563d7 100644 --- a/examples/task.rs +++ b/examples/task.rs @@ -17,16 +17,20 @@ const APP: () = { #[task(spawn = [bar, baz])] fn foo(c: foo::Context) { - hprintln!("foo").unwrap(); + hprintln!("foo - start").unwrap(); // spawns `bar` onto the task scheduler // `foo` and `bar` have the same priority so `bar` will not run until // after `foo` terminates c.spawn.bar().unwrap(); + hprintln!("foo - middle").unwrap(); + // spawns `baz` onto the task scheduler // `baz` has higher priority than `foo` so it immediately preempts `foo` c.spawn.baz().unwrap(); + + hprintln!("foo - end").unwrap(); } #[task] diff --git a/examples/types.rs b/examples/types.rs index 0c8097f..fc391d0 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -7,7 +7,7 @@ use cortex_m_semihosting::debug; use panic_semihosting as _; -use rtfm::cyccnt::Instant; +use rtfm::cyccnt; #[rtfm::app(device = lm3s6965, peripherals = true, monotonic = rtfm::cyccnt::CYCCNT)] const APP: () = { @@ -17,38 +17,39 @@ const APP: () = { } #[init(schedule = [foo], spawn = [foo])] - fn init(c: init::Context) { - let _: Instant = c.start; - let _: rtfm::Peripherals = c.core; - let _: lm3s6965::Peripherals = c.device; - let _: init::Schedule = c.schedule; - let _: init::Spawn = c.spawn; + fn init(cx: init::Context) { + let _: cyccnt::Instant = cx.start; + let _: rtfm::Peripherals = cx.core; + let _: lm3s6965::Peripherals = cx.device; + let _: init::Schedule = cx.schedule; + let _: init::Spawn = cx.spawn; debug::exit(debug::EXIT_SUCCESS); } - #[task(binds = SVCall, schedule = [foo], spawn = [foo])] - fn svcall(c: svcall::Context) { - let _: Instant = c.start; - let _: svcall::Schedule = c.schedule; - let _: svcall::Spawn = c.spawn; + #[idle(schedule = [foo], spawn = [foo])] + fn idle(cx: idle::Context) -> ! { + let _: idle::Schedule = cx.schedule; + let _: idle::Spawn = cx.spawn; + + loop {} } #[task(binds = UART0, resources = [shared], schedule = [foo], spawn = [foo])] - fn uart0(c: uart0::Context) { - let _: Instant = c.start; - let _: resources::shared = c.resources.shared; - let _: uart0::Schedule = c.schedule; - let _: uart0::Spawn = c.spawn; + fn uart0(cx: uart0::Context) { + let _: cyccnt::Instant = cx.start; + let _: resources::shared = cx.resources.shared; + let _: uart0::Schedule = cx.schedule; + let _: uart0::Spawn = cx.spawn; } #[task(priority = 2, resources = [shared], schedule = [foo], spawn = [foo])] - fn foo(c: foo::Context) { - let _: Instant = c.scheduled; - let _: &mut u32 = c.resources.shared; - let _: foo::Resources = c.resources; - let _: foo::Schedule = c.schedule; - let _: foo::Spawn = c.spawn; + fn foo(cx: foo::Context) { + let _: cyccnt::Instant = cx.scheduled; + let _: &mut u32 = cx.resources.shared; + let _: foo::Resources = cx.resources; + let _: foo::Schedule = cx.schedule; + let _: foo::Spawn = cx.spawn; } extern "C" { -- cgit v1.2.3 From 7ca5bbf404330a7f92e94e3d4d8cdf0438e0e5c6 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Wed, 21 Aug 2019 10:53:13 +0200 Subject: fix preempt example --- examples/preempt.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'examples') diff --git a/examples/preempt.rs b/examples/preempt.rs index 9f1b2a5..d7a7e64 100644 --- a/examples/preempt.rs +++ b/examples/preempt.rs @@ -12,26 +12,26 @@ use rtfm::app; const APP: () = { #[init] fn init(_: init::Context) { - rtfm::pend(Interrupt::UART0); + rtfm::pend(Interrupt::GPIOA); } - #[task(binds = UART0, priority = 1)] - fn uart0(_: uart0::Context) { - hprintln!("UART0 - start").unwrap(); - rtfm::pend(Interrupt::UART2); - hprintln!("UART0 - end").unwrap(); + #[task(binds = GPIOA, priority = 1)] + fn gpioa(_: gpioa::Context) { + hprintln!("GPIOA - start").unwrap(); + rtfm::pend(Interrupt::GPIOC); + hprintln!("GPIOA - end").unwrap(); debug::exit(debug::EXIT_SUCCESS); } - #[task(binds = UART1, priority = 2)] - fn uart1(_: uart1::Context) { - hprintln!(" UART1").unwrap(); + #[task(binds = GPIOB, priority = 2)] + fn gpiob(_: gpiob::Context) { + hprintln!(" GPIOB").unwrap(); } - #[task(binds = UART2, priority = 2)] - fn uart2(_: uart2::Context) { - hprintln!(" UART2 - start").unwrap(); - rtfm::pend(Interrupt::UART1); - hprintln!(" UART2 - end").unwrap(); + #[task(binds = GPIOC, priority = 2)] + fn gpioc(_: gpioc::Context) { + hprintln!(" GPIOC - start").unwrap(); + rtfm::pend(Interrupt::GPIOB); + hprintln!(" GPIOC - end").unwrap(); } }; -- cgit v1.2.3