2017-07-21 05:53:44 +02:00
|
|
|
//! Working with resources in a generic fashion
|
|
|
|
#![deny(unsafe_code)]
|
2017-12-09 15:12:42 +01:00
|
|
|
#![deny(warnings)]
|
2017-07-21 05:53:44 +02:00
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
extern crate cortex_m_rtfm as rtfm;
|
|
|
|
extern crate stm32f103xx;
|
|
|
|
|
|
|
|
use rtfm::{app, Resource, Threshold};
|
2018-08-24 16:31:04 +02:00
|
|
|
use stm32f103xx::{GPIOA, SPI1};
|
2017-07-21 05:53:44 +02:00
|
|
|
|
|
|
|
app! {
|
|
|
|
device: stm32f103xx,
|
|
|
|
|
2017-11-20 05:11:25 +01:00
|
|
|
resources: {
|
|
|
|
static GPIOA: GPIOA;
|
|
|
|
static SPI1: SPI1;
|
|
|
|
},
|
|
|
|
|
2017-07-21 05:53:44 +02:00
|
|
|
tasks: {
|
|
|
|
EXTI0: {
|
2017-07-27 18:40:15 +02:00
|
|
|
path: exti0,
|
2017-07-21 05:53:44 +02:00
|
|
|
priority: 1,
|
|
|
|
resources: [GPIOA, SPI1],
|
|
|
|
},
|
|
|
|
|
|
|
|
EXTI1: {
|
2017-07-27 18:40:15 +02:00
|
|
|
path: exti1,
|
2017-07-21 05:53:44 +02:00
|
|
|
priority: 2,
|
|
|
|
resources: [GPIOA, SPI1],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-12-09 14:45:59 +01:00
|
|
|
fn init(p: init::Peripherals) -> init::LateResources {
|
|
|
|
init::LateResources {
|
2017-11-20 05:11:25 +01:00
|
|
|
GPIOA: p.device.GPIOA,
|
|
|
|
SPI1: p.device.SPI1,
|
|
|
|
}
|
|
|
|
}
|
2017-07-21 05:53:44 +02:00
|
|
|
|
|
|
|
fn idle() -> ! {
|
|
|
|
loop {
|
|
|
|
rtfm::wfi();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// A generic function that uses some resources
|
2017-07-21 05:53:44 +02:00
|
|
|
fn work<G, S>(t: &mut Threshold, gpioa: &G, spi1: &S)
|
|
|
|
where
|
|
|
|
G: Resource<Data = GPIOA>,
|
|
|
|
S: Resource<Data = SPI1>,
|
|
|
|
{
|
|
|
|
gpioa.claim(t, |_gpioa, t| {
|
|
|
|
// drive NSS low
|
|
|
|
|
|
|
|
spi1.claim(t, |_spi1, _| {
|
|
|
|
// transfer data
|
|
|
|
});
|
|
|
|
|
|
|
|
// drive NSS high
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// This task needs critical sections to access the resources
|
2017-07-21 05:53:44 +02:00
|
|
|
fn exti0(t: &mut Threshold, r: EXTI0::Resources) {
|
|
|
|
work(t, &r.GPIOA, &r.SPI1);
|
|
|
|
}
|
|
|
|
|
2017-07-28 05:40:47 +02:00
|
|
|
// This task has direct access to the resources
|
2017-07-21 05:53:44 +02:00
|
|
|
fn exti1(t: &mut Threshold, r: EXTI1::Resources) {
|
2017-12-09 14:43:29 +01:00
|
|
|
work(t, &r.GPIOA, &r.SPI1);
|
2017-07-21 05:53:44 +02:00
|
|
|
}
|