Skip to main content

esp_hal/rtc_cntl/sleep/
esp32s3.rs

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