Skip to content

Commit 5cc7f79

Browse files
committed
Unified how pins are return in free calls
1 parent bcd5c5d commit 5cc7f79

File tree

12 files changed

+165
-37
lines changed

12 files changed

+165
-37
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
## Unreleased
44

5-
(no changes)
5+
### Changed
6+
7+
- Unified how pins are are returned in `free` calls
68

79
## [0.14.1]
810

nrf-hal-common/src/pwm.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -748,8 +748,42 @@ where
748748
}
749749

750750
/// Consumes `self` and returns back the raw peripheral.
751-
pub fn free(self) -> T {
752-
self.pwm
751+
pub fn free(
752+
self,
753+
) -> (
754+
T,
755+
Option<Pin<Output<PushPull>>>,
756+
Option<Pin<Output<PushPull>>>,
757+
Option<Pin<Output<PushPull>>>,
758+
Option<Pin<Output<PushPull>>>,
759+
) {
760+
let ch0 = self.pwm.psel.out[0].read();
761+
let ch1 = self.pwm.psel.out[1].read();
762+
let ch2 = self.pwm.psel.out[2].read();
763+
let ch3 = self.pwm.psel.out[3].read();
764+
(
765+
self.pwm,
766+
if ch0.connect().bit_is_set() {
767+
Some(unsafe { Pin::from_psel_bits(ch0.bits()) })
768+
} else {
769+
None
770+
},
771+
if ch1.connect().bit_is_set() {
772+
Some(unsafe { Pin::from_psel_bits(ch1.bits()) })
773+
} else {
774+
None
775+
},
776+
if ch2.connect().bit_is_set() {
777+
Some(unsafe { Pin::from_psel_bits(ch2.bits()) })
778+
} else {
779+
None
780+
},
781+
if ch3.connect().bit_is_set() {
782+
Some(unsafe { Pin::from_psel_bits(ch3.bits()) })
783+
} else {
784+
None
785+
},
786+
)
753787
}
754788
}
755789

nrf-hal-common/src/qdec.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ use {
1111
/// A safe wrapper around the `QDEC` peripheral with associated pins.
1212
pub struct Qdec {
1313
qdec: QDEC,
14-
pin_a: Pin<Input<PullUp>>,
15-
pin_b: Pin<Input<PullUp>>,
16-
pin_led: Option<Pin<Input<PullUp>>>,
1714
}
1815

1916
impl Qdec {
@@ -55,12 +52,7 @@ impl Qdec {
5552
SamplePeriod::_131ms => qdec.sampleper.write(|w| w.sampleper()._131ms()),
5653
}
5754

58-
Self {
59-
qdec,
60-
pin_a,
61-
pin_b,
62-
pin_led,
63-
}
55+
Self { qdec }
6456
}
6557

6658
/// Enables/disables input debounce filters.
@@ -155,7 +147,18 @@ impl Qdec {
155147
Pin<Input<PullUp>>,
156148
Option<Pin<Input<PullUp>>>,
157149
) {
158-
(self.qdec, self.pin_a, self.pin_b, self.pin_led)
150+
let pin_a = unsafe { Pin::from_psel_bits(self.qdec.psel.a.read().bits()) };
151+
let pin_b = unsafe { Pin::from_psel_bits(self.qdec.psel.b.read().bits()) };
152+
let pin_led = {
153+
let led = self.qdec.psel.led.read();
154+
if led.connect().bit_is_set() {
155+
Some(unsafe { Pin::from_psel_bits(led.bits()) })
156+
} else {
157+
None
158+
}
159+
};
160+
161+
(self.qdec, pin_a, pin_b, pin_led)
159162
}
160163
}
161164

nrf-hal-common/src/spi.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,23 @@ where
9797
.write(|w| unsafe { w.bits(pins.sck.pin().into()) });
9898

9999
// Optional pins.
100-
if let Some(ref pin) = pins.mosi {
101-
spi.pselmosi.write(|w| unsafe { w.bits(pin.pin().into()) });
102-
}
103-
if let Some(ref pin) = pins.miso {
104-
spi.pselmiso.write(|w| unsafe { w.bits(pin.pin().into()) });
105-
}
100+
spi.pselmosi.write(|w| unsafe {
101+
if let Some(ref pin) = pins.mosi {
102+
w.bits(pin.pin().into())
103+
} else {
104+
// Disconnect
105+
w.bits(0xFFFFFFFF)
106+
}
107+
});
108+
109+
spi.pselmiso.write(|w| unsafe {
110+
if let Some(ref pin) = pins.miso {
111+
w.bits(pin.pin().into())
112+
} else {
113+
// Disconnect
114+
w.bits(0xFFFFFFFF)
115+
}
116+
});
106117
}
107118

108119
#[cfg(not(feature = "51"))]

nrf-hal-common/src/spim.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,26 @@ where
358358
}
359359

