Commit a6dd0041 authored by Jorge Aparicio's avatar Jorge Aparicio

implement the Resource trait for owned resources

this unbreaks the "generics" example
parent 219e1726
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(proc_macro)]
#![no_std]
extern crate cortex_m_rtfm as rtfm;
extern crate stm32f103xx;
use rtfm::{app, Threshold};
pub struct Foo;
app! {
device: stm32f103xx,
resources: {
static CO_OWNED: Foo = Foo;
static ON: Foo = Foo;
static OWNED: Foo = Foo;
static SHARED: Foo = Foo;
},
idle: {
resources: [OWNED, SHARED],
},
tasks: {
SYS_TICK: {
path: sys_tick,
resources: [CO_OWNED, ON, SHARED],
},
TIM2: {
enabled: false,
path: tim2,
priority: 1,
resources: [CO_OWNED],
},
},
}
fn init(_p: ::init::Peripherals, _r: ::init::Resources) {}
fn idle(_t: &mut Threshold, _r: ::idle::Resources) -> ! {
loop {}
}
fn sys_tick(_t: &mut Threshold, _r: SYS_TICK::Resources) {}
fn tim2(_t: &mut Threshold, _r: TIM2::Resources) {}
......@@ -73,12 +73,12 @@ mod main {
}
}
fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
*r.ON = !*r.ON;
*r.CO_OWNED += 1;
}
fn tim2(_t: &mut Threshold, r: TIM2::Resources) {
fn tim2(_t: &mut Threshold, mut r: TIM2::Resources) {
*r.CO_OWNED += 1;
}
......@@ -70,5 +70,5 @@ fn exti0(t: &mut Threshold, r: EXTI0::Resources) {
// This task has direct access to the resources
fn exti1(t: &mut Threshold, r: EXTI1::Resources) {
work(t, r.GPIOA, r.SPI1);
work(t, &r.GPIOA, &r.SPI1);
}
//! Demonstrates initialization of resources in `init`.
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(proc_macro)]
......
......@@ -77,7 +77,7 @@ fn idle() -> ! {
// `r` is the set of resources this task has access to. `SYS_TICK::Resources`
// has one field per resource declared in `app!`.
#[allow(unsafe_code)]
fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
// toggle state
*r.ON = !*r.ON;
......
......@@ -42,7 +42,7 @@ fn idle() -> ! {
}
}
fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
// ..
// This task can't be preempted by `tim2` so it has direct access to the
......
......@@ -42,7 +42,7 @@ fn idle() -> ! {
// 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) {
fn sys_tick(_t: &mut Threshold, mut r: SYS_TICK::Resources) {
// ..
*r.COUNTER += 1;
......@@ -50,7 +50,7 @@ fn sys_tick(_t: &mut Threshold, r: SYS_TICK::Resources) {
// ..
}
fn tim2(_t: &mut Threshold, r: TIM2::Resources) {
fn tim2(_t: &mut Threshold, mut r: TIM2::Resources) {
// ..
*r.COUNTER += 1;
......
......@@ -17,6 +17,13 @@ pub enum Ownership {
}
impl Ownership {
pub fn ceiling(&self) -> u8 {
match *self {
Ownership::Owned { priority } => priority,
Ownership::Shared { ceiling } => ceiling,
}
}
pub fn is_owned(&self) -> bool {
match *self {
Ownership::Owned { .. } => true,
......
This diff is collapsed.
......@@ -45,22 +45,24 @@ fn idle() -> ! {
}
fn exti0(mut t: &mut Threshold, mut r: EXTI0::Resources) {
// ERROR need to lock to access the resource because priority < ceiling
if *r.ON {
//~^ error type `EXTI0::ON` cannot be dereferenced
}
// OK need to lock to access the resource
if r.ON.claim(&mut t, |on, _| **on) {}
if r.ON.claim(&mut t, |on, _| *on) {}
// OK can claim a resource with maximum ceiling
r.MAX.claim_mut(&mut t, |max, _| **max += 1);
r.MAX.claim_mut(&mut t, |max, _| *max += 1);
}
fn exti1(mut t: &mut Threshold, r: EXTI1::Resources) {
// ERROR no need to lock. Has direct access because priority == ceiling
if (**r.ON).claim(&mut t, |on, _| **on) {
//~^ error no method named `claim` found for type
}
// OK to directly access the resource because priority == ceiling
if *r.ON {}
if **r.ON {
// OK
}
// though the resource can still be claimed -- the claim is a no-op
if r.ON.claim(&mut t, |on, _| *on) {}
}
fn exti2(_t: &mut Threshold, _r: EXTI2::Resources) {}
......@@ -9,7 +9,7 @@ extern crate stm32f103xx;
use rtfm::{app, Threshold};
app! { //~ error bound `rtfm::Threshold: core::marker::Send` is not satisfied
app! { //~ error bound `*const (): core::marker::Send` is not satisfied
device: stm32f103xx,
resources: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment