From 15f9ca67e478c792792b8dac8ab18c9eb8fc2437 Mon Sep 17 00:00:00 2001 From: Maxime Borges Date: Fri, 28 Jan 2022 17:57:46 +0100 Subject: [PATCH 1/2] example/timer: Clear the interrupt flag --- examples/timer.rs | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/examples/timer.rs b/examples/timer.rs index 10677f0..85ca39d 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -7,18 +7,23 @@ extern crate panic_semihosting; use core::fmt::Write; +use core::cell::{RefCell}; +use cortex_m::interrupt::{free, Mutex}; use cortex_m_rt::entry; use cortex_m_semihosting::hio; use cortex_m::peripheral::NVIC; use stm32f7xx_hal::{ - interrupt, pac, + interrupt, + pac::{self, TIM2}, prelude::*, timer::{Event, Timer}, }; +static TIMER: Mutex>>> = Mutex::new(RefCell::new(None)); + #[entry] fn main() -> ! { let mut hstdout = hio::hstdout().unwrap(); @@ -29,17 +34,27 @@ fn main() -> ! { let mut rcc = dp.RCC.constrain(); let clocks = rcc.cfgr.freeze(); + let mut timer = Timer::tim2(dp.TIM2, 1.Hz(), clocks, &mut rcc.apb1); + timer.listen(Event::TimeOut); + + // Save information needed by the interrupt handler to the global variable + free(|cs| { + TIMER.borrow(cs).replace(Some(timer)); + }); + unsafe { NVIC::unmask(pac::Interrupt::TIM2); } - let mut timer = Timer::tim2(dp.TIM2, 1.Hz(), clocks, &mut rcc.apb1); - timer.listen(Event::TimeOut); loop {} } #[interrupt] fn TIM2() { + free(|cs| { + TIMER.borrow(cs).borrow_mut().as_mut().unwrap().clear_interrupt(Event::TimeOut) + }); + let mut hstdout = hio::hstdout().unwrap(); writeln!(hstdout, "TIM2 intr!").unwrap(); } From 96285139ae5bb6adb47681235e70c7086369fe8b Mon Sep 17 00:00:00 2001 From: Maxime Borges Date: Tue, 8 Feb 2022 18:05:53 +0100 Subject: [PATCH 2/2] Fix formating --- examples/timer.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/examples/timer.rs b/examples/timer.rs index 85ca39d..b22e2b3 100644 --- a/examples/timer.rs +++ b/examples/timer.rs @@ -6,8 +6,8 @@ extern crate panic_semihosting; +use core::cell::RefCell; use core::fmt::Write; -use core::cell::{RefCell}; use cortex_m::interrupt::{free, Mutex}; use cortex_m_rt::entry; @@ -52,7 +52,12 @@ fn main() -> ! { #[interrupt] fn TIM2() { free(|cs| { - TIMER.borrow(cs).borrow_mut().as_mut().unwrap().clear_interrupt(Event::TimeOut) + TIMER + .borrow(cs) + .borrow_mut() + .as_mut() + .unwrap() + .clear_interrupt(Event::TimeOut) }); let mut hstdout = hio::hstdout().unwrap();