diff --git a/examples/rtfm-blinky.rs b/examples/rtfm-blinky-systic.rs similarity index 98% rename from examples/rtfm-blinky.rs rename to examples/rtfm-blinky-systic.rs index 87eacd1453a95d448a69520eedd7b1f7ba28b20d..371674c5bee2d3fad7bd3b18d93fad59c94906f7 100644 --- a/examples/rtfm-blinky.rs +++ b/examples/rtfm-blinky-systic.rs @@ -47,7 +47,7 @@ fn init(p: init::Peripherals) -> init::LateResources { fn idle() -> ! { loop { - rtfm::wfi(); + // rtfm::wfi(); } } diff --git a/examples/rtfm-blinky-tim2.rs b/examples/rtfm-blinky-tim2.rs new file mode 100644 index 0000000000000000000000000000000000000000..e182b120b6e5922ad2f4b1472a438f81d07b2f45 --- /dev/null +++ b/examples/rtfm-blinky-tim2.rs @@ -0,0 +1,72 @@ +#![feature(proc_macro)] +#![deny(unsafe_code)] +// #![deny(warnings)] +#![no_std] + +extern crate cortex_m; +extern crate cortex_m_rtfm as rtfm; +extern crate stm32f4x_hal as hal; + +use hal::gpio::gpioa::PA5; +use hal::gpio::{Output, PushPull}; +use hal::prelude::*; +use hal::stm32f4x; +//use hal::stm32f4x::{self, TIM2}; // <- TIM2 multiple defined + +use hal::timer::{Event, Timer}; +use rtfm::{app, Threshold}; + +app! { + device: stm32f4x, + + resources: { + static GREEN_LED: PA5<Output<PushPull>>; + // TIM2 appears both as an interrupt identifier and a register block. + // Both cannot be in scope at the same time. + // Give a qualified path to the register block. + static TIMER: Timer<stm32f4x::TIM2>; + }, + + tasks: { + TIM2: { + path: tim2, + resources: [GREEN_LED, TIMER], + }, + } +} + +fn init(p: init::Peripherals) -> init::LateResources { + let mut flash = p.device.FLASH.constrain(); + let mut rcc = p.device.RCC.constrain(); + + let clocks = rcc.cfgr.freeze(&mut flash.acr); + let mut gpioa = p.device.GPIOA.split(&mut rcc.ahb1); + + let led = gpioa + .pa5 + .into_push_pull_output(&mut gpioa.moder, &mut gpioa.otyper); + + let mut timer = Timer::tim2(p.device.TIM2, 1.hz(), clocks, &mut rcc.apb1); + + timer.listen(Event::TimeOut); + + init::LateResources { + GREEN_LED: led, + TIMER: timer, + } +} + +fn idle() -> ! { + loop { + //rtfm::wfi(); + } +} + +fn tim2(_t: &mut Threshold, mut r: TIM2::Resources) { + r.TIMER.wait().unwrap(); // to clear the interrupt + if r.GREEN_LED.is_low() { + r.GREEN_LED.set_high() + } else { + r.GREEN_LED.set_low() + } +}