Skip to content

Commit e932eea

Browse files
Merge pull request #261 from timokroeger/usart-free-pins
Also release owned `Pins` with `Usart::free()`
2 parents 56cbdac + a7c193d commit e932eea

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- Improve SAADC docs ([#218]).
2222
- Update Embed.toml of all examples to new defaults ([#229]).
2323
- Make `ConfigurablePpi` and subtrait of `Ppi` ([#244]).
24+
- Release owned `Pins` with `Usart::free()` ([#261])
2425

2526
### Fixes
2627

nrf-hal-common/src/gpio.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,22 @@ pub enum Port {
4343
Port1,
4444
}
4545

46+
#[cfg(any(feature = "52833", feature = "52840"))]
4647
impl Port {
47-
#[cfg(any(feature = "52833", feature = "52840"))]
4848
pub(crate) fn bit(&self) -> bool {
4949
match self {
5050
Port::Port0 => false,
5151
Port::Port1 => true,
5252
}
5353
}
54+
55+
pub(crate) fn from_bit(bit: bool) -> Port {
56+
if bit {
57+
Port::Port1
58+
} else {
59+
Port::Port0
60+
}
61+
}
5462
}
5563

5664
// ===============================================================
@@ -81,7 +89,7 @@ use crate::hal::digital::v2::{InputPin, OutputPin, StatefulOutputPin};
8189
use void::Void;
8290

8391
impl<MODE> Pin<MODE> {
84-
fn new(port: Port, pin: u8) -> Self {
92+
pub(crate) fn new(port: Port, pin: u8) -> Self {
8593
let port_bits = match port {
8694
Port::Port0 => 0x00,
8795
#[cfg(any(feature = "52833", feature = "52840"))]

nrf-hal-common/src/uarte.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::pac::{uarte0_ns as uarte0, UARTE0_NS as UARTE0, UARTE1_NS as UARTE1};
1919
#[cfg(not(feature = "9160"))]
2020
use crate::pac::{uarte0, UARTE0};
2121

22-
use crate::gpio::{Floating, Input, Output, Pin, PushPull};
22+
use crate::gpio::{Floating, Input, Output, Pin, Port, PushPull};
2323
use crate::prelude::*;
2424
use crate::slice_in_ram_or;
2525
use crate::target_constants::EASY_DMA_SIZE;
@@ -329,8 +329,48 @@ where
329329
}
330330

331331
/// Return the raw interface to the underlying UARTE peripheral.
332-
pub fn free(self) -> T {
333-
self.0
332+
pub fn free(self) -> (T, Pins) {
333+
let rxd = self.0.psel.rxd.read();
334+
let txd = self.0.psel.txd.read();
335+
let cts = self.0.psel.cts.read();
336+
let rts = self.0.psel.rts.read();
337+
(
338+
self.0,
339+
Pins {
340+
#[cfg(any(feature = "52833", feature = "52840"))]
341+
rxd: Pin::new(Port::from_bit(rxd.port().bit()), rxd.pin().bits()),
342+
#[cfg(not(any(feature = "52833", feature = "52840")))]
343+
rxd: Pin::new(Port::Port0, rxd.pin().bits()),
344+
#[cfg(any(feature = "52833", feature = "52840"))]
345+
txd: Pin::new(Port::from_bit(txd.port().bit()), txd.pin().bits()),
346+
#[cfg(not(any(feature = "52833", feature = "52840")))]
347+
txd: Pin::new(Port::Port0, txd.pin().bits()),
348+
cts: if cts.connect().bit_is_set() {
349+
#[cfg(any(feature = "52833", feature = "52840"))]
350+
{
351+
Some(Pin::new(Port::from_bit(cts.port().bit()), cts.pin().bits()))
352+
}
353+
#[cfg(not(any(feature = "52833", feature = "52840")))]
354+
{
355+
Some(Pin::new(Port::Port0, cts.pin().bits()))
356+
}
357+
} else {
358+
None
359+
},
360+
rts: if rts.connect().bit_is_set() {
361+
#[cfg(any(feature = "52833", feature = "52840"))]
362+
{
363+
Some(Pin::new(Port::from_bit(rts.port().bit()), rts.pin().bits()))
364+
}
365+
#[cfg(not(any(feature = "52833", feature = "52840")))]
366+
{
367+
Some(Pin::new(Port::Port0, rts.pin().bits()))
368+
}
369+
} else {
370+
None
371+
},
372+
},
373+
)
334374
}
335375
}
336376

0 commit comments

Comments
 (0)