Struct esp_idf_hal::gpio::PinDriver
source · pub struct PinDriver<'d, T: Pin, MODE> { /* private fields */ }
Expand description
A driver for a GPIO pin.
The driver can set the pin as a disconnected/disabled one, input, or output pin, or both or analog. On some chips (i.e. esp32 and esp32s*), the driver can also set the pin in RTC IO mode. Depending on the current operating mode, different sets of functions are available.
The mode-setting depends on the capabilities of the pin as well, i.e. input-only pins cannot be set into output or input-output mode.
Implementations§
source§impl<'d, T: Pin> PinDriver<'d, T, Disabled>
impl<'d, T: Pin> PinDriver<'d, T, Disabled>
sourcepub fn disabled(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
pub fn disabled(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
Creates the driver for a pin in disabled state.
source§impl<'d, T: InputPin> PinDriver<'d, T, Input>
impl<'d, T: InputPin> PinDriver<'d, T, Input>
sourcepub fn input(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
pub fn input(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
Creates the driver for a pin in input state.
source§impl<'d, T: InputPin + OutputPin> PinDriver<'d, T, InputOutput>
impl<'d, T: InputPin + OutputPin> PinDriver<'d, T, InputOutput>
sourcepub fn input_output(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
pub fn input_output(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
Creates the driver for a pin in input-output state.
source§impl<'d, T: InputPin + OutputPin> PinDriver<'d, T, InputOutput>
impl<'d, T: InputPin + OutputPin> PinDriver<'d, T, InputOutput>
sourcepub fn input_output_od(
pin: impl Peripheral<P = T> + 'd,
) -> Result<Self, EspError>
pub fn input_output_od( pin: impl Peripheral<P = T> + 'd, ) -> Result<Self, EspError>
Creates the driver for a pin in input-output open-drain state.
source§impl<'d, T: OutputPin> PinDriver<'d, T, Output>
impl<'d, T: OutputPin> PinDriver<'d, T, Output>
sourcepub fn output(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
pub fn output(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
Creates the driver for a pin in output state.
source§impl<'d, T: OutputPin> PinDriver<'d, T, Output>
impl<'d, T: OutputPin> PinDriver<'d, T, Output>
sourcepub fn output_od(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
pub fn output_od(pin: impl Peripheral<P = T> + 'd) -> Result<Self, EspError>
Creates the driver for a pin in output open-drain state.
source§impl<'d, T: Pin, MODE> PinDriver<'d, T, MODE>
impl<'d, T: Pin, MODE> PinDriver<'d, T, MODE>
sourcepub fn into_disabled(self) -> Result<PinDriver<'d, T, Disabled>, EspError>
pub fn into_disabled(self) -> Result<PinDriver<'d, T, Disabled>, EspError>
Put the pin into disabled mode.
sourcepub fn into_input(self) -> Result<PinDriver<'d, T, Input>, EspError>where
T: InputPin,
pub fn into_input(self) -> Result<PinDriver<'d, T, Input>, EspError>where
T: InputPin,
Put the pin into input mode.
sourcepub fn into_input_output(
self,
) -> Result<PinDriver<'d, T, InputOutput>, EspError>
pub fn into_input_output( self, ) -> Result<PinDriver<'d, T, InputOutput>, EspError>
Put the pin into input + output mode.
sourcepub fn into_input_output_od(
self,
) -> Result<PinDriver<'d, T, InputOutput>, EspError>
pub fn into_input_output_od( self, ) -> Result<PinDriver<'d, T, InputOutput>, EspError>
Put the pin into input + output Open Drain mode.
This is commonly used for “open drain” mode. the hardware will drive the line low if you set it to low, and will leave it floating if you set it to high, in which case you can read the input to figure out whether another device is driving the line low.
sourcepub fn into_output(self) -> Result<PinDriver<'d, T, Output>, EspError>where
T: OutputPin,
pub fn into_output(self) -> Result<PinDriver<'d, T, Output>, EspError>where
T: OutputPin,
Put the pin into output mode.
sourcepub fn into_output_od(self) -> Result<PinDriver<'d, T, Output>, EspError>where
T: OutputPin,
pub fn into_output_od(self) -> Result<PinDriver<'d, T, Output>, EspError>where
T: OutputPin,
Put the pin into output Open Drain mode.
pub fn get_drive_strength(&self) -> Result<DriveStrength, EspError>where
MODE: OutputMode,
pub fn set_drive_strength(
&mut self,
strength: DriveStrength,
) -> Result<(), EspError>where
MODE: OutputMode,
pub fn is_high(&self) -> boolwhere
MODE: InputMode,
pub fn is_low(&self) -> boolwhere
MODE: InputMode,
pub fn get_level(&self) -> Levelwhere
MODE: InputMode,
pub fn is_set_high(&self) -> boolwhere
MODE: OutputMode,
sourcepub fn is_set_low(&self) -> boolwhere
MODE: OutputMode,
pub fn is_set_low(&self) -> boolwhere
MODE: OutputMode,
Is the output pin set as low?
pub fn set_high(&mut self) -> Result<(), EspError>where
MODE: OutputMode,
sourcepub fn set_low(&mut self) -> Result<(), EspError>where
MODE: OutputMode,
pub fn set_low(&mut self) -> Result<(), EspError>where
MODE: OutputMode,
Set the output as low.
pub fn set_level(&mut self, level: Level) -> Result<(), EspError>where
MODE: OutputMode,
sourcepub fn toggle(&mut self) -> Result<(), EspError>where
MODE: OutputMode,
pub fn toggle(&mut self) -> Result<(), EspError>where
MODE: OutputMode,
Toggle pin output
pub fn set_pull(&mut self, pull: Pull) -> Result<(), EspError>
sourcepub unsafe fn subscribe<F: FnMut() + Send + 'static>(
&mut self,
callback: F,
) -> Result<(), EspError>where
MODE: InputMode,
pub unsafe fn subscribe<F: FnMut() + Send + 'static>(
&mut self,
callback: F,
) -> Result<(), EspError>where
MODE: InputMode,
Subscribes the provided callback for ISR notifications.
As a side effect, interrupts will be disabled, so to receive a notification, one has
to also call PinDriver::enable_interrupt
after calling this method.
Note that PinDriver::enable_interrupt
should also be called after
each received notification from non-ISR context, because the driver will automatically
disable ISR interrupts on each received ISR notification (so as to avoid IWDT triggers).
§Safety
Care should be taken not to call STD, libc or FreeRTOS APIs (except for a few allowed ones) in the callback passed to this function, as it is executed in an ISR context.
sourcepub unsafe fn subscribe_nonstatic<F: FnMut() + Send + 'd>(
&mut self,
callback: F,
) -> Result<(), EspError>where
MODE: InputMode,
pub unsafe fn subscribe_nonstatic<F: FnMut() + Send + 'd>(
&mut self,
callback: F,
) -> Result<(), EspError>where
MODE: InputMode,
Subscribes the provided callback for ISR notifications.
As a side effect, interrupts will be disabled, so to receive a notification, one has
to also call PinDriver::enable_interrupt
after calling this method.
Note that PinDriver::enable_interrupt
should also be called after
each received notification from non-ISR context, because the driver will automatically
disable ISR interrupts on each received ISR notification (so as to avoid IWDT triggers).
§Safety
Care should be taken not to call STD, libc or FreeRTOS APIs (except for a few allowed ones) in the callback passed to this function, as it is executed in an ISR context.
Additionally, this method - in contrast to method subscribe
- allows
the passed-in callback/closure to be non-'static
. This enables users to borrow
- in the closure - variables that live on the stack - or more generally - in the same scope where the driver is created.
HOWEVER: care should be taken NOT to call core::mem::forget()
on the driver,
as that would immediately lead to an UB (crash).
Also note that forgetting the driver might happen with Rc
and Arc
when circular references are introduced: https://github.com/rust-lang/rust/issues/24456
The reason is that the closure is actually sent and owned by an ISR routine, which means that if the driver is forgotten, Rust is free to e.g. unwind the stack and the ISR routine will end up with references to variables that no longer exist.
The destructor of the driver takes care - prior to the driver being dropped and e.g. the stack being unwind - to unsubscribe the ISR routine. Unfortunately, when the driver is forgotten, the un-subscription does not happen and invalid references are left dangling.
This “local borrowing” will only be possible to express in a safe way once/if !Leak
types
are introduced to Rust (i.e. the impossibility to “forget” a type and thus not call its destructor).
pub fn unsubscribe(&mut self) -> Result<(), EspError>where
MODE: InputMode,
sourcepub fn enable_interrupt(&mut self) -> Result<(), EspError>where
MODE: InputMode,
pub fn enable_interrupt(&mut self) -> Result<(), EspError>where
MODE: InputMode,
Enables or re-enables the interrupt
Note that the interrupt is automatically disabled each time an interrupt is triggered (or else we risk entering a constant interrupt processing loop while the pin is in low/high state and the interrupt type is set to non-edge)
Therefore - to continue receiving ISR interrupts - user needs to call enable_interrupt
- from a non-ISR context - after each successful interrupt triggering.