Skip to content

Commit

Permalink
Implement OutputPin for Pin<I, DynFunction, P>
Browse files Browse the repository at this point in the history
Operations on this OutputPin are fallible: They return
Err(DynPinError::IncompatibleFunction) if the pin is not configured as
an output.
  • Loading branch information
jannic committed Oct 5, 2024
1 parent eb14a75 commit 433980f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions rp2040-hal/src/gpio/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ impl SioConfig for SioOutput {
//==============================================================================

/// Error type for invalid function conversion.
#[derive(Debug)]
pub struct InvalidFunction;

/// Marker of valid pin -> function combination.
Expand Down
48 changes: 46 additions & 2 deletions rp2040-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,18 @@ pub struct AsInputPin<'a, I: PinId, F: func::Function, P: PullType>(&'a Pin<I, F
/// GPIO error type.
pub type Error = core::convert::Infallible;

/// GPIO error type for pins with dynamic function
#[derive(Debug)]
pub enum DynPinError {
IncompatibleFunction,
}

impl embedded_hal::digital::Error for DynPinError {
fn kind(&self) -> embedded_hal::digital::ErrorKind {
embedded_hal::digital::ErrorKind::Other
}
}

impl<I, P> embedded_hal_0_2::digital::v2::OutputPin for Pin<I, FunctionSio<SioOutput>, P>
where
I: PinId,
Expand Down Expand Up @@ -1464,9 +1476,10 @@ impl<T: AnyPin> embedded_hal_0_2::digital::v2::OutputPin for InOutPin<T> {
mod eh1 {
use embedded_hal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin};

use crate::gpio::DynPinError;

use super::{
func, AnyPin, AsInputPin, Error, FunctionSio, InOutPin, OutputEnableOverride, Pin, PinId,
PullType, SioConfig, SioInput, SioOutput,
func, AnyPin, AsInputPin, DynFunction, Error, FunctionSio, InOutPin, OutputEnableOverride, Pin, PinId, PullType, SioConfig, SioInput, SioOutput
};

impl<I, P, S> ErrorType for Pin<I, FunctionSio<S>, P>
Expand All @@ -1478,6 +1491,15 @@ mod eh1 {
type Error = Error;
}

impl<I, P> ErrorType for Pin<I, DynFunction, P>
where
I: PinId,
P: PullType,
{
type Error = DynPinError;
}


impl<I, P> OutputPin for Pin<I, FunctionSio<SioOutput>, P>
where
I: PinId,
Expand All @@ -1494,6 +1516,28 @@ mod eh1 {
}
}

impl<I, P> OutputPin for Pin<I, DynFunction, P>
where
I: PinId,
P: PullType,
{
fn set_low(&mut self) -> Result<(), DynPinError> {
match self.function() {
DynFunction::Sio(func::DynSioConfig::Output) => self._set_low(),
_ => return Err(DynPinError::IncompatibleFunction),
}
Ok(())
}

fn set_high(&mut self) -> Result<(), DynPinError> {
match self.function() {
DynFunction::Sio(func::DynSioConfig::Output) => self._set_high(),
_ => return Err(DynPinError::IncompatibleFunction),
}
Ok(())
}
}

impl<I, P> StatefulOutputPin for Pin<I, FunctionSio<SioOutput>, P>
where
I: PinId,
Expand Down

0 comments on commit 433980f

Please sign in to comment.