From 70a32c5f7318a0b02c86cf9b4bbf27c5cbcea428 Mon Sep 17 00:00:00 2001 From: Evgenii Ponomarev Date: Tue, 21 Jan 2020 12:49:15 +0800 Subject: [PATCH] Update timer-interrupt-rtfm example --- examples/timer-interrupt-rtfm.rs | 52 +++++++++++++++++--------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/examples/timer-interrupt-rtfm.rs b/examples/timer-interrupt-rtfm.rs index cdfde84d..1093f60f 100644 --- a/examples/timer-interrupt-rtfm.rs +++ b/examples/timer-interrupt-rtfm.rs @@ -11,7 +11,6 @@ // you can put a breakpoint on `rust_begin_unwind` to catch panics use panic_halt as _; -use cortex_m::asm::wfi; use rtfm::app; use stm32f1xx_hal::{ @@ -22,15 +21,20 @@ use stm32f1xx_hal::{ }; use embedded_hal::digital::v2::OutputPin; -#[app(device = stm32f1xx_hal::pac)] +#[app(device = stm32f1xx_hal::pac, peripherals = true)] const APP: () = { - static mut LED: PC13> = (); - static mut TIMER_HANDLER: CountDownTimer = (); - static mut LED_STATE: bool = false; - + struct Resources { + led: PC13>, + timer_handler: CountDownTimer, + + #[init(false)] + led_state: bool, + } + #[init] - fn init() -> init::LateResources { + fn init(cx: init::Context) -> init::LateResources { + let device = cx.device; // Take ownership over the raw flash and rcc devices and convert them into the corresponding // HAL structs @@ -54,22 +58,22 @@ const APP: () = { // Init the static resources to use them later through RTFM init::LateResources { - LED: led, - TIMER_HANDLER: timer, + led, + timer_handler: timer, } } + // This is optional and can be removed. When removed, RTFM automatically creates an + // idle loop with WFI as its body. #[idle] - fn idle() -> ! { - + fn idle(_cx: idle::Context) -> ! { loop { - // Waits for interrupt - wfi(); + cortex_m::asm::wfi(); } } - #[interrupt(priority = 1, resources = [LED, TIMER_HANDLER, LED_STATE])] - fn TIM1_UP() { + #[task(binds = TIM1_UP, priority = 1, resources = [led, timer_handler, led_state])] + fn tim1_up(cx: tim1_up::Context) { // Depending on the application, you could want to delegate some of the work done here to // the idle task if you want to minimize the latency of interrupts with same priority (if // you have any). That could be done with some kind of machine state, etc. @@ -77,25 +81,25 @@ const APP: () = { // Count used to change the timer update frequency static mut COUNT: u8 = 0; - if *resources.LED_STATE { - // Uses resourcers managed by rtfm to turn led off (on bluepill) - resources.LED.set_high().unwrap(); - *resources.LED_STATE = false; + if *cx.resources.led_state { + // Uses resources managed by rtfm to turn led off (on bluepill) + cx.resources.led.set_high().unwrap(); + *cx.resources.led_state = false; } else { - resources.LED.set_low().unwrap(); - *resources.LED_STATE = true; + cx.resources.led.set_low().unwrap(); + *cx.resources.led_state = true; } *COUNT += 1; if *COUNT == 4 { // Changes timer update frequency - resources.TIMER_HANDLER.start(2.hz()); + cx.resources.timer_handler.start(2.hz()); } else if *COUNT == 12 { - resources.TIMER_HANDLER.start(1.hz()); + cx.resources.timer_handler.start(1.hz()); *COUNT = 0; } // Clears the update flag - resources.TIMER_HANDLER.clear_update_interrupt_flag(); + cx.resources.timer_handler.clear_update_interrupt_flag(); } };