Skip to content

Commit ac221e7

Browse files
committed
simplify PinGuard and reduce its size
PinGuard's sole purpose is to disconnect the pin when it is dropped. The only thing really required to do so is the contained pin number. The signal that it is connected to was in fact never used, but stored in the struct anyway. This changes PinGuard to only store the pin number, with pin number 0xFF still indicating an unconnected PinGuard. Previously, the size of PinGuard was 2 or 4, depending on device. With this change, PinGuard takes 1 or 2 bytes. In each case, Option<PinGuard> and PinGuard have identical size due to taking advantage of niches in the OutputSignal enum. Additionally, this replaces all uses of Option<PinGuard> by plain PinGuard: From my understanding, there was no meaningful difference between Option<PinGuard> and PinGuard::new_unconnected() (both implying that nothing should happen on Drop). One consequence is that PinGuard::new_unconnected() can now be constructed without being able to name an output signal. This will be useful for the RMT driver, which uses the same Channel struct for rx and tx configuration (Channel does not presently contain a PinGuard, but arguably, it should).
1 parent 5fde6a7 commit ac221e7

File tree

5 files changed

+31
-37
lines changed

5 files changed

+31
-37
lines changed

esp-hal/src/gpio/interconnect.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ impl Signal<'_> {
393393
}
394394
}
395395

