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

Remove AnyInputOnlyPin #2071

Merged
merged 4 commits into from
Sep 4, 2024
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 esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Removed the `async`, `embedded-hal-02`, `embedded-hal`, `embedded-io`, `embedded-io-async`, and `ufmt` features (#2070)
- Removed the `GpioN` type aliasses. Use `GpioPin<N>` instead. (#2073)
- Removed `Peripherals::take`. Use `esp_hal::init` to obtain `Peripherals` (#1999)
- Removed `AnyInputOnlyPin` in favour of `AnyPin`. (#2071)

## [0.20.1] - 2024-08-30

Expand Down
4 changes: 3 additions & 1 deletion esp-hal/MIGRATING-0.20.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ Instead of manually grabbing peripherals and setting up clocks, you should now c

## GPIO changes

The `GpioN` type aliasses are no longer available. You can use `GpioPin<N>` instead.
- The `GpioN` type aliasses are no longer available. You can use `GpioPin<N>` instead.
- The `AnyInputOnlyPin` has been removed. Replace any use with `AnyPin`.
- The `NoPinType` has been removed. You can use `DummyPin` in its place.
126 changes: 15 additions & 111 deletions esp-hal/src/gpio/any_pin.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,25 @@
use super::*;

#[derive(Clone, Copy)]
enum Inverted {
NonInverted,
Inverted,
}

impl Inverted {
fn is_inverted(&self) -> bool {
match self {
Inverted::NonInverted => false,
Inverted::Inverted => true,
}
}
}

/// Generic pin wrapper for pins which can be Output or Input.
/// A type-erased GPIO pin, with additional configuration options.
///
/// Note that accessing unsupported pin functions (e.g. trying to use an
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

/// input-only pin as output) will panic.
pub struct AnyPin<'d> {
pin: ErasedPin,
inverted: Inverted,
is_inverted: bool,
_phantom: PhantomData<&'d ()>,
}

impl<'d> AnyPin<'d> {
/// Create wrapper for the given pin.
#[inline]
pub fn new<P: OutputPin + InputPin + CreateErasedPin>(
pin: impl crate::peripheral::Peripheral<P = P> + 'd,
) -> Self {
pub fn new<P: CreateErasedPin>(pin: impl crate::peripheral::Peripheral<P = P> + 'd) -> Self {
crate::into_ref!(pin);
let pin = pin.erased_pin(private::Internal);

Self {
pin,
inverted: Inverted::NonInverted,
is_inverted: false,
_phantom: PhantomData,
}
}
Expand All @@ -49,7 +35,7 @@ impl<'d> AnyPin<'d> {

Self {
pin,
inverted: Inverted::Inverted,
is_inverted: true,
_phantom: PhantomData,
}
}
Expand All @@ -61,7 +47,7 @@ impl<'d> crate::peripheral::Peripheral for AnyPin<'d> {
unsafe fn clone_unchecked(&mut self) -> Self::P {
Self {
pin: unsafe { self.pin.clone_unchecked() },
inverted: self.inverted,
is_inverted: self.is_inverted,
_phantom: PhantomData,
}
}
Expand Down Expand Up @@ -114,10 +100,10 @@ impl<'d> OutputPin for AnyPin<'d> {
fn connect_peripheral_to_output(&mut self, signal: OutputSignal, _internal: private::Internal) {
self.pin.connect_peripheral_to_output_with_options(
signal,
self.inverted.is_inverted(),
self.is_inverted,
false,
false,
self.inverted.is_inverted(),
self.is_inverted,
private::Internal,
);
}
Expand All @@ -131,7 +117,7 @@ impl<'d> OutputPin for AnyPin<'d> {
force_via_gpio_mux: bool,
_internal: private::Internal,
) {
if self.inverted.is_inverted() {
if self.is_inverted {
self.pin.connect_peripheral_to_output_with_options(
signal,
true,
Expand Down Expand Up @@ -168,8 +154,8 @@ impl<'d> InputPin for AnyPin<'d> {
fn connect_input_to_peripheral(&mut self, signal: InputSignal, _internal: private::Internal) {
self.pin.connect_input_to_peripheral_with_options(
signal,
self.inverted.is_inverted(),
self.inverted.is_inverted(),
self.is_inverted,
self.is_inverted,
private::Internal,
);
}
Expand All @@ -181,7 +167,7 @@ impl<'d> InputPin for AnyPin<'d> {
force_via_gpio_mux: bool,
_internal: private::Internal,
) {
if self.inverted.is_inverted() {
if self.is_inverted {
self.pin.connect_input_to_peripheral_with_options(
signal,
true,
Expand All @@ -198,85 +184,3 @@ impl<'d> InputPin for AnyPin<'d> {
}
}
}

/// Generic pin wrapper for pins which can only be Input.
pub struct AnyInputOnlyPin<'d> {
pin: ErasedPin,
inverted: Inverted,
_phantom: PhantomData<&'d ()>,
}

impl<'d> AnyInputOnlyPin<'d> {
/// Create wrapper for the given pin.
#[inline]
pub fn new<P: InputPin + CreateErasedPin>(
pin: impl crate::peripheral::Peripheral<P = P> + 'd,
) -> Self {
crate::into_ref!(pin);
let pin = pin.erased_pin(private::Internal);

Self {
pin,
inverted: Inverted::NonInverted,
_phantom: PhantomData,
}
}
}

impl<'d> crate::peripheral::Peripheral for AnyInputOnlyPin<'d> {
type P = Self;

unsafe fn clone_unchecked(&mut self) -> Self::P {
Self {
pin: unsafe { self.pin.clone_unchecked() },
inverted: self.inverted,
_phantom: PhantomData,
}
}
}

impl<'d> private::Sealed for AnyInputOnlyPin<'d> {}

impl<'d> Pin for AnyInputOnlyPin<'d> {
delegate::delegate! {
to self.pin {
fn number(&self, _internal: private::Internal) -> u8;
fn sleep_mode(&mut self, on: bool, _internal: private::Internal);
fn set_alternate_function(&mut self, alternate: AlternateFunction, _internal: private::Internal);
fn is_listening(&self, _internal: private::Internal) -> bool;
fn listen_with_options(
&mut self,
event: Event,
int_enable: bool,
nmi_enable: bool,
wake_up_from_light_sleep: bool,
_internal: private::Internal,
);
fn unlisten(&mut self, _internal: private::Internal);
fn is_interrupt_set(&self, _internal: private::Internal) -> bool;
fn clear_interrupt(&mut self, _internal: private::Internal);
fn wakeup_enable(&mut self, enable: bool, event: WakeEvent, _internal: private::Internal);
}
}
}

impl<'d> InputPin for AnyInputOnlyPin<'d> {
delegate::delegate! {
to self.pin {
fn init_input(&self, pull_down: bool, pull_up: bool, _internal: private::Internal);
fn set_to_input(&mut self, _internal: private::Internal);
fn enable_input(&mut self, on: bool, _internal: private::Internal);
fn enable_input_in_sleep_mode(&mut self, on: bool, _internal: private::Internal);
fn is_input_high(&self, _internal: private::Internal) -> bool;
fn connect_input_to_peripheral(&mut self, signal: InputSignal, _internal: private::Internal);
fn connect_input_to_peripheral_with_options(
&mut self,
signal: InputSignal,
invert: bool,
force_via_gpio_mux: bool,
_internal: private::Internal,
);
fn disconnect_input_from_peripheral(&mut self, signal: InputSignal, _internal: private::Internal);
}
}
}
5 changes: 2 additions & 3 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
//! - [Input] pins can be used as digital inputs.
//! - [Output] and [OutputOpenDrain] pins can be used as digital outputs.
//! - [Flex] pin is a pin that can be used as an input and output pin.
//! - [AnyPin] and [AnyInputOnlyPin] are type-erased GPIO pins with support for
//! inverted signalling.
//! - [AnyPin] is a type-erased GPIO pin with support for inverted signalling.
//! - [DummyPin] is a useful for cases where peripheral driver requires a pin,
//! but real pin cannot be used.
//!
Expand Down Expand Up @@ -77,7 +76,7 @@ pub(crate) use crate::{touch_common, touch_into};
mod any_pin;
mod dummy_pin;

pub use any_pin::{AnyInputOnlyPin, AnyPin};
pub use any_pin::AnyPin;
pub use dummy_pin::DummyPin;

#[cfg(soc_etm)]
Expand Down