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

Implement InputPin and OutputPin for Pin<I, DynFunction, P> #859

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
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
69 changes: 67 additions & 2 deletions rp2040-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,19 @@ 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 {
/// The DynPin is in a state incompatible with the requested function.
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 +1477,11 @@ 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 +1493,14 @@ 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 +1517,48 @@ mod eh1 {
}
}

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

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

impl<I, P> InputPin for Pin<I, DynFunction, P>
where
I: PinId,
P: PullType,
{
fn is_high(&mut self) -> Result<bool, Self::Error> {
match self.function() {
DynFunction::Sio(func::DynSioConfig::Input) => Ok(self._is_high()),
_ => Err(DynPinError::IncompatibleFunction),
}
}

fn is_low(&mut self) -> Result<bool, Self::Error> {
match self.function() {
DynFunction::Sio(func::DynSioConfig::Input) => Ok(self._is_low()),
_ => Err(DynPinError::IncompatibleFunction),
}
}
}

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