24
24
//!
25
25
//! ---
26
26
27
+ #![ deny( warnings) ]
28
+ #![ feature( const_fn) ]
27
29
#![ no_std]
28
30
29
31
extern crate cortex_m;
32
+ extern crate cortex_m_semihosting;
30
33
#[ macro_use( exception, interrupt) ]
31
34
extern crate stm32f103xx;
32
35
33
- use cortex_m:: interrupt;
36
+ use core:: cell:: RefCell ;
37
+ use core:: fmt:: Write ;
38
+
39
+ use cortex_m:: interrupt:: { self , Mutex } ;
40
+ use cortex_m:: peripheral:: SystClkSource ;
41
+ use cortex_m_semihosting:: hio:: { self , HStdout } ;
42
+ use stm32f103xx:: Interrupt ;
43
+
44
+ static HSTDOUT : Mutex < RefCell < Option < HStdout > > > =
45
+ Mutex :: new ( RefCell :: new ( None ) ) ;
34
46
35
47
fn main ( ) {
36
48
interrupt:: free ( |cs| {
37
- let _gpioa = stm32f103xx:: GPIOA . borrow ( cs) ;
38
- // do something with GPIOA
49
+ let hstdout = HSTDOUT . borrow ( cs) ;
50
+ if let Ok ( fd) = hio:: hstdout ( ) {
51
+ * hstdout. borrow_mut ( ) = Some ( fd) ;
52
+ }
53
+
54
+ let nvic = stm32f103xx:: NVIC . borrow ( cs) ;
55
+ nvic. enable ( Interrupt :: TIM2 ) ;
56
+
57
+ let syst = stm32f103xx:: SYST . borrow ( cs) ;
58
+ syst. set_clock_source ( SystClkSource :: Core ) ;
59
+ syst. set_reload ( 8_000_000 ) ; // 1s
60
+ syst. enable_counter ( ) ;
61
+ syst. enable_interrupt ( ) ;
39
62
} ) ;
40
63
}
41
64
42
- exception ! ( SYS_TICK , tick, locals: {
43
- ticks: u32 = 0 ;
44
- } ) ;
65
+ exception ! ( SYS_TICK , tick) ;
66
+
67
+ fn tick ( ) {
68
+ interrupt:: free ( |cs| {
69
+ let hstdout = HSTDOUT . borrow ( cs) ;
70
+ if let Some ( hstdout) = hstdout. borrow_mut ( ) . as_mut ( ) {
71
+ writeln ! ( * hstdout, "Tick" ) . ok ( ) ;
72
+ }
73
+
74
+ let nvic = stm32f103xx:: NVIC . borrow ( cs) ;
45
75
46
- fn tick ( l : & mut SYS_TICK :: Locals ) {
47
- l. ticks += 1 ;
48
- // ..
76
+ nvic. set_pending ( Interrupt :: TIM2 ) ;
77
+ } ) ;
49
78
}
50
79
51
80
interrupt ! ( TIM2 , tock, locals: {
@@ -54,5 +83,11 @@ interrupt!(TIM2, tock, locals: {
54
83
55
84
fn tock ( l : & mut TIM2 :: Locals ) {
56
85
l. tocks += 1 ;
57
- // ..
86
+
87
+ interrupt:: free ( |cs| {
88
+ let hstdout = HSTDOUT . borrow ( cs) ;
89
+ if let Some ( hstdout) = hstdout. borrow_mut ( ) . as_mut ( ) {
90
+ writeln ! ( * hstdout, "Tock ({})" , l. tocks) . ok ( ) ;
91
+ }
92
+ } ) ;
58
93
}
0 commit comments