From aa2249454975a203e459597005944f5370c1d200 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Thu, 27 Jul 2017 11:40:15 -0500 Subject: update tests and examples with task! gone 3 types of errors / gotchas have been eliminated :tada: --- examples/full-syntax.rs | 16 ++++++---------- examples/generics.rs | 7 ++----- examples/modules.rs | 19 +++++++------------ examples/nested.rs | 10 +++------- examples/one-task.rs | 38 +++++++++++++++++++------------------- examples/preemption.rs | 8 ++------ examples/two-tasks.rs | 7 ++----- 7 files changed, 41 insertions(+), 64 deletions(-) (limited to 'examples') diff --git a/examples/full-syntax.rs b/examples/full-syntax.rs index 6965a63..918a2e6 100644 --- a/examples/full-syntax.rs +++ b/examples/full-syntax.rs @@ -5,7 +5,6 @@ #![feature(proc_macro)] #![no_std] -#[macro_use(task)] extern crate cortex_m_rtfm as rtfm; extern crate stm32f103xx; @@ -16,6 +15,7 @@ app! { resources: { static CO_OWNED: u32 = 0; + static ON: bool = false; static OWNED: bool = false; static SHARED: bool = false; }, @@ -31,12 +31,14 @@ app! { tasks: { SYS_TICK: { + path: sys_tick, priority: 1, - resources: [CO_OWNED, SHARED], + resources: [CO_OWNED, ON, SHARED], }, TIM2: { enabled: true, + path: tim2, priority: 1, resources: [CO_OWNED], }, @@ -59,18 +61,12 @@ fn idle_(t: &mut Threshold, mut r: idle::Resources) -> ! { } } -task!(SYS_TICK, sys_tick, Local { - static STATE: bool = true; -}); - -fn sys_tick(_t: &mut Threshold, l: &mut Local, r: SYS_TICK::Resources) { - *l.STATE = !*l.STATE; +fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) { + **r.ON = !**r.ON; **r.CO_OWNED += 1; } -task!(TIM2, tim2); - fn tim2(_t: &mut Threshold, r: TIM2::Resources) { **r.CO_OWNED += 1; } diff --git a/examples/generics.rs b/examples/generics.rs index 335d159..7c261d5 100644 --- a/examples/generics.rs +++ b/examples/generics.rs @@ -4,7 +4,6 @@ #![feature(proc_macro)] #![no_std] -#[macro_use(task)] extern crate cortex_m_rtfm as rtfm; extern crate stm32f103xx; @@ -17,12 +16,14 @@ app! { tasks: { EXTI0: { enabled: true, + path: exti0, priority: 1, resources: [GPIOA, SPI1], }, EXTI1: { enabled: true, + path: exti1, priority: 2, resources: [GPIOA, SPI1], }, @@ -54,15 +55,11 @@ where }); } -task!(EXTI0, exti0); - // this task needs critical sections to access the resources fn exti0(t: &mut Threshold, r: EXTI0::Resources) { work(t, &r.GPIOA, &r.SPI1); } -task!(EXTI1, exti1); - // this task has direct access to the resources fn exti1(t: &mut Threshold, r: EXTI1::Resources) { work(t, r.GPIOA, r.SPI1); diff --git a/examples/modules.rs b/examples/modules.rs index aabada4..313ebc4 100644 --- a/examples/modules.rs +++ b/examples/modules.rs @@ -1,11 +1,9 @@ //! Using paths and modules - #![deny(unsafe_code)] #![feature(const_fn)] #![feature(proc_macro)] #![no_std] -#[macro_use(task)] extern crate cortex_m_rtfm as rtfm; extern crate stm32f103xx; @@ -16,6 +14,7 @@ app! { resources: { static CO_OWNED: u32 = 0; + static ON: bool = false; static OWNED: bool = false; static SHARED: bool = false; }, @@ -31,12 +30,14 @@ app! { tasks: { SYS_TICK: { + path: tasks::sys_tick, priority: 1, - resources: [CO_OWNED, SHARED], + resources: [CO_OWNED, ON, SHARED], }, TIM2: { enabled: true, + path: tasks::tim2, priority: 1, resources: [CO_OWNED], }, @@ -66,19 +67,13 @@ mod main { pub mod tasks { use rtfm::Threshold; - task!(SYS_TICK, sys_tick, Locals { - static STATE: bool = true; - }); - - fn sys_tick(_t: &mut Threshold, l: &mut Locals, r: ::SYS_TICK::Resources) { - *l.STATE = !*l.STATE; + pub fn sys_tick(_t: &mut Threshold, r: ::SYS_TICK::Resources) { + **r.ON = !**r.ON; **r.CO_OWNED += 1; } - task!(TIM2, tim2); - - fn tim2(_t: &mut Threshold, r: ::TIM2::Resources) { + pub fn tim2(_t: &mut Threshold, r: ::TIM2::Resources) { **r.CO_OWNED += 1; } } diff --git a/examples/nested.rs b/examples/nested.rs index fda2a7d..2cb23f9 100644 --- a/examples/nested.rs +++ b/examples/nested.rs @@ -8,7 +8,6 @@ #![feature(proc_macro)] #![no_std] -#[macro_use(task)] extern crate cortex_m_rtfm as rtfm; extern crate stm32f103xx; @@ -26,18 +25,21 @@ app! { tasks: { EXTI0: { enabled: true, + path: exti0, priority: 1, resources: [LOW, HIGH], }, EXTI1: { enabled: true, + path: exti1, priority: 2, resources: [LOW], }, EXTI2: { enabled: true, + path: exti2, priority: 3, resources: [HIGH], }, @@ -58,8 +60,6 @@ fn idle() -> ! { } } -task!(EXTI0, exti0); - fn exti0(t: &mut Threshold, r: EXTI0::Resources) { // because this task has a priority of 1 the preemption threshold is also 1 @@ -113,15 +113,11 @@ fn exti0(t: &mut Threshold, r: EXTI0::Resources) { // ~> exti1 } -task!(EXTI1, exti1); - fn exti1(_t: &mut Threshold, _r: EXTI1::Resources) { // B, H rtfm::bkpt(); } -task!(EXTI2, exti2); - fn exti2(_t: &mut Threshold, _r: EXTI2::Resources) { // D, G rtfm::bkpt(); diff --git a/examples/one-task.rs b/examples/one-task.rs index 8cfe089..e1d598d 100644 --- a/examples/one-task.rs +++ b/examples/one-task.rs @@ -1,12 +1,10 @@ //! An application with one task - #![deny(unsafe_code)] #![feature(const_fn)] #![feature(proc_macro)] #![no_std] extern crate cortex_m; -#[macro_use(task)] extern crate cortex_m_rtfm as rtfm; extern crate stm32f103xx; @@ -16,6 +14,15 @@ use rtfm::{app, Threshold}; app! { device: stm32f103xx, + // Here resources are declared + // + // Resources are static variables that are safe to share across tasks + resources: { + // declaration of resources looks exactly like declaration of static + // variables + static ON: bool = false; + }, + // Here tasks are declared // // Each task corresponds to an interrupt or an exception. Every time the @@ -24,7 +31,11 @@ app! { tasks: { // Here we declare that we'll use the SYS_TICK exception as a task SYS_TICK: { + // Path to the task *handler* + path: sys_tick, + // This is the priority of the task. + // // 1 is the lowest priority a task can have. // The maximum priority is determined by the number of priority bits // the device has. This device has 4 priority bits so 16 is the @@ -34,12 +45,12 @@ app! { // These are the *resources* associated with this task // // The peripherals that the task needs can be listed here - resources: [GPIOC], + resources: [GPIOC, ON], }, } } -fn init(p: init::Peripherals) { +fn init(p: init::Peripherals, _r: init::Resources) { // power on GPIOC p.RCC.apb2enr.modify(|_, w| w.iopcen().enabled()); @@ -62,26 +73,15 @@ fn idle() -> ! { } } -// This binds the `sys_tick` handler to the `SYS_TICK` task -// -// This particular handler has local state associated to it. The value of the -// `STATE` variable will be preserved across invocations of this handler -task!(SYS_TICK, sys_tick, Locals { - static STATE: bool = false; -}); - // This is the task handler of the SYS_TICK exception // -// `t` is the preemption threshold token. We won't use it this time. -// `l` is the data local to this task. The type here must match the one declared -// in `task!`. // `r` is the resources this task has access to. `SYS_TICK::Resources` has one -// field per resource declared in `app!`. -fn sys_tick(_t: &mut Threshold, l: &mut Locals, r: SYS_TICK::Resources) { +// field per every resource declared in `app!`. +fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) { // toggle state - *l.STATE = !*l.STATE; + **r.ON = !**r.ON; - if *l.STATE { + if **r.ON { // set the pin PC13 high r.GPIOC.bsrr.write(|w| w.bs13().set()); } else { diff --git a/examples/preemption.rs b/examples/preemption.rs index 2ca6f95..e117695 100644 --- a/examples/preemption.rs +++ b/examples/preemption.rs @@ -1,11 +1,9 @@ //! Two tasks running at different priorities with access to the same resource - #![deny(unsafe_code)] #![feature(const_fn)] #![feature(proc_macro)] #![no_std] -#[macro_use(task)] extern crate cortex_m_rtfm as rtfm; extern crate stm32f103xx; @@ -21,12 +19,14 @@ app! { tasks: { // the task `SYS_TICK` has higher priority than `TIM2` SYS_TICK: { + path: sys_tick, priority: 2, resources: [COUNTER], }, TIM2: { enabled: true, + path: tim2, priority: 1, resources: [COUNTER], }, @@ -43,8 +43,6 @@ fn idle() -> ! { } } -task!(SYS_TICK, sys_tick); - fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) { // .. @@ -55,8 +53,6 @@ fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) { // .. } -task!(TIM2, tim2); - fn tim2(t: &mut Threshold, mut r: TIM2::Resources) { // .. diff --git a/examples/two-tasks.rs b/examples/two-tasks.rs index eb74fa8..7343137 100644 --- a/examples/two-tasks.rs +++ b/examples/two-tasks.rs @@ -5,7 +5,6 @@ #![feature(proc_macro)] #![no_std] -#[macro_use(task)] extern crate cortex_m_rtfm as rtfm; extern crate stm32f103xx; @@ -23,6 +22,7 @@ app! { tasks: { SYS_TICK: { + path: sys_tick, priority: 1, // Both this task and TIM2 have access to the `COUNTER` resource resources: [COUNTER], @@ -34,6 +34,7 @@ app! { // indicates if the interrupt will be enabled or disabled once // `idle` starts enabled: true, + path: tim2, priority: 1, resources: [COUNTER], }, @@ -52,8 +53,6 @@ fn idle() -> ! { } } -task!(SYS_TICK, sys_tick); - // As both tasks are running at the same priority one can't preempt the other. // Thus both tasks have direct access to the resource fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) { @@ -64,8 +63,6 @@ fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) { // .. } -task!(TIM2, tim2); - fn tim2(_t: &mut Threshold, r: TIM2::Resources) { // .. -- cgit v1.2.3