Skip to main content

esp_hal/rtc_cntl/sleep/
esp32s2.rs

1use super::SleepKind;
2use crate::{
3    peripherals::{EXTMEM, LPWR, SPI0, SPI1, SYSTEM},
4    rtc_cntl::Rtc,
5    soc::regi2c,
6};
7
8// Approximate mapping of voltages to RTC_CNTL_DBIAS_WAK, RTC_CNTL_DBIAS_SLP,
9// RTC_CNTL_DIG_DBIAS_WAK, RTC_CNTL_DIG_DBIAS_SLP values.
10// Valid if RTC_CNTL_DBG_ATTEN is 0.
11/// Digital bias setting for 0.90V.
12pub const RTC_CNTL_DBIAS_0V90: u8 = 0;
13/// Digital bias setting for 0.95V.
14pub const RTC_CNTL_DBIAS_0V95: u8 = 1;
15/// Digital bias setting for 1.00V.
16pub const RTC_CNTL_DBIAS_1V00: u8 = 2;
17/// Digital bias setting for 1.05V.
18pub const RTC_CNTL_DBIAS_1V05: u8 = 3;
19/// Digital bias setting for 1.10V.
20pub const RTC_CNTL_DBIAS_1V10: u8 = 4;
21/// Digital bias setting for 1.15V.
22pub const RTC_CNTL_DBIAS_1V15: u8 = 5;
23/// Digital bias setting for 1.20V.
24pub const RTC_CNTL_DBIAS_1V20: u8 = 6;
25/// Digital bias setting for 1.25V.
26pub const RTC_CNTL_DBIAS_1V25: u8 = 7;
27/// Default monitor debug attenuation value.
28pub const RTC_CNTL_DBG_ATTEN_MONITOR_DEFAULT: u8 = 0;
29/// ULP co-processor touch start wait time during sleep, set to maximum.
30pub const RTC_CNTL_ULPCP_TOUCH_START_WAIT_IN_SLEEP: u16 = 0xFF;
31/// ULP co-processor touch start wait time default value.
32pub const RTC_CNTL_ULPCP_TOUCH_START_WAIT_DEFAULT: u16 = 0x10;
33/// Default wait time for PLL buffer during startup.
34pub const RTC_CNTL_PLL_BUF_WAIT_DEFAULT: u8 = 20;
35/// Default wait time for CK8M during startup.
36pub const RTC_CNTL_CK8M_WAIT_DEFAULT: u8 = 20;
37/// Default wait time for XTL buffer during startup.
38pub const RTC_CNTL_XTL_BUF_WAIT_DEFAULT: u8 = 100;
39/// Minimum sleep value.
40pub const RTC_CNTL_MIN_SLP_VAL_MIN: u8 = 2;
41/// Deep sleep debug attenuation setting for ultra-low power mode.
42pub const RTC_CNTL_DBG_ATTEN_DEEPSLEEP_DEFAULT: u8 = 15;
43/// Power-up setting for other blocks.
44pub const OTHER_BLOCKS_POWERUP: u8 = 1;
45/// Waits cycles for other blocks.
46pub const OTHER_BLOCKS_WAIT: u16 = 1;
47/// WiFi power-up cycles.
48pub const WIFI_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
49/// WiFi wait cycles.
50pub const WIFI_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
51/// RTC power-up cycles.
52pub const RTC_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
53/// RTC wait cycles.
54pub const RTC_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
55/// DG wrap power-up cycles.
56pub const DG_WRAP_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
57/// DG wrap wait cycles.
58pub const DG_WRAP_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
59/// DG peripheral power-up cycles.
60pub const DG_PERI_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
61/// DG peripheral wait cycles.
62pub const DG_PERI_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
63/// RTC memory power-up cycles.
64pub const RTC_MEM_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
65/// RTC memory wait cycles.
66pub const RTC_MEM_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
67
68bitfield::bitfield! {
69    /// Configuration for the RTC sleep behavior.
70    #[derive(Clone, Copy)]
71    pub struct RtcSleepConfig(u64);
72    impl Debug;
73    /// force normal voltage in sleep mode (digital domain memory)
74    pub lslp_mem_inf_fpu, set_lslp_mem_inf_fpu: 0;
75    /// keep low voltage in sleep mode (even if ULP/touch is used)
76    pub rtc_mem_inf_follow_cpu, set_rtc_mem_inf_follow_cpu: 1;
77    /// power down RTC fast memory
78    pub rtc_fastmem_pd_en, set_rtc_fastmem_pd_en: 2;
79    /// power down RTC slow memory
80    pub rtc_slowmem_pd_en, set_rtc_slowmem_pd_en: 3;
81    /// power down RTC peripherals
82    pub rtc_peri_pd_en, set_rtc_peri_pd_en: 4;
83    /// power down Wifi
84    pub wifi_pd_en, set_wifi_pd_en: 5;
85    /// Powers down Internal 8M oscillator.
86    pub int_8m_pd_en, set_int_8m_pd_en: 6;
87    /// power down digital domain
88    pub deep_slp, set_deep_slp: 8;
89    /// enable WDT flashboot mode
90    pub wdt_flashboot_mod_en, set_wdt_flashboot_mod_en: 9;
91    /// set bias for digital domain, in sleep mode
92    pub u8, dig_dbias_slp, set_dig_dbias_slp: 12, 10;
93    /// set bias for RTC domain, in sleep mode
94    pub u8, rtc_dbias_slp, set_rtc_dbias_slp: 16, 13;
95    /// circuit control parameter, in monitor mode
96    pub bias_sleep_monitor, set_bias_sleep_monitor: 17;
97    /// voltage parameter, in sleep mode
98    pub u8, dbg_atten_slp, set_dbg_atten_slp: 22, 18;
99    /// circuit control parameter, in sleep mode
100    pub bias_sleep_slp, set_bias_sleep_slp: 23;
101    /// circuit control parameter, in monitor mode
102    pub pd_cur_monitor, set_pd_cur_monitor: 24;
103    /// circuit control parameter, in sleep mode
104    pub pd_cur_slp, set_pd_cur_slp: 25;
105    /// power down VDDSDIO regulator
106    pub vddsdio_pd_en, set_vddsdio_pd_en: 26;
107    /// keep main XTAL powered up in sleep
108    pub xtal_fpu, set_xtal_fpu: 27;
109    /// keep rtc regulator powered up in sleep
110    pub rtc_regulator_fpu, set_rtc_regulator_fpu: 28;
111    /// enable deep sleep reject
112    pub deep_slp_reject, set_deep_slp_reject: 29;
113    /// enable light sleep reject
114    pub light_slp_reject, set_light_slp_reject: 30;
115}
116
117impl Default for RtcSleepConfig {
118    fn default() -> Self {
119        let mut cfg = Self(Default::default());
120        cfg.set_deep_slp_reject(true);
121        cfg.set_light_slp_reject(true);
122        cfg.set_rtc_dbias_slp(RTC_CNTL_DBIAS_1V10);
123        cfg.set_dig_dbias_slp(RTC_CNTL_DBIAS_1V10);
124
125        // This is the light-sleep config. The main XTAL is powered down in sleep
126        // (`xtal_fpu` stays false), so the analog regulator/bias must use the
127        // same XTAL-down settings that `deep()` applies "because of xtal_fpu".
128        cfg.set_rtc_regulator_fpu(true);
129        cfg.set_bias_sleep_monitor(true);
130        cfg.set_pd_cur_monitor(true);
131        cfg.set_bias_sleep_slp(true);
132        cfg.set_pd_cur_slp(true);
133        cfg
134    }
135}
136
137fn rtc_sleep_pu(val: bool) {
138    // Note: Called rtc_sleep_pd in idf, but makes more sense like this with the
139    // single boolean argument
140    let rtc_cntl = LPWR::regs();
141    let syscon = unsafe { &*esp32s2::SYSCON::ptr() };
142    let bb = unsafe { &*esp32s2::BB::ptr() };
143    let i2s = unsafe { &*esp32s2::I2S0::ptr() };
144    let nrx = unsafe { &*esp32s2::NRX::ptr() };
145    let fe = unsafe { &*esp32s2::FE::ptr() };
146    let fe2 = unsafe { &*esp32s2::FE2::ptr() };
147
148    rtc_cntl
149        .dig_pwc()
150        .modify(|_, w| w.lslp_mem_force_pu().bit(val));
151
152    rtc_cntl
153        .pwc()
154        .modify(|_, w| w.slowmem_force_lpu().bit(val).fastmem_force_lpu().bit(val));
155
156    i2s.pd_conf()
157        .write(|w| w.plc_mem_force_pu().bit(val).fifo_force_pu().bit(val));
158
159    syscon.front_end_mem_pd().modify(|_r, w| {
160        w.dc_mem_force_pu()
161            .bit(val)
162            .pbus_mem_force_pu()
163            .bit(val)
164            .agc_mem_force_pu()
165            .bit(val)
166    });
167
168    bb.bbpd_ctrl()
169        .modify(|_r, w| w.fft_force_pu().bit(val).dc_est_force_pu().bit(val));
170
171    nrx.nrxpd_ctrl().modify(|_, w| {
172        w.rx_rot_force_pu()
173            .bit(val)
174            .vit_force_pu()
175            .bit(val)
176            .demap_force_pu()
177            .bit(val)
178    });
179
180    fe.gen_ctrl().modify(|_, w| w.iq_est_force_pu().bit(val));
181
182    fe2.tx_interp_ctrl()
183        .modify(|_, w| w.tx_inf_force_pu().bit(val));
184}
185
186impl RtcSleepConfig {
187    /// Configures the RTC for deep sleep mode.
188    ///
189    /// The RTC regulator is off in this mode, for the lowest current.
190    pub fn deep() -> Self {
191        // Set up for ultra-low power sleep. Wakeup sources may modify these settings. ESP-IDF
192        // uses this mode only on request, and does not support RTC IO inputs or RTC memory at high
193        // temperature in it:
194        // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/sleep_modes.c#L3083-L3085
195        // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/port/esp32s2/rtc_sleep.c#L95-L105
196        // https://github.com/espressif/esp-idf/blob/fff9895c82d/docs/en/api-reference/system/sleep_modes.rst#L54-L94
197        let mut cfg = Self::default();
198
199        cfg.set_lslp_mem_inf_fpu(false);
200        cfg.set_rtc_mem_inf_follow_cpu(true); // ?
201        cfg.set_rtc_fastmem_pd_en(true);
202        cfg.set_rtc_slowmem_pd_en(true);
203        cfg.set_rtc_peri_pd_en(true);
204        cfg.set_wifi_pd_en(true);
205        cfg.set_int_8m_pd_en(true);
206
207        // Because of force_flags
208        cfg.set_vddsdio_pd_en(true);
209
210        // because of dig_peri_pd_en
211        cfg.set_dig_dbias_slp(0);
212
213        cfg.set_deep_slp(true);
214        cfg.set_wdt_flashboot_mod_en(false);
215        cfg.set_vddsdio_pd_en(true);
216        cfg.set_xtal_fpu(false);
217        cfg.set_deep_slp_reject(true);
218        cfg.set_light_slp_reject(true);
219
220        // because of RTC_SLEEP_PD_DIG
221        // NOTE: Might be the a different case for RTC_SLEEP_PD_DIG in
222        // rtc_sleep_get_default_config
223        cfg.set_rtc_regulator_fpu(false);
224        cfg.set_dbg_atten_slp(RTC_CNTL_DBG_ATTEN_DEEPSLEEP_DEFAULT);
225        cfg.set_rtc_dbias_slp(0);
226
227        // because of xtal_fpu
228        cfg.set_xtal_fpu(false);
229        cfg.set_bias_sleep_monitor(true);
230        cfg.set_pd_cur_monitor(true);
231        cfg.set_bias_sleep_slp(true);
232        cfg.set_pd_cur_slp(true);
233
234        cfg
235    }
236
237    pub(crate) fn is_deep_sleep(&self) -> bool {
238        self.deep_slp()
239    }
240
241    pub(crate) fn set_sleep_kind(&mut self, kind: SleepKind) {
242        self.set_deep_slp(kind == SleepKind::Deep);
243    }
244
245    pub(crate) fn base_settings(_rtc: &Rtc<'_>) {
246        // settings derived from esp_clk_init -> rtc_init
247        unsafe {
248            let rtc_cntl = LPWR::regs();
249            let extmem = EXTMEM::regs();
250            let system = SYSTEM::regs();
251
252            rtc_cntl
253                .dig_pwc()
254                .modify(|_, w| w.wifi_force_pd().clear_bit());
255            rtc_cntl
256                .dig_iso()
257                .modify(|_, w| w.wifi_force_iso().clear_bit());
258
259            rtc_cntl.ana_conf().modify(|_, w| w.pvtmon_pu().clear_bit());
260
261            rtc_cntl.timer1().modify(|_, w| {
262                w.pll_buf_wait().bits(RTC_CNTL_PLL_BUF_WAIT_DEFAULT);
263                w.ck8m_wait().bits(RTC_CNTL_CK8M_WAIT_DEFAULT)
264            });
265
266            // idf: "Moved from rtc sleep to rtc init to save sleep function running time
267            // set shortest possible sleep time limit"
268
269            rtc_cntl
270                .timer5()
271                .modify(|_, w| w.min_slp_val().bits(RTC_CNTL_MIN_SLP_VAL_MIN));
272
273            rtc_cntl.timer3().modify(|_, w| {
274                // set wifi timer
275                w.wifi_powerup_timer().bits(WIFI_POWERUP_CYCLES);
276                w.wifi_wait_timer().bits(WIFI_WAIT_CYCLES)
277            });
278
279            rtc_cntl.timer4().modify(|_, w| {
280                // set rtc peri timer
281                w.powerup_timer().bits(RTC_POWERUP_CYCLES);
282                w.wait_timer().bits(RTC_WAIT_CYCLES);
283                // set digital wrap timer
284                w.dg_wrap_powerup_timer().bits(DG_WRAP_POWERUP_CYCLES);
285                w.dg_wrap_wait_timer().bits(DG_WRAP_WAIT_CYCLES)
286            });
287
288            rtc_cntl.timer5().modify(|_, w| {
289                w.rtcmem_powerup_timer().bits(RTC_MEM_POWERUP_CYCLES);
290                w.rtcmem_wait_timer().bits(RTC_MEM_WAIT_CYCLES)
291            });
292
293            rtc_cntl.bias_conf().modify(|_, w| {
294                w.dec_heartbeat_width().set_bit();
295                w.inc_heartbeat_period().set_bit()
296            });
297
298            // Reset RTC bias to default value (needed if waking up from deep sleep)
299            rtc_cntl.reg().modify(|_, w| {
300                w.dbias_wak().bits(RTC_CNTL_DBIAS_1V10);
301                w.dbias_slp().bits(RTC_CNTL_DBIAS_1V10)
302            });
303
304            // Set the wait time to the default value.
305            rtc_cntl.timer2().modify(|_, w| {
306                w.ulpcp_touch_start_wait()
307                    .bits(RTC_CNTL_ULPCP_TOUCH_START_WAIT_DEFAULT)
308            });
309
310            // clkctl_init
311            {
312                // clear CMMU clock force on
313                extmem
314                    .pro_cache_mmu_power_ctrl()
315                    .modify(|_, w| w.pro_cache_mmu_mem_force_on().clear_bit());
316
317                // clear tag clock force on
318                extmem
319                    .pro_dcache_tag_power_ctrl()
320                    .modify(|_, w| w.pro_dcache_tag_mem_force_on().clear_bit());
321
322                extmem
323                    .pro_icache_tag_power_ctrl()
324                    .modify(|_, w| w.pro_icache_tag_mem_force_on().clear_bit());
325
326                system.rom_ctrl_0().modify(|_, w| w.rom_fo().bits(0));
327                system.sram_ctrl_0().modify(|_, w| w.sram_fo().bits(0));
328
329                // clear register clock force on
330                SPI0::regs()
331                    .clock_gate()
332                    .modify(|_, w| w.clk_en().clear_bit());
333                SPI1::regs()
334                    .clock_gate()
335                    .modify(|_, w| w.clk_en().clear_bit());
336            }
337
338            // pwrctl_init
339            {
340                rtc_cntl
341                    .clk_conf()
342                    .modify(|_, w| w.ck8m_force_pu().clear_bit());
343
344                rtc_cntl
345                    .options0()
346                    .modify(|_, w| w.xtl_force_pu().clear_bit());
347
348                // CLEAR APLL close
349                rtc_cntl.ana_conf().modify(|_, w| {
350                    w.plla_force_pu().clear_bit();
351                    w.plla_force_pd().set_bit()
352                });
353
354                // cancel bbpll force pu if setting no force power up
355                rtc_cntl.options0().modify(|_, w| {
356                    w.bbpll_force_pu().clear_bit();
357                    w.bbpll_i2c_force_pu().clear_bit();
358                    w.bb_i2c_force_pu().clear_bit()
359                });
360
361                // cancel RTC REG force PU
362
363                rtc_cntl.pwc().modify(|_, w| w.force_pu().clear_bit());
364                rtc_cntl.reg().modify(|_, w| {
365                    w.regulator_force_pu().clear_bit();
366                    w.dboost_force_pu().clear_bit()
367                });
368
369                rtc_cntl.pwc().modify(|_, w| {
370                    w.slowmem_force_pu().clear_bit();
371                    w.fastmem_force_pu().clear_bit();
372                    w.slowmem_force_noiso().clear_bit();
373                    w.fastmem_force_noiso().clear_bit()
374                });
375
376                rtc_cntl.reg().modify(|_, w| w.dboost_force_pd().set_bit());
377
378                // cancel sar i2c pd force
379                rtc_cntl
380                    .ana_conf()
381                    .modify(|_, w| w.sar_i2c_force_pd().clear_bit());
382                // cancel digital pu force
383                // NOTE: duplicate from idf
384                rtc_cntl.pwc().modify(|_, w| {
385                    w.slowmem_force_pu().clear_bit();
386                    w.fastmem_force_pu().clear_bit()
387                });
388
389                // If this mask is enabled, all soc memories cannot enter power down mode
390                // We should control soc memory power down mode from RTC, so we will not touch
391                // this register any more
392
393                system
394                    .mem_pd_mask()
395                    .modify(|_, w| w.lslp_mem_pd_mask().clear_bit());
396
397                // If this pd_cfg is set to 1, all memory won't enter low power mode during
398                // light sleep If this pd_cfg is set to 0, all memory will enter low
399                // power mode during light sleep
400                rtc_sleep_pu(false);
401
402                rtc_cntl.dig_pwc().modify(|_, w| {
403                    w.dg_wrap_force_pu().clear_bit();
404                    w.wifi_force_pu().clear_bit()
405                });
406
407                rtc_cntl.dig_iso().modify(|_, w| {
408                    w.dg_wrap_force_noiso().clear_bit();
409                    // NOTE: not present in idf.
410                    w.dg_wrap_force_iso().clear_bit()
411                });
412
413                rtc_cntl.dig_iso().modify(|_, w| {
414                    w.wifi_force_noiso().clear_bit();
415                    // NOTE: not present in idf.
416                    w.wifi_force_iso().clear_bit()
417                });
418
419                rtc_cntl.pwc().modify(|_, w| w.force_noiso().clear_bit());
420
421                // cancel digital PADS force no iso
422                system
423                    .cpu_per_conf()
424                    .modify(|_, w| w.cpu_wait_mode_force_on().clear_bit());
425
426                // if DPORT_CPU_WAIT_MODE_FORCE_ON == 0,
427                // the cpu clk will be closed when cpu enter WAITI mode
428                rtc_cntl.dig_iso().modify(|_, w| {
429                    w.dg_pad_force_unhold().clear_bit();
430                    w.dg_pad_force_noiso().clear_bit()
431                });
432            }
433
434            // force power down wifi and bt power domain
435            rtc_cntl
436                .dig_iso()
437                .modify(|_, w| w.wifi_force_iso().set_bit());
438
439            rtc_cntl
440                .dig_pwc()
441                .modify(|_, w| w.wifi_force_pd().set_bit());
442
443            rtc_cntl.int_ena().write(|w| w.bits(0));
444            rtc_cntl.int_clr().write(|w| w.bits(u32::MAX));
445        }
446    }
447
448    pub(crate) fn apply(&self) {
449        // like esp-idf rtc_sleep_init() and deep_sleep_start()
450        let rtc_cntl = LPWR::regs();
451
452        if self.deep_slp() {
453            // "Due to hardware limitations, on S2 the brownout detector
454            // sometimes trigger during deep sleep to circumvent
455            // this we disable the brownout detector before sleeping' - from
456            // idf's deep_sleep_start()
457            unsafe {
458                // brownout_hal_config(brownlout_hal_config_t{0})
459                rtc_cntl.brown_out().modify(|_, w| {
460                    w.int_wait().bits(2);
461                    w.close_flash_ena().clear_bit();
462                    w.pd_rf_ena().clear_bit();
463                    w.cnt_clr().set_bit()
464                });
465                rtc_cntl.brown_out().modify(|_, w| {
466                    // Set followed by clear in idf
467                    w.cnt_clr().clear_bit();
468                    w.rst_wait().bits(0x3fff);
469                    w.rst_ena().clear_bit();
470                    w.brown_out2_ena().set_bit();
471                    w.rst_sel().set_bit()
472                });
473                regi2c::I2C_BOD_REG_THRESHOLD.write_field(0);
474                rtc_cntl.brown_out().modify(|_, w| w.ena().clear_bit());
475                rtc_cntl.int_ena().modify(|_, w| w.brown_out().clear_bit());
476                // NOTE: rtc_isr_deregister?
477            }
478        }
479
480        if self.lslp_mem_inf_fpu() {
481            rtc_sleep_pu(true);
482        }
483
484        let mem_folw_cpu = self.rtc_mem_inf_follow_cpu();
485        rtc_cntl.pwc().modify(|_, w| {
486            w.slowmem_folw_cpu().bit(mem_folw_cpu);
487            w.fastmem_folw_cpu().bit(mem_folw_cpu)
488        });
489
490        let rtc_fastmem_pd_en = self.rtc_fastmem_pd_en();
491        rtc_cntl.pwc().modify(|_, w| {
492            w.fastmem_pd_en().bit(rtc_fastmem_pd_en);
493            w.fastmem_force_pu().bit(!rtc_fastmem_pd_en);
494            w.fastmem_force_noiso().bit(!rtc_fastmem_pd_en)
495        });
496
497        let rtc_slowmem_pd_en = self.rtc_slowmem_pd_en();
498        rtc_cntl.pwc().modify(|_, w| {
499            w.slowmem_pd_en().bit(rtc_slowmem_pd_en);
500            w.slowmem_force_pu().bit(!rtc_slowmem_pd_en);
501            w.slowmem_force_noiso().bit(!rtc_slowmem_pd_en)
502        });
503
504        let rtc_peri_pd_en = self.rtc_peri_pd_en();
505        rtc_cntl.pwc().modify(|_, w| w.pd_en().bit(rtc_peri_pd_en));
506
507        if self.wifi_pd_en() {
508            rtc_cntl
509                .dig_iso()
510                .modify(|_, w| w.wifi_force_noiso().clear_bit());
511
512            rtc_cntl.dig_pwc().modify(|_, w| {
513                w.wifi_force_pu().clear_bit();
514                w.wifi_pd_en().set_bit()
515            });
516        } else {
517            rtc_cntl.dig_pwc().modify(|_, w| w.wifi_pd_en().clear_bit());
518        }
519
520        unsafe {
521            rtc_cntl.reg().modify(|_, w| {
522                w.dbias_slp().bits(self.rtc_dbias_slp());
523                w.dig_reg_dbias_slp().bits(self.dig_dbias_slp())
524            });
525
526            rtc_cntl.bias_conf().modify(|_, w| {
527                w.dbg_atten_monitor()
528                    .bits(RTC_CNTL_DBG_ATTEN_MONITOR_DEFAULT);
529                w.bias_sleep_monitor().bit(self.bias_sleep_monitor());
530                w.bias_sleep_deep_slp().bit(self.bias_sleep_slp());
531                w.pd_cur_monitor().bit(self.pd_cur_monitor());
532                w.pd_cur_deep_slp().bit(self.pd_cur_slp());
533                w.dbg_atten_deep_slp().bits(self.dbg_atten_slp())
534            });
535
536            if self.deep_slp() {
537                rtc_cntl
538                    .dig_pwc()
539                    .modify(|_, w| w.dg_wrap_pd_en().set_bit());
540
541                rtc_cntl.ana_conf().modify(|_, w| {
542                    w.ckgen_i2c_pu().clear_bit();
543                    w.pll_i2c_pu().clear_bit();
544                    w.rfrx_pbus_pu().clear_bit();
545                    w.txrf_i2c_pu().clear_bit()
546                });
547
548                rtc_cntl
549                    .options0()
550                    .modify(|_, w| w.bb_i2c_force_pu().clear_bit());
551            } else {
552                rtc_cntl
553                    .dig_pwc()
554                    .modify(|_, w| w.dg_wrap_pd_en().clear_bit());
555            }
556
557            let rtc_regulator_fpu = self.rtc_regulator_fpu();
558            rtc_cntl
559                .reg()
560                .modify(|_, w| w.regulator_force_pu().bit(rtc_regulator_fpu));
561
562            let int_8m_pd_en = self.int_8m_pd_en();
563            rtc_cntl
564                .clk_conf()
565                .modify(|_, w| w.ck8m_force_pu().bit(!int_8m_pd_en));
566
567            // enable VDDSDIO control by state machine
568            rtc_cntl.sdio_conf().modify(|_, w| {
569                w.sdio_force().clear_bit();
570                w.sdio_reg_pd_en().bit(self.vddsdio_pd_en())
571            });
572
573            rtc_cntl.slp_reject_conf().modify(|_, w| {
574                w.deep_slp_reject_en().bit(self.deep_slp_reject());
575                w.light_slp_reject_en().bit(self.light_slp_reject())
576            });
577
578            // Set wait cycle for touch or COCPU after deep sleep and light
579            // sleep.
580
581            rtc_cntl.timer2().modify(|_, w| {
582                w.ulpcp_touch_start_wait()
583                    .bits(RTC_CNTL_ULPCP_TOUCH_START_WAIT_IN_SLEEP)
584            });
585
586            rtc_cntl
587                .options0()
588                .modify(|_, w| w.xtl_force_pu().bit(self.xtal_fpu()));
589        }
590    }
591
592    /// Configures the wakeup and reject sources of the sleep.
593    ///
594    /// [`Self::enter_sleep`] requests the sleep after this call. The caller calls
595    /// [`SavedState::restore`] on the return value after [`Self::finish_sleep`].
596    #[crate::ram]
597    pub(crate) fn start_sleep(&self, wakeup_mask: u32, reject_mask: u32) -> SavedState {
598        let cpu_clk = super::SleepCpuClock::switch_to_xtal();
599
600        unsafe {
601            LPWR::regs()
602                .reset_state()
603                .modify(|_, w| w.procpu_stat_vector_sel().set_bit());
604
605            // set bits for what can wake us up
606            LPWR::regs()
607                .wakeup_state()
608                .modify(|_, w| w.wakeup_ena().bits(wakeup_mask));
609
610            // Set the bits of the sources that reject the sleep. The reject enables that `apply`
611            // wrote arm those sources.
612            LPWR::regs()
613                .slp_reject_conf()
614                .modify(|_, w| w.sleep_reject_ena().bits(reject_mask));
615        }
616
617        SavedState { cpu_clk }
618    }
619
620    /// Requests the sleep.
621    ///
622    /// The caller waits for the result of the request.
623    #[inline(always)]
624    pub(crate) fn enter_sleep(&self) -> bool {
625        LPWR::regs().state0().modify(|_, w| w.sleep_en().set_bit());
626        super::wait_for_sleep_result()
627    }
628
629    pub(crate) fn finish_sleep(&self) {
630        // In deep sleep mode, we never get here
631        unsafe {
632            LPWR::regs().int_clr().write(|w| {
633                w.slp_reject()
634                    .clear_bit_by_one()
635                    .slp_wakeup()
636                    .clear_bit_by_one()
637            });
638
639            // restore config if it is a light sleep
640            if self.lslp_mem_inf_fpu() {
641                rtc_sleep_pu(true);
642            }
643
644            // Recover default wait cycle for touch or COCPU after wakeup.
645
646            LPWR::regs().timer2().modify(|_, w| {
647                w.ulpcp_touch_start_wait()
648                    .bits(RTC_CNTL_ULPCP_TOUCH_START_WAIT_DEFAULT)
649            });
650        }
651    }
652}
653
654/// The state that [`RtcSleepConfig::start_sleep`] changes for the length of the sleep.
655#[must_use]
656pub(crate) struct SavedState {
657    cpu_clk: super::SleepCpuClock,
658}
659
660impl SavedState {
661    /// Restores the state. Runs after a wakeup and after a rejected sleep.
662    pub(crate) fn restore(self) {
663        self.cpu_clk.restore();
664    }
665}