Skip to main content

esp_hal/gpio/lp_io/low_level/
esp32p4.rs

1use crate::{
2    gpio::{
3        GPIO_LOCK,
4        LpPin,
5        lp_io::{LpFunction, LpInputSignal, LpOutputSignal},
6    },
7    peripherals::{HP_SYS, LP_GPIO, LP_IO_MUX},
8};
9
10for_each_lp_function! {
11    (($_lp_pin_name:ident, LP_GPIOn, $lp_pin:literal), $gpio:ident, $_af:ident, $_lp_in:tt $_lp_out:tt) => {
12        impl LpPin for crate::peripherals::$gpio<'_> {
13            fn lp_number(&self) -> u8 {
14                $lp_pin
15            }
16        }
17    };
18
19    // Each pad has its own signal table, so this code selects the table by the low-power number.
20    (LP_GPIOn $((
21        ($_lp_pin_name:ident, LP_GPIOn, $lp_pin:literal),
22        $gpio:ident,
23        $_af:ident,
24        ($( $lp_in_af:ident => $lp_in_signal:ident )*)
25        ($( $lp_out_af:ident => $lp_out_signal:ident )*)
26    )),*) => {
27        pub(crate) fn input_signals(lp: u8) -> &'static [(LpFunction, LpInputSignal)] {
28            match lp {
29                $( $lp_pin => &[$( (LpFunction::$lp_in_af, LpInputSignal::$lp_in_signal) ),*], )*
30                _ => unreachable!(),
31            }
32        }
33
34        pub(crate) fn output_signals(lp: u8) -> &'static [(LpFunction, LpOutputSignal)] {
35            match lp {
36                $( $lp_pin => &[$( (LpFunction::$lp_out_af, LpOutputSignal::$lp_out_signal) ),*], )*
37                _ => unreachable!(),
38            }
39        }
40    };
41}
42
43pub(crate) fn set_config(lp: u8, input_enable: bool, mux: bool, func: LpFunction) {
44    if mux {
45        LP_GPIO::regs()
46            .clk_en()
47            .modify(|_, w| w.reg_clk_en().set_bit());
48        while LP_GPIO::regs().clk_en().read().reg_clk_en().bit_is_clear() {}
49    }
50
51    LP_IO_MUX::regs().pad(lp as usize).modify(|_, w| unsafe {
52        w.mux_sel().bit(mux);
53        w.fun_ie().bit(input_enable);
54        w.fun_sel().bits(func as u8)
55    });
56}
57
58pub(crate) fn init_pin(lp: u8, input_enable: bool) -> u8 {
59    set_config(lp, input_enable, true, LpFunction::LP_GPIO);
60    lp
61}
62
63pub(crate) fn output_enable(lp: u8, enable: bool) {
64    if enable {
65        LP_GPIO::regs()
66            .enable_w1ts()
67            .write(|w| unsafe { w.bits(1 << lp) });
68    } else {
69        LP_GPIO::regs()
70            .enable_w1tc()
71            .write(|w| unsafe { w.bits(1 << lp) });
72    }
73}
74
75pub(crate) fn input_enable(lp: u8, enable: bool) {
76    LP_IO_MUX::regs()
77        .pad(lp as usize)
78        .modify(|_, w| w.slp_ie().bit(enable));
79}
80
81pub(crate) fn pullup_enable(lp: u8, enable: bool) {
82    LP_IO_MUX::regs()
83        .pad(lp as usize)
84        .modify(|_, w| w.rue().bit(enable));
85}
86
87pub(crate) fn pulldown_enable(lp: u8, enable: bool) {
88    LP_IO_MUX::regs()
89        .pad(lp as usize)
90        .modify(|_, w| w.rde().bit(enable));
91}
92
93pub(crate) fn pad_hold(lp: u8, enable: bool) {
94    let mask = 1 << lp;
95    GPIO_LOCK.lock(|| {
96        LP_IO_MUX::regs().lp_pad_hold().modify(|r, w| unsafe {
97            let bits = r.reg_lp_gpio_hold().bits();
98            w.reg_lp_gpio_hold()
99                .bits(if enable { bits | mask } else { bits & !mask })
100        });
101    });
102}
103
104/// Returns whether something holds the pad.
105pub(crate) fn is_pad_held(lp: u8) -> bool {
106    LP_IO_MUX::regs()
107        .lp_pad_hold()
108        .read()
109        .reg_lp_gpio_hold()
110        .bits()
111        & (1 << lp)
112        != 0
113}
114
115/// Takes or releases the hold of the pad of `gpio`.
116///
117/// The registers start at the first pad of the digital supply, which follows the 16 low-power pads.
118pub(crate) fn digital_pad_hold(gpio: u8, enable: bool) {
119    let Some(bit) = gpio.checked_sub(16) else {
120        return;
121    };
122
123    GPIO_LOCK.lock(|| {
124        if bit < 32 {
125            let mask = 1 << bit;
126            HP_SYS::regs().gpio_o_hold_ctrl0().modify(|r, w| unsafe {
127                let bits = r.reg_gpio_0_hold_low().bits();
128                w.reg_gpio_0_hold_low()
129                    .bits(if enable { bits | mask } else { bits & !mask })
130            });
131        } else {
132            let mask = 1 << (bit - 32);
133            HP_SYS::regs().gpio_o_hold_ctrl1().modify(|r, w| unsafe {
134                let bits = r.reg_gpio_0_hold_high().bits();
135                w.reg_gpio_0_hold_high()
136                    .bits(if enable { bits | mask } else { bits & !mask })
137            });
138        }
139    });
140}
141
142/// Returns whether something holds the pad of `gpio`.
143pub(crate) fn is_digital_pad_held(gpio: u8) -> bool {
144    let Some(bit) = gpio.checked_sub(16) else {
145        return false;
146    };
147
148    if bit < 32 {
149        HP_SYS::regs()
150            .gpio_o_hold_ctrl0()
151            .read()
152            .reg_gpio_0_hold_low()
153            .bits()
154            & (1 << bit)
155            != 0
156    } else {
157        HP_SYS::regs()
158            .gpio_o_hold_ctrl1()
159            .read()
160            .reg_gpio_0_hold_high()
161            .bits()
162            & (1 << (bit - 32))
163            != 0
164    }
165}
166
167// The pad driver bit is part of the digital GPIO peripheral, so this function takes the digital
168// number.
169pub(crate) fn set_open_drain_output(gpio: u8, enable: bool) {
170    crate::peripherals::GPIO::regs()
171        .pin(gpio as usize)
172        .modify(|_, w| w.pad_driver().bit(enable));
173}
174
175#[cfg(lp_i2c_master_driver_supported)]
176pub(crate) fn reset_pin(lp: u8) {
177    LP_GPIO::regs()
178        .clk_en()
179        .modify(|_, w| w.reg_clk_en().set_bit());
180    while LP_GPIO::regs().clk_en().read().reg_clk_en().bit_is_clear() {}
181
182    output_enable(lp, false);
183    // On this chip, each low-power pad has the same digital pin number.
184    set_open_drain_output(lp, false);
185
186    // Resistors, input enable, the pad's LP function and whether it is muxed to the LP IO at all
187    // are all held in this register. Resetting it hands the pad back to the digital IO MUX.
188    LP_IO_MUX::regs().pad(lp as usize).reset();
189}