Skip to content

Commit f05d837

Browse files
Allow Serial release after splitting
1 parent b9ec88d commit f05d837

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919

2020
- Allow access to the `Tx` and `Rx` parts of the `Serial` without the need for splitting.
2121
- Allow `Serial` reconfiguration by references to the `Tx` and `Rx` parts.
22+
- Allow `Serial` release after splitting.
2223

2324
## [v0.9.0] - 2022-03-02
2425

src/serial.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,15 @@ impl From<Bps> for Config {
232232

233233
use crate::pac::usart1 as uart_base;
234234

235-
/// Serial abstraction
236-
pub struct Serial<USART, PINS> {
235+
/// Stores data for release
236+
pub struct ReleaseToken<USART, PINS> {
237237
usart: USART,
238238
pins: PINS,
239+
}
240+
241+
/// Serial abstraction
242+
pub struct Serial<USART, PINS> {
243+
pub token: ReleaseToken<USART, PINS>,
239244
pub tx: Tx<USART>,
240245
pub rx: Rx<USART>,
241246
}
@@ -318,8 +323,7 @@ where
318323
.modify(|_r, w| w.ue().set_bit().re().set_bit().te().set_bit());
319324

320325
Serial {
321-
usart,
322-
pins,
326+
token: ReleaseToken { usart, pins },
323327
tx: Tx::new(),
324328
rx: Rx::new(),
325329
}
@@ -442,12 +446,36 @@ where
442446
}
443447

444448
/// 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+
/// ```
445466
pub fn release(self) -> (USART, PINS) {
446-
(self.usart, self.pins)
467+
(self.token.usart, self.token.pins)
447468
}
448469

449470
/// Separates the serial struct into separate channel objects for sending (Tx) and
450471
/// 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+
/// ```
451479
pub fn split(self) -> (Tx<USART>, Rx<USART>) {
452480
(self.tx, self.rx)
453481
}

0 commit comments

Comments
 (0)