Skip to content

Commit

Permalink
Merge pull request #250 from Dirbaio/pin-conf
Browse files Browse the repository at this point in the history
gpio: add conf() utility function.
  • Loading branch information
jonas-schievink authored Oct 19, 2020
2 parents 10367f1 + 451b9ff commit 3bc8f2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
17 changes: 10 additions & 7 deletions nrf-hal-common/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,13 @@ impl<MODE> Pin<MODE> {
unsafe { &*ptr }
}

pub(crate) fn conf(&self) -> &gpio::PIN_CNF {
&self.block().pin_cnf[self.pin() as usize]
}

/// Convert the pin to be a floating input
pub fn into_floating_input(self) -> Pin<Input<Floating>> {
self.block().pin_cnf[self.pin() as usize].write(|w| {
self.conf().write(|w| {
w.dir().input();
w.input().connect();
w.pull().disabled();
Expand All @@ -150,7 +154,7 @@ impl<MODE> Pin<MODE> {
}
}
pub fn into_pullup_input(self) -> Pin<Input<PullUp>> {
self.block().pin_cnf[self.pin() as usize].write(|w| {
self.conf().write(|w| {
w.dir().input();
w.input().connect();
w.pull().pullup();
Expand All @@ -165,7 +169,7 @@ impl<MODE> Pin<MODE> {
}
}
pub fn into_pulldown_input(self) -> Pin<Input<PullDown>> {
self.block().pin_cnf[self.pin() as usize].write(|w| {
self.conf().write(|w| {
w.dir().input();
w.input().connect();
w.pull().pulldown();
Expand All @@ -192,7 +196,7 @@ impl<MODE> Pin<MODE> {
Level::High => pin.set_high().unwrap(),
}

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

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

Pin {
_mode: PhantomData,
Expand Down
7 changes: 1 addition & 6 deletions nrf-hal-common/src/twim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,7 @@ where
// safe, as we own the pins now and have exclusive access to their
// registers.
for &pin in &[&pins.scl, &pins.sda] {
let port_ptr = match pin.port() {
Port::Port0 => P0::ptr(),
#[cfg(any(feature = "52833", feature = "52840"))]
Port::Port1 => P1::ptr(),
};
unsafe { &*port_ptr }.pin_cnf[pin.pin() as usize].write(|w| {
pin.conf().write(|w| {
w.dir()
.input()
.input()
Expand Down

0 comments on commit 3bc8f2b

Please sign in to comment.