Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement InputPin for Output<OpenDrain> pins #114

Merged
merged 6 commits into from
Jul 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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