mirror of
https://github.com/rtic-rs/rtic.git
synced 2025-12-16 21:05:35 +01:00
update tests and examples
with task! gone 3 types of errors / gotchas have been eliminated 🎉
This commit is contained in:
parent
0b5afce771
commit
aa22494549
17 changed files with 68 additions and 230 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
// ..
|
||||
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
// ..
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue