@@ -6,49 +6,46 @@ use panic_halt;
6
6
7
7
use stm32f0xx_hal as hal;
8
8
9
- use crate :: hal:: gpio:: * ;
10
- use crate :: hal:: prelude:: * ;
11
- use crate :: hal:: stm32;
9
+ use crate :: hal:: { gpio:: * , prelude:: * , stm32} ;
12
10
13
- use cortex_m:: interrupt:: Mutex ;
14
- use cortex_m:: peripheral:: syst:: SystClkSource :: Core ;
15
- use cortex_m:: peripheral:: Peripherals ;
11
+ use cortex_m:: { interrupt:: Mutex , peripheral:: syst:: SystClkSource :: Core , Peripherals } ;
16
12
use cortex_m_rt:: { entry, exception} ;
17
13
18
14
use core:: cell:: RefCell ;
19
15
use core:: ops:: DerefMut ;
20
16
17
+ // Mutex protected structure for our shared GPIO pin
21
18
static GPIO : Mutex < RefCell < Option < gpioa:: PA1 < Output < PushPull > > > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
22
19
23
20
#[ entry]
24
21
fn main ( ) -> ! {
25
- if let ( Some ( p) , Some ( cp) ) = ( stm32:: Peripherals :: take ( ) , Peripherals :: take ( ) ) {
22
+ if let ( Some ( mut p) , Some ( cp) ) = ( stm32:: Peripherals :: take ( ) , Peripherals :: take ( ) ) {
26
23
cortex_m:: interrupt:: free ( move |cs| {
27
- let mut flash = p. FLASH ;
28
- let mut rcc = p. RCC . configure ( ) . sysclk ( 48 . mhz ( ) ) . freeze ( & mut flash) ;
24
+ let mut rcc = p. RCC . configure ( ) . sysclk ( 48 . mhz ( ) ) . freeze ( & mut p. FLASH ) ;
29
25
30
26
let gpioa = p. GPIOA . split ( & mut rcc) ;
31
27
32
- let mut syst = cp. SYST ;
33
-
34
- /* (Re-)configure PA1 as output */
28
+ // (Re-)configure PA1 as output
35
29
let led = gpioa. pa1 . into_push_pull_output ( cs) ;
36
30
31
+ // Transfer GPIO into a shared structure
37
32
* GPIO . borrow ( cs) . borrow_mut ( ) = Some ( led) ;
38
33
39
- /* Initialise SysTick counter with a defined value */
34
+ let mut syst = cp. SYST ;
35
+
36
+ // Initialise SysTick counter with a defined value
40
37
unsafe { syst. cvr . write ( 1 ) } ;
41
38
42
- /* Set source for SysTick counter, here full operating frequency (== 8MHz) */
39
+ // Set source for SysTick counter, here full operating frequency (== 48MHz)
43
40
syst. set_clock_source ( Core ) ;
44
41
45
- /* Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms */
42
+ // Set reload value, i.e. timer delay 48 MHz/4 Mcounts == 12Hz or 83ms
46
43
syst. set_reload ( 4_000_000 - 1 ) ;
47
44
48
- /* Start counter */
45
+ // Start counting
49
46
syst. enable_counter ( ) ;
50
47
51
- /* Start interrupt generation */
48
+ // Enable interrupt generation
52
49
syst. enable_interrupt ( ) ;
53
50
} ) ;
54
51
}
@@ -58,28 +55,29 @@ fn main() -> ! {
58
55
}
59
56
}
60
57
61
- /* Define an exception, i.e. function to call when exception occurs. Here if our SysTick timer
62
- * trips the flash function will be called and the specified stated passed in via argument */
63
- //, flash, state: u8 = 1);
58
+ // Define an exception handler, i.e. function to call when exception occurs. Here, if our SysTick
59
+ // timer generates an exception the following handler will be called
64
60
#[ exception]
65
61
fn SysTick ( ) -> ! {
62
+ // Exception handler state variable
66
63
static mut state: u8 = 1 ;
67
64
68
- /* Enter critical section */
65
+ // Enter critical section
69
66
cortex_m:: interrupt:: free ( |cs| {
67
+ // Borrow access to our GPIO pin from the shared structure
70
68
if let Some ( ref mut led) = * GPIO . borrow ( cs) . borrow_mut ( ) . deref_mut ( ) {
71
- /* Check state variable, keep LED off most of the time and turn it on every 10th tick */
69
+ // Check state variable, keep LED off most of the time and turn it on every 10th tick
72
70
if * state < 10 {
73
- /* If set turn off the LED */
71
+ // Turn off the LED
74
72
led. set_low ( ) ;
75
73
76
- /* And now increment state variable */
74
+ // And now increment state variable
77
75
* state += 1 ;
78
76
} else {
79
- /* If not set, turn on the LED */
77
+ // Turn on the LED
80
78
led. set_high ( ) ;
81
79
82
- /* And set new state variable back to 0 */
80
+ // And set new state variable back to 0
83
81
* state = 0 ;
84
82
}
85
83
}
0 commit comments