From 433980fcdee016114e9932c0ad0bf4eac684ee15 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 5 Oct 2024 15:33:11 +0000 Subject: [PATCH 1/3] Implement OutputPin for Pin Operations on this OutputPin are fallible: They return Err(DynPinError::IncompatibleFunction) if the pin is not configured as an output. --- rp2040-hal/src/gpio/func.rs | 1 + rp2040-hal/src/gpio/mod.rs | 48 +++++++++++++++++++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/rp2040-hal/src/gpio/func.rs b/rp2040-hal/src/gpio/func.rs index f988bbb85..40d408b5e 100644 --- a/rp2040-hal/src/gpio/func.rs +++ b/rp2040-hal/src/gpio/func.rs @@ -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. diff --git a/rp2040-hal/src/gpio/mod.rs b/rp2040-hal/src/gpio/mod.rs index 71a346682..c8b9d2081 100644 --- a/rp2040-hal/src/gpio/mod.rs +++ b/rp2040-hal/src/gpio/mod.rs @@ -911,6 +911,18 @@ pub struct AsInputPin<'a, I: PinId, F: func::Function, P: PullType>(&'a Pin embedded_hal::digital::ErrorKind { + embedded_hal::digital::ErrorKind::Other + } +} + impl embedded_hal_0_2::digital::v2::OutputPin for Pin, P> where I: PinId, @@ -1464,9 +1476,10 @@ impl embedded_hal_0_2::digital::v2::OutputPin for InOutPin { 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 ErrorType for Pin, P> @@ -1478,6 +1491,15 @@ mod eh1 { type Error = Error; } + impl ErrorType for Pin + where + I: PinId, + P: PullType, + { + type Error = DynPinError; + } + + impl OutputPin for Pin, P> where I: PinId, @@ -1494,6 +1516,28 @@ mod eh1 { } } + impl OutputPin for Pin + 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 StatefulOutputPin for Pin, P> where I: PinId, From cbb617b93c8bc7b2cf32c62d485c130f5897ee51 Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 5 Oct 2024 16:31:04 +0000 Subject: [PATCH 2/3] Implement InputPin for Pin --- rp2040-hal/src/gpio/mod.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/rp2040-hal/src/gpio/mod.rs b/rp2040-hal/src/gpio/mod.rs index c8b9d2081..806c1e1dd 100644 --- a/rp2040-hal/src/gpio/mod.rs +++ b/rp2040-hal/src/gpio/mod.rs @@ -914,6 +914,7 @@ 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, } @@ -1521,7 +1522,7 @@ mod eh1 { I: PinId, P: PullType, { - fn set_low(&mut self) -> Result<(), DynPinError> { + fn set_low(&mut self) -> Result<(), Self::Error> { match self.function() { DynFunction::Sio(func::DynSioConfig::Output) => self._set_low(), _ => return Err(DynPinError::IncompatibleFunction), @@ -1529,7 +1530,7 @@ mod eh1 { Ok(()) } - fn set_high(&mut self) -> Result<(), DynPinError> { + fn set_high(&mut self) -> Result<(), Self::Error> { match self.function() { DynFunction::Sio(func::DynSioConfig::Output) => self._set_high(), _ => return Err(DynPinError::IncompatibleFunction), @@ -1538,6 +1539,26 @@ mod eh1 { } } + impl InputPin for Pin + where + I: PinId, + P: PullType, + { + fn is_high(&mut self) -> Result { + match self.function() { + DynFunction::Sio(func::DynSioConfig::Input) => Ok(self._is_high()), + _ => Err(DynPinError::IncompatibleFunction), + } + } + + fn is_low(&mut self) -> Result { + match self.function() { + DynFunction::Sio(func::DynSioConfig::Input) => Ok(self._is_low()), + _ => Err(DynPinError::IncompatibleFunction), + } + } + } + impl StatefulOutputPin for Pin, P> where I: PinId, From ebaba76861c5ec419985942e0a9b2e148a2d1aeb Mon Sep 17 00:00:00 2001 From: Jan Niehusmann Date: Sat, 5 Oct 2024 16:34:29 +0000 Subject: [PATCH 3/3] Run cargo fmt --- rp2040-hal/src/gpio/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rp2040-hal/src/gpio/mod.rs b/rp2040-hal/src/gpio/mod.rs index 806c1e1dd..b8e83080b 100644 --- a/rp2040-hal/src/gpio/mod.rs +++ b/rp2040-hal/src/gpio/mod.rs @@ -1480,7 +1480,8 @@ mod eh1 { use crate::gpio::DynPinError; use super::{ - func, AnyPin, AsInputPin, DynFunction, 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 ErrorType for Pin, P> @@ -1500,7 +1501,6 @@ mod eh1 { type Error = DynPinError; } - impl OutputPin for Pin, P> where I: PinId,