Skip to main content

esp_hal/gpio/lp_io/low_level/
esp32.rs

1use crate::{
2    gpio::{GPIO_LOCK, lp_io::LpFunction},
3    peripherals::{GPIO, LPWR, RTC_IO},
4};
5
6macro_rules! lp_io_analog {
7    ($pin_peri:ident, $lp_pin:expr, $pin_reg:expr, $prefix:pat, $hold:ident) => {
8        paste::paste! {
9            #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
10            impl crate::gpio::LpPin for crate::peripherals::$pin_peri<'_> {
11                fn lp_number(&self) -> u8 {
12                    $lp_pin
13                }
14            }
15
16            impl crate::peripherals::$pin_peri<'_> {
17                #[cfg(feature = "unstable")]
18                pub(crate) fn set_analog_impl(&self) {
19                    use crate::gpio::{LpPin, Pin};
20
21                    output_enable(self.lp_number(), false);
22                    set_open_drain_output(self.number(), false);
23
24                    GPIO_LOCK.lock(|| {
25                        RTC_IO::regs().$pin_reg.modify(|_, w| {
26                            w.[<$prefix fun_ie>]().clear_bit();
27                            w.[<$prefix mux_sel>]().set_bit();
28                            unsafe { w.[<$prefix fun_sel>]().bits(LpFunction::LP_GPIO as u8) };
29
30                            // Only output pins have PU/PD resistors.
31                            for_each_gpio! {
32                                ($n:tt, $pin_peri $in_afs:tt $out_afs:tt ($input:tt [Output])) => {
33                                    w.[<$prefix rue>]().bit(false);
34                                    w.[<$prefix rde>]().bit(false);
35                                };
36                            }
37
38                            w
39                        });
40                    });
41                }
42            }
43        }
44    };
45
46    (
47        $(($pin_peri:ident, $lp_pin:tt, $pin_reg:expr, $prefix:pat, $hold:ident))+
48    ) => {
49        $(
50            lp_io_analog!($pin_peri, $lp_pin, $pin_reg, $prefix, $hold);
51        )+
52
53        // Each pad has its own register, so this code selects the register by the low-power number.
54        pub(crate) fn set_config(lp: u8, input_enable: bool, mux: bool, func: LpFunction) {
55            paste::paste! {
56                match lp {
57                    $(
58                        $lp_pin => GPIO_LOCK.lock(|| {
59                            RTC_IO::regs().$pin_reg.modify(|_, w| unsafe {
60                                w.[<$prefix fun_ie>]().bit(input_enable);
61                                w.[<$prefix mux_sel>]().bit(mux);
62                                w.[<$prefix fun_sel>]().bits(func as u8)
63                            });
64                        }),
65                    )+
66                    _ => unreachable!(),
67                }
68            }
69        }
70
71        /// A hold takes two bits on this chip, one in the pad register and one
72        /// in the always-on register. The pad keeps the bit of the pad register
73        /// through a reset, so a release must clear both.
74        pub(crate) fn pad_hold(lp: u8, enable: bool) {
75            GPIO_LOCK.lock(|| {
76                paste::paste! {
77                    match lp {
78                        $(
79                            $lp_pin => {
80                                RTC_IO::regs()
81                                    .$pin_reg
82                                    .modify(|_, w| w.[<$prefix hold>]().bit(enable));
83                                LPWR::regs()
84                                    .hold_force()
85                                    .modify(|_, w| w.$hold().bit(enable));
86                            }
87                        )+
88                        _ => unreachable!(),
89                    }
90                }
91            });
92        }
93
94        /// Returns whether something holds the pad.
95        ///
96        /// Either bit of [`pad_hold`] alone keeps the pad frozen.
97        pub(crate) fn is_pad_held(lp: u8) -> bool {
98            paste::paste! {
99                match lp {
100                    $(
101                        $lp_pin => {
102                            RTC_IO::regs().$pin_reg.read().[<$prefix hold>]().bit_is_set()
103                                || LPWR::regs().hold_force().read().$hold().bit_is_set()
104                        }
105                    )+
106                    _ => unreachable!(),
107                }
108            }
109        }
110
111        /// One bit for each low-power pad.
112        const ALL_PADS: u32 = 0 $( | 1 << $lp_pin )+;
113
114        macro_rules! set_one_pad_field {
115            $(
116                ($lp_pin, $field:ident, $enable:ident) => {{
117                    paste::paste! {
118                        GPIO_LOCK.lock(|| {
119                            RTC_IO::regs()
120                                .$pin_reg
121                                .modify(|_, w|  w.[<$prefix $field>]().bit($enable));
122                        });
123                    }
124                }};
125            )+
126        }
127    };
128}
129
130lp_io_analog! {
131    (GPIO36, 0,  sensor_pads(),    sense1_, sense1    )
132    (GPIO37, 1,  sensor_pads(),    sense2_, sense2    )
133    (GPIO38, 2,  sensor_pads(),    sense3_, sense3    )
134    (GPIO39, 3,  sensor_pads(),    sense4_, sense4    )
135    (GPIO34, 4,  adc_pad(),        adc1_,   adc1      )
136    (GPIO35, 5,  adc_pad(),        adc2_,   adc2      )
137    (GPIO25, 6,  pad_dac1(),       "",      pdac1     )
138    (GPIO26, 7,  pad_dac2(),       "",      pdac2     )
139    (GPIO33, 8,  xtal_32k_pad(),   x32n_,   x32n      )
140    (GPIO32, 9,  xtal_32k_pad(),   x32p_,   x32p      )
141    (GPIO4,  10, touch_pad0(),     "",      touch_pad0)
142    (GPIO0,  11, touch_pad1(),     "",      touch_pad1)
143    (GPIO2,  12, touch_pad2(),     "",      touch_pad2)
144    (GPIO15, 13, touch_pad3(),     "",      touch_pad3)
145    (GPIO13, 14, touch_pad4(),     "",      touch_pad4)
146    (GPIO12, 15, touch_pad5(),     "",      touch_pad5)
147    (GPIO14, 16, touch_pad6(),     "",      touch_pad6)
148    (GPIO27, 17, touch_pad7(),     "",      touch_pad7)
149}
150
151macro_rules! set_pad_field {
152    ($pin:ident, $field:ident, $enable:ident) => {{
153        match $pin {
154            0 => set_one_pad_field!(0, $field, $enable),
155            1 => set_one_pad_field!(1, $field, $enable),
156            2 => set_one_pad_field!(2, $field, $enable),
157            3 => set_one_pad_field!(3, $field, $enable),
158            4 => set_one_pad_field!(4, $field, $enable),
159            5 => set_one_pad_field!(5, $field, $enable),
160            6 => set_one_pad_field!(6, $field, $enable),
161            7 => set_one_pad_field!(7, $field, $enable),
162            8 => set_one_pad_field!(8, $field, $enable),
163            9 => set_one_pad_field!(9, $field, $enable),
164            10 => set_one_pad_field!(10, $field, $enable),
165            11 => set_one_pad_field!(11, $field, $enable),
166            12 => set_one_pad_field!(12, $field, $enable),
167            13 => set_one_pad_field!(13, $field, $enable),
168            14 => set_one_pad_field!(14, $field, $enable),
169            15 => set_one_pad_field!(15, $field, $enable),
170            16 => set_one_pad_field!(16, $field, $enable),
171            17 => set_one_pad_field!(17, $field, $enable),
172            _ => unreachable!(),
173        }
174    }};
175}
176
177macro_rules! set_pull_field {
178    ($pin:ident, $field:ident, $enable:ident) => {{
179        match $pin {
180            0..=5 => warn!("Pin {} does not have PU/PD resistors", $pin),
181            6 => set_one_pad_field!(6, $field, $enable),
182            7 => set_one_pad_field!(7, $field, $enable),
183            8 => set_one_pad_field!(8, $field, $enable),
184            9 => set_one_pad_field!(9, $field, $enable),
185            10 => set_one_pad_field!(10, $field, $enable),
186            11 => set_one_pad_field!(11, $field, $enable),
187            12 => set_one_pad_field!(12, $field, $enable),
188            13 => set_one_pad_field!(13, $field, $enable),
189            14 => set_one_pad_field!(14, $field, $enable),
190            15 => set_one_pad_field!(15, $field, $enable),
191            16 => set_one_pad_field!(16, $field, $enable),
192            17 => set_one_pad_field!(17, $field, $enable),
193            _ => unreachable!(),
194        }
195    }};
196}
197
198/// Returns the bit of the pad of `gpio` in the hold register of the digital pads.
199///
200/// The register covers the pads of the digital supply, and lists them in its own order. The digital
201/// supply feeds no other pad, so no other pad reaches this function.
202fn digital_hold_bit(gpio: u8) -> Option<u8> {
203    Some(match gpio {
204        1 => 1,
205        3 => 0,
206        5 => 8,
207        6..=11 => gpio - 4,
208        16..=19 | 21..=23 => gpio - 7,
209        _ => return None,
210    })
211}
212
213/// Takes or releases the hold of the pad of `gpio`.
214pub(crate) fn digital_pad_hold(gpio: u8, enable: bool) {
215    let Some(bit) = digital_hold_bit(gpio) else {
216        return;
217    };
218
219    let mask = 1 << bit;
220    GPIO_LOCK.lock(|| {
221        RTC_IO::regs().dig_pad_hold().modify(|r, w| unsafe {
222            let bits = r.dig_pad_hold().bits();
223            w.dig_pad_hold()
224                .bits(if enable { bits | mask } else { bits & !mask })
225        });
226    });
227}
228
229/// Returns whether something holds the pad of `gpio`.
230pub(crate) fn is_digital_pad_held(gpio: u8) -> bool {
231    let Some(bit) = digital_hold_bit(gpio) else {
232        return false;
233    };
234
235    RTC_IO::regs().dig_pad_hold().read().dig_pad_hold().bits() & (1 << bit) != 0
236}
237
238pub(crate) fn apply_wakeup(lp: u8, wakeup: bool, level: crate::gpio::Level) {
239    RTC_IO::regs().pin(lp as usize).modify(|_, w| unsafe {
240        w.wakeup_enable().bit(wakeup);
241        w.int_type().bits(crate::gpio::lp_io::wake_trigger(level))
242    });
243}
244
245/// Returns the pads whose per-pin wakeup path triggered, as a mask of low-power numbers.
246pub(crate) fn wakeup_status() -> u32 {
247    RTC_IO::regs().status().read().int().bits()
248}
249
250/// Clears [`wakeup_status`], so that it reports the next sleep and no earlier sleep.
251pub(crate) fn clear_wakeup_status() {
252    RTC_IO::regs()
253        .status_w1tc()
254        .write(|w| unsafe { w.status_int_w1tc().bits(ALL_PADS) });
255}
256
257/// Returns the pads that can wake the chip through the per-pin path, as a mask of low-power
258/// numbers.
259pub(crate) fn wakeup_enabled_mask() -> u32 {
260    let mut mask = 0;
261    let mut pads = ALL_PADS;
262    while pads != 0 {
263        let lp = pads.trailing_zeros();
264        pads &= !(1 << lp);
265
266        if RTC_IO::regs()
267            .pin(lp as usize)
268            .read()
269            .wakeup_enable()
270            .bit_is_set()
271        {
272            mask |= 1 << lp;
273        }
274    }
275    mask
276}
277
278#[expect(dead_code)]
279pub(crate) fn init_pin(lp: u8, input_enable: bool) -> u8 {
280    set_config(lp, input_enable, true, LpFunction::LP_GPIO);
281    lp
282}
283
284pub(crate) fn output_enable(lp: u8, enable: bool) {
285    if enable {
286        RTC_IO::regs()
287            .enable_w1ts()
288            .write(|w| unsafe { w.enable_w1ts().bits(1 << lp) });
289    } else {
290        RTC_IO::regs()
291            .enable_w1tc()
292            .write(|w| unsafe { w.enable_w1tc().bits(1 << lp) });
293    }
294}
295
296#[expect(dead_code)]
297pub(crate) fn input_enable(lp: u8, enable: bool) {
298    set_pad_field!(lp, fun_ie, enable);
299}
300
301pub(crate) fn pullup_enable(lp: u8, enable: bool) {
302    set_pull_field!(lp, rue, enable);
303}
304
305pub(crate) fn pulldown_enable(lp: u8, enable: bool) {
306    set_pull_field!(lp, rde, enable);
307}
308
309// The pad driver bit is part of the digital GPIO peripheral, and this chip gives a pad a different
310// low-power number, so this function takes the digital number.
311pub(crate) fn set_open_drain_output(gpio: u8, enable: bool) {
312    GPIO::regs()
313        .pin(gpio as usize)
314        .modify(|_, w| w.pad_driver().bit(enable));
315}