esp_hal/rtc_cntl/sleep/
esp32.rs1use super::SleepKind;
2use crate::{
3 peripherals::{BB, DPORT, I2S0, LPWR, NRX},
4 rtc_cntl::{Rtc, WakeupSource},
5};
6
7pub const RTC_CNTL_DBIAS_0V90: u8 = 0;
12pub const RTC_CNTL_DBIAS_0V95: u8 = 1;
14pub const RTC_CNTL_DBIAS_1V00: u8 = 2;
16pub const RTC_CNTL_DBIAS_1V05: u8 = 3;
18pub const RTC_CNTL_DBIAS_1V10: u8 = 4;
20pub const RTC_CNTL_DBIAS_1V15: u8 = 5;
22pub const RTC_CNTL_DBIAS_1V20: u8 = 6;
24pub const RTC_CNTL_DBIAS_1V25: u8 = 7;
26
27pub const RTC_CNTL_XTL_BUF_WAIT_SLP_US: u32 = 1000;
30pub const RTC_CNTL_PLL_BUF_WAIT_SLP_CYCLES: u8 = 1;
32pub const RTC_CNTL_CK8M_WAIT_SLP_CYCLES: u8 = 4;
34pub const RTC_CNTL_WAKEUP_DELAY_CYCLES: u8 = 7;
36pub const RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES: u8 = 1;
38pub const RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES: u16 = 1;
40pub const RTC_CNTL_MIN_SLP_VAL_MIN: u8 = 128;
42pub const RTC_CNTL_DBG_ATTEN_DEFAULT: u8 = 3;
44
45pub const RTC_MEM_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
47pub const RTC_MEM_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
49pub const ROM_RAM_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
51pub const ROM_RAM_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
53pub const WIFI_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
55pub const WIFI_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
57pub const RTC_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
59pub const RTC_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
61pub const DG_WRAP_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
63pub const DG_WRAP_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
65
66pub const RTC_CNTL_CK8M_WAIT_DEFAULT: u8 = 20;
68pub const RTC_CK8M_ENABLE_WAIT_DEFAULT: u8 = 5;
70
71bitfield::bitfield! {
72 #[derive(Clone, Copy)]
73 pub struct RtcSleepConfig(u32);
75 impl Debug;
76 pub lslp_mem_inf_fpu, set_lslp_mem_inf_fpu: 0;
78 pub rtc_mem_inf_fpu, set_rtc_mem_inf_fpu: 1;
80 pub rtc_mem_inf_follow_cpu, set_rtc_mem_inf_follow_cpu: 2;
82 pub rtc_fastmem_pd_en, set_rtc_fastmem_pd_en: 3;
84 pub rtc_slowmem_pd_en, set_rtc_slowmem_pd_en: 4;
86 pub rtc_peri_pd_en, set_rtc_peri_pd_en: 5;
88 pub wifi_pd_en, set_wifi_pd_en: 6;
90 pub int_8m_pd_en, set_int_8m_pd_en: 7;
92 pub rom_mem_pd_en, set_rom_mem_pd_en: 8;
94 pub deep_slp, set_deep_slp: 9;
96 pub wdt_flashboot_mod_en, set_wdt_flashboot_mod_en: 10;
98 pub u8, dig_dbias_wak, set_dig_dbias_wak: 13, 11;
100 pub u8, dig_dbias_slp, set_dig_dbias_slp: 16, 14;
102 pub u8, rtc_dbias_wak, set_rtc_dbias_wak: 19, 17;
104 pub u8, rtc_dbias_slp, set_rtc_dbias_slp: 22, 20;
106 pub lslp_meminf_pd, set_lslp_meminf_pd: 23;
108 pub vddsdio_pd_en, set_vddsdio_pd_en: 24;
110 pub xtal_fpu, set_xtal_fpu: 25;
112 pub deep_slp_reject, set_deep_slp_reject: 26;
114 pub light_slp_reject, set_light_slp_reject: 27;
116}
117
118impl Default for RtcSleepConfig {
119 fn default() -> Self {
120 let mut cfg = Self(Default::default());
121 cfg.set_lslp_meminf_pd(true);
122 cfg.set_deep_slp_reject(true);
123 cfg.set_light_slp_reject(true);
124 cfg.set_dig_dbias_wak(RTC_CNTL_DBIAS_1V10);
125 cfg.set_dig_dbias_slp(RTC_CNTL_DBIAS_1V10);
126 cfg.set_rtc_dbias_wak(RTC_CNTL_DBIAS_1V10);
127 cfg.set_rtc_dbias_slp(RTC_CNTL_DBIAS_1V10);
128 cfg
129 }
130}
131
132impl RtcSleepConfig {
133 pub fn deep() -> Self {
135 let mut cfg = Self::default();
136 cfg.set_deep_slp(true);
137 cfg.set_dig_dbias_slp(RTC_CNTL_DBIAS_0V90);
138 cfg.set_rtc_dbias_slp(RTC_CNTL_DBIAS_0V90);
140 cfg.set_vddsdio_pd_en(true);
141 cfg.set_int_8m_pd_en(true);
142 cfg.set_xtal_fpu(false);
143 cfg.set_wifi_pd_en(true);
144 cfg.set_rom_mem_pd_en(true);
145 cfg.set_rtc_peri_pd_en(true);
146 cfg.set_rtc_fastmem_pd_en(true);
147 cfg.set_rtc_slowmem_pd_en(true);
148 cfg
149 }
150
151 pub(crate) fn is_deep_sleep(&self) -> bool {
152 self.deep_slp()
153 }
154
155 pub(crate) fn set_sleep_kind(&mut self, kind: SleepKind) {
156 self.set_deep_slp(kind == SleepKind::Deep);
157 }
158
159 pub(crate) fn base_settings(_rtc: &Rtc<'_>) {
160 let rtc_cntl = LPWR::regs();
162
163 rtc_cntl.options0().modify(|_, w| {
164 w.bias_core_force_pu().clear_bit();
165 w.bias_core_folw_8m().set_bit();
166 w.bias_i2c_force_pu().clear_bit();
167 w.bias_i2c_folw_8m().set_bit();
168 w.bias_force_nosleep().clear_bit();
169 w.bias_sleep_folw_8m().set_bit();
170 w.xtl_force_pu().clear_bit()
171 });
172
173 rtc_cntl.reg().modify(|_, w| {
174 w.force_pu().clear_bit();
175 w.dboost_force_pu().clear_bit();
176 w.dboost_force_pd().set_bit()
177 });
178
179 rtc_cntl.pwc().modify(|_, w| {
180 w.slowmem_force_pu().clear_bit();
181 w.fastmem_force_pu().clear_bit();
182 w.force_noiso().clear_bit();
183 w.slowmem_force_noiso().clear_bit();
184 w.fastmem_force_noiso().clear_bit()
185 });
186
187 rtc_cntl.dig_pwc().modify(|_, w| {
188 w.dg_wrap_force_pu().clear_bit();
189 w.wifi_force_pu().clear_bit();
190 w.wifi_force_pd().set_bit();
191 w.inter_ram4_force_pu().clear_bit();
192 w.inter_ram3_force_pu().clear_bit();
193 w.inter_ram2_force_pu().clear_bit();
194 w.inter_ram1_force_pu().clear_bit();
195 w.inter_ram0_force_pu().clear_bit();
196 w.rom0_force_pu().clear_bit();
197 w.lslp_mem_force_pu().clear_bit()
198 });
199
200 rtc_cntl.dig_iso().modify(|_, w| {
201 w.dg_wrap_force_noiso().clear_bit();
202 w.wifi_force_noiso().clear_bit();
203 w.wifi_force_iso().set_bit();
204 w.inter_ram4_force_noiso().clear_bit();
205 w.inter_ram3_force_noiso().clear_bit();
206 w.inter_ram2_force_noiso().clear_bit();
207 w.inter_ram1_force_noiso().clear_bit();
208 w.inter_ram0_force_noiso().clear_bit();
209 w.rom0_force_noiso().clear_bit();
210 w.dg_pad_force_unhold().clear_bit();
211 w.dg_pad_force_noiso().clear_bit()
212 });
213
214 rtc_cntl.int_ena().modify(|_, w| w.brown_out().set_bit());
215
216 rtc_cntl
219 .bias_conf()
220 .modify(|_, w| unsafe { w.dbg_atten().bits(RTC_CNTL_DBG_ATTEN_DEFAULT) });
221 rtc_cntl.int_clr().write(|w| unsafe { w.bits(u32::MAX) });
222 }
223
224 pub(crate) fn apply(&self) {
225 unsafe {
227 let rtc_cntl = LPWR::regs();
228
229 rtc_cntl.timer5().modify(|_, w| {
230 w.min_slp_val().bits(RTC_CNTL_MIN_SLP_VAL_MIN);
231 w.rtcmem_powerup_timer().bits(RTC_MEM_POWERUP_CYCLES);
233 w.rtcmem_wait_timer().bits(RTC_MEM_WAIT_CYCLES)
234 });
235
236 rtc_cntl.timer3().modify(|_, w| {
237 w.rom_ram_powerup_timer().bits(ROM_RAM_POWERUP_CYCLES);
239 w.rom_ram_wait_timer().bits(ROM_RAM_WAIT_CYCLES);
240 w.wifi_powerup_timer().bits(WIFI_POWERUP_CYCLES);
242 w.wifi_wait_timer().bits(WIFI_WAIT_CYCLES)
243 });
244
245 rtc_cntl.timer4().modify(|_, w| {
246 w.powerup_timer().bits(RTC_POWERUP_CYCLES);
248 w.wait_timer().bits(RTC_WAIT_CYCLES);
249 w.dg_wrap_powerup_timer().bits(DG_WRAP_POWERUP_CYCLES);
251 w.dg_wrap_wait_timer().bits(DG_WRAP_WAIT_CYCLES)
252 });
253
254 rtc_cntl
255 .dig_pwc()
256 .modify(|_, w| w.lslp_mem_force_pu().bit(self.lslp_mem_inf_fpu()));
257
258 if self.lslp_meminf_pd() {
260 rtc_cntl
261 .dig_pwc()
262 .modify(|_, w| w.lslp_mem_force_pu().clear_bit());
263
264 rtc_cntl.pwc().modify(|_, w| {
265 w.slowmem_force_pu().clear_bit();
266 w.fastmem_force_pu().clear_bit()
267 });
268
269 DPORT::regs()
272 .mem_pd_mask()
273 .modify(|_, w| w.lslp_mem_pd_mask().clear_bit());
274
275 I2S0::regs()
276 .pd_conf()
277 .modify(|_, w| w.plc_mem_force_pu().clear_bit().fifo_force_pu().clear_bit());
278
279 BB::regs()
280 .bbpd_ctrl()
281 .modify(|_, w| w.fft_force_pu().clear_bit().dc_est_force_pu().clear_bit());
282
283 NRX::regs().nrxpd_ctrl().modify(|_, w| {
284 w.rx_rot_force_pu().clear_bit();
285 w.vit_force_pu().clear_bit();
286 w.demap_force_pu().clear_bit()
287 });
288 }
296
297 rtc_cntl.pwc().modify(|_, w| {
298 w.slowmem_folw_cpu().bit(self.rtc_mem_inf_follow_cpu());
299 w.fastmem_folw_cpu().bit(self.rtc_mem_inf_follow_cpu());
300
301 w.fastmem_force_pu().bit(!self.rtc_fastmem_pd_en());
306 w.fastmem_force_lpu().bit(!self.rtc_fastmem_pd_en());
307 w.fastmem_force_noiso().bit(!self.rtc_fastmem_pd_en());
308 w.slowmem_pd_en().bit(self.rtc_slowmem_pd_en());
309 w.slowmem_force_pu().bit(!self.rtc_slowmem_pd_en());
310 w.slowmem_force_noiso().bit(!self.rtc_slowmem_pd_en());
311 w.slowmem_force_lpu().bit(!self.rtc_slowmem_pd_en());
312 w.pd_en().bit(self.rtc_peri_pd_en())
313 });
314
315 if self.wifi_pd_en() {
317 rtc_cntl
318 .dig_iso()
319 .modify(|_, w| w.wifi_force_noiso().clear_bit());
320 rtc_cntl.dig_pwc().modify(|_, w| {
321 w.wifi_force_pu().clear_bit();
322 w.wifi_pd_en().set_bit()
323 });
324 } else {
325 rtc_cntl.dig_pwc().modify(|_, w| w.wifi_pd_en().clear_bit());
326 }
327
328 if self.deep_slp() {
329 rtc_cntl.dig_iso().modify(|_, w| {
330 w.dg_wrap_force_noiso().clear_bit();
331 w.wifi_force_noiso().clear_bit();
332 w.dg_pad_force_iso().clear_bit();
333 w.dg_pad_force_noiso().clear_bit()
334 });
335
336 rtc_cntl.dig_pwc().modify(|_, w| {
337 w.dg_wrap_pd_en().set_bit();
338 w.dg_wrap_force_pu().clear_bit();
339 w.dg_wrap_force_pd().clear_bit()
340 });
341
342 rtc_cntl.options0().modify(|_, w| {
343 w.bias_force_nosleep().clear_bit();
344 w.bb_i2c_force_pu().clear_bit()
345 });
346
347 rtc_cntl.ana_conf().modify(|_, w| {
348 w.ckgen_i2c_pu().clear_bit();
349 w.pll_i2c_pu().clear_bit();
350 w.rfrx_pbus_pu().clear_bit();
351 w.txrf_i2c_pu().clear_bit()
352 });
353 } else {
354 rtc_cntl
355 .dig_pwc()
356 .modify(|_, w| w.dg_wrap_pd_en().clear_bit());
357 }
358
359 let dbg_atten = if self.deep_slp() && self.int_8m_pd_en() {
361 RTC_CNTL_DBG_ATTEN_DEFAULT
362 } else {
363 0
364 };
365 rtc_cntl
366 .bias_conf()
367 .modify(|_, w| w.dbg_atten().bits(dbg_atten));
368
369 rtc_cntl
370 .options0()
371 .modify(|_, w| w.xtl_force_pu().bit(self.xtal_fpu()));
372
373 rtc_cntl
374 .clk_conf()
375 .modify(|_, w| w.ck8m_force_pu().bit(!self.int_8m_pd_en()));
376
377 rtc_cntl.sdio_conf().modify(|_, w| {
380 w.sdio_force().clear_bit();
381 w.sdio_pd_en().bit(self.vddsdio_pd_en())
382 });
383
384 rtc_cntl.reg().modify(|_, w| {
385 w.dbias_slp().bits(self.rtc_dbias_slp());
386 w.dbias_wak().bits(self.rtc_dbias_wak());
387 w.dig_dbias_slp().bits(self.dig_dbias_slp());
388 w.dig_dbias_wak().bits(self.dig_dbias_wak())
389 });
390
391 rtc_cntl.slp_reject_conf().modify(|_, w| {
392 w.deep_slp_reject_en().bit(self.deep_slp_reject());
393 w.light_slp_reject_en().bit(self.light_slp_reject())
394 });
395 }
396 }
397
398 #[crate::ram]
403 pub(crate) fn start_sleep(&self, wakeup_mask: u32, reject_mask: u32) -> SavedState {
404 let cpu_clk = super::SleepCpuClock::switch_to_xtal();
405
406 LPWR::regs()
407 .reset_state()
408 .modify(|_, w| w.procpu_stat_vector_sel().set_bit());
409
410 LPWR::regs()
412 .wakeup_state()
413 .modify(|_, w| unsafe { w.wakeup_ena().bits(wakeup_mask as u16) });
414
415 let rejects = enumset::EnumSet::<WakeupSource>::from_u32_truncated(reject_mask);
418 LPWR::regs().slp_reject_conf().modify(|_, w| {
419 w.gpio_reject_en().bit(rejects.contains(WakeupSource::Gpio));
420 w.sdio_reject_en().bit(rejects.contains(WakeupSource::Sdio))
421 });
422
423 LPWR::regs().int_clr().write(|w| {
426 w.slp_reject().clear_bit_by_one();
427 w.slp_wakeup().clear_bit_by_one()
428 });
429
430 SavedState { cpu_clk }
431 }
432
433 #[inline(always)]
437 pub(crate) fn enter_sleep(&self) -> bool {
438 LPWR::regs().state0().modify(|_, w| w.sleep_en().set_bit());
439 super::wait_for_sleep_result()
440 }
441
442 pub(crate) fn finish_sleep(&self) {
443 LPWR::regs().int_clr().write(|w| {
445 w.slp_reject().clear_bit_by_one();
446 w.slp_wakeup().clear_bit_by_one()
447 });
448
449 LPWR::regs()
452 .bias_conf()
453 .modify(|_, w| unsafe { w.dbg_atten().bits(RTC_CNTL_DBG_ATTEN_DEFAULT) });
454 }
455}
456
457#[must_use]
459pub(crate) struct SavedState {
460 cpu_clk: super::SleepCpuClock,
461}
462
463impl SavedState {
464 pub(crate) fn restore(self) {
466 self.cpu_clk.restore();
467 }
468}