Skip to content

Commit

Permalink
Merge pull request #114 from ra-kete/inputpin-for-outputpin
Browse files Browse the repository at this point in the history
Implement InputPin for Output<OpenDrain> pins
  • Loading branch information
teskje authored Jul 18, 2020
2 parents 753d61c + 0e656a7 commit 8232126
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added

- Implement `InputPin` for `Output<OpenDrain>` pins ([#114](https://github.com/stm32-rs/stm32f3xx-hal/pull/114))

### Fixed

- `PLL` was calculated wrong for devices, which do not divide `HSI` ([#67](https://github.com/stm32-rs/stm32f3xx-hal/pull/67))
Expand All @@ -28,7 +32,7 @@ let clocks = rcc
.use_hse(32.mhz())
.sysclk(72.mhz())
```
This is possible through utilizing the divider, which can devide the
This is possible through utilizing the divider, which can divide the
external oscillator clock on most devices. Some devices have even the
possibility to divide the internal oscillator clock.

Expand Down
53 changes: 53 additions & 0 deletions src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,31 @@ macro_rules! gpio {
}
}

#[cfg(feature = "unproven")]
impl InputPin for PXx<Output<OpenDrain>> {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
Ok(!self.is_low()?)
}

fn is_low(&self) -> Result<bool, Self::Error> {
// NOTE(unsafe) atomic read with no side effects
Ok(unsafe {
match &self.gpio {
$(
#[cfg(all(any(
$(feature = $device,)+
), not(any(
$(feature = $device_except,)*
))))]
Gpio::$GPIOX => (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0,
)+
}
})
}
}

#[cfg(feature = "unproven")]
impl <MODE> StatefulOutputPin for PXx<Output<MODE>> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Expand Down Expand Up @@ -412,6 +437,20 @@ macro_rules! gpio {
}
}

#[cfg(feature = "unproven")]
impl InputPin for $PXx<Output<OpenDrain>> {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
Ok(!self.is_low()?)
}

fn is_low(&self) -> Result<bool, Self::Error> {
// NOTE(unsafe) atomic read with no side effects
Ok(unsafe { (*$GPIOX::ptr()).idr.read().bits() & (1 << self.i) == 0 })
}
}

#[cfg(feature = "unproven")]
impl<MODE> StatefulOutputPin for $PXx<Output<MODE>> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Expand Down Expand Up @@ -598,6 +637,20 @@ macro_rules! gpio {
}
}

#[cfg(feature = "unproven")]
impl InputPin for $PXi<Output<OpenDrain>> {
type Error = Infallible;

fn is_high(&self) -> Result<bool, Self::Error> {
Ok(!self.is_low()?)
}

fn is_low(&self) -> Result<bool, Self::Error> {
// NOTE(unsafe) atomic read with no side effects
Ok(unsafe { (*$GPIOX::ptr()).idr.read().$idri().is_low()})
}
}

#[cfg(feature = "unproven")]
impl<MODE> StatefulOutputPin for $PXi<Output<MODE>> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Expand Down

0 comments on commit 8232126

Please sign in to comment.