File tree 1 file changed +18
-3
lines changed
1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change 7
7
extern crate panic_semihosting;
8
8
9
9
use core:: fmt:: Write ;
10
+ use core:: cell:: { RefCell } ;
10
11
12
+ use cortex_m:: interrupt:: { free, Mutex } ;
11
13
use cortex_m_rt:: entry;
12
14
use cortex_m_semihosting:: hio;
13
15
14
16
use cortex_m:: peripheral:: NVIC ;
15
17
16
18
use stm32f7xx_hal:: {
17
- interrupt, pac,
19
+ interrupt,
20
+ pac:: { self , TIM2 } ,
18
21
prelude:: * ,
19
22
timer:: { Event , Timer } ,
20
23
} ;
21
24
25
+ static TIMER : Mutex < RefCell < Option < Timer < TIM2 > > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
26
+
22
27
#[ entry]
23
28
fn main ( ) -> ! {
24
29
let mut hstdout = hio:: hstdout ( ) . unwrap ( ) ;
@@ -29,17 +34,27 @@ fn main() -> ! {
29
34
let mut rcc = dp. RCC . constrain ( ) ;
30
35
let clocks = rcc. cfgr . freeze ( ) ;
31
36
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
+
32
45
unsafe {
33
46
NVIC :: unmask ( pac:: Interrupt :: TIM2 ) ;
34
47
}
35
- let mut timer = Timer :: tim2 ( dp. TIM2 , 1 . Hz ( ) , clocks, & mut rcc. apb1 ) ;
36
- timer. listen ( Event :: TimeOut ) ;
37
48
38
49
loop { }
39
50
}
40
51
41
52
#[ interrupt]
42
53
fn TIM2 ( ) {
54
+ free ( |cs| {
55
+ TIMER . borrow ( cs) . borrow_mut ( ) . as_mut ( ) . unwrap ( ) . clear_interrupt ( Event :: TimeOut )
56
+ } ) ;
57
+
43
58
let mut hstdout = hio:: hstdout ( ) . unwrap ( ) ;
44
59
writeln ! ( hstdout, "TIM2 intr!" ) . unwrap ( ) ;
45
60
}
You can’t perform that action at this time.
0 commit comments