@@ -2,17 +2,14 @@ use core::fmt;
2
2
use core:: marker:: PhantomData ;
3
3
use core:: ptr;
4
4
5
- use crate :: gpio:: gpioa:: * ;
5
+ use crate :: gpio:: { gpioa:: * , gpiob :: * , gpioc :: * , gpiod :: * , gpioe :: * } ;
6
6
use crate :: gpio:: AltMode ;
7
7
use crate :: hal;
8
8
use crate :: hal:: prelude:: * ;
9
- pub use crate :: pac:: USART2 ;
9
+ pub use crate :: pac:: { LPUART1 , USART1 , USART2 , USART4 , USART5 } ;
10
10
use crate :: rcc:: Rcc ;
11
11
use nb:: block;
12
12
13
- #[ cfg( feature = "stm32l0x1" ) ]
14
- pub use crate :: pac:: LPUART1 ;
15
-
16
13
#[ cfg( any( feature = "stm32l0x2" , feature = "stm32l0x3" ) ) ]
17
14
use core:: {
18
15
ops:: { Deref , DerefMut } ,
@@ -23,14 +20,7 @@ use core::{
23
20
use as_slice:: { AsMutSlice , AsSlice } ;
24
21
25
22
#[ cfg( any( feature = "stm32l0x2" , feature = "stm32l0x3" ) ) ]
26
- pub use crate :: {
27
- dma,
28
- gpio:: gpiob:: * ,
29
- gpio:: gpioc:: * ,
30
- gpio:: gpiod:: * ,
31
- gpio:: gpioe:: * ,
32
- pac:: { LPUART1 , USART1 , USART4 , USART5 } ,
33
- } ;
23
+ pub use crate :: dma;
34
24
35
25
#[ cfg( any( feature = "stm32l0x2" , feature = "stm32l0x3" ) ) ]
36
26
use dma:: Buffer ;
@@ -144,44 +134,69 @@ impl Default for Config {
144
134
}
145
135
}
146
136
147
- pub trait Pins < USART > {
137
+ /// Trait to mark serial pins with transmit capability.
138
+ pub trait TxPin < USART > {
139
+ fn setup ( & self ) ;
140
+ }
141
+
142
+ /// Trait to mark serial pins with receive capability.
143
+ pub trait RxPin < USART > {
148
144
fn setup ( & self ) ;
149
145
}
150
146
147
+ /// Macro to implement `TxPin` / `RxPin` for a certain pin, using a certain
148
+ /// alternative function and for a certain serial peripheral.
151
149
macro_rules! impl_pins {
152
- ( $( $instance : ty , $tx : ident, $rx : ident , $alt : ident; ) * ) => {
150
+ ( $( $pin : ident , $alt : ident, $instance : ty , $trait : ident; ) * ) => {
153
151
$(
154
- impl <Tx , Rx > Pins <$instance> for ( $tx< Tx > , $rx< Rx > ) {
152
+ impl <MODE > $trait <$instance> for $pin< MODE > {
155
153
fn setup( & self ) {
156
- self . 0 . set_alt_mode( AltMode :: $alt) ;
157
- self . 1 . set_alt_mode( AltMode :: $alt) ;
154
+ self . set_alt_mode( AltMode :: $alt) ;
158
155
}
159
156
}
160
157
) *
161
158
}
162
159
}
163
160
164
- #[ cfg( feature = "stm32l0x1" ) ]
165
- impl_pins ! (
166
- LPUART1 , PA2 , PA3 , AF6 ;
167
- USART2 , PA9 , PA10 , AF4 ;
168
- ) ;
169
-
170
- #[ cfg( any( feature = "stm32l0x2" , feature = "stm32l0x3" ) ) ]
171
161
impl_pins ! (
172
- LPUART1 , PA2 , PA3 , AF6 ;
173
- LPUART1 , PB10 , PB11 , AF4 ;
174
- LPUART1 , PB11 , PB10 , AF7 ;
175
- USART1 , PA9 , PA10 , AF4 ;
176
- USART1 , PB6 , PB7 , AF0 ;
177
- USART2 , PA2 , PA3 , AF4 ;
178
- USART2 , PA14 , PA15 , AF4 ;
179
- USART2 , PD5 , PD6 , AF0 ;
180
- USART4 , PA0 , PA1 , AF6 ;
181
- USART4 , PC10 , PC11 , AF6 ;
182
- USART4 , PE8 , PE9 , AF6 ;
183
- USART5 , PB3 , PB4 , AF6 ;
184
- USART5 , PE10 , PE11 , AF6 ;
162
+ PA0 , AF6 , USART4 , TxPin ;
163
+ PA1 , AF6 , USART4 , RxPin ;
164
+ PA2 , AF4 , USART2 , TxPin ;
165
+ PA2 , AF6 , LPUART1 , TxPin ;
166
+ PA3 , AF4 , USART2 , RxPin ;
167
+ PA3 , AF6 , LPUART1 , RxPin ;
168
+ PA9 , AF4 , USART1 , TxPin ;
169
+ PA10 , AF4 , USART1 , RxPin ;
170
+ PA13 , AF6 , LPUART1 , RxPin ;
171
+ PA14 , AF4 , USART2 , TxPin ;
172
+ PA14 , AF6 , LPUART1 , TxPin ;
173
+ PA15 , AF4 , USART2 , RxPin ;
174
+ PB3 , AF6 , USART5 , TxPin ;
175
+ PB4 , AF6 , USART5 , RxPin ;
176
+ PB6 , AF0 , USART1 , TxPin ;
177
+ PB7 , AF0 , USART1 , RxPin ;
178
+ PB10 , AF4 , LPUART1 , TxPin ;
179
+ PB10 , AF7 , LPUART1 , RxPin ;
180
+ PB11 , AF4 , LPUART1 , RxPin ;
181
+ PB11 , AF7 , LPUART1 , TxPin ;
182
+ PC0 , AF6 , LPUART1 , RxPin ;
183
+ PC1 , AF6 , LPUART1 , TxPin ;
184
+ PC4 , AF2 , LPUART1 , TxPin ;
185
+ PC5 , AF2 , LPUART1 , RxPin ;
186
+ PC10 , AF0 , LPUART1 , TxPin ;
187
+ PC10 , AF6 , USART4 , TxPin ;
188
+ PC11 , AF0 , LPUART1 , RxPin ;
189
+ PC11 , AF6 , USART4 , RxPin ;
190
+ PC12 , AF2 , USART5 , TxPin ;
191
+ PD2 , AF6 , USART5 , RxPin ;
192
+ PD5 , AF0 , USART2 , TxPin ;
193
+ PD6 , AF0 , USART2 , RxPin ;
194
+ PD8 , AF0 , LPUART1 , TxPin ;
195
+ PD9 , AF0 , LPUART1 , RxPin ;
196
+ PE8 , AF6 , USART4 , TxPin ;
197
+ PE9 , AF6 , USART4 , RxPin ;
198
+ PE10 , AF6 , USART5 , TxPin ;
199
+ PE11 , AF6 , USART5 , RxPin ;
185
200
) ;
186
201
187
202
/// Serial abstraction
@@ -206,30 +221,34 @@ macro_rules! usart {
206
221
$USARTX: ident: ( $usartX: ident, $apbXenr: ident, $usartXen: ident, $pclkX: ident, $SerialExt: ident) ,
207
222
) +) => {
208
223
$(
209
- pub trait $SerialExt<PINS > {
210
- fn usart( self , pins : PINS , config: Config , rcc: & mut Rcc ) -> Result <Serial <$USARTX>, InvalidConfig >;
224
+ pub trait $SerialExt<TX , RX > {
225
+ fn usart( self , tx : TX , rx : RX , config: Config , rcc: & mut Rcc ) -> Result <Serial <$USARTX>, InvalidConfig >;
211
226
}
212
227
213
- impl <PINS > $SerialExt<PINS > for $USARTX
228
+ impl <TX , RX > $SerialExt<TX , RX > for $USARTX
214
229
where
215
- PINS : Pins <$USARTX>,
230
+ TX : TxPin <$USARTX>,
231
+ RX : RxPin <$USARTX>,
216
232
{
217
- fn usart( self , pins : PINS , config: Config , rcc: & mut Rcc ) -> Result <Serial <$USARTX>, InvalidConfig > {
218
- Serial :: $usartX( self , pins , config, rcc)
233
+ fn usart( self , tx : TX , rx : RX , config: Config , rcc: & mut Rcc ) -> Result <Serial <$USARTX>, InvalidConfig > {
234
+ Serial :: $usartX( self , tx , rx , config, rcc)
219
235
}
220
236
}
221
237
222
238
impl Serial <$USARTX> {
223
- pub fn $usartX<PINS >(
239
+ pub fn $usartX<TX , RX >(
224
240
usart: $USARTX,
225
- pins: PINS ,
241
+ tx: TX ,
242
+ rx: RX ,
226
243
config: Config ,
227
244
rcc: & mut Rcc ,
228
245
) -> Result <Self , InvalidConfig >
229
246
where
230
- PINS : Pins <$USARTX>,
247
+ TX : TxPin <$USARTX>,
248
+ RX : RxPin <$USARTX>,
231
249
{
232
- pins. setup( ) ;
250
+ tx. setup( ) ;
251
+ rx. setup( ) ;
233
252
234
253
// Enable clock for USART
235
254
rcc. rb. $apbXenr. modify( |_, w| w. $usartXen( ) . set_bit( ) ) ;
0 commit comments