Skip to content

Commit 451b9ff

Browse files
committed
gpio: add conf() utility function.
1 parent 10367f1 commit 451b9ff

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

nrf-hal-common/src/gpio.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,13 @@ impl<MODE> Pin<MODE> {
133133
unsafe { &*ptr }
134134
}
135135

136+
pub(crate) fn conf(&self) -> &gpio::PIN_CNF {
137+
&self.block().pin_cnf[self.pin() as usize]
138+
}
139+
136140
/// Convert the pin to be a floating input
137141
pub fn into_floating_input(self) -> Pin<Input<Floating>> {
138-
self.block().pin_cnf[self.pin() as usize].write(|w| {
142+
self.conf().write(|w| {
139143
w.dir().input();
140144
w.input().connect();
141145
w.pull().disabled();
@@ -150,7 +154,7 @@ impl<MODE> Pin<MODE> {
150154
}
151155
}
152156
pub fn into_pullup_input(self) -> Pin<Input<PullUp>> {
153-
self.block().pin_cnf[self.pin() as usize].write(|w| {
157+
self.conf().write(|w| {
154158
w.dir().input();
155159
w.input().connect();
156160
w.pull().pullup();
@@ -165,7 +169,7 @@ impl<MODE> Pin<MODE> {
165169
}
166170
}
167171
pub fn into_pulldown_input(self) -> Pin<Input<PullDown>> {
168-
self.block().pin_cnf[self.pin() as usize].write(|w| {
172+
self.conf().write(|w| {
169173
w.dir().input();
170174
w.input().connect();
171175
w.pull().pulldown();
@@ -192,7 +196,7 @@ impl<MODE> Pin<MODE> {
192196
Level::High => pin.set_high().unwrap(),
193197
}
194198

195-
self.block().pin_cnf[self.pin() as usize].write(|w| {
199+
self.conf().write(|w| {
196200
w.dir().output();
197201
w.input().connect(); // AJM - hack for SPI
198202
w.pull().disabled();
@@ -224,8 +228,7 @@ impl<MODE> Pin<MODE> {
224228
}
225229

226230
// This is safe, as we restrict our access to the dedicated register for this pin.
227-
let pin_cnf = &self.block().pin_cnf[self.pin() as usize];
228-
pin_cnf.write(|w| {
231+
self.conf().write(|w| {
229232
w.dir().output();
230233
w.input().disconnect();
231234
w.pull().disabled();
@@ -243,7 +246,7 @@ impl<MODE> Pin<MODE> {
243246
/// It is primarily useful to reduce power usage.
244247
pub fn into_disconnected(self) -> Pin<Disconnected> {
245248
// Reset value is disconnected.
246-
self.block().pin_cnf[self.pin() as usize].reset();
249+
self.conf().reset();
247250

248251
Pin {
249252
_mode: PhantomData,

nrf-hal-common/src/twim.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,7 @@ where
5252
// safe, as we own the pins now and have exclusive access to their
5353
// registers.
5454
for &pin in &[&pins.scl, &pins.sda] {
55-
let port_ptr = match pin.port() {
56-
Port::Port0 => P0::ptr(),
57-
#[cfg(any(feature = "52833", feature = "52840"))]
58-
Port::Port1 => P1::ptr(),
59-
};
60-
unsafe { &*port_ptr }.pin_cnf[pin.pin() as usize].write(|w| {
55+
pin.conf().write(|w| {
6156
w.dir()
6257
.input()
6358
.input()

0 commit comments

Comments
 (0)