@@ -208,6 +208,8 @@ impl From<Bps> for Config {
208
208
pub struct Serial < USART , PINS > {
209
209
usart : USART ,
210
210
pins : PINS ,
211
+ tx : Tx < USART > ,
212
+ rx : Rx < USART > ,
211
213
}
212
214
213
215
/// Serial receiver
@@ -372,7 +374,11 @@ macro_rules! hal {
372
374
. cr1
373
375
. modify( |_, w| w. ue( ) . set_bit( ) . re( ) . set_bit( ) . te( ) . set_bit( ) ) ;
374
376
375
- Serial { usart, pins }
377
+ Serial {
378
+ usart, pins,
379
+ tx: Tx { _usart: PhantomData } ,
380
+ rx: Rx { _usart: PhantomData } ,
381
+ }
376
382
}
377
383
378
384
/// Starts listening for an interrupt event
@@ -400,10 +406,28 @@ macro_rules! hal {
400
406
///
401
407
/// See [`Rx::check_for_error`].
402
408
pub fn check_for_error( ) -> Result <( ) , Error > {
403
- let mut rx: Rx <pac:: $USARTX> = Rx {
404
- _usart: PhantomData ,
405
- } ;
406
- rx. check_for_error( )
409
+ // NOTE(unsafe): Only used for atomic access.
410
+ let isr = unsafe { ( * pac:: $USARTX:: ptr( ) ) . isr. read( ) } ;
411
+ let icr = unsafe { & ( * pac:: $USARTX:: ptr( ) ) . icr } ;
412
+
413
+ if isr. pe( ) . bit_is_set( ) {
414
+ icr. write( |w| w. pecf( ) . clear( ) ) ;
415
+ return Err ( Error :: Parity ) ;
416
+ }
417
+ if isr. fe( ) . bit_is_set( ) {
418
+ icr. write( |w| w. fecf( ) . clear( ) ) ;
419
+ return Err ( Error :: Framing ) ;
420
+ }
421
+ if isr. nf( ) . bit_is_set( ) {
422
+ icr. write( |w| w. ncf( ) . clear( ) ) ;
423
+ return Err ( Error :: Noise ) ;
424
+ }
425
+ if isr. ore( ) . bit_is_set( ) {
426
+ icr. write( |w| w. orecf( ) . clear( ) ) ;
427
+ return Err ( Error :: Overrun ) ;
428
+ }
429
+
430
+ Ok ( ( ) )
407
431
}
408
432
409
433
/// Stops listening for an interrupt event
@@ -430,12 +454,8 @@ macro_rules! hal {
430
454
/// Splits the `Serial` abstraction into a transmitter and a receiver half
431
455
pub fn split( self ) -> ( Tx <pac:: $USARTX>, Rx <pac:: $USARTX>) {
432
456
(
433
- Tx {
434
- _usart: PhantomData ,
435
- } ,
436
- Rx {
437
- _usart: PhantomData ,
438
- } ,
457
+ self . tx,
458
+ self . rx,
439
459
)
440
460
}
441
461
@@ -449,10 +469,7 @@ macro_rules! hal {
449
469
type Error = Error ;
450
470
451
471
fn read( & mut self ) -> nb:: Result <u8 , Error > {
452
- let mut rx: Rx <pac:: $USARTX> = Rx {
453
- _usart: PhantomData ,
454
- } ;
455
- rx. read( )
472
+ self . rx. read( )
456
473
}
457
474
}
458
475
@@ -480,17 +497,11 @@ macro_rules! hal {
480
497
type Error = Error ;
481
498
482
499
fn flush( & mut self ) -> nb:: Result <( ) , Error > {
483
- let mut tx: Tx <pac:: $USARTX> = Tx {
484
- _usart: PhantomData ,
485
- } ;
486
- tx. flush( )
500
+ self . tx. flush( )
487
501
}
488
502
489
503
fn write( & mut self , byte: u8 ) -> nb:: Result <( ) , Error > {
490
- let mut tx: Tx <pac:: $USARTX> = Tx {
491
- _usart: PhantomData ,
492
- } ;
493
- tx. write( byte)
504
+ self . tx. write( byte)
494
505
}
495
506
}
496
507
@@ -579,28 +590,7 @@ macro_rules! hal {
579
590
/// `Ok(())`, it should be possible to proceed with the next
580
591
/// `read` call unimpeded.
581
592
pub fn check_for_error( & mut self ) -> Result <( ) , Error > {
582
- // NOTE(unsafe): Only used for atomic access.
583
- let isr = unsafe { ( * pac:: $USARTX:: ptr( ) ) . isr. read( ) } ;
584
- let icr = unsafe { & ( * pac:: $USARTX:: ptr( ) ) . icr } ;
585
-
586
- if isr. pe( ) . bit_is_set( ) {
587
- icr. write( |w| w. pecf( ) . clear( ) ) ;
588
- return Err ( Error :: Parity ) ;
589
- }
590
- if isr. fe( ) . bit_is_set( ) {
591
- icr. write( |w| w. fecf( ) . clear( ) ) ;
592
- return Err ( Error :: Framing ) ;
593
- }
594
- if isr. nf( ) . bit_is_set( ) {
595
- icr. write( |w| w. ncf( ) . clear( ) ) ;
596
- return Err ( Error :: Noise ) ;
597
- }
598
- if isr. ore( ) . bit_is_set( ) {
599
- icr. write( |w| w. orecf( ) . clear( ) ) ;
600
- return Err ( Error :: Overrun ) ;
601
- }
602
-
603
- Ok ( ( ) )
593
+ <Serial <pac:: $USARTX, ( ) >>:: check_for_error( )
604
594
}
605
595
606
596
/// Checks to see if the USART peripheral has detected an idle line and clears
0 commit comments