pub struct DedicatedGpioFlexBundle<'lt> { /* private fields */ }Expand description
A bundle of dedicated GPIO flex drivers (input + output).
A flex bundle precomputes a channel mask from one or more DedicatedGpioFlex
drivers. This lets you:
- update multiple dedicated output channels with a single low-level write, and
- read multiple dedicated input channels with a single low-level read.
Attaching a driver does not change any pin state. The bundle only stores a channel mask; it does not own pins or remember which GPIOs were connected.
§Relationship to pins
A DedicatedGpioFlex connects one dedicated channel to one GPIO pin for both
input and output. The bundle operates on channels, not pins: writing channel bits
affects the pins currently connected to those channels.
§Examples
use esp_hal::gpio::{
Flex,
Level,
dedicated::{DedicatedGpio, DedicatedGpioFlex, DedicatedGpioFlexBundle},
};
// Create channels:
let channels = DedicatedGpio::new(peripherals.GPIO_DEDICATED);
// Create flex drivers for two channels:
let p0 = Flex::new(peripherals.GPIO0);
let p1 = Flex::new(peripherals.GPIO1);
let f0 = DedicatedGpioFlex::new(channels.channel0, p0);
let f1 = DedicatedGpioFlex::new(channels.channel1, p1);
// Build a bundle that controls channels 0 and 1:
let mut bundle = DedicatedGpioFlexBundle::new();
bundle.enable_flex(&f0).enable_flex(&f1);
// Set channel 0 high:
bundle.set_high(0b0000_0001);
// Drive both channels at once:
bundle.write_bits(0b0000_0010); // ch0 low, ch1 high
// Read both channels at once:
let in_bits = bundle.levels();…
Implementations§
Source§impl<'lt> DedicatedGpioFlexBundle<'lt>
impl<'lt> DedicatedGpioFlexBundle<'lt>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new, empty dedicated GPIO flex bundle.
A bundle is a logical grouping of one or more DedicatedGpioFlex drivers.
Internally, it stores a precomputed channel mask (see Self::mask) which allows
writing multiple dedicated GPIO channels efficiently.
The returned bundle initially contains no channels. Add flex drivers using
Self::enable_flex.
§Notes
- Creating a bundle does not configure any hardware by itself.
Sourcepub fn mask(&self) -> u32
pub fn mask(&self) -> u32
Returns the channel mask of this bundle.
Each bit corresponds to one dedicated GPIO channel:
- bit 0 -> channel 0
- bit 1 -> channel 1
- …
A bit is set to 1 if that channel is currently included in the bundle.
§Example
use esp_hal::gpio::{
Flex,
dedicated::{DedicatedGpio, DedicatedGpioFlex, DedicatedGpioFlexBundle},
};
let channels = DedicatedGpio::new(peripherals.GPIO_DEDICATED);
let f0 = DedicatedGpioFlex::new(channels.channel0, Flex::new(peripherals.GPIO0));
let f2 = DedicatedGpioFlex::new(channels.channel2, Flex::new(peripherals.GPIO2));
let mut bundle = DedicatedGpioFlexBundle::new();
bundle.enable_flex(&f0).enable_flex(&f2);
let mask = bundle.mask(); // bit 0 -> ch0, bit 2 -> ch2, ...
// For this bundle: mask == 0b0000_0101 (channels 0 and 2).
Sourcepub fn enable_flex<'d>(&mut self, flex: &'lt DedicatedGpioFlex<'d>) -> &mut Self
pub fn enable_flex<'d>(&mut self, flex: &'lt DedicatedGpioFlex<'d>) -> &mut Self
Attaches (enables) an already-configured dedicated flex driver to this bundle. After
enabling, you will be able to control the input/output channels of flex via this
bundle.
This method logically borrows the provided DedicatedGpioFlex for the lifetime 'lt.
Multiple bundles may borrow the same flex driver at the same time. Check examples in the
module-level documentation for more.
§Notes
- This function does not change any input/output state.
Sourcepub fn disable_flex<'d>(
&mut self,
flex: &'lt DedicatedGpioFlex<'d>,
) -> &mut Self
pub fn disable_flex<'d>( &mut self, flex: &'lt DedicatedGpioFlex<'d>, ) -> &mut Self
Disables a dedicated flex driver in this bundle.
This updates the internal mask by clearing the channel bit(s) of flex. After disabling,
future bundle operations will no longer touch those channels.
§Notes
- This does not affect
flexitself. It only changes which channels future bundle operations will touch. - This does not end the lifetime-based borrow of
flex. Even after disabling,flexstill cannot be moved or dropped while this bundle exists. - You can re-enable it later via
Self::enable_flex.
Sourcepub fn set_high(&mut self, bits: u32)
pub fn set_high(&mut self, bits: u32)
Sets selected channels high.
For every bit set to 1 in bits, the corresponding channel is driven high.
Bits set to 0 are left unchanged.
§Example
If the bundle contains channels 0 and 2, then bundle.set_high(0b0000_0101) sets
channel 0 and 2 high.
The caller must ensure that bits only contains channels included in this bundle,
i.e. bits & !self.mask() == 0.
For example, if the bundle mask is 0b0000_0011 (channels 0 and 1), then bits must not
set bit 2 (e.g. 0b0000_0100), or you would modify channel 2 outside the bundle.
When compiled with debug-assertions, this condition is checked at runtime.
Sourcepub fn set_low(&mut self, bits: u32)
pub fn set_low(&mut self, bits: u32)
Sets selected channels low.
For every bit set to 1 in bits, the corresponding channel is driven low.
Bits set to 0 are left unchanged.
§Example
If the bundle contains channels 0 and 2, then bundle.set_low(0b0000_0101) sets
channel 0 and 2 low.
The caller must ensure that bits only contains channels included in this bundle,
i.e. bits & !self.mask() == 0.
For example, if the bundle mask is 0b0000_0011 (channels 0 and 1), then bits must not
set bit 7 (e.g. 0b1000_0000), or you would modify channel 7 outside the bundle.
When compiled with debug-assertions, this condition is checked at runtime.
Sourcepub fn write_bits(&mut self, bits: u32)
pub fn write_bits(&mut self, bits: u32)
Writes output levels for all channels included by this bundle.
This method updates the output state of every channel whose bit is set in
Self::mask. Channels not included in the bundle are not modified.
bits provides the level for each channel in the bundle:
- bit = 0 -> low
- bit = 1 -> high
§Example
If self.mask() is 0b1111_0000 (bundle contains channels 4..=7), then
bundle.write_bits(0b0001_0000) sets channel 4 high and channels 5..=7 low,
while leaving channels 0..=3 unchanged.
This function only writes channels selected by Self::mask. It will not
change channels outside the mask, even if bits contains 1s there.
Make sure this “masked write” behavior matches what you intend.
Sourcepub fn output_levels(&self) -> u32
pub fn output_levels(&self) -> u32
Returns the current output levels of the channels included by this bundle.
For channels outside the bundle mask, the corresponding bits are always 0.
§Example
If the bundle mask is 0b0000_1011 (channels 0, 1, and 3), then
output_levels() will only contain bits 0, 1, and 3, regardless of the output
state of other channels.
Sourcepub fn levels(&self) -> u32
pub fn levels(&self) -> u32
Reads the current state of the channels included by this bundle.
For channels outside the bundle mask, the corresponding bits are always 0.
§Example
If the bundle mask is 0b0000_1011 (channels 0, 1, and 3), then
levels() will only contain bits 0, 1, and 3, regardless of the state
of other channels.