esp_hal/gpio/lp_io/low_level/
v2.rs1use crate::{
2 gpio::{GPIO_LOCK, Level, LpPin, lp_io::LpFunction},
3 peripherals::{GPIO, LPWR, RTC_IO},
4};
5
6#[rustfmt::skip]
7macro_rules! pin_reg {
8 (GPIO0) => { RTC_IO::regs().touch_pad(0) };
9 (GPIO1) => { RTC_IO::regs().touch_pad(1) };
10 (GPIO2) => { RTC_IO::regs().touch_pad(2) };
11 (GPIO3) => { RTC_IO::regs().touch_pad(3) };
12 (GPIO4) => { RTC_IO::regs().touch_pad(4) };
13 (GPIO5) => { RTC_IO::regs().touch_pad(5) };
14 (GPIO6) => { RTC_IO::regs().touch_pad(6) };
15 (GPIO7) => { RTC_IO::regs().touch_pad(7) };
16 (GPIO8) => { RTC_IO::regs().touch_pad(8) };
17 (GPIO9) => { RTC_IO::regs().touch_pad(9) };
18 (GPIO10) => { RTC_IO::regs().touch_pad(10) };
19 (GPIO11) => { RTC_IO::regs().touch_pad(11) };
20 (GPIO12) => { RTC_IO::regs().touch_pad(12) };
21 (GPIO13) => { RTC_IO::regs().touch_pad(13) };
22 (GPIO14) => { RTC_IO::regs().touch_pad(14) };
23 (GPIO15) => { RTC_IO::regs().xtal_32p_pad() };
24 (GPIO16) => { RTC_IO::regs().xtal_32n_pad() };
25 (GPIO17) => { RTC_IO::regs().pad_dac1() };
26 (GPIO18) => { RTC_IO::regs().pad_dac2() };
27 (GPIO19) => { RTC_IO::regs().rtc_pad19() };
28 (GPIO20) => { RTC_IO::regs().rtc_pad20() };
29 (GPIO21) => { RTC_IO::regs().rtc_pad21() };
30}
31
32#[rustfmt::skip]
33macro_rules! hold_field {
34 ($reg:ident, GPIO0) => { $reg.touch_pad0() };
35 ($reg:ident, GPIO1) => { $reg.touch_pad1() };
36 ($reg:ident, GPIO2) => { $reg.touch_pad2() };
37 ($reg:ident, GPIO3) => { $reg.touch_pad3() };
38 ($reg:ident, GPIO4) => { $reg.touch_pad4() };
39 ($reg:ident, GPIO5) => { $reg.touch_pad5() };
40 ($reg:ident, GPIO6) => { $reg.touch_pad6() };
41 ($reg:ident, GPIO7) => { $reg.touch_pad7() };
42 ($reg:ident, GPIO8) => { $reg.touch_pad8() };
43 ($reg:ident, GPIO9) => { $reg.touch_pad9() };
44 ($reg:ident, GPIO10) => { $reg.touch_pad10() };
45 ($reg:ident, GPIO11) => { $reg.touch_pad11() };
46 ($reg:ident, GPIO12) => { $reg.touch_pad12() };
47 ($reg:ident, GPIO13) => { $reg.touch_pad13() };
48 ($reg:ident, GPIO14) => { $reg.touch_pad14() };
49 ($reg:ident, GPIO15) => { $reg.x32p() };
50 ($reg:ident, GPIO16) => { $reg.x32n() };
51 ($reg:ident, GPIO17) => { $reg.pdac1() };
52 ($reg:ident, GPIO18) => { $reg.pdac2() };
53 ($reg:ident, GPIO19) => { $reg.pad19() };
54 ($reg:ident, GPIO20) => { $reg.pad20() };
55 ($reg:ident, GPIO21) => { $reg.pad21() };
56}
57
58for_each_lp_function!(
60 (LP_GPIOn $(
61 (($_lp:ident, LP_GPIOn, $n:literal), $gpio:ident, $_af:ident, $_lp_in:tt $_lp_out:tt)
62 ),*) => {
63 macro_rules! with_pin_reg {
64 ($pin:expr, |$reg:ident| $code:expr) => {{
65 match $pin {
66 $(
67 $n => {
68 let $reg = pin_reg!($gpio);
69 $code
70 }
71 )*
72 _ => unreachable!(),
73 }
74 }};
75 }
76 };
77);
78
79for_each_lp_function! {
80 (($_lp:ident, LP_GPIOn, $n:literal), $gpio:ident, $_af:ident, $_lp_in:tt $_lp_out:tt) => {
81 #[cfg_attr(docsrs, doc(cfg(feature = "unstable")))]
82 impl LpPin for crate::peripherals::$gpio<'_> {
83 fn lp_number(&self) -> u8 {
84 $n
85 }
86 }
87 };
88
89 (LP_GPIOn $( (($_lp:ident, LP_GPIOn, $n:literal), $gpio:ident, $_af:ident, $_lp_in:tt $_lp_out:tt) ),*) => {
92 pub(crate) fn pad_hold(lp: u8, enable: bool) {
93 GPIO_LOCK.lock(|| {
94 LPWR::regs().pad_hold().modify(|_, w| {
95 match lp {
96 $( $n => hold_field!(w, $gpio).bit(enable), )*
97 _ => unreachable!(),
98 }
99 });
100 });
101 }
102
103 pub(crate) fn is_pad_held(lp: u8) -> bool {
105 let r = LPWR::regs().pad_hold().read();
106 match lp {
107 $( $n => hold_field!(r, $gpio).bit_is_set(), )*
108 _ => unreachable!(),
109 }
110 }
111
112 #[cfg(sleep_driver_supported)]
114 const ALL_PADS: u32 = 0 $( | 1 << $n )*;
115 };
116}
117
118fn digital_hold_bit(gpio: u8) -> Option<u8> {
123 gpio.checked_sub(21)
124}
125
126pub(crate) fn digital_pad_hold(gpio: u8, enable: bool) {
128 let Some(bit) = digital_hold_bit(gpio) else {
129 return;
130 };
131
132 let mask = 1 << bit;
133 GPIO_LOCK.lock(|| {
134 LPWR::regs().dig_pad_hold().modify(|r, w| unsafe {
135 let bits = r.dig_pad_hold().bits();
136 w.dig_pad_hold()
137 .bits(if enable { bits | mask } else { bits & !mask })
138 });
139 });
140}
141
142pub(crate) fn is_digital_pad_held(gpio: u8) -> bool {
144 let Some(bit) = digital_hold_bit(gpio) else {
145 return false;
146 };
147
148 LPWR::regs().dig_pad_hold().read().dig_pad_hold().bits() & (1 << bit) != 0
149}
150
151pub(crate) fn set_config(lp: u8, input_enable: bool, mux: bool, func: LpFunction) {
152 enable_iomux_clk_gate();
153 with_pin_reg!(lp, |reg| reg.modify(|_, w| unsafe {
154 w.fun_ie().bit(input_enable);
155 w.mux_sel().bit(mux);
156 w.fun_sel().bits(func as u8)
157 }));
158}
159
160pub(crate) fn apply_wakeup(lp: u8, wakeup: bool, level: Level) {
161 RTC_IO::regs().pin(lp as usize).modify(|_, w| unsafe {
162 w.wakeup_enable().bit(wakeup);
163 w.int_type().bits(crate::gpio::lp_io::wake_trigger(level))
164 });
165}
166
167#[cfg(sleep_driver_supported)]
169pub(crate) fn wakeup_status() -> u32 {
170 let status = RTC_IO::regs().rtc_gpio_status().read();
171 cfg_select! {
172 esp32s2 => status.gpio_status_int().bits(),
173 esp32s3 => status.int().bits(),
174 }
175}
176
177#[cfg(sleep_driver_supported)]
180pub(crate) fn wakeup_enabled_mask() -> u32 {
181 let mut mask = 0;
182 let mut pads = ALL_PADS;
183 while pads != 0 {
184 let lp = pads.trailing_zeros();
185 pads &= !(1 << lp);
186
187 if RTC_IO::regs()
188 .pin(lp as usize)
189 .read()
190 .wakeup_enable()
191 .bit_is_set()
192 {
193 mask |= 1 << lp;
194 }
195 }
196 mask
197}
198
199#[cfg(sleep_driver_supported)]
201pub(crate) fn clear_wakeup_status() {
202 RTC_IO::regs().rtc_gpio_status_w1tc().write(|w| unsafe {
203 cfg_select! {
204 esp32s2 => w.gpio_status_int_w1tc().bits(ALL_PADS),
205 esp32s3 => w.rtc_gpio_status_int_w1tc().bits(ALL_PADS),
206 }
207 });
208}
209
210for_each_analog_function! {
211 (($_ch:ident, ADCn_CHm, $_n:literal, $_m:literal), $gpio:ident) => {
212 impl crate::peripherals::$gpio<'_> {
213 #[cfg(feature = "unstable")]
214 pub(crate) fn set_analog_impl(&self) {
215 use crate::gpio::{LpPin, Pin};
216
217 enable_iomux_clk_gate();
218
219 output_enable(self.lp_number(), false);
220 set_open_drain_output(self.number(), false);
221
222 pin_reg!($gpio).modify(|_, w| {
223 w.fun_ie().clear_bit();
224 w.mux_sel().set_bit();
225 unsafe { w.fun_sel().bits(LpFunction::LP_GPIO as u8) };
226 w.rue().bit(false);
227 w.rde().bit(false)
228 });
229 }
230 }
231 };
232}
233
234pub(crate) fn init_pin(lp: u8, input_enable: bool) -> u8 {
235 set_config(lp, input_enable, true, LpFunction::LP_GPIO);
236 lp
237}
238
239pub(crate) fn output_enable(lp: u8, enable: bool) {
240 if enable {
241 RTC_IO::regs()
242 .rtc_gpio_enable_w1ts()
243 .write(|w| unsafe { w.rtc_gpio_enable_w1ts().bits(1 << lp) });
244 } else {
245 RTC_IO::regs()
246 .enable_w1tc()
247 .write(|w| unsafe { w.enable_w1tc().bits(1 << lp) });
248 }
249}
250
251pub(crate) fn input_enable(lp: u8, enable: bool) {
252 with_pin_reg!(lp, |reg| reg.modify(|_, w| w.fun_ie().bit(enable)));
253}
254
255pub(crate) fn pullup_enable(lp: u8, enable: bool) {
256 with_pin_reg!(lp, |reg| reg.modify(|_, w| w.rue().bit(enable)));
257}
258
259pub(crate) fn pulldown_enable(lp: u8, enable: bool) {
260 with_pin_reg!(lp, |reg| reg.modify(|_, w| w.rde().bit(enable)));
261}
262
263pub(crate) fn set_open_drain_output(gpio: u8, enable: bool) {
266 GPIO::regs()
267 .pin(gpio as usize)
268 .modify(|_, w| w.pad_driver().bit(enable));
269}
270
271#[cfg(lp_i2c_master_driver_supported)]
272pub(crate) fn reset_pin(lp: u8) {
273 output_enable(lp, false);
274 set_open_drain_output(lp, false);
276
277 enable_iomux_clk_gate();
280 with_pin_reg!(lp, |reg| reg.reset());
281}
282
283fn enable_iomux_clk_gate() {
284 cfg_select! {
285 esp32s2 => {
286 crate::peripherals::SENS::regs()
287 .sar_io_mux_conf()
288 .modify(|_, w| w.iomux_clk_gate_en().set_bit());
289 }
290 esp32s3 => crate::system::PeripheralClockControl::set_enabled_untracked(
291 crate::system::Peripheral::RtcIoMux,
292 true,
293 ),
294 }
295}