esp_hal/soc/esp32s3/
gpio.rs

1//! # GPIO configuration module (ESP32-S3)
2//!
3//! ## Overview
4//!
5//! The `GPIO` module provides functions and configurations for controlling the
6//! `General Purpose Input/Output` pins on the `ESP32-S3` chip. It allows you to
7//! configure pins as inputs or outputs, set their state and read their state.
8//!
9//! Let's get through the functionality and configurations provided by this GPIO
10//! module:
11//!   - `gpio` block:
12//!       * Defines the pin configurations for various GPIO pins. Each line represents a pin and its
13//!         associated options such as input/output mode, analog capability, and corresponding
14//!         functions.
15//!   - `analog` block:
16//!       * Block defines the analog capabilities of various GPIO pins. Each line represents a pin
17//!         and its associated options such as mux selection, function selection, and input enable.
18//!   - `enum InputSignal`:
19//!       * This enumeration defines input signals for the GPIO mux. Each input signal is assigned a
20//!         specific value.
21//!   - `enum OutputSignal`:
22//!       * This enumeration defines output signals for the GPIO mux. Each output signal is assigned
23//!         a specific value.
24//!
25//! This trait provides functions to read the interrupt status and NMI status
26//! registers for both the `PRO CPU` and `APP CPU`. The implementation uses the
27//! `gpio` peripheral to access the appropriate registers.
28
29macro_rules! rtcio_analog {
30    ($pin_num:expr, $pin_reg:expr, $hold:ident $(, $analog:literal)?) => {
31        paste::paste! {
32            #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
33            impl $crate::gpio::RtcPin for $crate::peripherals::[<GPIO $pin_num>]<'_> {
34                fn rtc_number(&self) -> u8 {
35                    $pin_num
36                }
37
38                /// Set the RTC properties of the pin. If `mux` is true then then pin is
39                /// routed to RTC, when false it is routed to IO_MUX.
40                fn rtc_set_config(&self, input_enable: bool, mux: bool, func: $crate::gpio::RtcFunction) {
41                    enable_iomux_clk_gate();
42
43                    // We need `paste` to rewrite something in each function, so that rustc
44                    // doesn't trip over trying to substitute a partial expression as `$pin_reg`
45                    $crate::peripherals::[<RTC _IO>]::regs()
46                        .$pin_reg.modify(|_,w| unsafe {
47                            w.fun_ie().bit(input_enable);
48                            w.mux_sel().bit(mux);
49                            w.fun_sel().bits(func as u8)
50                        });
51                }
52
53                fn rtcio_pad_hold(&self, enable: bool) {
54                    $crate::peripherals::LPWR::regs()
55                        .pad_hold()
56                        .modify(|_, w| w.$hold().bit(enable));
57                }
58            }
59
60            #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
61            impl $crate::gpio::RtcPinWithResistors for $crate::peripherals::[<GPIO $pin_num>]<'_> {
62                fn rtcio_pullup(&self, enable: bool) {
63                    $crate::peripherals::[<RTC _IO>]::regs()
64                        .$pin_reg.modify(|_, w| w.rue().bit(enable));
65                }
66
67                fn rtcio_pulldown(&self, enable: bool) {
68                    $crate::peripherals::[<RTC _IO>]::regs()
69                        .$pin_reg.modify(|_, w| w.rde().bit(enable));
70                }
71            }
72
73            $(
74                $crate::ignore!($analog);
75                impl $crate::peripherals::[<GPIO $pin_num>]<'_> {
76                    /// Configures the pin for analog mode.
77                    pub(crate) fn set_analog_impl(&self) {
78                        use $crate::gpio::RtcPin;
79                        enable_iomux_clk_gate();
80
81                        let rtcio = $crate::peripherals::[<RTC _IO>]::regs();
82
83                        // disable output
84                        rtcio.enable_w1tc().write(|w| unsafe { w.enable_w1tc().bits(1 << self.rtc_number()) });
85
86                        // disable open drain
87                        rtcio.pin(self.rtc_number() as usize).modify(|_,w| w.pad_driver().bit(false));
88
89                        rtcio.$pin_reg.modify(|_,w| {
90                            w.fun_ie().clear_bit();
91
92                            // Connect pin to analog / RTC module instead of standard GPIO
93                            w.mux_sel().set_bit();
94
95                            // Select function "RTC function 1" (GPIO) for analog use
96                            unsafe { w.fun_sel().bits(0b00) };
97
98                            // Disable pull-up and pull-down resistors on the pin
99                            w.rue().bit(false);
100                            w.rde().bit(false);
101
102                            w
103                        });
104                    }
105                }
106            )?
107        }
108    };
109
110    (
111        $( ( $pin_num:expr, $pin_reg:expr, $hold:ident $(, $analog:literal)? ) )+
112    ) => {
113        $(
114            rtcio_analog!($pin_num, $pin_reg, $hold $(, $analog)?);
115        )+
116    };
117}
118
119rtcio_analog! {
120    ( 0, touch_pad(0),   touch_pad0 )
121    ( 1, touch_pad(1),   touch_pad1 , true)
122    ( 2, touch_pad(2),   touch_pad2 , true)
123    ( 3, touch_pad(3),   touch_pad3 , true)
124    ( 4, touch_pad(4),   touch_pad4 , true)
125    ( 5, touch_pad(5),   touch_pad5 , true)
126    ( 6, touch_pad(6),   touch_pad6 , true)
127    ( 7, touch_pad(7),   touch_pad7 , true)
128    ( 8, touch_pad(8),   touch_pad8 , true)
129    ( 9, touch_pad(9),   touch_pad9 , true)
130    (10, touch_pad(10),  touch_pad10, true)
131    (11, touch_pad(11),  touch_pad11, true)
132    (12, touch_pad(12),  touch_pad12, true)
133    (13, touch_pad(13),  touch_pad13, true)
134    (14, touch_pad(14),  touch_pad14, true)
135    (15, xtal_32p_pad(), x32p       , true)
136    (16, xtal_32n_pad(), x32n       , true)
137    (17, pad_dac1(),     pdac1      , true)
138    (18, pad_dac2(),     pdac2      , true)
139    (19, rtc_pad19(),    pad19      , true)
140    (20, rtc_pad20(),    pad20      , true)
141    (21, rtc_pad21(),    pad21      )
142}
143
144fn enable_iomux_clk_gate() {
145    crate::peripherals::SENS::regs()
146        .sar_peri_clk_gate_conf()
147        .modify(|_, w| w.iomux_clk_en().set_bit());
148}