@@ -17,17 +17,14 @@ use hal::{
1717 delay:: Delay ,
1818 gpio:: {
1919 gpioa:: { PA15 , PA2 } ,
20- gpiob:: PB3 ,
21- Alternate , Output , PushPull , AF1 ,
20+ Alternate , AF1 ,
2221 } ,
2322 pac:: { self , interrupt, Interrupt , USART1 } ,
2423 prelude:: * ,
2524 serial:: Serial ,
2625} ;
2726use stm32f0xx_hal as hal;
2827
29- // setup some types for our globally shared resources
30- type LED_PIN = PB3 < Output < PushPull > > ;
3128type SERIAL = Serial < USART1 , PA2 < Alternate < AF1 > > , PA15 < Alternate < AF1 > > > ;
3229
3330/*
@@ -40,48 +37,11 @@ that the resource inside the Mutex will not violate
4037the RefMut's runtime borrowing rules (Given that we do not
4138try to borrow the RefMut more than once per CriticalSection).
4239 */
43- static LED : Mutex < RefCell < Option < LED_PIN > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
4440static SER_PORT : Mutex < RefCell < Option < SERIAL > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
4541
46- // some helper macros to ensure safe usage of our globals
47- macro_rules! init_global {
48- ( $CS: ident, $GLOBAL: ident, $VAL: expr) => {
49- * $GLOBAL. borrow( $CS) . borrow_mut( ) = Some ( $VAL) ;
50- } ;
51- }
52-
53- macro_rules! with_global_mut {
54- ( $CS: ident, $GLOBAL: ident, $LOCAL: ident, $CODE: block) => {
55- if let Some ( $LOCAL) = $GLOBAL. borrow( $CS) . borrow_mut( ) . as_mut( ) {
56- $CODE
57- } else {
58- panic!( ) ;
59- }
60- } ;
61- }
62-
63- // a helper macro to generalize the initialization of a UART
64- macro_rules! setup_uart {
65- ( $CS: ident, $DP: ident, $RCC: ident, $GPIOx: ident ) => {
66- let ( tx, rx) = (
67- $GPIOx. pa2. into_alternate_af1( $CS) ,
68- $GPIOx. pa15. into_alternate_af1( $CS) ,
69- ) ;
70-
71- init_global!(
72- $CS,
73- SER_PORT ,
74- Serial :: usart1( $DP. USART1 , ( tx, rx) , 9_600 . bps( ) , & mut $RCC)
75- ) ;
76- with_global_mut!( $CS, SER_PORT , ser, {
77- ser. listen( hal:: serial:: Event :: Rxne ) ;
78- } ) ;
79- } ;
80- }
81-
8242#[ entry]
8343fn main ( ) -> ! {
84- let mut delay = cortex_m:: interrupt:: free ( |cs| {
44+ let ( mut delay, mut led ) = cortex_m:: interrupt:: free ( |cs| {
8545 let dp = pac:: Peripherals :: take ( ) . unwrap ( ) ; // might as well panic if this doesn't work
8646 let cp = cortex_m:: peripheral:: Peripherals :: take ( ) . unwrap ( ) ;
8747 let mut flash = dp. FLASH ;
@@ -91,11 +51,24 @@ fn main() -> ! {
9151 let gpiob = dp. GPIOB . split ( & mut rcc) ;
9252
9353 let delay = Delay :: new ( cp. SYST , & rcc) ;
94- setup_uart ! ( cs, dp, rcc, gpioa) ;
9554
96- init_global ! ( cs, LED , gpiob. pb3. into_push_pull_output( cs) ) ;
55+ // setup UART
56+ let ( tx, rx) = (
57+ gpioa. pa2 . into_alternate_af1 ( cs) ,
58+ gpioa. pa15 . into_alternate_af1 ( cs) ,
59+ ) ;
60+
61+ // initialize global serial
62+ * SER_PORT . borrow ( cs) . borrow_mut ( ) =
63+ Some ( Serial :: usart1 ( dp. USART1 , ( tx, rx) , 9_600 . bps ( ) , & mut rcc) ) ;
64+
65+ if let Some ( ser) = SER_PORT . borrow ( cs) . borrow_mut ( ) . as_mut ( ) {
66+ ser. listen ( hal:: serial:: Event :: Rxne ) ; // trigger the USART1 interrupt when bytes are available (receive buffer not empty)
67+ }
68+
69+ let led = gpiob. pb3 . into_push_pull_output ( cs) ;
9770
98- delay
71+ ( delay, led )
9972 } ) ;
10073
10174 #[ allow( unsafe_code) ] // just this once ;)
@@ -104,11 +77,7 @@ fn main() -> ! {
10477 }
10578
10679 loop {
107- cortex_m:: interrupt:: free ( |cs| {
108- with_global_mut ! ( cs, LED , led, {
109- led. toggle( ) . ok( ) ;
110- } ) ;
111- } ) ;
80+ led. toggle ( ) . ok ( ) ;
11281
11382 delay. delay_ms ( 1_000u16 ) ;
11483 }
@@ -117,7 +86,7 @@ fn main() -> ! {
11786#[ interrupt]
11887fn USART1 ( ) {
11988 cortex_m:: interrupt:: free ( |cs| {
120- with_global_mut ! ( cs , SER_PORT , ser, {
89+ if let Some ( ser) = SER_PORT . borrow ( cs ) . borrow_mut ( ) . as_mut ( ) {
12190 if let Ok ( data) = block ! ( ser. read( ) ) {
12291 block ! ( ser. write( data) ) . ok ( ) ;
12392 } else {
@@ -128,6 +97,6 @@ fn USART1() {
12897 was disconnected or something.
12998 */
13099 }
131- } ) ;
100+ }
132101 } ) ;
133102}
0 commit comments