From c91369f83ca79241128abee2abb4d23d98273ee4 Mon Sep 17 00:00:00 2001 From: Per Lindgren <per.lindgren@ltu.se> Date: Mon, 5 Feb 2018 14:27:01 +0100 Subject: [PATCH] rtfm-examples --- .../{rtfm-blinky.rs => rtfm-blinky-systic.rs} | 2 +- examples/rtfm-blinky-tim2.rs | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) rename examples/{rtfm-blinky.rs => rtfm-blinky-systic.rs} (98%) create mode 100644 examples/rtfm-blinky-tim2.rs 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 87eacd1..371674c 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 0000000..e182b12 --- /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() + } +} -- GitLab