@@ -17,17 +17,14 @@ use hal::{
17
17
delay:: Delay ,
18
18
gpio:: {
19
19
gpioa:: { PA15 , PA2 } ,
20
- gpiob:: PB3 ,
21
- Alternate , Output , PushPull , AF1 ,
20
+ Alternate , AF1 ,
22
21
} ,
23
22
pac:: { self , interrupt, Interrupt , USART1 } ,
24
23
prelude:: * ,
25
24
serial:: Serial ,
26
25
} ;
27
26
use stm32f0xx_hal as hal;
28
27
29
- // setup some types for our globally shared resources
30
- type LED_PIN = PB3 < Output < PushPull > > ;
31
28
type SERIAL = Serial < USART1 , PA2 < Alternate < AF1 > > , PA15 < Alternate < AF1 > > > ;
32
29
33
30
/*
@@ -40,48 +37,11 @@ that the resource inside the Mutex will not violate
40
37
the RefMut's runtime borrowing rules (Given that we do not
41
38
try to borrow the RefMut more than once per CriticalSection).
42
39
*/
43
- static LED : Mutex < RefCell < Option < LED_PIN > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
44
40
static SER_PORT : Mutex < RefCell < Option < SERIAL > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
45
41
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
-
82
42
#[ entry]
83
43
fn main ( ) -> ! {
84
- let mut delay = cortex_m:: interrupt:: free ( |cs| {
44
+ let ( mut delay, mut led ) = cortex_m:: interrupt:: free ( |cs| {
85
45
let dp = pac:: Peripherals :: take ( ) . unwrap ( ) ; // might as well panic if this doesn't work
86
46
let cp = cortex_m:: peripheral:: Peripherals :: take ( ) . unwrap ( ) ;
87
47
let mut flash = dp. FLASH ;
@@ -91,11 +51,24 @@ fn main() -> ! {
91
51
let gpiob = dp. GPIOB . split ( & mut rcc) ;
92
52
93
53
let delay = Delay :: new ( cp. SYST , & rcc) ;
94
- setup_uart ! ( cs, dp, rcc, gpioa) ;
95
54
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) ;
97
70
98
- delay
71
+ ( delay, led )
99
72
} ) ;
100
73
101
74
#[ allow( unsafe_code) ] // just this once ;)
@@ -104,11 +77,7 @@ fn main() -> ! {
104
77
}
105
78
106
79
loop {
107
- cortex_m:: interrupt:: free ( |cs| {
108
- with_global_mut ! ( cs, LED , led, {
109
- led. toggle( ) . ok( ) ;
110
- } ) ;
111
- } ) ;
80
+ led. toggle ( ) . ok ( ) ;
112
81
113
82
delay. delay_ms ( 1_000u16 ) ;
114
83
}
@@ -117,7 +86,7 @@ fn main() -> ! {
117
86
#[ interrupt]
118
87
fn USART1 ( ) {
119
88
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 ( ) {
121
90
if let Ok ( data) = block ! ( ser. read( ) ) {
122
91
block ! ( ser. write( data) ) . ok ( ) ;
123
92
} else {
@@ -128,6 +97,6 @@ fn USART1() {
128
97
was disconnected or something.
129
98
*/
130
99
}
131
- } ) ;
100
+ }
132
101
} ) ;
133
102
}
0 commit comments