@@ -186,16 +186,53 @@ unsafe impl<USART> Send for Tx<USART> {}
186
186
187
187
#[ cfg( feature = "device-selected" ) ]
188
188
macro_rules! usart {
189
- ( $( $USART: ident: ( $usart: ident, $usartXen: ident, $apbenr: ident) , ) +) => {
189
+ ( $( $USART: ident: ( $usart: ident, $usarttx : ident , $usartrx : ident , $ usartXen: ident, $apbenr: ident) , ) +) => {
190
190
$(
191
191
use crate :: stm32:: $USART;
192
- impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN > {
192
+ impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN >
193
+ where
194
+ TXPIN : TxPin <$USART>,
195
+ RXPIN : RxPin <$USART>,
196
+ {
193
197
/// Creates a new serial instance
194
198
pub fn $usart( usart: $USART, pins: ( TXPIN , RXPIN ) , baud_rate: Bps , clocks: Clocks ) -> Self
195
- where
196
- TXPIN : TxPin <$USART>,
197
- RXPIN : RxPin <$USART>,
198
199
{
200
+ let mut serial = Serial { usart, pins } ;
201
+ serial. enable( baud_rate, clocks) ;
202
+ serial
203
+ }
204
+ }
205
+
206
+ impl <TXPIN > Serial <$USART, TXPIN , ( ) >
207
+ where
208
+ TXPIN : TxPin <$USART>,
209
+ {
210
+ /// Creates a new tx-only serial instance
211
+ pub fn $usarttx( usart: $USART, txpin: TXPIN , baud_rate: Bps , clocks: Clocks ) -> Self
212
+ {
213
+ let rxpin = ( ) ;
214
+ let mut serial = Serial { usart, pins: ( txpin, rxpin) } ;
215
+ serial. enable( baud_rate, clocks) ;
216
+ serial
217
+ }
218
+ }
219
+
220
+ impl <RXPIN > Serial <$USART, ( ) , RXPIN >
221
+ where
222
+ RXPIN : RxPin <$USART>,
223
+ {
224
+ /// Creates a new tx-only serial instance
225
+ pub fn $usartrx( usart: $USART, rxpin: RXPIN , baud_rate: Bps , clocks: Clocks ) -> Self
226
+ {
227
+ let txpin = ( ) ;
228
+ let mut serial = Serial { usart, pins: ( txpin, rxpin) } ;
229
+ serial. enable( baud_rate, clocks) ;
230
+ serial
231
+ }
232
+ }
233
+
234
+ impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN > {
235
+ fn enable( & mut self , baud_rate: Bps , clocks: Clocks ) {
199
236
// NOTE(unsafe) This executes only during initialisation
200
237
let rcc = unsafe { & ( * crate :: stm32:: RCC :: ptr( ) ) } ;
201
238
@@ -204,16 +241,14 @@ macro_rules! usart {
204
241
205
242
// Calculate correct baudrate divisor on the fly
206
243
let brr = clocks. pclk( ) . 0 / baud_rate. 0 ;
207
- usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
244
+ self . usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
208
245
209
246
// Reset other registers to disable advanced USART features
210
- usart. cr2. reset( ) ;
211
- usart. cr3. reset( ) ;
247
+ self . usart. cr2. reset( ) ;
248
+ self . usart. cr3. reset( ) ;
212
249
213
250
// Enable transmission and receiving
214
- usart. cr1. modify( |_, w| w. te( ) . set_bit( ) . re( ) . set_bit( ) . ue( ) . set_bit( ) ) ;
215
-
216
- Serial { usart, pins }
251
+ self . usart. cr1. modify( |_, w| w. te( ) . set_bit( ) . re( ) . set_bit( ) . ue( ) . set_bit( ) ) ;
217
252
}
218
253
219
254
/// Starts listening for an interrupt event
@@ -252,7 +287,7 @@ macro_rules! usart {
252
287
253
288
#[ cfg( feature = "device-selected" ) ]
254
289
usart ! {
255
- USART1 : ( usart1, usart1en, apb2enr) ,
290
+ USART1 : ( usart1, usart1tx , usart1rx , usart1en, apb2enr) ,
256
291
}
257
292
#[ cfg( any(
258
293
feature = "stm32f030x8" ,
@@ -262,21 +297,21 @@ usart! {
262
297
feature = "stm32f072" ,
263
298
) ) ]
264
299
usart ! {
265
- USART2 : ( usart2, usart2en, apb1enr) ,
300
+ USART2 : ( usart2, usart2tx , usart2rx , usart2en, apb1enr) ,
266
301
}
267
302
#[ cfg( any(
268
303
feature = "stm32f030xc" ,
269
304
feature = "stm32f070xb" ,
270
305
feature = "stm32f072" ,
271
306
) ) ]
272
307
usart ! {
273
- USART3 : ( usart3, usart3en, apb1enr) ,
274
- USART4 : ( usart4, usart4en, apb1enr) ,
308
+ USART3 : ( usart3, usart3tx , usart3rx , usart3en, apb1enr) ,
309
+ USART4 : ( usart4, usart4tx , usart4rx , usart4en, apb1enr) ,
275
310
}
276
311
#[ cfg( feature = "stm32f030xc" ) ]
277
312
usart ! {
278
- USART5 : ( usart5, usart5en, apb1enr) ,
279
- USART6 : ( usart6, usart6en, apb2enr) ,
313
+ USART5 : ( usart5, usart5tx , usart5rx , usart5en, apb1enr) ,
314
+ USART6 : ( usart6, usart6tx , usart6rx , usart6en, apb2enr) ,
280
315
}
281
316
282
317
// It's s needed for the impls, but rustc doesn't recognize that
@@ -313,6 +348,22 @@ where
313
348
}
314
349
}
315
350
351
+ impl < USART , TXPIN , RXPIN > embedded_hal:: serial:: Read < u8 > for Serial < USART , TXPIN , RXPIN >
352
+ where
353
+ USART : Deref < Target = SerialRegisterBlock > ,
354
+ RXPIN : RxPin < USART > ,
355
+ {
356
+ type Error = Error ;
357
+
358
+ /// Tries to read a byte from the uart
359
+ fn read ( & mut self ) -> nb:: Result < u8 , Error > {
360
+ Rx {
361
+ usart : & self . usart as * const _ ,
362
+ }
363
+ . read ( )
364
+ }
365
+ }
366
+
316
367
#[ cfg( feature = "device-selected" ) ]
317
368
impl < USART > embedded_hal:: serial:: Write < u8 > for Tx < USART >
318
369
where
@@ -349,14 +400,43 @@ where
349
400
}
350
401
}
351
402
403
+ impl < USART , TXPIN , RXPIN > embedded_hal:: serial:: Write < u8 > for Serial < USART , TXPIN , RXPIN >
404
+ where
405
+ USART : Deref < Target = SerialRegisterBlock > ,
406
+ TXPIN : TxPin < USART > ,
407
+ {
408
+ type Error = void:: Void ;
409
+
410
+ /// Ensures that none of the previously written words are still buffered
411
+ fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
412
+ Tx {
413
+ usart : & self . usart as * const _ ,
414
+ }
415
+ . flush ( )
416
+ }
417
+
418
+ /// Tries to write a byte to the uart
419
+ /// Fails if the transmit buffer is full
420
+ fn write ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Self :: Error > {
421
+ Tx {
422
+ usart : & self . usart as * const _ ,
423
+ }
424
+ . write ( byte)
425
+ }
426
+ }
427
+
352
428
#[ cfg( feature = "device-selected" ) ]
353
429
impl < USART , TXPIN , RXPIN > Serial < USART , TXPIN , RXPIN >
354
430
where
355
431
USART : Deref < Target = SerialRegisterBlock > ,
356
432
{
357
433
/// Splits the UART Peripheral in a Tx and an Rx part
358
434
/// This is required for sending/receiving
359
- pub fn split ( self ) -> ( Tx < USART > , Rx < USART > ) {
435
+ pub fn split ( self ) -> ( Tx < USART > , Rx < USART > )
436
+ where
437
+ TXPIN : TxPin < USART > ,
438
+ RXPIN : RxPin < USART > ,
439
+ {
360
440
(
361
441
Tx {
362
442
usart : & self . usart as * const _ ,
@@ -366,6 +446,7 @@ where
366
446
} ,
367
447
)
368
448
}
449
+
369
450
pub fn release ( self ) -> ( USART , ( TXPIN , RXPIN ) ) {
370
451
( self . usart , self . pins )
371
452
}
0 commit comments