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

Add more input trait impls for open drain outputs #905

Merged
merged 2 commits into from
Nov 8, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add a `defmt` feature to the `esp-hal-smartled` package (#846)
- Support 16MB octal PS-RAM for ESP32-S3 (#858)
- RISCV TRACE Encoder driver for ESP32-C6 / ESP32-H2 (#864)
- `embedded_hal` 1 `InputPin` and `embedded_hal_async` `Wait` impls for open drain outputs (#905)

### Changed

Expand Down
40 changes: 40 additions & 0 deletions esp-hal-common/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,20 @@ where
}
}

#[cfg(feature = "eh1")]
impl<const GPIONUM: u8> embedded_hal_1::digital::InputPin for GpioPin<Output<OpenDrain>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsOutputPin,
{
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(<Self as GpioProperties>::Bank::read_input() & (1 << (GPIONUM % 32)) != 0)
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(!self.is_high()?)
}
}

impl<MODE, const GPIONUM: u8> GpioPin<MODE, GPIONUM>
where
Self: GpioProperties,
Expand Down Expand Up @@ -2378,6 +2392,32 @@ mod asynch {
}
}

impl<const GPIONUM: u8> Wait for GpioPin<Output<OpenDrain>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsInputPin + IsOutputPin,
{
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
PinFuture::new(self, Event::HighLevel).await
}

async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
PinFuture::new(self, Event::LowLevel).await
}

async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
PinFuture::new(self, Event::RisingEdge).await
}

async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
PinFuture::new(self, Event::FallingEdge).await
}

async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
PinFuture::new(self, Event::AnyEdge).await
}
}

pub struct PinFuture<'a, P> {
pin: &'a mut P,
}
Expand Down