Skip to content

Commit

Permalink
Implementations for gpio module
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeaurivage committed Jan 31, 2024
1 parent 0f20c20 commit 1698b0b
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 17 deletions.
62 changes: 54 additions & 8 deletions hal/src/gpio/dynpin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
//==============================================================================
Expand Down Expand Up @@ -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()
Expand All @@ -524,6 +530,46 @@ impl OutputPin for DynPin {

#[cfg(feature = "unproven")]
impl InputPin for DynPin {
#[inline]
fn is_high(&mut self) -> Result<bool, Self::Error> {
self._is_high()
}
#[inline]
fn is_low(&mut self) -> Result<bool, Self::Error> {
self._is_low()
}
}

#[cfg(feature = "unproven")]
impl StatefulOutputPin for DynPin {
#[inline]
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
self._is_set_high()
}
#[inline]
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
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<bool, Self::Error> {
Expand All @@ -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> {
Expand All @@ -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<bool, Self::Error> {
self._is_set_high()
Expand Down
107 changes: 98 additions & 9 deletions hal/src/gpio/pin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -879,10 +877,101 @@ pub trait SomePin: AnyPin {}
impl<P: AnyPin> SomePin for P {}

//==============================================================================
// Embedded HAL traits
// Embedded HAL v1 traits
//==============================================================================

impl<I, M> ErrorType for Pin<I, M>
where
I: PinId,
M: PinMode,
{
type Error = Infallible;
}

impl<I, C> OutputPin for Pin<I, Output<C>>
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<I> InputPin for Pin<I, ReadableOutput>
where
I: PinId,
{
#[inline]
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_high())
}
#[inline]
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_low())
}
}

#[cfg(feature = "unproven")]
impl<I, C> InputPin for Pin<I, Input<C>>
where
I: PinId,
C: InputConfig,
{
#[inline]
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_high())
}
#[inline]
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_low())
}
}

#[cfg(feature = "unproven")]
impl<I, C> InputPin for Pin<I, Interrupt<C>>
where
I: PinId,
C: InterruptConfig,
{
#[inline]
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_high())
}
#[inline]
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_low())
}
}

impl<I, C> StatefulOutputPin for Pin<I, Output<C>>
where
I: PinId,
C: OutputConfig,
{
#[inline]
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_set_high())
}
#[inline]
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(self._is_set_low())
}
}

//==============================================================================
// Embedded HAL v0.2 traits
//==============================================================================

impl<I, C> crate::ehal_02::digital::v2::OutputPin for Pin<I, Output<C>>
where
I: PinId,
C: OutputConfig,
Expand All @@ -901,7 +990,7 @@ where
}

#[cfg(feature = "unproven")]
impl<I> InputPin for Pin<I, ReadableOutput>
impl<I> crate::ehal_02::digital::v2::InputPin for Pin<I, ReadableOutput>
where
I: PinId,
{
Expand All @@ -917,7 +1006,7 @@ where
}

#[cfg(feature = "unproven")]
impl<I, C> InputPin for Pin<I, Input<C>>
impl<I, C> crate::ehal_02::digital::v2::InputPin for Pin<I, Input<C>>
where
I: PinId,
C: InputConfig,
Expand All @@ -934,7 +1023,7 @@ where
}

#[cfg(feature = "unproven")]
impl<I, C> InputPin for Pin<I, Interrupt<C>>
impl<I, C> crate::ehal_02::digital::v2::InputPin for Pin<I, Interrupt<C>>
where
I: PinId,
C: InterruptConfig,
Expand All @@ -951,7 +1040,7 @@ where
}

#[cfg(feature = "unproven")]
impl<I, C> ToggleableOutputPin for Pin<I, Output<C>>
impl<I, C> crate::ehal_02::digital::v2::ToggleableOutputPin for Pin<I, Output<C>>
where
I: PinId,
C: OutputConfig,
Expand All @@ -965,7 +1054,7 @@ where
}

#[cfg(feature = "unproven")]
impl<I, C> StatefulOutputPin for Pin<I, Output<C>>
impl<I, C> crate::ehal_02::digital::v2::StatefulOutputPin for Pin<I, Output<C>>
where
I: PinId,
C: OutputConfig,
Expand Down

0 comments on commit 1698b0b

Please sign in to comment.