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

Implemented InputPin trait for output pins #105

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,32 @@ macro_rules! gpio {
}
}


#[cfg(feature = "unproven")]
impl<MODE> InputPin for PXx<Output<MODE>> {
type Error = Infallible;

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

fn is_low(&self) -> Result<bool, Self::Error> {
// NOTE(unsafe) atomic read with no side effects
Ok(unsafe {
match &self.gpio {
$(
#[cfg(all(any(
$(feature = $device,)+
), not(any(
$(feature = $device_except,)*
))))]
Gpio::$GPIOX => (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0,
)+
}
})
}
}

#[cfg(feature = "unproven")]
impl <MODE> StatefulOutputPin for PXx<Output<MODE>> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Expand Down Expand Up @@ -397,6 +423,20 @@ macro_rules! gpio {
}
}

#[cfg(feature = "unproven")]
impl<MODE> InputPin for $PXx<Output<MODE>> {
teskje marked this conversation as resolved.
Show resolved Hide resolved
type Error = Infallible;

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

fn is_low(&self) -> Result<bool, Self::Error> {
// NOTE(unsafe) atomic read with no side effects
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
}
}

#[cfg(feature = "unproven")]
impl<MODE> InputPin for $PXx<Input<MODE>> {
type Error = Infallible;
Expand Down Expand Up @@ -673,6 +713,20 @@ macro_rules! gpio {
}
}

#[cfg(feature = "unproven")]
impl<MODE> InputPin for $PXi<Output<MODE>> {
type Error = Infallible;

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

fn is_low(&self) -> Result<bool, Self::Error> {
// NOTE(unsafe) atomic read with no side effects
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << $i) == 0 })
}
}

#[cfg(feature = "unproven")]
impl<MODE> InputPin for $PXi<Input<MODE>> {
type Error = Infallible;
Expand Down