Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OpenDrainIO pin state (with InputPin capability) #401

Merged
merged 6 commits into from
Sep 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
100 changes: 100 additions & 0 deletions nrf-hal-common/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub struct PushPull;
/// Open drain output (type state).
pub struct OpenDrain;

/// Open drain input/output (type state).
pub struct OpenDrainIO;

/// Represents a digital input or output level.
#[derive(Debug, Eq, PartialEq)]
pub enum Level {
Expand Down Expand Up @@ -284,6 +287,40 @@ impl<MODE> Pin<MODE> {
pin
}

/// Convert the pin to be an open-drain input/output.
///
/// Similar to [`into_open_drain_output`](Self::into_open_drain_output), but can also be read from.
///
/// This method currently does not support configuring an internal pull-up or pull-down
/// resistor.
pub fn into_open_drain_input_output(
self,
config: OpenDrainConfig,
initial_output: Level,
) -> Pin<Output<OpenDrainIO>> {
let mut pin = Pin {
_mode: PhantomData,
pin_port: self.pin_port,
};

match initial_output {
Level::Low => pin.set_low().unwrap(),
Level::High => pin.set_high().unwrap(),
}

// This is safe, as we restrict our access to the dedicated register for this pin.
self.conf().write(|w| {
w.dir().output();
w.input().connect();
w.pull().disabled();
w.drive().variant(config.variant());
w.sense().disabled();
w
});

pin
}

/// Disconnects the pin.
///
/// In disconnected mode the pin cannot be used as input or output.
Expand Down Expand Up @@ -311,6 +348,18 @@ impl<MODE> InputPin for Pin<Input<MODE>> {
}
}

impl InputPin for Pin<Output<OpenDrainIO>> {
type Error = Void;

fn is_high(&self) -> Result<bool, Self::Error> {
self.is_low().map(|v| !v)
}

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.block().in_.read().bits() & (1 << self.pin()) == 0)
}
}

impl<MODE> OutputPin for Pin<Output<MODE>> {
type Error = Void;

Expand Down Expand Up @@ -406,6 +455,7 @@ macro_rules! gpio {
PullDown,
PullUp,
PushPull,
OpenDrainIO,

PhantomData,
$PX
Expand Down Expand Up @@ -555,6 +605,44 @@ macro_rules! gpio {
pin
}

/// Convert the pin to be an open-drain input/output
///
/// Similar to [`into_open_drain_output`](Self::into_open_drain_output), but can also be read from.
///
/// This method currently does not support configuring an
/// internal pull-up or pull-down resistor.
pub fn into_open_drain_input_output(self,
config: OpenDrainConfig,
initial_output: Level,
)
-> $PXi<Output<OpenDrainIO>>
{
let mut pin = $PXi {
_mode: PhantomData,
};

match initial_output {
Level::Low => pin.set_low().unwrap(),
Level::High => pin.set_high().unwrap(),
}

// This is safe, as we restrict our access to the
// dedicated register for this pin.
let pin_cnf = unsafe {
&(*$PX::ptr()).pin_cnf[$i]
};
pin_cnf.write(|w| {
w.dir().output();
w.input().connect();
w.pull().disabled();
w.drive().variant(config.variant());
w.sense().disabled();
w
});

pin
}

/// Disconnects the pin.
///
/// In disconnected mode the pin cannot be used as input or output.
Expand Down Expand Up @@ -586,6 +674,18 @@ macro_rules! gpio {
}
}

impl InputPin for $PXi<Output<OpenDrainIO>> {
type Error = Void;

fn is_high(&self) -> Result<bool, Self::Error> {
self.is_low().map(|v| !v)
}

fn is_low(&self) -> Result<bool, Self::Error> {
Ok(unsafe { ((*$PX::ptr()).in_.read().bits() & (1 << $i)) == 0 })
}
}

impl<MODE> From<$PXi<MODE>> for Pin<MODE> {
fn from(value: $PXi<MODE>) -> Self {
value.degrade()
Expand Down