diff --git a/esp-hal/CHANGELOG.md b/esp-hal/CHANGELOG.md index 65fd89f3abe..3cd1dd8f7ba 100644 --- a/esp-hal/CHANGELOG.md +++ b/esp-hal/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- Added the dynamic_output_gpio example - Added new `Io::new_no_bind_interrupt` constructor (#1861) ### Changed diff --git a/examples/src/bin/dynamic_output_gpio.rs b/examples/src/bin/dynamic_output_gpio.rs new file mode 100644 index 00000000000..3ca7007a62a --- /dev/null +++ b/examples/src/bin/dynamic_output_gpio.rs @@ -0,0 +1,68 @@ +//! Iterates over an array of LEDs, demonstrating how to use AnyPin and Flex to drive GPIOs. +//! +//! The following wiring is assumed: +//! - LED => GPIO0 +//! - LED => GPIO1 +//! - LED => GPIO2 + +//% CHIPS: esp32c6 + +#![no_std] +#![no_main] + +use esp_backtrace as _; +use esp_hal::{ + clock::ClockControl, + delay::Delay, + gpio::{any_pin::AnyPin, Flex, Io}, + peripherals::Peripherals, + prelude::*, + system::SystemControl, +}; + +pub struct FlexIo<'a> { + pub gpio0: Flex<'a, AnyPin<'a>>, + pub gpio1: Flex<'a, AnyPin<'a>>, + pub gpio2: Flex<'a, AnyPin<'a>>, +} + +impl<'a> FlexIo<'a> { + pub fn new(io: Io) -> Self { + FlexIo { + gpio0: Flex::new(AnyPin::new(io.pins.gpio0)), + gpio1: Flex::new(AnyPin::new(io.pins.gpio1)), + gpio2: Flex::new(AnyPin::new(io.pins.gpio2)), + } + } + pub fn get_pin(&mut self, index: u32) -> &mut Flex<'a, AnyPin<'a>> { + match index { + 0 => &mut self.gpio0, + 1 => &mut self.gpio1, + 2 => &mut self.gpio2, + _ => panic!("No such pin"), + } + } +} + +#[entry] +fn main() -> ! { + let peripherals = Peripherals::take(); + let system = SystemControl::new(peripherals.SYSTEM); + let clocks = ClockControl::boot_defaults(system.clock_control).freeze(); + + let io = Io::new(peripherals.GPIO, peripherals.IO_MUX); + let delay = Delay::new(&clocks); + + let mut flexIo = FlexIo::new(io); + // You can also use set_as_input() to use those GPIOs easily as input too + flexIo.gpio0.set_as_output(); + flexIo.gpio1.set_as_output(); + flexIo.gpio2.set_as_output(); + + loop { + for n in 0..3 { + flexIo.get_pin(n).toggle(); + delay.delay_millis(250); + } + } +}