Skip to main content

esp_hal/gpio/lp_io/low_level/
v3.rs

1use crate::{
2    gpio::{AlternateFunction, GPIO_LOCK, Level, LpPin, lp_io::LpFunction},
3    peripherals::{GPIO, IO_MUX, LPWR},
4};
5
6for_each_lp_function! {
7    (($_lp:ident, LP_GPIOn, $pin:literal), $gpio:ident, $_af:ident, $_lp_in:tt $_lp_out:tt) => {
8        #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
9        impl LpPin for crate::peripherals::$gpio<'_> {
10            fn lp_number(&self) -> u8 {
11                $pin
12            }
13        }
14    };
15
16    // The wakeup register and the hold register have one named field for each pad, and no index, so
17    // these functions select the field by the low-power number.
18    (LP_GPIOn $( (($_lp:ident, LP_GPIOn, $pin:literal), $gpio:ident, $_af:ident, $_lp_in:tt $_lp_out:tt) ),*) => {
19        paste::paste! {
20            pub(crate) fn apply_wakeup(lp: u8, wakeup: bool, level: Level) {
21                let trigger = crate::gpio::lp_io::wake_trigger(level);
22
23                let gpio_wakeup = cfg_select! {
24                    esp32c2 => LPWR::regs().cntl_gpio_wakeup(),
25                    esp32c3 => LPWR::regs().gpio_wakeup(),
26                };
27
28                gpio_wakeup.modify(|_, w| unsafe {
29                    match lp {
30                        $(
31                            $pin => {
32                                w.[<gpio_pin $pin _wakeup_enable>]().bit(wakeup);
33                                w.[<gpio_pin $pin _int_type>]().bits(trigger)
34                            }
35                        )*
36                        _ => unreachable!(),
37                    }
38                });
39            }
40
41            pub(crate) fn pad_hold(lp: u8, enable: bool) {
42                GPIO_LOCK.lock(|| {
43                    LPWR::regs().pad_hold().modify(|_, w| {
44                        match lp {
45                            $( $pin => w.[<gpio_pin $pin _hold>]().bit(enable), )*
46                            _ => unreachable!(),
47                        }
48                    });
49                });
50            }
51
52            /// Returns whether something holds the pad.
53            pub(crate) fn is_pad_held(lp: u8) -> bool {
54                let r = LPWR::regs().pad_hold().read();
55                match lp {
56                    $( $pin => r.[<gpio_pin $pin _hold>]().bit_is_set(), )*
57                    _ => unreachable!(),
58                }
59            }
60
61            /// Returns the pads that can wake the chip through the per-pin path, as a mask of
62            /// low-power numbers.
63            #[cfg(sleep_driver_supported)]
64            pub(crate) fn wakeup_enabled_mask() -> u32 {
65                let gpio_wakeup = cfg_select! {
66                    esp32c2 => LPWR::regs().cntl_gpio_wakeup().read(),
67                    esp32c3 => LPWR::regs().gpio_wakeup().read(),
68                };
69
70                let mut mask = 0;
71                $(
72                    if gpio_wakeup.[<gpio_pin $pin _wakeup_enable>]().bit_is_set() {
73                        mask |= 1 << $pin;
74                    }
75                )*
76                mask
77            }
78        }
79    };
80}
81
82/// Takes or releases the hold of the pad of `gpio`.
83///
84/// The register numbers the pads of the digital supply the way the digital registers do, and the
85/// low-power pads have their own register.
86pub(crate) fn digital_pad_hold(gpio: u8, enable: bool) {
87    let mask = 1 << gpio;
88    GPIO_LOCK.lock(|| {
89        LPWR::regs().dig_pad_hold().modify(|r, w| unsafe {
90            let bits = r.dig_pad_hold().bits();
91            w.dig_pad_hold()
92                .bits(if enable { bits | mask } else { bits & !mask })
93        });
94    });
95}
96
97/// Returns whether something holds the pad of `gpio`.
98pub(crate) fn is_digital_pad_held(gpio: u8) -> bool {
99    LPWR::regs().dig_pad_hold().read().dig_pad_hold().bits() & (1 << gpio) != 0
100}
101
102/// Configures the pad.
103///
104/// The low-power domain reaches the pad through the digital IO MUX. There is thus no low-power
105/// function to select, and the low-power number is the digital pin number.
106pub(crate) fn set_config(lp: u8, input_enable: bool, _mux: bool, _func: LpFunction) {
107    IO_MUX::regs().gpio(lp as usize).modify(|_, w| unsafe {
108        w.slp_sel().bit(false);
109        w.mcu_sel().bits(AlternateFunction::GPIO as u8);
110        w.fun_ie().bit(input_enable)
111    });
112}
113
114#[expect(dead_code)]
115pub(crate) fn init_pin(lp: u8, enable_input: bool) -> u8 {
116    input_enable(lp, enable_input);
117    lp
118}
119
120#[expect(dead_code)]
121pub(crate) fn output_enable(lp: u8, enable: bool) {
122    if enable {
123        GPIO::regs()
124            .enable_w1ts()
125            .write(|w| unsafe { w.enable_w1ts().bits(1 << lp) });
126    } else {
127        GPIO::regs()
128            .enable_w1tc()
129            .write(|w| unsafe { w.enable_w1tc().bits(1 << lp) });
130    }
131}
132
133pub(crate) fn input_enable(lp: u8, enable: bool) {
134    IO_MUX::regs()
135        .gpio(lp as usize)
136        .modify(|_, w| w.fun_ie().bit(enable));
137}
138
139#[expect(dead_code)]
140pub(crate) fn set_open_drain_output(lp: u8, enable: bool) {
141    GPIO::regs()
142        .pin(lp as usize)
143        .modify(|_, w| w.pad_driver().bit(enable));
144}