esp_hal/gpio/lp_io/low_level/
v4.rs1use crate::{
2 gpio::{GPIO_LOCK, LpPin, lp_io::LpFunction},
3 peripherals::{LP_AON, LP_PERI},
4};
5
6cfg_select! {
7 esp32c6 => {
8 use crate::peripherals::{LP_IO as LP_GPIO, LP_IO as LP_IO_MUX};
9 }
10 any(esp32c5, esp32c61, esp32h4) => {
11 #[cfg(any(
13 ulp_riscv_driver_supported,
14 lp_io_has_gpio_matrix,
15 lp_i2c_master_driver_supported,
16 ))]
17 use crate::peripherals::LP_GPIO;
18 use crate::peripherals::LP_IO_MUX;
19 }
20}
21
22for_each_lp_function! {
23 (($_lp:ident, LP_GPIOn, $pin:literal), $gpio:ident, $_af:ident, $_lp_in:tt $_lp_out:tt) => {
24 #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
25 impl LpPin for crate::peripherals::$gpio<'_> {
26 fn lp_number(&self) -> u8 {
27 $pin
28 }
29 }
30 };
31}
32
33pub(crate) fn pad_hold(lp: u8, enable: bool) {
36 digital_pad_hold(lp, enable);
37}
38
39pub(crate) fn is_pad_held(lp: u8) -> bool {
41 is_digital_pad_held(lp)
42}
43
44pub(crate) fn digital_pad_hold(gpio: u8, enable: bool) {
46 let update = |bits: u32, mask: u32| if enable { bits | mask } else { bits & !mask };
47 GPIO_LOCK.lock(|| {
48 if gpio < 32 {
49 LP_AON::regs()
50 .gpio_hold0()
51 .modify(|r, w| unsafe { w.bits(update(r.bits(), 1 << gpio)) });
52 } else {
53 LP_AON::regs()
54 .gpio_hold1()
55 .modify(|r, w| unsafe { w.bits(update(r.bits(), 1 << (gpio - 32))) });
56 }
57 });
58}
59
60pub(crate) fn is_digital_pad_held(gpio: u8) -> bool {
62 if gpio < 32 {
63 LP_AON::regs().gpio_hold0().read().bits() & (1 << gpio) != 0
64 } else {
65 LP_AON::regs().gpio_hold1().read().bits() & (1 << (gpio - 32)) != 0
66 }
67}
68
69fn enable_io_clock() {
72 LP_PERI::regs()
73 .clk_en()
74 .modify(|_, w| w.lp_io_ck_en().set_bit());
75 while LP_PERI::regs().clk_en().read().lp_io_ck_en().bit_is_clear() {}
76}
77
78pub(crate) fn set_config(lp: u8, input_enable: bool, mux: bool, func: LpFunction) {
79 enable_io_clock();
80
81 let mask = 1 << lp;
82 GPIO_LOCK.lock(|| {
83 LP_AON::regs().gpio_mux().modify(|r, w| unsafe {
84 let bits = r.sel().bits();
85 w.sel().bits(if mux { bits | mask } else { bits & !mask })
86 });
87 });
88
89 LP_IO_MUX::regs().gpio(lp as usize).modify(|_, w| unsafe {
90 w.slp_sel().bit(false);
91 w.fun_ie().bit(input_enable);
92 w.mcu_ie().bit(input_enable);
93 w.mcu_sel().bits(func as u8)
94 });
95}
96
97#[cfg(any(ulp_riscv_driver_supported, lp_io_has_gpio_matrix))]
98pub(crate) fn init_pin(lp: u8, input_enable: bool) -> u8 {
99 set_config(lp, input_enable, true, LpFunction::LP_GPIO);
100 lp
101}
102
103#[cfg(any(
104 ulp_riscv_driver_supported,
105 lp_io_has_gpio_matrix,
106 lp_i2c_master_driver_supported,
107))]
108pub(crate) fn output_enable(lp: u8, enable: bool) {
109 if enable {
110 LP_GPIO::regs()
111 .out_enable_w1ts()
112 .write(|w| unsafe { w.enable_w1ts().bits(1 << lp) });
113 } else {
114 LP_GPIO::regs()
115 .out_enable_w1tc()
116 .write(|w| unsafe { w.enable_w1tc().bits(1 << lp) });
117 }
118}
119
120#[cfg(any(ulp_riscv_driver_supported, lp_io_has_gpio_matrix))]
121pub(crate) fn input_enable(lp: u8, enable: bool) {
122 LP_IO_MUX::regs().gpio(lp as usize).modify(|_, w| {
123 w.fun_ie().bit(enable);
124 w.mcu_ie().bit(enable)
125 });
126}
127
128pub(crate) fn pullup_enable(lp: u8, enable: bool) {
129 enable_io_clock();
130 LP_IO_MUX::regs()
131 .gpio(lp as usize)
132 .modify(|_, w| w.fun_wpu().bit(enable));
133}
134
135pub(crate) fn pulldown_enable(lp: u8, enable: bool) {
136 enable_io_clock();
137 LP_IO_MUX::regs()
138 .gpio(lp as usize)
139 .modify(|_, w| w.fun_wpd().bit(enable));
140}
141
142#[cfg(any(
145 ulp_riscv_driver_supported,
146 lp_io_has_gpio_matrix,
147 lp_i2c_master_driver_supported,
148))]
149pub(crate) fn set_open_drain_output(lp: u8, enable: bool) {
150 LP_GPIO::regs()
151 .pin(lp as usize)
152 .modify(|_, w| w.pad_driver().bit(enable));
153}
154
155#[cfg(lp_i2c_master_driver_supported)]
156pub(crate) fn reset_pin(lp: u8) {
157 output_enable(lp, false);
158 set_open_drain_output(lp, false);
159
160 LP_IO_MUX::regs().gpio(lp as usize).reset();
162
163 GPIO_LOCK.lock(|| {
165 LP_AON::regs()
166 .gpio_mux()
167 .modify(|r, w| unsafe { w.sel().bits(r.sel().bits() & !(1 << lp)) });
168 });
169}