Skip to content

Commit 15f9ca6

Browse files
committed
example/timer: Clear the interrupt flag
1 parent 679b4d9 commit 15f9ca6

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

examples/timer.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,23 @@
77
extern crate panic_semihosting;
88

99
use core::fmt::Write;
10+
use core::cell::{RefCell};
1011

12+
use cortex_m::interrupt::{free, Mutex};
1113
use cortex_m_rt::entry;
1214
use cortex_m_semihosting::hio;
1315

1416
use cortex_m::peripheral::NVIC;
1517

1618
use stm32f7xx_hal::{
17-
interrupt, pac,
19+
interrupt,
20+
pac::{self, TIM2},
1821
prelude::*,
1922
timer::{Event, Timer},
2023
};
2124

25+
static TIMER: Mutex<RefCell<Option<Timer<TIM2>>>> = Mutex::new(RefCell::new(None));
26+
2227
#[entry]
2328
fn main() -> ! {
2429
let mut hstdout = hio::hstdout().unwrap();
@@ -29,17 +34,27 @@ fn main() -> ! {
2934
let mut rcc = dp.RCC.constrain();
3035
let clocks = rcc.cfgr.freeze();
3136

37+
let mut timer = Timer::tim2(dp.TIM2, 1.Hz(), clocks, &mut rcc.apb1);
38+
timer.listen(Event::TimeOut);
39+
40+
// Save information needed by the interrupt handler to the global variable
41+
free(|cs| {
42+
TIMER.borrow(cs).replace(Some(timer));
43+
});
44+
3245
unsafe {
3346
NVIC::unmask(pac::Interrupt::TIM2);
3447
}
35-
let mut timer = Timer::tim2(dp.TIM2, 1.Hz(), clocks, &mut rcc.apb1);
36-
timer.listen(Event::TimeOut);
3748

3849
loop {}
3950
}
4051

4152
#[interrupt]
4253
fn TIM2() {
54+
free(|cs| {
55+
TIMER.borrow(cs).borrow_mut().as_mut().unwrap().clear_interrupt(Event::TimeOut)
56+
});
57+
4358
let mut hstdout = hio::hstdout().unwrap();
4459
writeln!(hstdout, "TIM2 intr!").unwrap();
4560
}

0 commit comments

Comments
 (0)