Skip to content

Commit

Permalink
Remove logic from InputPin and OutputPin (#2926)
Browse files Browse the repository at this point in the history
* Hide PAC types exposed via peripheral singletons

* Rename AlternateFunction variants

* Move logic to AnyPin

* Remove degrade_pin

* Add a note to GPIO

* Remove mask

* Fix test
  • Loading branch information
bugadani authored Jan 10, 2025
1 parent 03ea4f6 commit e13c09e
Show file tree
Hide file tree
Showing 28 changed files with 416 additions and 508 deletions.
10 changes: 5 additions & 5 deletions esp-hal/src/gpio/etm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<const C: u8> EventChannel<C> {
) -> Event<'d> {
crate::into_mapped_ref!(pin);

pin.init_input(pin_config.pull, private::Internal);
pin.init_input(pin_config.pull);

enable_event_channel(C, pin.number());
Event {
Expand Down Expand Up @@ -289,12 +289,12 @@ impl<const C: u8> TaskChannel<C> {
) -> Task<'d> {
crate::into_mapped_ref!(pin);

pin.set_output_high(pin_config.initial_state.into(), private::Internal);
pin.set_output_high(pin_config.initial_state.into());
if pin_config.open_drain {
pin.pull_direction(pin_config.pull, private::Internal);
pin.set_to_open_drain_output(private::Internal);
pin.pull_direction(pin_config.pull);
pin.set_to_open_drain_output();
} else {
pin.set_to_push_pull_output(private::Internal);
pin.set_to_push_pull_output();
}

enable_task_channel(C, pin.number());
Expand Down
117 changes: 61 additions & 56 deletions esp-hal/src/gpio/interconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl gpio::OutputSignal {
pub fn connect_to(self, pin: impl Peripheral<P = impl PeripheralOutput>) {
crate::into_mapped_ref!(pin);

// FIXME: disconnect previous connection(s)
pin.connect_peripheral_to_output(self);
}

Expand Down Expand Up @@ -154,7 +155,7 @@ fn connect_pin_to_input_signal(
.unwrap_or(GPIO_FUNCTION)
};

pin.set_alternate_function(af, private::Internal);
pin.set_alternate_function(af);

connect_input_signal(signal, pin.number(), is_inverted, af == GPIO_FUNCTION);
}
Expand Down Expand Up @@ -183,7 +184,7 @@ fn connect_peripheral_to_output(
signal
);

pin.set_alternate_function(af, private::Internal);
pin.set_alternate_function(af);

// Inlined because output signals can only be connected to pins or nothing, so
// there is no other user.
Expand All @@ -202,7 +203,7 @@ fn connect_peripheral_to_output(
}

fn disconnect_peripheral_output_from_pin(pin: &mut AnyPin, signal: gpio::OutputSignal) {
pin.set_alternate_function(GPIO_FUNCTION, private::Internal);
pin.set_alternate_function(GPIO_FUNCTION);

unsafe { GPIO::steal() }
.func_in_sel_cfg(signal as usize - FUNC_IN_SEL_OFFSET)
Expand Down Expand Up @@ -267,7 +268,7 @@ impl InputSignal {

/// Returns the current signal level.
pub fn level(&self) -> Level {
self.is_input_high(private::Internal).into()
self.is_input_high().into()
}

/// Inverts the peripheral's input signal.
Expand Down Expand Up @@ -298,11 +299,11 @@ impl InputSignal {
delegate::delegate! {
#[doc(hidden)]
to self.pin {
pub fn pull_direction(&self, pull: Pull, _internal: private::Internal);
pub fn pull_direction(&self, pull: Pull);
pub fn input_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::InputSignal)];
pub fn init_input(&self, pull: Pull, _internal: private::Internal);
pub fn is_input_high(&self, _internal: private::Internal) -> bool;
pub fn enable_input(&mut self, on: bool, _internal: private::Internal);
pub fn init_input(&self, pull: Pull);
pub fn is_input_high(&self) -> bool;
pub fn enable_input(&mut self, on: bool);
}
}
}
Expand Down Expand Up @@ -336,11 +337,11 @@ impl DirectInputSignal {

delegate::delegate! {
to self.pin {
fn pull_direction(&self, pull: Pull, _internal: private::Internal);
fn pull_direction(&self, pull: Pull);
fn input_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::InputSignal)];
fn init_input(&self, pull: Pull, _internal: private::Internal);
fn is_input_high(&self, _internal: private::Internal) -> bool;
fn enable_input(&mut self, on: bool, _internal: private::Internal);
fn init_input(&self, pull: Pull);
fn is_input_high(&self) -> bool;
fn enable_input(&mut self, on: bool);
}
}
}
Expand Down Expand Up @@ -428,22 +429,22 @@ impl OutputSignal {
delegate::delegate! {
#[doc(hidden)]
to self.pin {
pub fn pull_direction(&self, pull: Pull, _internal: private::Internal);
pub fn pull_direction(&self, pull: Pull);
pub fn input_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::InputSignal)];
pub fn init_input(&self, pull: Pull, _internal: private::Internal);
pub fn is_input_high(&self, _internal: private::Internal) -> bool;
pub fn enable_input(&mut self, on: bool, _internal: private::Internal);
pub fn init_input(&self, pull: Pull);
pub fn is_input_high(&self) -> bool;
pub fn enable_input(&mut self, on: bool);

pub fn output_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::OutputSignal)];
pub fn set_to_open_drain_output(&mut self, _internal: private::Internal);
pub fn set_to_push_pull_output(&mut self, _internal: private::Internal);
pub fn enable_output(&mut self, on: bool, _internal: private::Internal);
pub fn set_output_high(&mut self, on: bool, _internal: private::Internal);
pub fn set_drive_strength(&mut self, strength: gpio::DriveStrength, _internal: private::Internal);
pub fn enable_open_drain(&mut self, on: bool, _internal: private::Internal);
pub fn internal_pull_up_in_sleep_mode(&mut self, on: bool, _internal: private::Internal);
pub fn internal_pull_down_in_sleep_mode(&mut self, on: bool, _internal: private::Internal);
pub fn is_set_high(&self, _internal: private::Internal) -> bool;
pub fn set_to_open_drain_output(&mut self);
pub fn set_to_push_pull_output(&mut self);
pub fn enable_output(&mut self, on: bool);
pub fn set_output_high(&mut self, on: bool);
pub fn set_drive_strength(&mut self, strength: gpio::DriveStrength);
pub fn enable_open_drain(&mut self, on: bool);
pub fn internal_pull_up_in_sleep_mode(&mut self, on: bool);
pub fn internal_pull_down_in_sleep_mode(&mut self, on: bool);
pub fn is_set_high(&self) -> bool;
}
}
}
Expand Down Expand Up @@ -476,22 +477,22 @@ impl DirectOutputSignal {

delegate::delegate! {
to self.pin {
fn pull_direction(&self, pull: Pull, _internal: private::Internal);
fn pull_direction(&self, pull: Pull);
fn input_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::InputSignal)];
fn init_input(&self, pull: Pull, _internal: private::Internal);
fn is_input_high(&self, _internal: private::Internal) -> bool;
fn enable_input(&mut self, on: bool, _internal: private::Internal);
fn init_input(&self, pull: Pull);
fn is_input_high(&self) -> bool;
fn enable_input(&mut self, on: bool);

fn output_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::OutputSignal)];
fn set_to_open_drain_output(&mut self, _internal: private::Internal);
fn set_to_push_pull_output(&mut self, _internal: private::Internal);
fn enable_output(&mut self, on: bool, _internal: private::Internal);
fn set_output_high(&mut self, on: bool, _internal: private::Internal);
fn set_drive_strength(&mut self, strength: gpio::DriveStrength, _internal: private::Internal);
fn enable_open_drain(&mut self, on: bool, _internal: private::Internal);
fn internal_pull_up_in_sleep_mode(&mut self, on: bool, _internal: private::Internal);
fn internal_pull_down_in_sleep_mode(&mut self, on: bool, _internal: private::Internal);
fn is_set_high(&self, _internal: private::Internal) -> bool;
fn set_to_open_drain_output(&mut self);
fn set_to_push_pull_output(&mut self);
fn enable_output(&mut self, on: bool);
fn set_output_high(&mut self, on: bool);
fn set_drive_strength(&mut self, strength: gpio::DriveStrength);
fn enable_open_drain(&mut self, on: bool);
fn internal_pull_up_in_sleep_mode(&mut self, on: bool);
fn internal_pull_down_in_sleep_mode(&mut self, on: bool);
fn is_set_high(&self) -> bool;
}
}
}
Expand Down Expand Up @@ -589,9 +590,9 @@ impl InputConnection {
InputConnectionInner::DirectInput(pin) => pin,
InputConnectionInner::Constant(level) => level,
} {
pub fn pull_direction(&self, pull: Pull, _internal: private::Internal);
pub fn init_input(&self, pull: Pull, _internal: private::Internal);
pub fn is_input_high(&self, _internal: private::Internal) -> bool;
pub fn pull_direction(&self, pull: Pull);
pub fn init_input(&self, pull: Pull);
pub fn is_input_high(&self) -> bool;
pub fn input_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::InputSignal)];
}

