@@ -232,10 +232,15 @@ impl From<Bps> for Config {
232
232
233
233
use crate :: pac:: usart1 as uart_base;
234
234
235
- /// Serial abstraction
236
- pub struct Serial < USART , PINS > {
235
+ /// Stores data for release
236
+ pub struct ReleaseToken < USART , PINS > {
237
237
usart : USART ,
238
238
pins : PINS ,
239
+ }
240
+
241
+ /// Serial abstraction
242
+ pub struct Serial < USART , PINS > {
243
+ pub token : ReleaseToken < USART , PINS > ,
239
244
pub tx : Tx < USART > ,
240
245
pub rx : Rx < USART > ,
241
246
}
@@ -318,8 +323,7 @@ where
318
323
. modify ( |_r, w| w. ue ( ) . set_bit ( ) . re ( ) . set_bit ( ) . te ( ) . set_bit ( ) ) ;
319
324
320
325
Serial {
321
- usart,
322
- pins,
326
+ token : ReleaseToken { usart, pins } ,
323
327
tx : Tx :: new ( ) ,
324
328
rx : Rx :: new ( ) ,
325
329
}
@@ -442,12 +446,36 @@ where
442
446
}
443
447
444
448
/// Returns ownership of the borrowed register handles
449
+ ///
450
+ /// # Examples
451
+ ///
452
+ /// Basic usage:
453
+ ///
454
+ /// ```
455
+ /// let mut serial = Serial::new(usart, (tx_pin, rx_pin), &mut afio.mapr, 9600.bps(), &clocks);
456
+ ///
457
+ /// // You can split the `Serial`
458
+ /// let Serial { tx, rx, token } = serial;
459
+ ///
460
+ /// // You can reunite the `Serial` back
461
+ /// let serial = Serial { tx, rx, token };
462
+ ///
463
+ /// // Release `Serial`
464
+ /// let (usart, (tx_pin, rx_pin)) = serial.release();
465
+ /// ```
445
466
pub fn release ( self ) -> ( USART , PINS ) {
446
- ( self . usart , self . pins )
467
+ ( self . token . usart , self . token . pins )
447
468
}
448
469
449
470
/// Separates the serial struct into separate channel objects for sending (Tx) and
450
471
/// receiving (Rx)
472
+ ///
473
+ /// If in the future it will be necessary to free up resources,
474
+ /// then you need to use a different method of separation:
475
+ ///
476
+ /// ```
477
+ /// let Serial { tx, rx, token } = serial;
478
+ /// ```
451
479
pub fn split ( self ) -> ( Tx < USART > , Rx < USART > ) {
452
480
( self . tx , self . rx )
453
481
}
0 commit comments