396-
fn connect_with_guard(self, signal: crate::gpio::OutputSignal) -> PinGuard {
396+
fn into_guard(self) -> PinGuard {
397397
match self {
398-
Signal::Pin(pin) => PinGuard::new(pin, signal),
399-
Signal::Level(_) => PinGuard::new_unconnected(signal),
398+
Signal::Pin(pin) => PinGuard::new(pin),
399+
Signal::Level(_) => PinGuard::new_unconnected(),
400400
}
401401
}
402402

@@ -864,7 +864,7 @@ impl<'d> OutputSignal<'d> {
864864
#[instability::unstable]
865865
pub(crate) fn connect_with_guard(self, signal: crate::gpio::OutputSignal) -> PinGuard {
866866
signal.connect_to(&self);
867-
self.pin.connect_with_guard(signal)
867+
self.pin.into_guard()
868868
}
869869

870870
delegate::delegate! {

esp-hal/src/gpio/mod.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ crate::unstable_module! {
6464
#[cfg(all(soc_has_rtc_io, not(esp32)))]
6565
pub mod rtc_io;
6666
}
67+
use interconnect::PeripheralOutput;
6768

6869
mod asynch;
6970
mod embedded_hal_impls;
@@ -99,24 +100,17 @@ pub(crate) static GPIO_LOCK: RawMutex = RawMutex::new();
99100
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
100101
pub(crate) struct PinGuard {
101102
pin: u8,
102-
signal: OutputSignal,
103103
}
104104

105105
impl crate::private::Sealed for PinGuard {}
106106

107107
impl PinGuard {
108-
pub(crate) fn new(pin: AnyPin<'_>, signal: OutputSignal) -> Self {
109-
Self {
110-
pin: pin.number(),
111-
signal,
112-
}
108+
pub(crate) fn new(pin: AnyPin<'_>) -> Self {
109+
Self { pin: pin.number() }
113110
}
114111

115-
pub(crate) fn new_unconnected(signal: OutputSignal) -> Self {
116-
Self {
117-
pin: u8::MAX,
118-
signal,
119-
}
112+
pub(crate) fn new_unconnected() -> Self {
113+
Self { pin: u8::MAX }
120114
}
121115

122116
#[allow(unused)]
@@ -133,7 +127,7 @@ impl Drop for PinGuard {
133127
fn drop(&mut self) {
134128
if self.pin != u8::MAX {
135129
let pin = unsafe { AnyPin::steal(self.pin) };
136-
self.signal.disconnect_from(&pin);
130+
pin.disconnect_from_peripheral_output();
137131
}
138132
}
139133
}

esp-hal/src/i2c/master/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -715,8 +715,8 @@ impl<'d> I2c<'d, Blocking> {
715715
pub fn new(i2c: impl Instance + 'd, config: Config) -> Result<Self, ConfigError> {
716716
let guard = PeripheralGuard::new(i2c.info().peripheral);
717717

718-
let sda_pin = PinGuard::new_unconnected(i2c.info().sda_output);
719-
let scl_pin = PinGuard::new_unconnected(i2c.info().scl_output);
718+
let sda_pin = PinGuard::new_unconnected();
719+
let scl_pin = PinGuard::new_unconnected();
720720

721721
let mut i2c = I2c {
722722
i2c: i2c.degrade(),

esp-hal/src/spi/master.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -616,16 +616,16 @@ struct SpiPinGuard {
616616
cs_pin: PinGuard,
617617
sio0_pin: PinGuard,
618618
sio1_pin: PinGuard,
619-
sio2_pin: Option<PinGuard>,
620-
sio3_pin: Option<PinGuard>,
619+
sio2_pin: PinGuard,
620+
sio3_pin: PinGuard,
621621
#[cfg(spi_master_has_octal)]
622-
sio4_pin: Option<PinGuard>,
622+
sio4_pin: PinGuard,
623623
#[cfg(spi_master_has_octal)]
624-
sio5_pin: Option<PinGuard>,
624+
sio5_pin: PinGuard,
625625
#[cfg(spi_master_has_octal)]
626-
sio6_pin: Option<PinGuard>,
626+
sio6_pin: PinGuard,
627627
#[cfg(spi_master_has_octal)]
628-
sio7_pin: Option<PinGuard>,
628+
sio7_pin: PinGuard,
629629
}
630630

631631
/// Configuration errors.
@@ -710,20 +710,20 @@ impl<'d> Spi<'d, Blocking> {
710710
_mode: PhantomData,
711711
guard,
712712
pins: SpiPinGuard {
713-
sclk_pin: PinGuard::new_unconnected(spi.info().sclk),
714-
cs_pin: PinGuard::new_unconnected(spi.info().cs(0)),
715-
sio0_pin: PinGuard::new_unconnected(spi.info().sio_output(0)),
716-
sio1_pin: PinGuard::new_unconnected(spi.info().sio_output(1)),
717-
sio2_pin: spi.info().opt_sio_output(2).map(PinGuard::new_unconnected),
718-
sio3_pin: spi.info().opt_sio_output(3).map(PinGuard::new_unconnected),
713+
sclk_pin: PinGuard::new_unconnected(),
714+
cs_pin: PinGuard::new_unconnected(),
715+
sio0_pin: PinGuard::new_unconnected(),
716+
sio1_pin: PinGuard::new_unconnected(),
717+
sio2_pin: PinGuard::new_unconnected(),
718+
sio3_pin: PinGuard::new_unconnected(),
719719
#[cfg(spi_master_has_octal)]
720-
sio4_pin: spi.info().opt_sio_output(4).map(PinGuard::new_unconnected),
720+
sio4_pin: PinGuard::new_unconnected(),
721721
#[cfg(spi_master_has_octal)]
722-
sio5_pin: spi.info().opt_sio_output(5).map(PinGuard::new_unconnected),
722+
sio5_pin: PinGuard::new_unconnected(),
723723
#[cfg(spi_master_has_octal)]
724-
sio6_pin: spi.info().opt_sio_output(6).map(PinGuard::new_unconnected),
724+
sio6_pin: PinGuard::new_unconnected(),
725725
#[cfg(spi_master_has_octal)]
726-
sio7_pin: spi.info().opt_sio_output(7).map(PinGuard::new_unconnected),
726+
sio7_pin: PinGuard::new_unconnected(),
727727
},
728728
spi: spi.degrade(),
729729
};
@@ -922,7 +922,7 @@ macro_rules! def_with_sio_pin {
922922
#[doc = concat!(" to the SIO", stringify!($n), " output and input signals.")]
923923
#[instability::unstable]
924924
pub fn $fn(mut self, sio: impl PeripheralOutput<'d>) -> Self {
925-
self.pins.$field = Some(self.connect_sio_pin(sio.into(), $n));
925+
self.pins.$field = self.connect_sio_pin(sio.into(), $n);
926926

927927
self
928928
}

esp-hal/src/uart/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,8 @@ where
469469

470470
let mem_guard = create_mem_guard(unsafe { self.uart.clone_unchecked() });
471471

472-
let rts_pin = PinGuard::new_unconnected(self.uart.info().rts_signal);
473-
let tx_pin = PinGuard::new_unconnected(self.uart.info().tx_signal);
472+
let rts_pin = PinGuard::new_unconnected();
473+
let tx_pin = PinGuard::new_unconnected();
474474

475475
let mut serial = Uart {
476476
rx: UartRx {

0 commit comments

Comments
 (0)