Expand All @@ -601,7 +602,9 @@ impl InputConnection {
InputConnectionInner::DirectInput(pin) => pin,
InputConnectionInner::Constant(level) => level,
} {
pub fn enable_input(&mut self, on: bool, _internal: private::Internal);
pub fn enable_input(&mut self, on: bool);

// This doesn't need to be public, the intended way is `connect_to` and `disconnect_from`
fn connect_input_to_peripheral(&mut self, signal: gpio::InputSignal);
}
}
Expand Down Expand Up @@ -683,10 +686,10 @@ impl OutputConnection {
OutputConnectionInner::DirectOutput(pin) => pin,
OutputConnectionInner::Constant(level) => level,
} {
pub fn is_input_high(&self, _internal: private::Internal) -> bool;
pub fn is_input_high(&self) -> bool;
pub fn input_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::InputSignal)];

pub fn is_set_high(&self, _internal: private::Internal) -> bool;
pub fn is_set_high(&self) -> bool;
pub fn output_signals(&self, _internal: private::Internal) -> &'static [(AlternateFunction, gpio::OutputSignal)];
}
#[doc(hidden)]
Expand All @@ -695,18 +698,20 @@ impl OutputConnection {
OutputConnectionInner::DirectOutput(pin) => pin,
OutputConnectionInner::Constant(level) => level,
} {
pub fn pull_direction(&mut self, pull: Pull, _internal: private::Internal);
pub fn init_input(&mut self, pull: Pull, _internal: private::Internal);
pub fn enable_input(&mut self, on: bool, _internal: private::Internal);

pub fn set_to_open_drain_output(&mut self, _internal: private::Internal);
pub fn set_to_push_pull_output(&mut self, _internal: private::Internal);
pub fn enable_output(&mut self, on: bool, _internal: private::Internal);
pub fn set_output_high(&mut self, on: bool, _internal: private::Internal);
pub fn set_drive_strength(&mut self, strength: gpio::DriveStrength, _internal: private::Internal);
pub fn enable_open_drain(&mut self, on: bool, _internal: private::Internal);
pub fn internal_pull_up_in_sleep_mode(&mut self, on: bool, _internal: private::Internal);
pub fn internal_pull_down_in_sleep_mode(&mut self, on: bool, _internal: private::Internal);
pub fn pull_direction(&mut self, pull: Pull);
pub fn init_input(&mut self, pull: Pull);
pub fn enable_input(&mut self, on: bool);

pub fn set_to_open_drain_output(&mut self);
pub fn set_to_push_pull_output(&mut self);
pub fn enable_output(&mut self, on: bool);
pub fn set_output_high(&mut self, on: bool);
pub fn set_drive_strength(&mut self, strength: gpio::DriveStrength);
pub fn enable_open_drain(&mut self, on: bool);
pub fn internal_pull_up_in_sleep_mode(&mut self, on: bool);
pub fn internal_pull_down_in_sleep_mode(&mut self, on: bool);

// These don't need to be public, the intended way is `connect_to` and `disconnect_from`
fn connect_peripheral_to_output(&mut self, signal: gpio::OutputSignal);
fn disconnect_from_peripheral_output(&mut self, signal: gpio::OutputSignal);
}
Expand Down
Loading

0 comments on commit e13c09e

Please sign in to comment.