From 1698b0b8eb6058569aa6ff1339492f052fbaff1d Mon Sep 17 00:00:00 2001 From: Justin Beaurivage Date: Wed, 31 Jan 2024 15:42:25 -0500 Subject: [PATCH] Implementations for `gpio` module --- hal/src/gpio/dynpin.rs | 62 +++++++++++++++++++++--- hal/src/gpio/pin.rs | 107 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 152 insertions(+), 17 deletions(-) diff --git a/hal/src/gpio/dynpin.rs b/hal/src/gpio/dynpin.rs index 808f4cc69cc1..4bf4dbea4fbc 100644 --- a/hal/src/gpio/dynpin.rs +++ b/hal/src/gpio/dynpin.rs @@ -61,12 +61,9 @@ use core::convert::TryFrom; +use crate::ehal::digital::{ErrorKind, ErrorType, InputPin, OutputPin, StatefulOutputPin}; use paste::paste; -use crate::ehal_02::digital::v2::OutputPin; -#[cfg(feature = "unproven")] -use crate::ehal_02::digital::v2::{InputPin, StatefulOutputPin, ToggleableOutputPin}; - use super::pin::*; use super::reg::RegisterInterface; @@ -256,12 +253,19 @@ impl DynRegisters { /// /// [`DynPin`]s are not tracked and verified at compile-time, so run-time /// operations are fallible. This `enum` represents the corresponding errors. +#[derive(Debug)] #[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum Error { /// The pin did not have the correct ID or mode for the requested operation InvalidPinType, } +impl crate::ehal::digital::Error for Error { + fn kind(&self) -> crate::ehal::digital::ErrorKind { + ErrorKind::Other + } +} + //============================================================================== // DynPin //============================================================================== @@ -507,11 +511,13 @@ where } //============================================================================== -// Embedded HAL traits +// Embedded HAL v1 traits //============================================================================== +impl ErrorType for DynPin { + type Error = Error; +} impl OutputPin for DynPin { - type Error = Error; #[inline] fn set_high(&mut self) -> Result<(), Self::Error> { self._set_high() @@ -524,6 +530,46 @@ impl OutputPin for DynPin { #[cfg(feature = "unproven")] impl InputPin for DynPin { + #[inline] + fn is_high(&mut self) -> Result { + self._is_high() + } + #[inline] + fn is_low(&mut self) -> Result { + self._is_low() + } +} + +#[cfg(feature = "unproven")] +impl StatefulOutputPin for DynPin { + #[inline] + fn is_set_high(&mut self) -> Result { + self._is_set_high() + } + #[inline] + fn is_set_low(&mut self) -> Result { + self._is_set_low() + } +} + +//============================================================================== +// Embedded HAL v0.2 traits +//============================================================================== + +impl crate::ehal_02::digital::v2::OutputPin for DynPin { + type Error = Error; + #[inline] + fn set_high(&mut self) -> Result<(), Self::Error> { + self._set_high() + } + #[inline] + fn set_low(&mut self) -> Result<(), Self::Error> { + self._set_low() + } +} + +#[cfg(feature = "unproven")] +impl crate::ehal_02::digital::v2::InputPin for DynPin { type Error = Error; #[inline] fn is_high(&self) -> Result { @@ -536,7 +582,7 @@ impl InputPin for DynPin { } #[cfg(feature = "unproven")] -impl ToggleableOutputPin for DynPin { +impl crate::ehal_02::digital::v2::ToggleableOutputPin for DynPin { type Error = Error; #[inline] fn toggle(&mut self) -> Result<(), Self::Error> { @@ -545,7 +591,7 @@ impl ToggleableOutputPin for DynPin { } #[cfg(feature = "unproven")] -impl StatefulOutputPin for DynPin { +impl crate::ehal_02::digital::v2::StatefulOutputPin for DynPin { #[inline] fn is_set_high(&self) -> Result { self._is_set_high() diff --git a/hal/src/gpio/pin.rs b/hal/src/gpio/pin.rs index be8c912d680d..f15afab1a859 100644 --- a/hal/src/gpio/pin.rs +++ b/hal/src/gpio/pin.rs @@ -100,9 +100,7 @@ use core::convert::Infallible; use core::marker::PhantomData; use core::mem::transmute; -use crate::ehal_02::digital::v2::OutputPin; -#[cfg(feature = "unproven")] -use crate::ehal_02::digital::v2::{InputPin, StatefulOutputPin, ToggleableOutputPin}; +use crate::ehal::digital::{ErrorType, InputPin, OutputPin, StatefulOutputPin}; use paste::paste; use crate::pac::PORT; @@ -879,10 +877,101 @@ pub trait SomePin: AnyPin {} impl SomePin for P {} //============================================================================== -// Embedded HAL traits +// Embedded HAL v1 traits //============================================================================== +impl ErrorType for Pin +where + I: PinId, + M: PinMode, +{ + type Error = Infallible; +} + impl OutputPin for Pin> +where + I: PinId, + C: OutputConfig, +{ + #[inline] + fn set_low(&mut self) -> Result<(), Self::Error> { + self._set_low(); + Ok(()) + } + + #[inline] + fn set_high(&mut self) -> Result<(), Self::Error> { + self._set_high(); + Ok(()) + } +} + +impl InputPin for Pin +where + I: PinId, +{ + #[inline] + fn is_high(&mut self) -> Result { + Ok(self._is_high()) + } + #[inline] + fn is_low(&mut self) -> Result { + Ok(self._is_low()) + } +} + +#[cfg(feature = "unproven")] +impl InputPin for Pin> +where + I: PinId, + C: InputConfig, +{ + #[inline] + fn is_high(&mut self) -> Result { + Ok(self._is_high()) + } + #[inline] + fn is_low(&mut self) -> Result { + Ok(self._is_low()) + } +} + +#[cfg(feature = "unproven")] +impl InputPin for Pin> +where + I: PinId, + C: InterruptConfig, +{ + #[inline] + fn is_high(&mut self) -> Result { + Ok(self._is_high()) + } + #[inline] + fn is_low(&mut self) -> Result { + Ok(self._is_low()) + } +} + +impl StatefulOutputPin for Pin> +where + I: PinId, + C: OutputConfig, +{ + #[inline] + fn is_set_high(&mut self) -> Result { + Ok(self._is_set_high()) + } + #[inline] + fn is_set_low(&mut self) -> Result { + Ok(self._is_set_low()) + } +} + +//============================================================================== +// Embedded HAL v0.2 traits +//============================================================================== + +impl crate::ehal_02::digital::v2::OutputPin for Pin> where I: PinId, C: OutputConfig, @@ -901,7 +990,7 @@ where } #[cfg(feature = "unproven")] -impl InputPin for Pin +impl crate::ehal_02::digital::v2::InputPin for Pin where I: PinId, { @@ -917,7 +1006,7 @@ where } #[cfg(feature = "unproven")] -impl InputPin for Pin> +impl crate::ehal_02::digital::v2::InputPin for Pin> where I: PinId, C: InputConfig, @@ -934,7 +1023,7 @@ where } #[cfg(feature = "unproven")] -impl InputPin for Pin> +impl crate::ehal_02::digital::v2::InputPin for Pin> where I: PinId, C: InterruptConfig, @@ -951,7 +1040,7 @@ where } #[cfg(feature = "unproven")] -impl ToggleableOutputPin for Pin> +impl crate::ehal_02::digital::v2::ToggleableOutputPin for Pin> where I: PinId, C: OutputConfig, @@ -965,7 +1054,7 @@ where } #[cfg(feature = "unproven")] -impl StatefulOutputPin for Pin> +impl crate::ehal_02::digital::v2::StatefulOutputPin for Pin> where I: PinId, C: OutputConfig,