Skip to main content

esp_hal/gpio/lp_io/low_level/
esp32h2.rs

1use crate::{
2    gpio::{AlternateFunction, GPIO_LOCK, LpPin, lp_io::LpFunction},
3    peripherals::{GPIO, IO_MUX, LP_AON},
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
17// One register holds every pad, and it numbers the pads the way the digital registers do.
18pub(crate) fn pad_hold(lp: u8, enable: bool) {
19    digital_pad_hold(lp_pin_to_gpio(lp), enable);
20}
21
22/// Returns whether something holds the pad.
23pub(crate) fn is_pad_held(lp: u8) -> bool {
24    is_digital_pad_held(lp_pin_to_gpio(lp))
25}
26
27/// Takes or releases the hold of the pad of `gpio`.
28pub(crate) fn digital_pad_hold(gpio: u8, enable: bool) {
29    let mask = 1 << gpio;
30    GPIO_LOCK.lock(|| {
31        LP_AON::regs().gpio_hold0().modify(|r, w| unsafe {
32            let bits = r.gpio_hold0().bits();
33            w.gpio_hold0()
34                .bits(if enable { bits | mask } else { bits & !mask })
35        });
36    });
37}
38
39/// Returns whether something holds the pad of `gpio`.
40pub(crate) fn is_digital_pad_held(gpio: u8) -> bool {
41    LP_AON::regs().gpio_hold0().read().gpio_hold0().bits() & (1 << gpio) != 0
42}
43
44/// Configures the pad.
45///
46/// The low-power domain reaches the pad through the digital IO MUX. There is thus no low-power
47/// function to select.
48pub(crate) fn set_config(lp: u8, input_enable: bool, _mux: bool, _func: LpFunction) {
49    IO_MUX::regs()
50        .gpio(lp_pin_to_gpio(lp) as usize)
51        .modify(|_, w| unsafe {
52            w.slp_sel().bit(false);
53            w.mcu_sel().bits(AlternateFunction::GPIO as u8);
54            w.fun_ie().bit(input_enable)
55        });
56}
57
58#[expect(dead_code)]
59pub(crate) fn init_pin(lp: u8, enable_input: bool) -> u8 {
60    input_enable(lp, enable_input);
61    lp
62}
63
64fn lp_pin_to_gpio(lp: u8) -> u8 {
65    lp + 7
66}
67
68#[expect(dead_code)]
69pub(crate) fn output_enable(lp: u8, enable: bool) {
70    let gpio = lp_pin_to_gpio(lp);
71    if enable {
72        GPIO::regs()
73            .enable_w1ts()
74            .write(|w| unsafe { w.enable_w1ts().bits(1 << gpio) });
75    } else {
76        GPIO::regs()
77            .enable_w1tc()
78            .write(|w| unsafe { w.enable_w1tc().bits(1 << gpio) });
79    }
80}
81
82pub(crate) fn input_enable(lp: u8, enable: bool) {
83    IO_MUX::regs()
84        .gpio(lp_pin_to_gpio(lp) as usize)
85        .modify(|_, w| w.fun_ie().bit(enable));
86}
87
88#[expect(dead_code)]
89pub(crate) fn pullup_enable(lp: u8, enable: bool) {
90    IO_MUX::regs()
91        .gpio(lp_pin_to_gpio(lp) as usize)
92        .modify(|_, w| w.fun_wpu().bit(enable));
93}
94
95#[expect(dead_code)]
96pub(crate) fn pulldown_enable(lp: u8, enable: bool) {
97    IO_MUX::regs()
98        .gpio(lp_pin_to_gpio(lp) as usize)
99        .modify(|_, w| w.fun_wpd().bit(enable));
100}
101
102// The pad driver bit is part of the digital GPIO peripheral, and this chip gives a pad a different
103// low-power number, so this function takes the digital number.
104#[expect(dead_code)]
105pub(crate) fn set_open_drain_output(gpio: u8, enable: bool) {
106    GPIO::regs()
107        .pin(gpio as usize)
108        .modify(|_, w| w.pad_driver().bit(enable));
109}