360360
/// Return the raw interface to the underlying SPIM peripheral.
361-
pub fn free(self) -> T {
362-
self.0
361+
pub fn free(self) -> (T, Pins) {
362+
let sck = self.0.psel.sck.read();
363+
let mosi = self.0.psel.mosi.read();
364+
let miso = self.0.psel.miso.read();
365+
(
366+
self.0,
367+
Pins {
368+
sck: unsafe { Pin::from_psel_bits(sck.bits()) },
369+
mosi: if mosi.connect().bit_is_set() {
370+
Some(unsafe { Pin::from_psel_bits(mosi.bits()) })
371+
} else {
372+
None
373+
},
374+
miso: if miso.connect().bit_is_set() {
375+
Some(unsafe { Pin::from_psel_bits(miso.bits()) })
376+
} else {
377+
None
378+
},
379+
},
380+
)
363381
}
364382
}
365383

nrf-hal-common/src/spis.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ use embedded_dma::*;
3737
/// Interface to a SPIS instance.
3838
pub struct Spis<T: Instance> {
3939
spis: T,
40-
pins: Pins,
4140
}
4241

4342
impl<T> Spis<T>
@@ -75,7 +74,7 @@ where
7574
spis.config
7675
.modify(|_r, w| w.cpha().bit(Phase::Trailing.into()));
7776
spis.enable.write(|w| w.enable().enabled());
78-
Self { spis, pins }
77+
Self { spis }
7978
}
8079

8180
/// Sets the ´default´ character (character clocked out in case of an ignored transaction).
@@ -406,7 +405,27 @@ where
406405

407406
/// Returns the raw interface to the underlying SPIS peripheral.
408407
pub fn free(self) -> (T, Pins) {
409-
(self.spis, self.pins)
408+
let sck = self.spis.psel.sck.read();
409+
let cs = self.spis.psel.csn.read();
410+
let copi = self.spis.psel.mosi.read();
411+
let cipo = self.spis.psel.miso.read();
412+
(
413+
self.spis,
414+
Pins {
415+
sck: unsafe { Pin::from_psel_bits(sck.bits()) },
416+
cs: unsafe { Pin::from_psel_bits(cs.bits()) },
417+
copi: if copi.connect().bit_is_set() {
418+
Some(unsafe { Pin::from_psel_bits(copi.bits()) })
419+
} else {
420+
None
421+
},
422+
cipo: if cipo.connect().bit_is_set() {
423+
Some(unsafe { Pin::from_psel_bits(cipo.bits()) })
424+
} else {
425+
None
426+
},
427+
},
428+
)
410429
}
411430
}
412431

nrf-hal-common/src/twi.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,16 @@ where
235235
}
236236

237237
/// Return the raw interface to the underlying TWI peripheral.
238-
pub fn free(self) -> T {
239-
self.0
238+
pub fn free(self) -> (T, Pins) {
239+
let scl = self.0.pselscl.read();
240+
let sda = self.0.pselsda.read();
241+
(
242+
self.0,
243+
Pins {
244+
scl: unsafe { Pin::from_psel_bits(scl.bits()) },
245+
sda: unsafe { Pin::from_psel_bits(sda.bits()) },
246+
},
247+
)
240248
}
241249
}
242250

nrf-hal-common/src/twim.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,8 +378,16 @@ where
378378
}
379379

380380
/// Return the raw interface to the underlying TWIM peripheral.
381-
pub fn free(self) -> T {
382-
self.0
381+
pub fn free(self) -> (T, Pins) {
382+
let scl = self.0.psel.scl.read();
383+
let sda = self.0.psel.sda.read();
384+
(
385+
self.0,
386+
Pins {
387+
scl: unsafe { Pin::from_psel_bits(scl.bits()) },
388+
sda: unsafe { Pin::from_psel_bits(sda.bits()) },
389+
},
390+
)
383391
}
384392
}
385393

nrf-hal-common/src/twis.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -450,8 +450,16 @@ where
450450
}
451451

452452
/// Return the raw interface to the underlying TWIS peripheral.
453-
pub fn free(self) -> T {
454-
self.0
453+
pub fn free(self) -> (T, Pins) {
454+
let scl = self.0.psel.scl.read();
455+
let sda = self.0.psel.sda.read();
456+
(
457+
self.0,
458+
Pins {
459+
scl: unsafe { Pin::from_psel_bits(scl.bits()) },
460+
sda: unsafe { Pin::from_psel_bits(sda.bits()) },
461+
},
462+
)
455463
}
456464
}
457465

nrf-hal-common/src/uart.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,28 @@ where
7070
}
7171

7272
/// Return the raw interface to the underlying UARTE peripheral.
73-
pub fn free(self) -> T {
74-
self.0
73+
pub fn free(self) -> (T, Pins) {
74+
let rxd = self.0.pselrxd.read();
75+
let txd = self.0.pseltxd.read();
76+
let cts = self.0.pselcts.read();
77+
let rts = self.0.pselrts.read();
78+
(
79+
self.0,
80+
Pins {
81+
rxd: unsafe { Pin::from_psel_bits(rxd.bits()) },
82+
txd: unsafe { Pin::from_psel_bits(txd.bits()) },
83+
cts: if cts.bits() != 0xFFFFFFFF {
84+
Some(unsafe { Pin::from_psel_bits(cts.bits()) })
85+
} else {
86+
None
87+
},
88+
rts: if rts.bits() != 0xFFFFFFFF {
89+
Some(unsafe { Pin::from_psel_bits(rts.bits()) })
90+
} else {
91+
None
92+
},
93+
},
94+
)
7595
}
7696
}
7797

0 commit comments

Comments
 (0)