Skip to content

Commit 2fb147b

Browse files
authored
Merge pull request #169 from maximeborges/bugfix/example-timer
example/timer: Clear the interrupt flag
2 parents 90ca932 + 9628513 commit 2fb147b

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

examples/timer.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,24 @@
66

77
extern crate panic_semihosting;
88

9+
use core::cell::RefCell;
910
use core::fmt::Write;
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,32 @@ 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
56+
.borrow(cs)
57+
.borrow_mut()
58+
.as_mut()
59+
.unwrap()
60+
.clear_interrupt(Event::TimeOut)
61+
});
62+
4363
let mut hstdout = hio::hstdout().unwrap();
4464
writeln!(hstdout, "TIM2 intr!").unwrap();
4565
}

0 commit comments

Comments
 (0)