Skip to main content

esp_hal/gpio/lp_io/low_level/
esp32s31.rs

1#[cfg(lp_uart_driver_supported)]
2use crate::gpio::lp_io::{LpInputSignal, LpOutputSignal};
3use crate::{
4    gpio::{GPIO_LOCK, LpPin, lp_io::LpFunction},
5    pac::lp_io_mux::gpio::W,
6    peripherals::{LP_GPIO, LP_IO_MUX, LP_PERI, LP_SYS},
7};
8
9/// Number of pads that the low-power registers reach. Digital hold bits start after this count.
10const LP_PAD_COUNT: u8 = 8;
11
12for_each_lp_function! {
13    (($_lp_pin_name:ident, LP_GPIOn, $lp_pin:literal), $gpio:ident, $_af:ident, $_lp_in:tt $_lp_out:tt) => {
14        impl LpPin for crate::peripherals::$gpio<'_> {
15            fn lp_number(&self) -> u8 {
16                $lp_pin
17            }
18        }
19    };
20
21    // Each pad has its own signal table, so this code selects the table by the low-power number.
22    (LP_GPIOn $((
23        ($_lp_pin_name:ident, LP_GPIOn, $lp_pin:literal),
24        $gpio:ident,
25        $_af:ident,
26        ($( $lp_in_af:ident => $lp_in_signal:ident )*)
27        ($( $lp_out_af:ident => $lp_out_signal:ident )*)
28    )),*) => {
29        #[cfg(lp_uart_driver_supported)]
30        pub(crate) fn input_signals(lp: u8) -> &'static [(LpFunction, LpInputSignal)] {
31            match lp {
32                $( $lp_pin => &[$( (LpFunction::$lp_in_af, LpInputSignal::$lp_in_signal) ),*], )*
33                _ => unreachable!(),
34            }
35        }
36
37        #[cfg(lp_uart_driver_supported)]
38        pub(crate) fn output_signals(lp: u8) -> &'static [(LpFunction, LpOutputSignal)] {
39            match lp {
40                $( $lp_pin => &[$( (LpFunction::$lp_out_af, LpOutputSignal::$lp_out_signal) ),*], )*
41                _ => unreachable!(),
42            }
43        }
44    };
45}
46
47fn enable_io_clock() {
48    LP_PERI::regs()
49        .iomux_ctrl()
50        .modify(|_, w| w.lp_iomux_clk_en().set_bit());
51    LP_GPIO::regs()
52        .clock_gate()
53        .modify(|_, w| w.clk_en().set_bit());
54    while LP_PERI::regs()
55        .iomux_ctrl()
56        .read()
57        .lp_iomux_clk_en()
58        .bit_is_clear()
59        || LP_GPIO::regs().clock_gate().read().clk_en().bit_is_clear()
60    {}
61}
62
63pub(crate) fn set_config(lp: u8, input_enable: bool, mux: bool, func: LpFunction) {
64    if mux {
65        enable_io_clock();
66    }
67
68    // PAD_MUX_SEL and PAD_HOLD share one register across all LP pads.
69    GPIO_LOCK.lock(|| {
70        LP_SYS::regs().padctrl().modify(|r, w| unsafe {
71            let mut sel = r.pad_mux_sel().bits();
72            if mux {
73                sel |= 1u8 << lp;
74            } else {
75                sel &= !(1u8 << lp);
76            }
77            w.pad_mux_sel().bits(sel)
78        });
79    });
80
81    LP_IO_MUX::regs().gpio(lp as usize).modify(|_, w| {
82        configure_iomux(w, input_enable, func);
83        w
84    });
85}
86
87fn configure_iomux(w: &mut W, input_enable: bool, func: LpFunction) {
88    w.slp_sel().clear_bit();
89    w.fun_ie().bit(input_enable);
90    unsafe { w.mcu_sel().bits(func as u8) };
91}
92
93pub(crate) fn init_pin(lp: u8, input_enable: bool) -> u8 {
94    set_config(lp, input_enable, true, LpFunction::LP_GPIO);
95    lp
96}
97
98#[cfg(all(dac_version = "2", feature = "unstable"))]
99pub(crate) fn init_analog(lp: u8) {
100    enable_io_clock();
101    output_enable(lp, false);
102    // Disconnect peripheral output and output-enable signals from the pad.
103    LP_GPIO::regs().func_out_sel_cfg(lp as usize).reset();
104    LP_IO_MUX::regs().gpio(lp as usize).modify(|_, w| {
105        configure_iomux(w, false, LpFunction::LP_GPIO);
106        w.fun_wpu().clear_bit();
107        w.fun_wpd().clear_bit()
108    });
109
110    // Release the hold with the LP pad configuration ready.
111    let mask = 1u8 << lp;
112    GPIO_LOCK.lock(|| {
113        LP_SYS::regs().padctrl().modify(|r, w| unsafe {
114            w.pad_mux_sel().bits(r.pad_mux_sel().bits() | mask);
115            w.pad_hold().bits(r.pad_hold().bits() & !mask)
116        });
117    });
118}
119
120pub(crate) fn output_enable(lp: u8, enable: bool) {
121    if enable {
122        LP_GPIO::regs()
123            .enable_w1ts()
124            .write(|w| unsafe { w.enable_w1ts().bits(1u8 << lp) });
125    } else {
126        LP_GPIO::regs()
127            .enable_w1tc()
128            .write(|w| unsafe { w.enable_w1tc().bits(1u8 << lp) });
129    }
130}
131
132pub(crate) fn input_enable(lp: u8, enable: bool) {
133    LP_IO_MUX::regs()
134        .gpio(lp as usize)
135        .modify(|_, w| w.fun_ie().bit(enable));
136}
137
138pub(crate) fn pullup_enable(lp: u8, enable: bool) {
139    LP_IO_MUX::regs()
140        .gpio(lp as usize)
141        .modify(|_, w| w.fun_wpu().bit(enable));
142}
143
144pub(crate) fn pulldown_enable(lp: u8, enable: bool) {
145    LP_IO_MUX::regs()
146        .gpio(lp as usize)
147        .modify(|_, w| w.fun_wpd().bit(enable));
148}
149
150pub(crate) fn pad_hold(lp: u8, enable: bool) {
151    GPIO_LOCK.lock(|| {
152        LP_SYS::regs().padctrl().modify(|r, w| unsafe {
153            let mut hold = r.pad_hold().bits();
154            if enable {
155                hold |= 1u8 << lp;
156            } else {
157                hold &= !(1u8 << lp);
158            }
159            w.pad_hold().bits(hold)
160        });
161    });
162}
163
164/// Returns whether something holds the pad.
165pub(crate) fn is_pad_held(lp: u8) -> bool {
166    LP_SYS::regs().padctrl().read().pad_hold().bits() & (1u8 << lp) != 0
167}
168
169/// Takes or releases the hold of the pad of `gpio`.
170///
171/// The digital hold registers start at GPIO8, which is the first pad of the digital supply.
172pub(crate) fn digital_pad_hold(gpio: u8, enable: bool) {
173    let Some(bit) = gpio.checked_sub(LP_PAD_COUNT) else {
174        return;
175    };
176
177    GPIO_LOCK.lock(|| {
178        if bit < 32 {
179            let mask = 1 << bit;
180            LP_SYS::regs().hp_gpio_o_hold_ctrl0().modify(|r, w| unsafe {
181                let bits = r.hp_gpio_0_hold_ctrl0().bits();
182                w.hp_gpio_0_hold_ctrl0()
183                    .bits(if enable { bits | mask } else { bits & !mask })
184            });
185        } else {
186            let mask = 1 << (bit - 32);
187            LP_SYS::regs().hp_gpio_o_hold_ctrl1().modify(|r, w| unsafe {
188                let bits = r.hp_gpio_0_hold_ctrl1().bits();
189                w.hp_gpio_0_hold_ctrl1()
190                    .bits(if enable { bits | mask } else { bits & !mask })
191            });
192        }
193    });
194}
195
196/// Returns whether something holds the pad of `gpio`.
197pub(crate) fn is_digital_pad_held(gpio: u8) -> bool {
198    let Some(bit) = gpio.checked_sub(LP_PAD_COUNT) else {
199        return false;
200    };
201
202    if bit < 32 {
203        LP_SYS::regs()
204            .hp_gpio_o_hold_ctrl0()
205            .read()
206            .hp_gpio_0_hold_ctrl0()
207            .bits()
208            & (1 << bit)
209            != 0
210    } else {
211        LP_SYS::regs()
212            .hp_gpio_o_hold_ctrl1()
213            .read()
214            .hp_gpio_0_hold_ctrl1()
215            .bits()
216            & (1 << (bit - 32))
217            != 0
218    }
219}
220
221pub(crate) fn set_open_drain_output(lp: u8, enable: bool) {
222    LP_GPIO::regs()
223        .pin(lp as usize)
224        .modify(|_, w| w.pad_driver().bit(enable));
225}