1use super::SleepKind;
2use crate::{
3 peripherals::{EXTMEM, LPWR, SPI0, SPI1, SYSTEM},
4 rtc_cntl::Rtc,
5 soc::regi2c,
6};
7
8pub const RTC_CNTL_DBIAS_0V90: u8 = 0;
13pub const RTC_CNTL_DBIAS_0V95: u8 = 1;
15pub const RTC_CNTL_DBIAS_1V00: u8 = 2;
17pub const RTC_CNTL_DBIAS_1V05: u8 = 3;
19pub const RTC_CNTL_DBIAS_1V10: u8 = 4;
21pub const RTC_CNTL_DBIAS_1V15: u8 = 5;
23pub const RTC_CNTL_DBIAS_1V20: u8 = 6;
25pub const RTC_CNTL_DBIAS_1V25: u8 = 7;
27pub const RTC_CNTL_DBG_ATTEN_MONITOR_DEFAULT: u8 = 0;
29pub const RTC_CNTL_ULPCP_TOUCH_START_WAIT_IN_SLEEP: u16 = 0xFF;
31pub const RTC_CNTL_ULPCP_TOUCH_START_WAIT_DEFAULT: u16 = 0x10;
33pub const RTC_CNTL_PLL_BUF_WAIT_DEFAULT: u8 = 20;
35pub const RTC_CNTL_CK8M_WAIT_DEFAULT: u8 = 20;
37pub const RTC_CNTL_XTL_BUF_WAIT_DEFAULT: u8 = 100;
39pub const RTC_CNTL_MIN_SLP_VAL_MIN: u8 = 2;
41pub const RTC_CNTL_DBG_ATTEN_DEEPSLEEP_DEFAULT: u8 = 15;
43pub const OTHER_BLOCKS_POWERUP: u8 = 1;
45pub const OTHER_BLOCKS_WAIT: u16 = 1;
47pub const WIFI_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
49pub const WIFI_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
51pub const RTC_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
53pub const RTC_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
55pub const DG_WRAP_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
57pub const DG_WRAP_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
59pub const DG_PERI_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
61pub const DG_PERI_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
63pub const RTC_MEM_POWERUP_CYCLES: u8 = OTHER_BLOCKS_POWERUP;
65pub const RTC_MEM_WAIT_CYCLES: u16 = OTHER_BLOCKS_WAIT;
67
68bitfield::bitfield! {
69 #[derive(Clone, Copy)]
71 pub struct RtcSleepConfig(u64);
72 impl Debug;
73 pub lslp_mem_inf_fpu, set_lslp_mem_inf_fpu: 0;
75 pub rtc_mem_inf_follow_cpu, set_rtc_mem_inf_follow_cpu: 1;
77 pub rtc_fastmem_pd_en, set_rtc_fastmem_pd_en: 2;
79 pub rtc_slowmem_pd_en, set_rtc_slowmem_pd_en: 3;
81 pub rtc_peri_pd_en, set_rtc_peri_pd_en: 4;
83 pub wifi_pd_en, set_wifi_pd_en: 5;
85 pub int_8m_pd_en, set_int_8m_pd_en: 6;
87 pub deep_slp, set_deep_slp: 8;
89 pub wdt_flashboot_mod_en, set_wdt_flashboot_mod_en: 9;
91 pub u8, dig_dbias_slp, set_dig_dbias_slp: 12, 10;
93 pub u8, rtc_dbias_slp, set_rtc_dbias_slp: 16, 13;
95 pub bias_sleep_monitor, set_bias_sleep_monitor: 17;
97 pub u8, dbg_atten_slp, set_dbg_atten_slp: 22, 18;
99 pub bias_sleep_slp, set_bias_sleep_slp: 23;
101 pub pd_cur_monitor, set_pd_cur_monitor: 24;
103 pub pd_cur_slp, set_pd_cur_slp: 25;
105 pub vddsdio_pd_en, set_vddsdio_pd_en: 26;
107 pub xtal_fpu, set_xtal_fpu: 27;
109 pub rtc_regulator_fpu, set_rtc_regulator_fpu: 28;
111 pub deep_slp_reject, set_deep_slp_reject: 29;
113 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 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 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 pub fn deep() -> Self {
191 let mut cfg = Self::default();
198
199 cfg.set_lslp_mem_inf_fpu(false);
200 cfg.set_rtc_mem_inf_follow_cpu(true); 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 cfg.set_vddsdio_pd_en(true);
209
210 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 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 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 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 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 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 w.powerup_timer().bits(RTC_POWERUP_CYCLES);
282 w.wait_timer().bits(RTC_WAIT_CYCLES);
283 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 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 rtc_cntl.timer2().modify(|_, w| {
306 w.ulpcp_touch_start_wait()
307 .bits(RTC_CNTL_ULPCP_TOUCH_START_WAIT_DEFAULT)
308 });
309
310 {
312 extmem
314 .pro_cache_mmu_power_ctrl()
315 .modify(|_, w| w.pro_cache_mmu_mem_force_on().clear_bit());
316
317 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 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 {
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 rtc_cntl.ana_conf().modify(|_, w| {
350 w.plla_force_pu().clear_bit();
351 w.plla_force_pd().set_bit()
352 });
353
354 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 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 rtc_cntl
380 .ana_conf()
381 .modify(|_, w| w.sar_i2c_force_pd().clear_bit());
382 rtc_cntl.pwc().modify(|_, w| {
385 w.slowmem_force_pu().clear_bit();
386 w.fastmem_force_pu().clear_bit()
387 });
388
389 system
394 .mem_pd_mask()
395 .modify(|_, w| w.lslp_mem_pd_mask().clear_bit());
396
397 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 w.dg_wrap_force_iso().clear_bit()
411 });
412
413 rtc_cntl.dig_iso().modify(|_, w| {
414 w.wifi_force_noiso().clear_bit();
415 w.wifi_force_iso().clear_bit()
417 });
418
419 rtc_cntl.pwc().modify(|_, w| w.force_noiso().clear_bit());
420
421 system
423 .cpu_per_conf()
424 .modify(|_, w| w.cpu_wait_mode_force_on().clear_bit());
425
426 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 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 let rtc_cntl = LPWR::regs();
451
452 if self.deep_slp() {
453 unsafe {
458 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 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 }
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 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 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 #[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 LPWR::regs()
607 .wakeup_state()
608 .modify(|_, w| w.wakeup_ena().bits(wakeup_mask));
609
610 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 #[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 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 if self.lslp_mem_inf_fpu() {
641 rtc_sleep_pu(true);
642 }
643
644 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#[must_use]
656pub(crate) struct SavedState {
657 cpu_clk: super::SleepCpuClock,
658}
659
660impl SavedState {
661 pub(crate) fn restore(self) {
663 self.cpu_clk.restore();
664 }
665}