@@ -6,49 +6,46 @@ use panic_halt;
66
77use stm32f0xx_hal as hal;
88
9- use crate :: hal:: gpio:: * ;
10- use crate :: hal:: prelude:: * ;
11- use crate :: hal:: stm32;
9+ use crate :: hal:: { gpio:: * , prelude:: * , stm32} ;
1210
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 } ;
1612use cortex_m_rt:: { entry, exception} ;
1713
1814use core:: cell:: RefCell ;
1915use core:: ops:: DerefMut ;
2016
17+ // Mutex protected structure for our shared GPIO pin
2118static GPIO : Mutex < RefCell < Option < gpioa:: PA1 < Output < PushPull > > > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
2219
2320#[ entry]
2421fn 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 ( ) ) {
2623 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 ) ;
2925
3026 let gpioa = p. GPIOA . split ( & mut rcc) ;
3127
32- let mut syst = cp. SYST ;
33-
34- /* (Re-)configure PA1 as output */
28+ // (Re-)configure PA1 as output
3529 let led = gpioa. pa1 . into_push_pull_output ( cs) ;
3630
31+ // Transfer GPIO into a shared structure
3732 * GPIO . borrow ( cs) . borrow_mut ( ) = Some ( led) ;
3833
39- /* Initialise SysTick counter with a defined value */
34+ let mut syst = cp. SYST ;
35+
36+ // Initialise SysTick counter with a defined value
4037 unsafe { syst. cvr . write ( 1 ) } ;
4138
42- /* Set source for SysTick counter, here full operating frequency (== 8MHz) */
39+ // Set source for SysTick counter, here full operating frequency (== 48MHz)
4340 syst. set_clock_source ( Core ) ;
4441
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
4643 syst. set_reload ( 4_000_000 - 1 ) ;
4744
48- /* Start counter */
45+ // Start counting
4946 syst. enable_counter ( ) ;
5047
51- /* Start interrupt generation */
48+ // Enable interrupt generation
5249 syst. enable_interrupt ( ) ;
5350 } ) ;
5451 }
@@ -58,28 +55,29 @@ fn main() -> ! {
5855 }
5956}
6057
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
6460#[ exception]
6561fn SysTick ( ) -> ! {
62+ // Exception handler state variable
6663 static mut state: u8 = 1 ;
6764
68- /* Enter critical section */
65+ // Enter critical section
6966 cortex_m:: interrupt:: free ( |cs| {
67+ // Borrow access to our GPIO pin from the shared structure
7068 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
7270 if * state < 10 {
73- /* If set turn off the LED */
71+ // Turn off the LED
7472 led. set_low ( ) ;
7573
76- /* And now increment state variable */
74+ // And now increment state variable
7775 * state += 1 ;
7876 } else {
79- /* If not set, turn on the LED */
77+ // Turn on the LED
8078 led. set_high ( ) ;
8179
82- /* And set new state variable back to 0 */
80+ // And set new state variable back to 0
8381 * state = 0 ;
8482 }
8583 }
0 commit comments