Skip to content

Commit

Permalink
Add eh1 impl for gpio
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis authored and mciantyre committed Jan 19, 2024
1 parent bc37393 commit 15f1795
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## [Unreleased]

Add embedded-hal 1 implementations for the following drivers:

- GPIO

## [0.5.4] 2023-11-26

Add CCM APIs for configuring FlexIO clocks on 1000 targets.
Expand Down
64 changes: 64 additions & 0 deletions src/common/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub struct Output<P> {
pin: P,
// Logical ownership:
// - DR: read only
// - PSR: read only
// - DR_SET, DR_CLEAR, DR_TOGGLE: write 1 to set value in DR
gpio: &'static ral::gpio::RegisterBlock,
offset: u32,
Expand Down Expand Up @@ -170,6 +171,13 @@ impl<P> Output<P> {
ral::read_reg!(ral::gpio, self.gpio, DR) & self.mask() != 0
}

/// Returns `true` if the value of the pad is high.
///
/// Can differ from [`is_set()`](Self::is_set), especially in an open drain config.
pub fn is_pad_high(&self) -> bool {
ral::read_reg!(ral::gpio, self.gpio, PSR) & self.mask() != 0
}

/// Release the underlying pin object.
pub fn release(self) -> P {
self.pin
Expand Down Expand Up @@ -348,3 +356,59 @@ impl<P> eh02::digital::v2::InputPin for Input<P> {
Ok(!self.is_set())
}
}

impl<P> eh1::digital::ErrorType for Output<P> {
type Error = core::convert::Infallible;
}

impl<P> eh1::digital::OutputPin for Output<P> {
fn set_high(&mut self) -> Result<(), Self::Error> {
Output::set(self);
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
Output::clear(self);
Ok(())
}
}

impl<P> eh1::digital::StatefulOutputPin for Output<P> {
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(Output::is_set(self))
}

fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(!Output::is_set(self))
}

fn toggle(&mut self) -> Result<(), Self::Error> {
Output::toggle(self);
Ok(())
}
}

// For open drain or simply reading back the actual state
// of the pin.
impl<P> eh1::digital::InputPin for Output<P> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(Output::is_pad_high(self))
}

fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(!Output::is_pad_high(self))
}
}

impl<P> eh1::digital::ErrorType for Input<P> {
type Error = core::convert::Infallible;
}

impl<P> eh1::digital::InputPin for Input<P> {
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(Input::is_set(self))
}

fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(!Input::is_set(self))
}
}

0 comments on commit 15f1795

Please sign in to comment.