1use super::SleepKind;
2use crate::{
3 peripherals::{APB_CTRL, BB, EXTMEM, FE, FE2, LPWR, NRX, SPI0, SPI1, SYSTEM},
4 rtc_cntl::Rtc,
5 soc::regi2c,
6};
7
8pub const RTC_CNTL_DBIAS_0V90: u8 = 13;
13pub const RTC_CNTL_DBIAS_0V95: u8 = 16;
15pub const RTC_CNTL_DBIAS_1V00: u8 = 18;
17pub const RTC_CNTL_DBIAS_1V05: u8 = 20;
19pub const RTC_CNTL_DBIAS_1V10: u8 = 23;
21pub const RTC_CNTL_DBIAS_1V15: u8 = 25;
23pub const RTC_CNTL_DBIAS_1V20: u8 = 28;
25pub const RTC_CNTL_DBIAS_1V25: u8 = 30;
27pub const RTC_CNTL_DBIAS_1V30: u8 = 31;
29
30pub const RTC_CNTL_DBG_ATTEN_LIGHTSLEEP_DEFAULT: u8 = 5;
32pub const RTC_CNTL_DBG_ATTEN_LIGHTSLEEP_NODROP: u8 = 0;
34pub const RTC_CNTL_DBG_ATTEN_DEEPSLEEP_DEFAULT: u8 = 15;
36pub const RTC_CNTL_DBG_ATTEN_DEEPSLEEP_NODROP: u8 = 0;
38
39pub const RTC_CNTL_BIASSLP_SLEEP_DEFAULT: u8 = 1;
41pub const RTC_CNTL_BIASSLP_SLEEP_ON: u8 = 0;
43
44pub const RTC_CNTL_PD_CUR_SLEEP_DEFAULT: u8 = 1;
46pub const RTC_CNTL_PD_CUR_SLEEP_ON: u8 = 0;
48
49pub const RTC_CNTL_DG_VDD_DRV_B_SLP_DEFAULT: u8 = 254;
51
52pub const RTC_CNTL_DBG_ATTEN_MONITOR_DEFAULT: u8 = 0;
54pub const RTC_CNTL_BIASSLP_MONITOR_DEFAULT: bool = false;
56pub const RTC_CNTL_PD_CUR_MONITOR_DEFAULT: bool = false;
58
59pub const RTC_CNTL_PLL_BUF_WAIT_DEFAULT: u8 = 20;
61pub const RTC_CNTL_XTL_BUF_WAIT_DEFAULT: u8 = 100;
63pub const RTC_CNTL_CK8M_WAIT_DEFAULT: u8 = 20;
65pub const RTC_CK8M_ENABLE_WAIT_DEFAULT: u8 = 5;
67
68pub const RTC_CNTL_MIN_SLP_VAL_MIN: u8 = 2;
70
71pub const OTHER_BLOCKS_POWERUP: u8 = 1;
73pub const OTHER_BLOCKS_WAIT: u16 = 1;
75
76pub const GPIO_INTR_DISABLE: u8 = 0;
78pub const GPIO_INTR_LOW_LEVEL: u8 = 4;
80pub const GPIO_INTR_HIGH_LEVEL: u8 = 5;
82
83pub const PIN_FUNC_GPIO: u8 = 1;
85pub const SIG_GPIO_OUT_IDX: u32 = 128;
87pub const GPIO_NUM_MAX: usize = 22;
89
90bitfield::bitfield! {
91 #[derive(Clone, Copy)]
92 pub struct RtcConfig(u32);
94 impl Debug;
95 pub u8, ck8m_wait, set_ck8m_wait: 7, 0;
97 pub u8, xtal_wait, set_xtal_wait: 15, 8;
99 pub u8, pll_wait, set_pll_wait: 23, 16;
101 pub clkctl_init, set_clkctl_init: 24;
103 pub pwrctl_init, set_pwrctl_init: 25;
105 pub rtc_dboost_fpd, set_rtc_dboost_fpd: 26;
107 pub xtal_fpu, set_xtal_fpu: 27;
109 pub bbpll_fpu, set_bbpll_fpu: 28;
111 pub cpu_waiti_clk_gate, set_cpu_waiti_clk_gate: 29;
113 pub cali_ocode, set_cali_ocode: 30;
115}
116
117impl Default for RtcConfig {
118 fn default() -> Self {
119 let mut cfg = Self(Default::default());
120 cfg.set_ck8m_wait(RTC_CNTL_CK8M_WAIT_DEFAULT);
121 cfg.set_xtal_wait(RTC_CNTL_XTL_BUF_WAIT_DEFAULT);
122 cfg.set_pll_wait(RTC_CNTL_PLL_BUF_WAIT_DEFAULT);
123 cfg.set_clkctl_init(true);
124 cfg.set_pwrctl_init(true);
125 cfg.set_rtc_dboost_fpd(true);
126 cfg.set_cpu_waiti_clk_gate(true);
127 cfg
128 }
129}
130
131bitfield::bitfield! {
132 #[derive(Clone, Copy)]
133 pub struct RtcInitConfig(u128);
135 impl Debug;
136 pub u8, wifi_powerup_cycles, set_wifi_powerup_cycles: 6, 0;
138 pub u16, wifi_wait_cycles, set_wifi_wait_cycles: 15, 7;
140 pub u8, bt_powerup_cycles, set_bt_powerup_cycles: 22, 16;
142 pub u16, bt_wait_cycles, set_bt_wait_cycles: 31, 23;
144 pub u8, cpu_top_powerup_cycles, set_cpu_top_powerup_cycles: 38, 32;
146 pub u16, cpu_top_wait_cycles, set_cpu_top_wait_cycles: 47, 39;
148 pub u8, dg_wrap_powerup_cycles, set_dg_wrap_powerup_cycles: 54, 48;
150 pub u16, dg_wrap_wait_cycles, set_dg_wrap_wait_cycles: 63, 55;
152 pub u8, dg_peri_powerup_cycles, set_dg_peri_powerup_cycles: 70, 64;
154 pub u16, dg_peri_wait_cycles, set_dg_peri_wait_cycles: 79, 71;
156}
157
158impl Default for RtcInitConfig {
159 fn default() -> Self {
160 let mut cfg = Self(Default::default());
161 cfg.set_wifi_powerup_cycles(OTHER_BLOCKS_POWERUP);
162 cfg.set_bt_powerup_cycles(OTHER_BLOCKS_POWERUP);
163 cfg.set_cpu_top_powerup_cycles(OTHER_BLOCKS_POWERUP);
164 cfg.set_dg_wrap_powerup_cycles(OTHER_BLOCKS_POWERUP);
165 cfg.set_dg_peri_powerup_cycles(OTHER_BLOCKS_POWERUP);
166 cfg.set_wifi_wait_cycles(OTHER_BLOCKS_WAIT);
167 cfg.set_bt_wait_cycles(OTHER_BLOCKS_WAIT);
168 cfg.set_cpu_top_wait_cycles(OTHER_BLOCKS_WAIT);
169 cfg.set_dg_wrap_wait_cycles(OTHER_BLOCKS_WAIT);
170 cfg.set_dg_peri_wait_cycles(OTHER_BLOCKS_WAIT);
171 cfg
172 }
173}
174
175bitfield::bitfield! {
176 #[derive(Clone, Copy)]
177 pub struct RtcSleepConfig(u64);
179 impl Debug;
180 pub lslp_mem_inf_fpu, set_lslp_mem_inf_fpu: 0;
182 pub rtc_mem_inf_follow_cpu, set_rtc_mem_inf_follow_cpu: 1;
184 pub rtc_fastmem_pd_en, set_rtc_fastmem_pd_en: 2;
186 pub rtc_slowmem_pd_en, set_rtc_slowmem_pd_en: 3;
188 pub rtc_peri_pd_en, set_rtc_peri_pd_en: 4;
190 pub wifi_pd_en, set_wifi_pd_en: 5;
192 pub bt_pd_en, set_bt_pd_en: 6;
194 pub(crate) cpu_pd_en, set_cpu_pd_en: 7;
199 pub int_8m_pd_en, set_int_8m_pd_en: 8;
201 pub dig_peri_pd_en, set_dig_peri_pd_en: 9;
203 pub deep_slp, set_deep_slp: 10;
205 pub wdt_flashboot_mod_en, set_wdt_flashboot_mod_en: 11;
207 pub u8, dig_dbias_slp, set_dig_dbias_slp: 16, 12;
209 pub u8, rtc_dbias_slp, set_rtc_dbias_slp: 21, 17;
211 pub u8, dbg_atten_slp, set_dbg_atten_slp: 25, 22;
213 pub bias_sleep_monitor, set_bias_sleep_monitor: 26;
215 pub bias_sleep_slp, set_bias_sleep_slp: 27;
217 pub pd_cur_slp, set_pd_cur_slp: 28;
219 pub vddsdio_pd_en, set_vddsdio_pd_en: 29;
221 pub xtal_fpu, set_xtal_fpu: 30;
223 pub rtc_regulator_fpu, set_rtc_regulator_fpu: 31;
225 pub deep_slp_reject, set_deep_slp_reject: 32;
227 pub light_slp_reject, set_light_slp_reject: 33;
229}
230
231impl Default for RtcSleepConfig {
232 fn default() -> Self {
233 let mut cfg = Self(Default::default());
234 cfg.set_deep_slp_reject(true);
235 cfg.set_light_slp_reject(true);
236 cfg.set_rtc_dbias_slp(RTC_CNTL_DBIAS_1V10);
237 cfg.set_dig_dbias_slp(RTC_CNTL_DBIAS_1V10);
238
239 cfg.set_rtc_regulator_fpu(true);
243 cfg.set_bias_sleep_monitor(true);
244 cfg.set_bias_sleep_slp(true);
245 cfg.set_pd_cur_slp(true);
246 cfg
247 }
248}
249
250const SYSCON_SRAM_POWER_UP: u8 = 0x0000000F;
251const SYSCON_ROM_POWER_UP: u8 = 0x00000003;
252
253fn rtc_sleep_pu(val: bool) {
254 LPWR::regs()
255 .dig_pwc()
256 .modify(|_, w| w.lslp_mem_force_pu().bit(val).fastmem_force_lpu().bit(val));
257
258 APB_CTRL::regs().front_end_mem_pd().modify(|_r, w| {
259 w.dc_mem_force_pu().bit(val);
260 w.pbus_mem_force_pu().bit(val);
261 w.agc_mem_force_pu().bit(val)
262 });
263
264 BB::regs()
265 .bbpd_ctrl()
266 .modify(|_r, w| w.fft_force_pu().bit(val).dc_est_force_pu().bit(val));
267
268 NRX::regs().nrxpd_ctrl().modify(|_, w| {
269 w.rx_rot_force_pu().bit(val);
270 w.vit_force_pu().bit(val);
271 w.demap_force_pu().bit(val)
272 });
273
274 FE::regs()
275 .gen_ctrl()
276 .modify(|_, w| w.iq_est_force_pu().bit(val));
277
278 FE2::regs()
279 .tx_interp_ctrl()
280 .modify(|_, w| w.tx_inf_force_pu().bit(val));
281
282 APB_CTRL::regs().mem_power_up().modify(|_r, w| unsafe {
283 w.sram_power_up()
284 .bits(if val { SYSCON_SRAM_POWER_UP } else { 0 });
285 w.rom_power_up()
286 .bits(if val { SYSCON_ROM_POWER_UP } else { 0 })
287 });
288}
289
290impl RtcSleepConfig {
291 pub fn deep() -> Self {
295 let mut cfg = Self::default();
301
302 cfg.set_lslp_mem_inf_fpu(false);
303 cfg.set_rtc_mem_inf_follow_cpu(true); cfg.set_rtc_fastmem_pd_en(true);
305 cfg.set_rtc_slowmem_pd_en(true);
306 cfg.set_rtc_peri_pd_en(true);
307 cfg.set_wifi_pd_en(true);
308 cfg.set_bt_pd_en(true);
309 cfg.set_cpu_pd_en(true);
310 cfg.set_int_8m_pd_en(true);
311
312 cfg.set_dig_peri_pd_en(true);
313 cfg.set_dig_dbias_slp(0); cfg.set_deep_slp(true);
316 cfg.set_wdt_flashboot_mod_en(false);
317 cfg.set_vddsdio_pd_en(true);
318 cfg.set_xtal_fpu(false);
319 cfg.set_deep_slp_reject(true);
320 cfg.set_light_slp_reject(true);
321 cfg.set_rtc_dbias_slp(RTC_CNTL_DBIAS_1V10);
322
323 cfg.set_rtc_regulator_fpu(false);
325 cfg.set_dbg_atten_slp(RTC_CNTL_DBG_ATTEN_DEEPSLEEP_DEFAULT);
326
327 cfg.set_bias_sleep_monitor(true);
329 cfg.set_bias_sleep_slp(true);
330 cfg.set_pd_cur_slp(true);
331
332 cfg
333 }
334
335 pub(crate) fn is_deep_sleep(&self) -> bool {
336 self.deep_slp()
337 }
338
339 pub(crate) fn set_sleep_kind(&mut self, kind: SleepKind) {
340 self.set_deep_slp(kind == SleepKind::Deep);
341 }
342
343 pub(crate) fn base_settings(_rtc: &Rtc<'_>) {
344 let cfg = RtcConfig::default();
345
346 let rtc_cntl = LPWR::regs();
348 let extmem = EXTMEM::regs();
349 let system = SYSTEM::regs();
350
351 unsafe {
352 rtc_cntl
353 .dig_pwc()
354 .modify(|_, w| w.wifi_force_pd().clear_bit());
355 rtc_cntl
356 .dig_iso()
357 .modify(|_, w| w.wifi_force_iso().clear_bit());
358
359 regi2c::I2C_DIG_REG_XPD_RTC_REG.write_field(0);
360 regi2c::I2C_DIG_REG_XPD_DIG_REG.write_field(0);
361
362 rtc_cntl.ana_conf().modify(|_, w| w.pvtmon_pu().clear_bit());
363
364 rtc_cntl.timer1().modify(|_, w| {
365 w.pll_buf_wait().bits(cfg.pll_wait());
366 w.ck8m_wait().bits(cfg.ck8m_wait())
367 });
368
369 rtc_cntl
373 .timer5()
374 .modify(|_, w| w.min_slp_val().bits(RTC_CNTL_MIN_SLP_VAL_MIN));
375
376 let init_cfg = RtcInitConfig::default();
377
378 rtc_cntl.timer3().modify(|_, w| {
379 w.wifi_powerup_timer().bits(init_cfg.wifi_powerup_cycles());
381 w.wifi_wait_timer().bits(init_cfg.wifi_wait_cycles());
382 w.bt_powerup_timer().bits(init_cfg.bt_powerup_cycles());
384 w.bt_wait_timer().bits(init_cfg.bt_wait_cycles())
385 });
386
387 rtc_cntl.timer4().modify(|_, w| {
388 w.cpu_top_powerup_timer()
389 .bits(init_cfg.cpu_top_powerup_cycles());
390 w.cpu_top_wait_timer().bits(init_cfg.cpu_top_wait_cycles());
391 w.dg_wrap_powerup_timer()
393 .bits(init_cfg.dg_wrap_powerup_cycles());
394 w.dg_wrap_wait_timer().bits(init_cfg.dg_wrap_wait_cycles())
395 });
396
397 rtc_cntl.timer6().modify(|_, w| {
398 w.dg_peri_powerup_timer()
399 .bits(init_cfg.dg_peri_powerup_cycles());
400 w.dg_peri_wait_timer().bits(init_cfg.dg_peri_wait_cycles())
401 });
402
403 regi2c::I2C_DIG_REG_EXT_RTC_DREG_SLEEP.write_field(RTC_CNTL_DBIAS_1V10);
407
408 regi2c::I2C_DIG_REG_EXT_RTC_DREG.write_field(RTC_CNTL_DBIAS_1V25);
414 regi2c::I2C_DIG_REG_EXT_DIG_DREG.write_field(RTC_CNTL_DBIAS_1V25);
415
416 if cfg.clkctl_init() {
417 extmem
420 .cache_mmu_power_ctrl()
421 .modify(|_, w| w.cache_mmu_mem_force_on().clear_bit());
422 extmem
425 .icache_tag_power_ctrl()
426 .modify(|_, w| w.icache_tag_mem_force_on().clear_bit());
427 SPI0::regs()
429 .clock_gate()
430 .modify(|_, w| w.clk_en().clear_bit());
431 SPI1::regs()
432 .clock_gate()
433 .modify(|_, w| w.clk_en().clear_bit());
434 }
435
436 if cfg.pwrctl_init() {
437 rtc_cntl
438 .clk_conf()
439 .modify(|_, w| w.ck8m_force_pu().clear_bit());
440
441 rtc_cntl
442 .options0()
443 .modify(|_, w| w.xtl_force_pu().bit(cfg.xtal_fpu() || cfg.bbpll_fpu()));
444
445 rtc_cntl
448 .ana_conf()
449 .modify(|_, w| w.plla_force_pu().clear_bit().plla_force_pd().set_bit());
450
451 rtc_cntl.ana_conf().modify(|_, w| {
452 w
453 .reset_por_force_pd()
455 .clear_bit()
456 });
457
458 rtc_cntl.options0().modify(|_, w| {
461 w.bbpll_force_pu().bit(cfg.bbpll_fpu());
462 w.bbpll_i2c_force_pu().bit(cfg.bbpll_fpu());
463 w.bb_i2c_force_pu().bit(cfg.bbpll_fpu())
464 });
465
466 rtc_cntl.rtc_cntl().modify(|_, w| {
467 w.regulator_force_pu().clear_bit();
468 w.dboost_force_pu().clear_bit();
469 w.dboost_force_pd().bit(cfg.rtc_dboost_fpd())
470 });
471
472 system
477 .mem_pd_mask()
478 .modify(|_, w| w.lslp_mem_pd_mask().clear_bit());
479
480 rtc_sleep_pu(false);
484
485 rtc_cntl.dig_pwc().modify(|_, w| {
486 w.dg_wrap_force_pu().clear_bit();
487 w.wifi_force_pu().clear_bit();
488 w.bt_force_pu().clear_bit();
489 w.cpu_top_force_pu().clear_bit();
490 w.dg_peri_force_pu().clear_bit()
491 });
492
493 rtc_cntl.dig_iso().modify(|_, w| {
494 w.dg_wrap_force_noiso().clear_bit();
495 w.wifi_force_noiso().clear_bit();
496 w.bt_force_noiso().clear_bit();
497 w.cpu_top_force_noiso().clear_bit();
498 w.dg_peri_force_noiso().clear_bit()
499 });
500
501 system
505 .cpu_per_conf()
506 .modify(|_, w| w.cpu_wait_mode_force_on().bit(!cfg.cpu_waiti_clk_gate()));
507
508 rtc_cntl.dig_iso().modify(|_, w| {
511 w.dg_pad_force_unhold().clear_bit();
512 w.dg_pad_force_noiso().clear_bit()
513 });
514 }
515
516 rtc_cntl
519 .dig_iso()
520 .modify(|_, w| w.wifi_force_iso().set_bit().bt_force_iso().set_bit());
521
522 rtc_cntl
523 .dig_pwc()
524 .modify(|_, w| w.wifi_force_pd().set_bit().bt_force_pd().set_bit());
525
526 rtc_cntl.int_ena().write(|w| w.bits(0));
527 rtc_cntl.int_clr().write(|w| w.bits(u32::MAX));
528
529 regi2c::I2C_ULP_IR_FORCE_XPD_CK.write_field(1);
530 }
531 }
532
533 pub(crate) fn apply(&self) {
534 let rtc_cntl = LPWR::regs();
536
537 if self.lslp_mem_inf_fpu() {
538 rtc_sleep_pu(true);
539 }
540
541 if self.wifi_pd_en() {
543 rtc_cntl
544 .dig_iso()
545 .modify(|_, w| w.wifi_force_noiso().clear_bit());
546
547 rtc_cntl.dig_pwc().modify(|_, w| {
548 w.wifi_force_pu().clear_bit();
549 w.wifi_pd_en().set_bit()
550 });
551 } else {
552 rtc_cntl.dig_pwc().modify(|_, w| w.wifi_pd_en().clear_bit());
553 }
554 if self.bt_pd_en() {
555 rtc_cntl
556 .dig_iso()
557 .modify(|_, w| w.bt_force_noiso().clear_bit());
558
559 rtc_cntl.dig_pwc().modify(|_, w| {
560 w.bt_force_pu().clear_bit();
561 w.bt_pd_en().set_bit()
562 });
563 } else {
564 rtc_cntl.dig_pwc().modify(|_, w| w.bt_pd_en().clear_bit());
565 }
566
567 rtc_cntl.dig_pwc().modify(|_, w| {
568 w.cpu_top_pd_en().bit(self.cpu_pd_en());
569 w.dg_peri_pd_en().bit(self.dig_peri_pd_en())
570 });
571
572 unsafe {
573 rtc_cntl.bias_conf().modify(|_, w| {
574 w.dbg_atten_monitor()
575 .bits(RTC_CNTL_DBG_ATTEN_MONITOR_DEFAULT);
576
577 w.bias_sleep_monitor().bit(RTC_CNTL_BIASSLP_MONITOR_DEFAULT);
581 w.pd_cur_monitor().bit(RTC_CNTL_PD_CUR_MONITOR_DEFAULT)
582 });
583
584 let xtal_on = self.xtal_fpu() && !self.deep_slp();
587 let bias_sleep_slp = self.bias_sleep_slp() && !xtal_on;
588 let pd_cur_slp = self.pd_cur_slp() && !xtal_on;
589 let dbg_atten_slp = if xtal_on {
590 RTC_CNTL_DBG_ATTEN_LIGHTSLEEP_NODROP
591 } else {
592 self.dbg_atten_slp()
593 };
594
595 assert!(!pd_cur_slp || bias_sleep_slp);
596
597 regi2c::I2C_DIG_REG_EXT_RTC_DREG_SLEEP.write_field(self.rtc_dbias_slp());
598 regi2c::I2C_DIG_REG_EXT_DIG_DREG_SLEEP.write_field(self.dig_dbias_slp());
599
600 rtc_cntl.bias_conf().modify(|_, w| {
601 w.dbg_atten_deep_slp().bits(dbg_atten_slp);
602 w.bias_sleep_deep_slp().bit(bias_sleep_slp);
603 w.pd_cur_deep_slp().bit(pd_cur_slp)
604 });
605
606 if self.deep_slp() {
607 regi2c::I2C_ULP_IR_FORCE_XPD_CK.write_field(0);
608
609 rtc_cntl
610 .dig_pwc()
611 .modify(|_, w| w.dg_wrap_pd_en().set_bit());
612
613 rtc_cntl.ana_conf().modify(|_, w| {
614 w.ckgen_i2c_pu().clear_bit();
615 w.pll_i2c_pu().clear_bit();
616 w.rfrx_pbus_pu().clear_bit();
617 w.txrf_i2c_pu().clear_bit()
618 });
619
620 rtc_cntl
621 .options0()
622 .modify(|_, w| w.bb_i2c_force_pu().clear_bit());
623 } else {
624 rtc_cntl.bias_conf().modify(|_, w| {
625 w.dg_vdd_drv_b_slp_en().set_bit();
626 w.dg_vdd_drv_b_slp().bits(RTC_CNTL_DG_VDD_DRV_B_SLP_DEFAULT)
627 });
628
629 rtc_cntl
630 .dig_pwc()
631 .modify(|_, w| w.dg_wrap_pd_en().clear_bit());
632 }
633
634 rtc_cntl
637 .dig_pwc()
638 .modify(|_, w| w.lslp_mem_force_pu().set_bit());
639
640 rtc_cntl
641 .rtc_cntl()
642 .modify(|_, w| w.regulator_force_pu().bit(self.rtc_regulator_fpu()));
643
644 rtc_cntl.clk_conf().modify(|_, w| {
645 w.ck8m_force_pu().bit(!self.int_8m_pd_en());
646 w.ck8m_force_nogating().bit(!self.int_8m_pd_en())
647 });
648
649 rtc_cntl.sdio_conf().modify(|_, w| {
652 w.sdio_force().clear_bit();
653 w.sdio_reg_pd_en().bit(self.vddsdio_pd_en())
654 });
655
656 rtc_cntl.slp_reject_conf().modify(|_, w| {
657 w.deep_slp_reject_en().bit(self.deep_slp_reject());
658 w.light_slp_reject_en().bit(self.light_slp_reject())
659 });
660
661 rtc_cntl
662 .options0()
663 .modify(|_, w| w.xtl_force_pu().bit(self.xtal_fpu()));
664
665 rtc_cntl
666 .clk_conf()
667 .modify(|_, w| w.xtal_global_force_nogating().bit(self.xtal_fpu()));
668 }
669 }
670
671 #[crate::ram]
676 pub(crate) fn start_sleep(&self, wakeup_mask: u32, reject_mask: u32) -> SavedState {
677 let cpu_clk = super::SleepCpuClock::switch_to_xtal();
678
679 LPWR::regs()
681 .wakeup_state()
682 .modify(|_, w| unsafe { w.wakeup_ena().bits(wakeup_mask) });
683
684 LPWR::regs()
687 .slp_reject_conf()
688 .modify(|_, w| unsafe { w.sleep_reject_ena().bits(reject_mask) });
689
690 SavedState { cpu_clk }
691 }
692
693 #[inline(always)]
697 pub(crate) fn enter_sleep(&self) -> bool {
698 LPWR::regs().state0().modify(|_, w| w.sleep_en().set_bit());
699 super::wait_for_sleep_result()
700 }
701
702 pub(crate) fn finish_sleep(&self) {
703 LPWR::regs().int_clr().write(|w| {
705 w.slp_reject().clear_bit_by_one();
706 w.slp_wakeup().clear_bit_by_one()
707 });
708
709 if self.lslp_mem_inf_fpu() {
711 rtc_sleep_pu(true);
712 }
713 }
714}
715
716#[must_use]
718pub(crate) struct SavedState {
719 cpu_clk: super::SleepCpuClock,
720}
721
722impl SavedState {
723 pub(crate) fn restore(self) {
725 self.cpu_clk.restore();
726 }
727}