Skip to content

Commit

Permalink
inline gpio modules
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Mar 15, 2024
1 parent 15c27b1 commit b972889
Show file tree
Hide file tree
Showing 3 changed files with 337 additions and 339 deletions.
342 changes: 337 additions & 5 deletions esp-hal/src/gpio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ use crate::peripherals::{GPIO, IO_MUX};
pub(crate) use crate::rtc_pins;
pub use crate::soc::gpio::*;

#[cfg(feature = "embedded-hal-02")]
mod eh02;
#[cfg(feature = "embedded-hal")]
mod eh1;

/// Convenience type-alias for a no-pin / don't care - pin
pub type NoPinType = Gpio0<Unknown>;

Expand Down Expand Up @@ -3122,3 +3117,340 @@ mod asynch {
}
}
}

#[cfg(feature = "embedded-hal-02")]
mod eh02 {
use embedded_hal_02::digital::v2 as digital;

use super::{
AnyPin, BankGpioRegisterAccess, GpioPin, GpioProperties, Infallible, Input, IsOutputPin,
OpenDrain, Output,
};

impl<MODE, const GPIONUM: u8> digital::InputPin for GpioPin<Input<MODE>, GPIONUM>
where
Self: GpioProperties,
{
type Error = Infallible;
fn is_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_high())
}
fn is_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_low())
}
}

impl<const GPIONUM: u8> digital::InputPin for GpioPin<Output<OpenDrain>, GPIONUM>
where
Self: GpioProperties,
{
type Error = Infallible;
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_low())
}
}

impl<MODE, const GPIONUM: u8> digital::OutputPin for GpioPin<Output<MODE>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsOutputPin,
{
type Error = Infallible;
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Ok(())
}
fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_low();
Ok(())
}
}

impl<MODE, const GPIONUM: u8> digital::StatefulOutputPin for GpioPin<Output<MODE>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsOutputPin,
{
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
}
fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
}
}

impl<MODE, const GPIONUM: u8> digital::ToggleableOutputPin for GpioPin<Output<MODE>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsOutputPin,
{
type Error = Infallible;
fn toggle(&mut self) -> Result<(), Self::Error> {
self.toggle();
Ok(())
}
}

impl<MODE, TYPE> digital::InputPin for AnyPin<Input<MODE>, TYPE> {
type Error = core::convert::Infallible;

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

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

impl<MODE, TYPE> digital::OutputPin for AnyPin<Output<MODE>, TYPE> {
type Error = Infallible;

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

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

impl<MODE, TYPE> digital::StatefulOutputPin for AnyPin<Output<MODE>, TYPE> {
fn is_set_high(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_high())
}

fn is_set_low(&self) -> Result<bool, Self::Error> {
Ok(self.is_set_low())
}
}

impl<MODE, TYPE> digital::ToggleableOutputPin for AnyPin<Output<MODE>, TYPE> {
type Error = Infallible;

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

#[cfg(feature = "embedded-hal")]
mod eh1 {
use embedded_hal::digital;

use super::{
AnyPin, GpioPin, GpioProperties, Infallible, Input, IsOutputPin, OpenDrain, Output,
};

impl<MODE, const GPIONUM: u8> digital::ErrorType for GpioPin<Input<MODE>, GPIONUM>
where
Self: GpioProperties,
{
type Error = Infallible;
}

impl<MODE, const GPIONUM: u8> digital::InputPin for GpioPin<Input<MODE>, GPIONUM>
where
Self: GpioProperties,
{
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(Self::is_high(self))
}
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(Self::is_low(self))
}
}

impl<const GPIONUM: u8> digital::InputPin for GpioPin<Output<OpenDrain>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsOutputPin,
{
fn is_high(&mut self) -> Result<bool, Self::Error> {
Ok(Self::is_high(self))
}
fn is_low(&mut self) -> Result<bool, Self::Error> {
Ok(Self::is_low(self))
}
}

impl<MODE, const GPIONUM: u8> digital::ErrorType for GpioPin<Output<MODE>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsOutputPin,
{
type Error = Infallible;
}

impl<MODE, const GPIONUM: u8> digital::OutputPin for GpioPin<Output<MODE>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsOutputPin,
{
fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_low();
Ok(())
}
fn set_high(&mut self) -> Result<(), Self::Error> {
self.set_high();
Ok(())
}
}

impl<MODE, const GPIONUM: u8> digital::StatefulOutputPin for GpioPin<Output<MODE>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsOutputPin,
{
fn is_set_high(&mut self) -> Result<bool, Self::Error> {
Ok(Self::is_set_high(self))
}
fn is_set_low(&mut self) -> Result<bool, Self::Error> {
Ok(Self::is_set_low(self))
}
}

impl<MODE, TYPE> digital::ErrorType for AnyPin<Input<MODE>, TYPE> {
type Error = Infallible;
}

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

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

impl<MODE, TYPE> digital::ErrorType for AnyPin<Output<MODE>, TYPE> {
type Error = Infallible;
}

impl<MODE, TYPE> digital::OutputPin for AnyPin<Output<MODE>, TYPE> {
fn set_low(&mut self) -> Result<(), Self::Error> {
self.set_low();
Ok(())
}

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

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

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

#[cfg(feature = "async")]
use embedded_hal_async::digital::Wait;

#[cfg(feature = "async")]
use super::IsInputPin;

#[cfg(feature = "async")]
impl<MODE, const GPIONUM: u8> Wait for GpioPin<Input<MODE>, GPIONUM>
where
Self: GpioProperties,
<Self as GpioProperties>::PinType: IsInputPin,
{
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
}

async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}

async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}

async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}

async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
}

#[cfg(feature = "async")]
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> {
self.wait_for_high().await;
Ok(())
}

async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}

async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}

async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}

async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
}

#[cfg(feature = "async")]
impl<MODE, TYPE> embedded_hal_async::digital::Wait for AnyPin<Input<MODE>, TYPE> {
async fn wait_for_high(&mut self) -> Result<(), Self::Error> {
self.wait_for_high().await;
Ok(())
}

async fn wait_for_low(&mut self) -> Result<(), Self::Error> {
self.wait_for_low().await;
Ok(())
}

async fn wait_for_rising_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_rising_edge().await;
Ok(())
}

async fn wait_for_falling_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_falling_edge().await;
Ok(())
}

async fn wait_for_any_edge(&mut self) -> Result<(), Self::Error> {
self.wait_for_any_edge().await;
Ok(())
}
}
}
Loading

0 comments on commit b972889

Please sign in to comment.