Skip to main content

DedicatedGpioFlexBundle

Struct DedicatedGpioFlexBundle 

Source
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>

Source

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.
Source

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).
Source

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.
Source

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 flex itself. It only changes which channels future bundle operations will touch.
  • This does not end the lifetime-based borrow of flex. Even after disabling, flex still cannot be moved or dropped while this bundle exists.
  • You can re-enable it later via Self::enable_flex.
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn all_high(&self) -> bool

Returns true if all channels in this bundle are currently high.

Source

pub fn all_low(&self) -> bool

Returns true if all channels in this bundle are currently low.

Trait Implementations§

Source§

impl<'lt> Default for DedicatedGpioFlexBundle<'lt>

Available on crate feature unstable only.
Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'lt> Freeze for DedicatedGpioFlexBundle<'lt>

§

impl<'lt> RefUnwindSafe for DedicatedGpioFlexBundle<'lt>

§

impl<'lt> Send for DedicatedGpioFlexBundle<'lt>

§

impl<'lt> Sync for DedicatedGpioFlexBundle<'lt>

§

impl<'lt> Unpin for DedicatedGpioFlexBundle<'lt>

§

impl<'lt> UnsafeUnpin for DedicatedGpioFlexBundle<'lt>

§

impl<'lt> UnwindSafe for DedicatedGpioFlexBundle<'lt>

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.