Skip to content

Commit

Permalink
Avoid using AlternateFunction::from_usize
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Oct 14, 2024
1 parent a3d8d13 commit cf74f75
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 45 deletions.
33 changes: 15 additions & 18 deletions esp-hal/src/gpio/interconnect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,9 @@ impl InputSignal {
GPIO_FUNCTION
} else {
self.input_signals(private::Internal)
.into_iter()
.position(|s| s == Some(signal))
.ok_or(())
.and_then(AlternateFunction::try_from)
.iter()
.find(|(_af, s)| *s == signal)
.map(|(af, _)| *af)
.unwrap_or(GPIO_FUNCTION)
};

Expand Down Expand Up @@ -162,7 +161,7 @@ impl InputSignal {
#[doc(hidden)]
to self.pin {
pub fn pull_direction(&mut self, pull: Pull, _internal: private::Internal);
pub fn input_signals(&self, _internal: private::Internal) -> [Option<gpio::InputSignal>; 6];
pub fn input_signals(&self, _internal: private::Internal) -> &[(AlternateFunction, gpio::InputSignal)];
pub fn init_input(&mut 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);
Expand Down Expand Up @@ -274,10 +273,9 @@ impl OutputSignal {
GPIO_FUNCTION
} else {
self.input_signals(private::Internal)
.into_iter()
.position(|s| s == Some(signal))
.ok_or(())
.and_then(AlternateFunction::try_from)
.iter()
.find(|(_af, s)| *s == signal)
.map(|(af, _)| *af)
.unwrap_or(GPIO_FUNCTION)
};

Expand Down Expand Up @@ -322,10 +320,9 @@ impl OutputSignal {
GPIO_FUNCTION
} else {
self.output_signals(private::Internal)
.into_iter()
.position(|s| s == Some(signal))
.ok_or(())
.and_then(AlternateFunction::try_from)
.iter()
.find(|(_af, s)| *s == signal)
.map(|(af, _)| *af)
.unwrap_or(GPIO_FUNCTION)
};

Expand Down Expand Up @@ -369,13 +366,13 @@ impl OutputSignal {
#[doc(hidden)]
to self.pin {
pub fn pull_direction(&mut self, pull: Pull, _internal: private::Internal);
pub fn input_signals(&self, _internal: private::Internal) -> [Option<gpio::InputSignal>; 6];
pub fn input_signals(&self, _internal: private::Internal) -> &[(AlternateFunction, gpio::InputSignal)];
pub fn init_input(&mut 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 enable_input_in_sleep_mode(&mut self, on: bool, _internal: private::Internal);

pub fn output_signals(&self, _internal: private::Internal) -> [Option<gpio::OutputSignal>; 6];
pub fn output_signals(&self, _internal: private::Internal) -> &[(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);
Expand Down Expand Up @@ -451,7 +448,7 @@ impl OutputConnection {
OutputConnectionInner::Dummy(_) => Level::Low,
} {
pub fn is_input_high(&self, _internal: private::Internal) -> bool;
pub fn input_signals(&self, _internal: private::Internal) -> [Option<gpio::InputSignal>; 6];
pub fn input_signals(&self, _internal: private::Internal) -> &[(AlternateFunction, gpio::InputSignal)];
}
#[doc(hidden)]
to match &mut self.0 {
Expand All @@ -472,7 +469,7 @@ impl OutputConnection {
OutputConnectionInner::Dummy(pin) => pin,
} {
pub fn is_set_high(&self, _internal: private::Internal) -> bool;
pub fn output_signals(&self, _internal: private::Internal) -> [Option<gpio::OutputSignal>; 6];
pub fn output_signals(&self, _internal: private::Internal) -> &[(AlternateFunction, gpio::OutputSignal)];
}

#[doc(hidden)]
Expand Down Expand Up @@ -555,7 +552,7 @@ impl InputConnection {
InputConnectionInner::Constant(level) => level,
} {
pub fn is_input_high(&self, _internal: private::Internal) -> bool;
pub fn input_signals(&self, _internal: private::Internal) -> [Option<gpio::InputSignal>; 6];
pub fn input_signals(&self, _internal: private::Internal) -> &[(AlternateFunction, gpio::InputSignal)];
}

#[doc(hidden)]
Expand Down
43 changes: 21 additions & 22 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ pub trait Pin: Sealed {
}

#[doc(hidden)]
fn output_signals(&self, _: private::Internal) -> [Option<OutputSignal>; 6];
fn output_signals(&self, _: private::Internal) -> &[(AlternateFunction, OutputSignal)];

#[doc(hidden)]
fn input_signals(&self, _: private::Internal) -> [Option<InputSignal>; 6];
fn input_signals(&self, _: private::Internal) -> &[(AlternateFunction, InputSignal)];

#[doc(hidden)]
fn gpio_bank(&self, _: private::Internal) -> GpioRegisterAccess;
Expand Down Expand Up @@ -992,29 +992,28 @@ macro_rules! gpio {
$crate::gpio::GpioRegisterAccess::[<Bank $bank >]
}

fn output_signals(&self, _: $crate::private::Internal) -> [Option<OutputSignal>; 6]{
#[allow(unused_mut)]
let mut output_signals = [None; 6];

$(
fn output_signals(&self, _: $crate::private::Internal) -> &[(AlternateFunction, OutputSignal)]{
static OUTPUT_SIGNALS: &[(AlternateFunction, OutputSignal)] = &[
$(
output_signals[ $af_output_num ] = Some( OutputSignal::$af_output_signal );
)*
)?
$(
(AlternateFunction::[< Function $af_output_num >], OutputSignal::$af_output_signal ),
)*
)?
];

output_signals
}
fn input_signals(&self, _: $crate::private::Internal) -> [Option<InputSignal>; 6] {
#[allow(unused_mut)]
let mut input_signals = [None; 6];

$(
OUTPUT_SIGNALS
}
fn input_signals(&self, _: $crate::private::Internal) -> &[(AlternateFunction, InputSignal)] {
static INPUT_SIGNALS: &[(AlternateFunction, InputSignal)] = &[
$(
input_signals[ $af_input_num ] = Some( InputSignal::$af_input_signal );
)*
)?
$(
(AlternateFunction::[< Function $af_input_num >], InputSignal::$af_input_signal ),
)*
)?
];

input_signals
INPUT_SIGNALS
}
}

Expand Down Expand Up @@ -2041,13 +2040,13 @@ pub(crate) mod internal {
});
}

fn output_signals(&self, _: private::Internal) -> [Option<OutputSignal>; 6] {
fn output_signals(&self, _: private::Internal) -> &[(AlternateFunction, OutputSignal)] {
handle_gpio_input!(&self.0, target, {
Pin::output_signals(target, private::Internal)
})
}

fn input_signals(&self, _: private::Internal) -> [Option<InputSignal>; 6] {
fn input_signals(&self, _: private::Internal) -> &[(AlternateFunction, InputSignal)] {
handle_gpio_input!(&self.0, target, {
Pin::input_signals(target, private::Internal)
})
Expand Down
16 changes: 11 additions & 5 deletions esp-hal/src/gpio/placeholder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ impl crate::peripheral::Peripheral for Level {
impl Level {
pub(crate) fn pull_direction(&self, _pull: Pull, _internal: private::Internal) {}

pub(crate) fn input_signals(&self, _: private::Internal) -> [Option<InputSignal>; 6] {
[None; 6]
pub(crate) fn input_signals(
&self,
_internal: private::Internal,
) -> &[(AlternateFunction, InputSignal)] {
&[]
}

pub(crate) fn init_input(&self, _pull: Pull, _: private::Internal) {}
Expand Down Expand Up @@ -75,8 +78,11 @@ impl Level {
false
}

pub(crate) fn output_signals(&self, _: private::Internal) -> [Option<OutputSignal>; 6] {
[None; 6]
pub(crate) fn output_signals(
&self,
_internal: private::Internal,
) -> &[(AlternateFunction, OutputSignal)] {
&[]
}

pub(crate) fn connect_peripheral_to_output(
Expand Down Expand Up @@ -122,7 +128,7 @@ impl NoPin {
pub(crate) fn internal_pull_up_in_sleep_mode(&mut self, _on: bool, _internal: private::Internal);
pub(crate) fn internal_pull_down_in_sleep_mode(&mut self, _on: bool, _internal: private::Internal);
pub(crate) fn is_set_high(&self, _internal: private::Internal) -> bool;
pub(crate) fn output_signals(&self, _internal: private::Internal) -> [Option<OutputSignal>; 6];
pub(crate) fn output_signals(&self, _internal: private::Internal) -> &[(AlternateFunction, OutputSignal)];
pub(crate) fn connect_peripheral_to_output(&mut self, _signal: OutputSignal, _internal: private::Internal);
pub(crate) fn disconnect_from_peripheral_output(&mut self, _signal: OutputSignal, _internal: private::Internal);
}
Expand Down

0 comments on commit cf74f75

Please sign in to comment.