Skip to content

Commit

Permalink
p.GPIO
Browse files Browse the repository at this point in the history
  • Loading branch information
bugadani committed Nov 9, 2024
1 parent da53932 commit a09ae82
Show file tree
Hide file tree
Showing 119 changed files with 1,100 additions and 1,090 deletions.
2 changes: 1 addition & 1 deletion esp-hal/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- SPI transactions are now cancelled if the transfer object (or async Future) is dropped. (#2216)
- The DMA channel types have been removed from peripherals (#2261)
- `I2C` driver renamed to `I2c` (#2320)
- The GPIO pins are now accessible via `Peripherals` and are no longer part of the `Io` struct (#2508)
- The GPIO pins are now accessible via `Peripherals` and are no longer part of the `Io` struct (#2508)

### Fixed

Expand Down
2 changes: 1 addition & 1 deletion esp-hal/MIGRATING-0.21.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
let peripherals = esp_hal::init(Default::default());
-let io = Io::new(peripherals.GPIO, peripherals.IOMUX);
-let pin = io.pins.gpio5;
+let pin = peripherals.pins.gpio5;
+let pin = peripherals.GPIO5;
```

### `Io` constructor changes
Expand Down
9 changes: 3 additions & 6 deletions esp-hal/src/analog/adc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@
//! # use esp_hal::analog::adc::Attenuation;
//! # use esp_hal::analog::adc::Adc;
//! # use esp_hal::delay::Delay;
#![cfg_attr(esp32, doc = "let analog_pin = peripherals.pins.gpio32;")]
#![cfg_attr(
any(esp32s2, esp32s3),
doc = "let analog_pin = peripherals.pins.gpio3;"
)]
#![cfg_attr(esp32, doc = "let analog_pin = peripherals.GPIO32;")]
#![cfg_attr(any(esp32s2, esp32s3), doc = "let analog_pin = peripherals.GPIO3;")]
#![cfg_attr(
not(any(esp32, esp32s2, esp32s3)),
doc = "let analog_pin = peripherals.pins.gpio2;"
doc = "let analog_pin = peripherals.GPIO2;"
)]
//! let mut adc1_config = AdcConfig::new();
//! let mut pin = adc1_config.enable_pin(
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/analog/dac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
//! # use esp_hal::analog::dac::Dac;
//! # use esp_hal::delay::Delay;
//! # use embedded_hal::delay::DelayNs;
#![cfg_attr(esp32, doc = "let dac1_pin = peripherals.pins.gpio25;")]
#![cfg_attr(esp32s2, doc = "let dac1_pin = peripherals.pins.gpio17;")]
#![cfg_attr(esp32, doc = "let dac1_pin = peripherals.GPIO25;")]
#![cfg_attr(esp32s2, doc = "let dac1_pin = peripherals.GPIO17;")]
//! let mut dac1 = Dac::new(peripherals.DAC1, dac1_pin);
//!
//! let mut delay = Delay::new();
Expand Down
8 changes: 4 additions & 4 deletions esp-hal/src/dma/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
//! let dma = Dma::new(peripherals.DMA);
#![cfg_attr(any(esp32, esp32s2), doc = "let dma_channel = dma.spi2channel;")]
#![cfg_attr(not(any(esp32, esp32s2)), doc = "let dma_channel = dma.channel0;")]
//! let sclk = peripherals.pins.gpio0;
//! let miso = peripherals.pins.gpio2;
//! let mosi = peripherals.pins.gpio4;
//! let cs = peripherals.pins.gpio5;
//! let sclk = peripherals.GPIO0;
//! let miso = peripherals.GPIO2;
//! let mosi = peripherals.GPIO4;
//! let cs = peripherals.GPIO5;
//!
//! let mut spi = Spi::new_with_config(
//! peripherals.SPI2,
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/etm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
//! # use esp_hal::gpio::Pull;
//! # use esp_hal::gpio::Level;
//!
//! let mut led = peripherals.pins.gpio1;
//! let button = peripherals.pins.gpio9;
//! let mut led = peripherals.GPIO1;
//! let button = peripherals.GPIO9;
//!
//! // setup ETM
//! let gpio_ext = Channels::new(peripherals.GPIO_SD);
Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/gpio/etm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
//! # use esp_hal::gpio::Pull;
//! # use esp_hal::gpio::Level;
//! #
//! # let mut led = peripherals.pins.gpio1;
//! # let button = peripherals.pins.gpio9;
//! # let mut led = peripherals.GPIO1;
//! # let button = peripherals.GPIO9;
//!
//! let gpio_ext = Channels::new(peripherals.GPIO_SD);
//! let led_task = gpio_ext.channel0_task.toggle(
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/gpio/lp_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! use esp_hal::gpio::lp_io::LowPowerOutput;
//! // configure GPIO 1 as LP output pin
//! let lp_pin: LowPowerOutput<'_, 1> =
//! LowPowerOutput::new(peripherals.pins.gpio1);
//! LowPowerOutput::new(peripherals.GPIO1);
//! # }
//! ```

Expand Down
87 changes: 36 additions & 51 deletions esp-hal/src/gpio/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
//! ```rust, no_run
#![doc = crate::before_snippet!()]
//! # use esp_hal::gpio::{Io, Level, Output};
//! let mut led = Output::new(peripherals.pins.gpio5, Level::High);
//! let mut led = Output::new(peripherals.GPIO5, Level::High);
//! # }
//! ```
//!
Expand All @@ -70,11 +70,18 @@
use portable_atomic::{AtomicPtr, Ordering};
use procmacros::ram;

#[cfg(any(lp_io, rtc_cntl))]
use crate::peripherals::gpio::{handle_rtcio, handle_rtcio_with_resistors};
pub use crate::soc::gpio::*;
use crate::{
interrupt::{self, InterruptHandler, Priority},
peripheral::{Peripheral, PeripheralRef},
peripherals::{Interrupt, GPIO, IO_MUX},
peripherals::{
gpio::{handle_gpio_input, handle_gpio_output, AnyPinInner},
Interrupt,
GPIO,
IO_MUX,
},
private::{self, Sealed},
InterruptConfigurable,
DEFAULT_INTERRUPT_HANDLER,
Expand Down Expand Up @@ -715,6 +722,9 @@ impl Bank1GpioRegisterAccess {
/// GPIO pin
pub struct GpioPin<const GPIONUM: u8>;

/// Type-erased GPIO pin
pub struct AnyPin(pub(crate) AnyPinInner);

impl<const GPIONUM: u8> GpioPin<GPIONUM>
where
Self: Pin,
Expand Down Expand Up @@ -905,20 +915,20 @@ macro_rules! if_rtcio_pin {
#[macro_export]
macro_rules! io_type {
(Input, $gpionum:literal) => {
impl $crate::gpio::InputPin for GpioPin<$gpionum> {}
impl $crate::gpio::InputPin for $crate::gpio::GpioPin<$gpionum> {}
};
(Output, $gpionum:literal) => {
impl $crate::gpio::OutputPin for GpioPin<$gpionum> {}
impl $crate::gpio::OutputPin for $crate::gpio::GpioPin<$gpionum> {}
};
(Analog, $gpionum:literal) => {
// FIXME: the implementation shouldn't be in the GPIO module
#[cfg(any(esp32c2, esp32c3, esp32c6, esp32h2))]
impl $crate::gpio::AnalogPin for GpioPin<$gpionum> {
impl $crate::gpio::AnalogPin for $crate::gpio::GpioPin<$gpionum> {
/// Configures the pin for analog mode.
fn set_analog(&self, _: $crate::private::Internal) {
use $crate::peripherals::GPIO;

get_io_mux_reg($gpionum).modify(|_, w| unsafe {
$crate::gpio::get_io_mux_reg($gpionum).modify(|_, w| unsafe {
w.mcu_sel().bits(1);
w.fun_ie().clear_bit();
w.fun_wpu().clear_bit();
Expand Down Expand Up @@ -950,91 +960,70 @@ macro_rules! gpio {
)+
) => {
paste::paste! {
/// Pins available on this chip
pub struct Pins {
$(
#[doc = concat!("GPIO pin number ", $gpionum, ".")]
pub [< gpio $gpionum >] : GpioPin<$gpionum>,
)+
}

impl Pins {
/// Unsafely create GPIO pins.
///
/// # Safety
///
/// The caller must ensure that only one instance of a pin is in use at one time.
pub unsafe fn steal() -> Self {
Self {
$(
[< gpio $gpionum >]: GpioPin::steal(),
)+
}
}
}

$(
$(
$crate::io_type!($type, $gpionum);
)*

impl $crate::gpio::Pin for GpioPin<$gpionum> {
impl $crate::gpio::Pin for $crate::gpio::GpioPin<$gpionum> {
fn number(&self) -> u8 {
$gpionum
}

fn degrade_pin(&self, _: $crate::private::Internal) -> AnyPin {
AnyPin($crate::gpio::AnyPinInner::[< Gpio $gpionum >](unsafe { Self::steal() }))
fn degrade_pin(&self, _: $crate::private::Internal) -> $crate::gpio::AnyPin {
$crate::gpio::AnyPin(AnyPinInner::[< Gpio $gpionum >](unsafe { Self::steal() }))
}

fn gpio_bank(&self, _: $crate::private::Internal) -> $crate::gpio::GpioRegisterAccess {
$crate::gpio::GpioRegisterAccess::from($gpionum)
}

fn output_signals(&self, _: $crate::private::Internal) -> &[(AlternateFunction, OutputSignal)] {
fn output_signals(&self, _: $crate::private::Internal) -> &[($crate::gpio::AlternateFunction, $crate::gpio::OutputSignal)] {
&[
$(
$(
(AlternateFunction::[< Function $af_output_num >], OutputSignal::$af_output_signal ),
(
$crate::gpio::AlternateFunction::[< Function $af_output_num >],
$crate::gpio::OutputSignal::$af_output_signal
),
)*
)?
]
}

fn input_signals(&self, _: $crate::private::Internal) -> &[(AlternateFunction, InputSignal)] {
fn input_signals(&self, _: $crate::private::Internal) -> &[($crate::gpio::AlternateFunction, $crate::gpio::InputSignal)] {
&[
$(
$(
(AlternateFunction::[< Function $af_input_num >], InputSignal::$af_input_signal ),
(
$crate::gpio::AlternateFunction::[< Function $af_input_num >],
$crate::gpio::InputSignal::$af_input_signal
),
)*
)?
]
}
}

impl From<GpioPin<$gpionum>> for AnyPin {
fn from(pin: GpioPin<$gpionum>) -> Self {
use $crate::gpio::Pin;
pin.degrade()
impl From<$crate::gpio::GpioPin<$gpionum>> for $crate::gpio::AnyPin {
fn from(pin: $crate::gpio::GpioPin<$gpionum>) -> Self {
$crate::gpio::Pin::degrade(pin)
}
}
)+

pub(crate) enum AnyPinInner {
$(
[<Gpio $gpionum >](GpioPin<$gpionum>),
[<Gpio $gpionum >]($crate::gpio::GpioPin<$gpionum>),
)+
}

/// Type-erased GPIO pin
pub struct AnyPin(pub(crate) AnyPinInner);

impl $crate::peripheral::Peripheral for AnyPin {
type P = AnyPin;
impl $crate::peripheral::Peripheral for $crate::gpio::AnyPin {
type P = $crate::gpio::AnyPin;
unsafe fn clone_unchecked(&self) -> Self {
match self.0 {
$(AnyPinInner::[<Gpio $gpionum >](_) => {
Self(AnyPinInner::[< Gpio $gpionum >](unsafe { GpioPin::steal() }))
Self(AnyPinInner::[< Gpio $gpionum >](unsafe { $crate::gpio::GpioPin::steal() }))
})+
}
}
Expand All @@ -1043,7 +1032,6 @@ macro_rules! gpio {
// These macros call the code block on the actually contained GPIO pin.

#[doc(hidden)]
#[macro_export]
macro_rules! handle_gpio_output {
($this:expr, $inner:ident, $code:tt) => {
match $this {
Expand All @@ -1060,7 +1048,6 @@ macro_rules! gpio {
}

#[doc(hidden)]
#[macro_export]
macro_rules! handle_gpio_input {
($this:expr, $inner:ident, $code:tt) => {
match $this {
Expand All @@ -1077,7 +1064,6 @@ macro_rules! gpio {
cfg_if::cfg_if! {
if #[cfg(any(lp_io, rtc_cntl))] {
#[doc(hidden)]
#[macro_export]
macro_rules! handle_rtcio {
($this:expr, $inner:ident, $code:tt) => {
match $this {
Expand All @@ -1094,7 +1080,6 @@ macro_rules! gpio {
}

#[doc(hidden)]
#[macro_export]
macro_rules! handle_rtcio_with_resistors {
($this:expr, $inner:ident, $code:tt) => {
match $this {
Expand Down
2 changes: 1 addition & 1 deletion esp-hal/src/gpio/rtc_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#![doc = crate::before_snippet!()]
//! # use esp_hal::gpio::rtc_io::LowPowerOutput;
//! // configure GPIO 1 as ULP output pin
//! let lp_pin = LowPowerOutput::<'static, 1>::new(peripherals.pins.gpio1);
//! let lp_pin = LowPowerOutput::<'static, 1>::new(peripherals.GPIO1);
//! # }
//! ```

Expand Down
4 changes: 2 additions & 2 deletions esp-hal/src/i2c/master/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
//! peripherals.I2C0,
//! Config::default(),
//! )
//! .with_sda(peripherals.pins.gpio1)
//! .with_scl(peripherals.pins.gpio2);
//! .with_sda(peripherals.GPIO1)
//! .with_scl(peripherals.GPIO2);
//!
//! loop {
//! let mut data = [0u8; 22];
Expand Down
8 changes: 4 additions & 4 deletions esp-hal/src/i2s/master.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
//! rx_descriptors,
//! tx_descriptors,
//! );
#![cfg_attr(not(esp32), doc = "let i2s = i2s.with_mclk(peripherals.pins.gpio0);")]
#![cfg_attr(not(esp32), doc = "let i2s = i2s.with_mclk(peripherals.GPIO0);")]
//! let mut i2s_rx = i2s.i2s_rx
//! .with_bclk(peripherals.pins.gpio1)
//! .with_ws(peripherals.pins.gpio2)
//! .with_din(peripherals.pins.gpio5)
//! .with_bclk(peripherals.GPIO1)
//! .with_ws(peripherals.GPIO2)
//! .with_din(peripherals.GPIO5)
//! .build();
//!
//! let mut transfer = i2s_rx.read_dma_circular(&mut rx_buffer).unwrap();
Expand Down
24 changes: 12 additions & 12 deletions esp-hal/src/lcd_cam/cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@
//! # DmaPriority::Priority0,
//! # );
//!
//! let mclk_pin = peripherals.pins.gpio15;
//! let vsync_pin = peripherals.pins.gpio6;
//! let href_pin = peripherals.pins.gpio7;
//! let pclk_pin = peripherals.pins.gpio13;
//! let mclk_pin = peripherals.GPIO15;
//! let vsync_pin = peripherals.GPIO6;
//! let href_pin = peripherals.GPIO7;
//! let pclk_pin = peripherals.GPIO13;
//! let data_pins = RxEightBits::new(
//! peripherals.pins.gpio11,
//! peripherals.pins.gpio9,
//! peripherals.pins.gpio8,
//! peripherals.pins.gpio10,
//! peripherals.pins.gpio12,
//! peripherals.pins.gpio18,
//! peripherals.pins.gpio17,
//! peripherals.pins.gpio16,
//! peripherals.GPIO11,
//! peripherals.GPIO9,
//! peripherals.GPIO8,
//! peripherals.GPIO10,
//! peripherals.GPIO12,
//! peripherals.GPIO18,
//! peripherals.GPIO17,
//! peripherals.GPIO16,
//! );
//!
//! let lcd_cam = LcdCam::new(peripherals.LCD_CAM);
Expand Down
Loading

0 comments on commit a09ae82

Please sign in to comment.