@@ -185,8 +185,12 @@ use crate::pac::usart1 as uart_base;
185
185
186
186
/// Serial abstraction
187
187
pub struct Serial < USART , PINS > {
188
- usart : USART ,
189
188
pins : PINS ,
189
+ inner : ErasedSerial < USART > ,
190
+ }
191
+
192
+ pub struct ErasedSerial < USART > {
193
+ usart : USART ,
190
194
tx : Tx < USART > ,
191
195
rx : Rx < USART > ,
192
196
}
@@ -224,7 +228,7 @@ impl<USART> Tx<USART> {
224
228
}
225
229
}
226
230
227
- impl < USART , PINS > Serial < USART , PINS >
231
+ impl < USART > ErasedSerial < USART >
228
232
where
229
233
USART : Instance ,
230
234
{
@@ -348,15 +352,81 @@ where
348
352
}
349
353
}
350
354
355
+ /// Separates the serial struct into separate channel objects for sending (Tx) and
356
+ /// receiving (Rx)
357
+ pub fn split ( self ) -> ( Tx < USART > , Rx < USART > ) {
358
+ ( self . tx , self . rx )
359
+ }
360
+ }
361
+
362
+ impl < USART , PINS > Serial < USART , PINS >
363
+ where
364
+ USART : Instance ,
365
+ {
351
366
/// Returns ownership of the borrowed register handles
352
367
pub fn release ( self ) -> ( USART , PINS ) {
353
- ( self . usart , self . pins )
368
+ ( self . inner . usart , self . pins )
369
+ }
370
+
371
+ /// Erase the pins used for the Serial from the type
372
+ pub fn erase ( self ) -> ErasedSerial < USART > {
373
+ self . inner
354
374
}
355
375
356
376
/// Separates the serial struct into separate channel objects for sending (Tx) and
357
377
/// receiving (Rx)
378
+ #[ inline( always) ]
358
379
pub fn split ( self ) -> ( Tx < USART > , Rx < USART > ) {
359
- ( self . tx , self . rx )
380
+ self . inner . split ( )
381
+ }
382
+
383
+ /// Reconfigure the USART instance.
384
+ ///
385
+ /// If a transmission is currently in progress, this returns
386
+ /// [`nb::Error::WouldBlock`].
387
+ #[ inline( always) ]
388
+ pub fn reconfigure ( & mut self , config : impl Into < Config > , clocks : Clocks ) -> nb:: Result < ( ) , ( ) > {
389
+ self . inner . reconfigure ( config, clocks)
390
+ }
391
+
392
+ /// Starts listening to the USART by enabling the _Received data
393
+ /// ready to be read (RXNE)_ interrupt and _Transmit data
394
+ /// register empty (TXE)_ interrupt
395
+ #[ inline( always) ]
396
+ pub fn listen ( & mut self , event : Event ) {
397
+ self . inner . listen ( event)
398
+ }
399
+
400
+ /// Stops listening to the USART by disabling the _Received data
401
+ /// ready to be read (RXNE)_ interrupt and _Transmit data
402
+ /// register empty (TXE)_ interrupt
403
+ #[ inline( always) ]
404
+ pub fn unlisten ( & mut self , event : Event ) {
405
+ self . inner . unlisten ( event)
406
+ }
407
+
408
+ /// Returns true if the line idle status is set
409
+ #[ inline( always) ]
410
+ pub fn is_idle ( & self ) -> bool {
411
+ self . inner . is_idle ( )
412
+ }
413
+
414
+ /// Returns true if the tx register is empty (and can accept data)
415
+ #[ inline( always) ]
416
+ pub fn is_tx_empty ( & self ) -> bool {
417
+ self . inner . is_tx_empty ( )
418
+ }
419
+
420
+ /// Returns true if the rx register is not empty (and can be read)
421
+ #[ inline( always) ]
422
+ pub fn is_rx_not_empty ( & self ) -> bool {
423
+ self . inner . is_rx_not_empty ( )
424
+ }
425
+
426
+ /// Clear idle line interrupt flag
427
+ #[ inline( always) ]
428
+ pub fn clear_idle_interrupt ( & self ) {
429
+ self . inner . clear_idle_interrupt ( )
360
430
}
361
431
}
362
432
@@ -406,12 +476,12 @@ macro_rules! hal {
406
476
PINS : Pins <$USARTX>,
407
477
{
408
478
#[ allow( unused_unsafe) ]
409
- Serial { usart , pins , tx: Tx :: new( ) , rx: Rx :: new( ) } . init( config. into( ) , clocks, || {
479
+ Serial { pins , inner : ErasedSerial { usart , tx: Tx :: new( ) , rx: Rx :: new( ) } . init( config. into( ) , clocks, || {
410
480
mapr. modify_mapr( |_, w| unsafe {
411
481
#[ allow( clippy:: redundant_closure_call) ]
412
482
w. $usartX_remap( ) . $bit( ( $closure) ( PINS :: REMAP ) )
413
483
} )
414
- } )
484
+ } ) }
415
485
}
416
486
}
417
487
@@ -557,7 +627,7 @@ where
557
627
}
558
628
}
559
629
560
- impl < USART , PINS > crate :: hal:: serial:: Read < u8 > for Serial < USART , PINS >
630
+ impl < USART > crate :: hal:: serial:: Read < u8 > for ErasedSerial < USART >
561
631
where
562
632
USART : Instance ,
563
633
{
@@ -568,7 +638,7 @@ where
568
638
}
569
639
}
570
640
571
- impl < USART , PINS > crate :: hal:: serial:: Write < u8 > for Serial < USART , PINS >
641
+ impl < USART > crate :: hal:: serial:: Write < u8 > for ErasedSerial < USART >
572
642
where
573
643
USART : Instance ,
574
644
{
@@ -583,6 +653,32 @@ where
583
653
}
584
654
}
585
655
656
+ impl < USART , PINS > crate :: hal:: serial:: Read < u8 > for Serial < USART , PINS >
657
+ where
658
+ USART : Instance ,
659
+ {
660
+ type Error = Error ;
661
+
662
+ fn read ( & mut self ) -> nb:: Result < u8 , Error > {
663
+ self . inner . rx . read ( )
664
+ }
665
+ }
666
+
667
+ impl < USART , PINS > crate :: hal:: serial:: Write < u8 > for Serial < USART , PINS >
668
+ where
669
+ USART : Instance ,
670
+ {
671
+ type Error = Infallible ;
672
+
673
+ fn flush ( & mut self ) -> nb:: Result < ( ) , Self :: Error > {
674
+ self . inner . tx . flush ( )
675
+ }
676
+
677
+ fn write ( & mut self , byte : u8 ) -> nb:: Result < ( ) , Self :: Error > {
678
+ self . inner . tx . write ( byte)
679
+ }
680
+ }
681
+
586
682
impl < USART > core:: fmt:: Write for Tx < USART >
587
683
where
588
684
Tx < USART > : embedded_hal:: serial:: Write < u8 > ,
0 commit comments