@@ -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:: { PinMode , 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 ;
@@ -152,44 +142,69 @@ impl Default for Config {
152
142
}
153
143
}
154
144
155
- pub trait Pins < USART > {
145
+ /// Trait to mark serial pins with transmit capability.
146
+ pub trait TxPin < USART > {
147
+ fn setup ( & self ) ;
148
+ }
149
+
150
+ /// Trait to mark serial pins with receive capability.
151
+ pub trait RxPin < USART > {
156
152
fn setup ( & self ) ;
157
153
}
158
154
155
+ /// Macro to implement `TxPin` / `RxPin` for a certain pin, using a certain
156
+ /// alternative function and for a certain serial peripheral.
159
157
macro_rules! impl_pins {
160
- ( $( $instance : ty , $tx : ident, $rx : ident , $alt : ident; ) * ) => {
158
+ ( $( $pin : ident , $alt : ident, $instance : ty , $trait : ident; ) * ) => {
161
159
$(
162
- impl <Tx : PinMode , Rx : PinMode > Pins <$instance> for ( $tx< Tx > , $rx< Rx > ) {
160
+ impl <MODE : PinMode > $trait <$instance> for $pin< MODE > {
163
161
fn setup( & self ) {
164
- self . 0 . set_alt_mode( AltMode :: $alt) ;
165
- self . 1 . set_alt_mode( AltMode :: $alt) ;
162
+ self . set_alt_mode( AltMode :: $alt) ;
166
163
}
167
164
}
168
165
) *
169
166
}
170
167
}
171
168
172
- #[ cfg( feature = "stm32l0x1" ) ]
173
- impl_pins ! (
174
- LPUART1 , PA2 , PA3 , AF6 ;
175
- USART2 , PA9 , PA10 , AF4 ;
176
- ) ;
177
-
178
- #[ cfg( any( feature = "stm32l0x2" , feature = "stm32l0x3" ) ) ]
179
169
impl_pins ! (
180
- LPUART1 , PA2 , PA3 , AF6 ;
181
- LPUART1 , PB10 , PB11 , AF4 ;
182
- LPUART1 , PB11 , PB10 , AF7 ;
183
- USART1 , PA9 , PA10 , AF4 ;
184
- USART1 , PB6 , PB7 , AF0 ;
185
- USART2 , PA2 , PA3 , AF4 ;
186
- USART2 , PA14 , PA15 , AF4 ;
187
- USART2 , PD5 , PD6 , AF0 ;
188
- USART4 , PA0 , PA1 , AF6 ;
189
- USART4 , PC10 , PC11 , AF6 ;
190
- USART4 , PE8 , PE9 , AF6 ;
191
- USART5 , PB3 , PB4 , AF6 ;
192
- USART5 , PE10 , PE11 , AF6 ;
170
+ PA0 , AF6 , USART4 , TxPin ;
171
+ PA1 , AF6 , USART4 , RxPin ;
172
+ PA2 , AF4 , USART2 , TxPin ;
173
+ PA2 , AF6 , LPUART1 , TxPin ;
174
+ PA3 , AF4 , USART2 , RxPin ;
175
+ PA3 , AF6 , LPUART1 , RxPin ;
176
+ PA9 , AF4 , USART1 , TxPin ;
177
+ PA10 , AF4 , USART1 , RxPin ;
178
+ PA13 , AF6 , LPUART1 , RxPin ;
179
+ PA14 , AF4 , USART2 , TxPin ;
180
+ PA14 , AF6 , LPUART1 , TxPin ;
181
+ PA15 , AF4 , USART2 , RxPin ;
182
+ PB3 , AF6 , USART5 , TxPin ;
183
+ PB4 , AF6 , USART5 , RxPin ;
184
+ PB6 , AF0 , USART1 , TxPin ;
185
+ PB7 , AF0 , USART1 , RxPin ;
186
+ PB10 , AF4 , LPUART1 , TxPin ;
187
+ PB10 , AF7 , LPUART1 , RxPin ;
188
+ PB11 , AF4 , LPUART1 , RxPin ;
189
+ PB11 , AF7 , LPUART1 , TxPin ;
190
+ PC0 , AF6 , LPUART1 , RxPin ;
191
+ PC1 , AF6 , LPUART1 , TxPin ;
192
+ PC4 , AF2 , LPUART1 , TxPin ;
193
+ PC5 , AF2 , LPUART1 , RxPin ;
194
+ PC10 , AF0 , LPUART1 , TxPin ;
195
+ PC10 , AF6 , USART4 , TxPin ;
196
+ PC11 , AF0 , LPUART1 , RxPin ;
197
+ PC11 , AF6 , USART4 , RxPin ;
198
+ PC12 , AF2 , USART5 , TxPin ;
199
+ PD2 , AF6 , USART5 , RxPin ;
200
+ PD5 , AF0 , USART2 , TxPin ;
201
+ PD6 , AF0 , USART2 , RxPin ;
202
+ PD8 , AF0 , LPUART1 , TxPin ;
203
+ PD9 , AF0 , LPUART1 , RxPin ;
204
+ PE8 , AF6 , USART4 , TxPin ;
205
+ PE9 , AF6 , USART4 , RxPin ;
206
+ PE10 , AF6 , USART5 , TxPin ;
207
+ PE11 , AF6 , USART5 , RxPin ;
193
208
) ;
194
209
195
210
/// Serial abstraction
@@ -214,30 +229,34 @@ macro_rules! usart {
214
229
$USARTX: ident: ( $usartX: ident, $apbXenr: ident, $usartXen: ident, $pclkX: ident, $SerialExt: ident) ,
215
230
) +) => {
216
231
$(
217
- pub trait $SerialExt<PINS > {
218
- fn usart( self , pins : PINS , config: Config , rcc: & mut Rcc ) -> Result <Serial <$USARTX>, InvalidConfig >;
232
+ pub trait $SerialExt<TX , RX > {
233
+ fn usart( self , tx : TX , rx : RX , config: Config , rcc: & mut Rcc ) -> Result <Serial <$USARTX>, InvalidConfig >;
219
234
}
220
235
221
- impl <PINS > $SerialExt<PINS > for $USARTX
236
+ impl <TX , RX > $SerialExt<TX , RX > for $USARTX
222
237
where
223
- PINS : Pins <$USARTX>,
238
+ TX : TxPin <$USARTX>,
239
+ RX : RxPin <$USARTX>,
224
240
{
225
- fn usart( self , pins : PINS , config: Config , rcc: & mut Rcc ) -> Result <Serial <$USARTX>, InvalidConfig > {
226
- Serial :: $usartX( self , pins , config, rcc)
241
+ fn usart( self , tx : TX , rx : RX , config: Config , rcc: & mut Rcc ) -> Result <Serial <$USARTX>, InvalidConfig > {
242
+ Serial :: $usartX( self , tx , rx , config, rcc)
227
243
}
228
244
}
229
245
230
246
impl Serial <$USARTX> {
231
- pub fn $usartX<PINS >(
247
+ pub fn $usartX<TX , RX >(
232
248
usart: $USARTX,
233
- pins: PINS ,
249
+ tx: TX ,
250
+ rx: RX ,
234
251
config: Config ,
235
252
rcc: & mut Rcc ,
236
253
) -> Result <Self , InvalidConfig >
237
254
where
238
- PINS : Pins <$USARTX>,
255
+ TX : TxPin <$USARTX>,
256
+ RX : RxPin <$USARTX>,
239
257
{
240
- pins. setup( ) ;
258
+ tx. setup( ) ;
259
+ rx. setup( ) ;
241
260
242
261
// Enable clock for USART
243
262
rcc. rb. $apbXenr. modify( |_, w| w. $usartXen( ) . set_bit( ) ) ;
0 commit comments