@@ -210,16 +210,53 @@ unsafe impl<USART> Send for Tx<USART> {}
210
210
211
211
#[ cfg( feature = "device-selected" ) ]
212
212
macro_rules! usart {
213
- ( $( $USART: ident: ( $usart: ident, $usartXen: ident, $apbenr: ident) , ) +) => {
213
+ ( $( $USART: ident: ( $usart: ident, $usarttx : ident , $usartrx : ident , $ usartXen: ident, $apbenr: ident) , ) +) => {
214
214
$(
215
215
use crate :: stm32:: $USART;
216
- impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN > {
216
+ impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN >
217
+ where
218
+ TXPIN : TxPin <$USART>,
219
+ RXPIN : RxPin <$USART>,
220
+ {
217
221
/// Creates a new serial instance
218
222
pub fn $usart( usart: $USART, pins: ( TXPIN , RXPIN ) , baud_rate: Bps , clocks: Clocks ) -> Self
219
- where
220
- TXPIN : TxPin <$USART>,
221
- RXPIN : RxPin <$USART>,
222
223
{
224
+ let mut serial = Serial { usart, pins } ;
225
+ serial. enable( baud_rate, clocks) ;
226
+ serial
227
+ }
228
+ }
229
+
230
+ impl <TXPIN > Serial <$USART, TXPIN , ( ) >
231
+ where
232
+ TXPIN : TxPin <$USART>,
233
+ {
234
+ /// Creates a new tx-only serial instance
235
+ pub fn $usarttx( usart: $USART, txpin: TXPIN , baud_rate: Bps , clocks: Clocks ) -> Self
236
+ {
237
+ let rxpin = ( ) ;
238
+ let mut serial = Serial { usart, pins: ( txpin, rxpin) } ;
239
+ serial. enable( baud_rate, clocks) ;
240
+ serial
241
+ }
242
+ }
243
+
244
+ impl <RXPIN > Serial <$USART, ( ) , RXPIN >
245
+ where
246
+ RXPIN : RxPin <$USART>,
247
+ {
248
+ /// Creates a new tx-only serial instance
249
+ pub fn $usartrx( usart: $USART, rxpin: RXPIN , baud_rate: Bps , clocks: Clocks ) -> Self
250
+ {
251
+ let txpin = ( ) ;
252
+ let mut serial = Serial { usart, pins: ( txpin, rxpin) } ;
253
+ serial. enable( baud_rate, clocks) ;
254
+ serial
255
+ }
256
+ }
257
+
258
+ impl <TXPIN , RXPIN > Serial <$USART, TXPIN , RXPIN > {
259
+ fn enable( & mut self , baud_rate: Bps , clocks: Clocks ) {
223
260
// NOTE(unsafe) This executes only during initialisation
224
261
let rcc = unsafe { & ( * crate :: stm32:: RCC :: ptr( ) ) } ;
225
262
@@ -228,16 +265,14 @@ macro_rules! usart {
228
265
229
266
// Calculate correct baudrate divisor on the fly
230
267
let brr = clocks. pclk( ) . 0 / baud_rate. 0 ;
231
- usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
268
+ self . usart. brr. write( |w| unsafe { w. bits( brr) } ) ;
232
269
233
270
// Reset other registers to disable advanced USART features
234
- usart. cr2. reset( ) ;
235
- usart. cr3. reset( ) ;
271
+ self . usart. cr2. reset( ) ;
272
+ self . usart. cr3. reset( ) ;
236
273
237
274
// Enable transmission and receiving
238
- usart. cr1. modify( |_, w| w. te( ) . set_bit( ) . re( ) . set_bit( ) . ue( ) . set_bit( ) ) ;
239
-
240
- Serial { usart, pins }
275
+ self . usart. cr1. modify( |_, w| w. te( ) . set_bit( ) . re( ) . set_bit( ) . ue( ) . set_bit( ) ) ;
241
276
}
242
277
243
278
/// Starts listening for an interrupt event
@@ -276,7 +311,7 @@ macro_rules! usart {
276
311
277
312
#[ cfg( feature = "device-selected" ) ]
278
313
usart ! {
279
- USART1 : ( usart1, usart1en, apb2enr) ,
314
+ USART1 : ( usart1, usart1tx , usart1rx , usart1en, apb2enr) ,
280
315
}
281
316
#[ cfg( any(
282
317
feature = "stm32f030x8" ,
@@ -287,7 +322,7 @@ usart! {
287
322
feature = "stm32f091" ,
288
323
) ) ]
289
324
usart ! {
290
- USART2 : ( usart2, usart2en, apb1enr) ,
325
+ USART2 : ( usart2, usart2tx , usart2rx , usart2en, apb1enr) ,
291
326
}
292
327
#[ cfg( any(
293
328
feature = "stm32f030xc" ,
@@ -296,13 +331,13 @@ usart! {
296
331
feature = "stm32f091" ,
297
332
) ) ]
298
333
usart ! {
299
- USART3 : ( usart3, usart3en, apb1enr) ,
300
- USART4 : ( usart4, usart4en, apb1enr) ,
334
+ USART3 : ( usart3, usart3tx , usart3rx , usart3en, apb1enr) ,
335
+ USART4 : ( usart4, usart4tx , usart4rx , usart4en, apb1enr) ,
301
336
}
302
337
#[ cfg( any( feature = "stm32f030xc" , feature = "stm32f091" ) ) ]
303
338
usart ! {
304
- USART5 : ( usart5, usart5en, apb1enr) ,
305
- USART6 : ( usart6, usart6en, apb2enr) ,
339
+ USART5 : ( usart5, usart5tx , usart5rx , usart5en, apb1enr) ,
340
+ USART6 : ( usart6, usart6tx , usart6rx , usart6en, apb2enr) ,
306
341
}
307
342
308
343
// It's s needed for the impls, but rustc doesn't recognize that
@@ -339,6 +374,22 @@ where
339
374
}
340
375
}
341
376
377
+ impl < USART , TXPIN , RXPIN > embedded_hal:: serial:: Read < u8 > for Serial < USART , TXPIN , RXPIN >
378
+ where
379
+ USART : Deref < Target = SerialRegisterBlock > ,
380
+ RXPIN : RxPin < USART > ,
381
+ {
382
+ type Error = Error ;
383
+
384
+ /// Tries to read a byte from the uart
385
+ fn read ( & mut self ) -> nb:: Result < u8 , Error > {
386
+ Rx {
387
+ usart : & self . usart as * const _ ,
388
+ }
389
+ . read ( )
390
+ }
391
+ }
392
+
342
393
#[ cfg( feature = "device-selected" ) ]
343
394
impl < USART > embedded_hal:: serial:: Write < u8 > for Tx < USART >
344
395
where
@@ -375,14 +426,43 @@ where
375
426
}
376
427
}
377
428
429
+ impl < USART , TXPIN , RXPIN > embedded_hal:: serial:: Write < u8 > for Serial < USART , TXPIN , RXPIN >
430
+ where
431
+ USART : Deref < Target = SerialRegisterBlock > ,
432
+ TXPIN : TxPin < USART > ,
433
+ {
434
+ type Error = void:: Void ;
435
+
436
+ /// Ensures that none of the previously written words are still buffered
437
+ fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
438
+ Tx {
439
+ usart : & self . usart as * const _ ,
440
+ }
441
+ . flush ( )
442
+ }
443
+
444
+ /// Tries to write a byte to the uart
445
+ /// Fails if the transmit buffer is full
446
+ fn write ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Self :: Error > {
447
+ Tx {
448
+ usart : & self . usart as * const _ ,
449
+ }
450
+ . write ( byte)
451
+ }
452
+ }
453
+
378
454
#[ cfg( feature = "device-selected" ) ]
379
455
impl < USART , TXPIN , RXPIN > Serial < USART , TXPIN , RXPIN >
380
456
where
381
457
USART : Deref < Target = SerialRegisterBlock > ,
382
458
{
383
459
/// Splits the UART Peripheral in a Tx and an Rx part
384
460
/// This is required for sending/receiving
385
- pub fn split ( self ) -> ( Tx < USART > , Rx < USART > ) {
461
+ pub fn split ( self ) -> ( Tx < USART > , Rx < USART > )
462
+ where
463
+ TXPIN : TxPin < USART > ,
464
+ RXPIN : RxPin < USART > ,
465
+ {
386
466
(
387
467
Tx {
388
468
usart : & self . usart as * const _ ,
@@ -392,6 +472,7 @@ where
392
472
} ,
393
473
)
394
474
}
475
+
395
476
pub fn release ( self ) -> ( USART , ( TXPIN , RXPIN ) ) {
396
477
( self . usart , self . pins )
397
478
}
0 commit comments