Skip to main content

esp_hal/rtc_cntl/sleep/
esp32.rs

1use super::SleepKind;
2use crate::{
3    peripherals::{BB, DPORT, I2S0, LPWR, NRX},
4    rtc_cntl::{Rtc, WakeupSource},
5};
6
7// Approximate mapping of voltages to RTC_CNTL_DBIAS_WAK, RTC_CNTL_DBIAS_SLP,
8// RTC_CNTL_DIG_DBIAS_WAK, RTC_CNTL_DIG_DBIAS_SLP values.
9// Valid if RTC_CNTL_DBG_ATTEN is 0.
10/// RTC digital bias setting corresponding to 0.90V.
11pub const RTC_CNTL_DBIAS_0V90: u8 = 0;
12/// RTC digital bias setting corresponding to 0.95V.
13pub const RTC_CNTL_DBIAS_0V95: u8 = 1;
14/// RTC digital bias setting corresponding to 1.00V.
15pub const RTC_CNTL_DBIAS_1V00: u8 = 2;
16/// RTC digital bias setting corresponding to 1.05V.
17pub const RTC_CNTL_DBIAS_1V05: u8 = 3;
18/// RTC digital bias setting corresponding to 1.10V.
19pub const RTC_CNTL_DBIAS_1V10: u8 = 4;
20/// RTC digital bias setting corresponding to 1.15V.
21pub const RTC_CNTL_DBIAS_1V15: u8 = 5;
22/// RTC digital bias setting corresponding to 1.20V.
23pub const RTC_CNTL_DBIAS_1V20: u8 = 6;
24/// RTC digital bias setting corresponding to 1.25V.
25pub const RTC_CNTL_DBIAS_1V25: u8 = 7;
26
27// Various delays to be programmed into power control state machines
28/// Time (in microseconds) for waiting the XTL buffer to stabilize during sleep.
29pub const RTC_CNTL_XTL_BUF_WAIT_SLP_US: u32 = 1000;
30/// Cycles to wait for PLL buffer stabilization.
31pub const RTC_CNTL_PLL_BUF_WAIT_SLP_CYCLES: u8 = 1;
32/// Cycles to wait for the 8 MHz clock to stabilize.
33pub const RTC_CNTL_CK8M_WAIT_SLP_CYCLES: u8 = 4;
34/// Delay in cycles for wakeup signal to be applied.
35pub const RTC_CNTL_WAKEUP_DELAY_CYCLES: u8 = 7;
36/// Power-up cycles for other blocks.
37pub const RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES: u8 = 1;
38/// Waits cycles for other blocks.
39pub const RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES: u16 = 1;
40/// Minimum sleep value (in cycles).
41pub const RTC_CNTL_MIN_SLP_VAL_MIN: u8 = 128;
42/// Default debug attenuation value.
43pub const RTC_CNTL_DBG_ATTEN_DEFAULT: u8 = 3;
44
45/// Power-up cycles for RTC memory.
46pub const RTC_MEM_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
47/// Waits cycles for RTC memory.
48pub const RTC_MEM_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
49/// Power-up cycles for ROM and RAM.
50pub const ROM_RAM_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
51/// Waits cycles for ROM and RAM.
52pub const ROM_RAM_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
53/// Power-up cycles for Wi-Fi.
54pub const WIFI_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
55/// Waits cycles for Wi-Fi.
56pub const WIFI_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
57/// Power-up cycles for RTC components.
58pub const RTC_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
59/// Waits cycles for RTC components.
60pub const RTC_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
61/// Power-up cycles for the digital wrap components.
62pub const DG_WRAP_POWERUP_CYCLES: u8 = RTC_CNTL_OTHER_BLOCKS_POWERUP_CYCLES;
63/// Waits cycles for the digital wrap components.
64pub const DG_WRAP_WAIT_CYCLES: u16 = RTC_CNTL_OTHER_BLOCKS_WAIT_CYCLES;
65
66/// Default wait cycles for the 8 MHz clock.
67pub const RTC_CNTL_CK8M_WAIT_DEFAULT: u8 = 20;
68/// Default wait cycles to enable the 8 MHz clock.
69pub const RTC_CK8M_ENABLE_WAIT_DEFAULT: u8 = 5;
70
71bitfield::bitfield! {
72    #[derive(Clone, Copy)]
73    /// Configuration for the RTC sleep behavior.
74    pub struct RtcSleepConfig(u32);
75    impl Debug;
76    /// force normal voltage in sleep mode (digital domain memory)
77    pub lslp_mem_inf_fpu, set_lslp_mem_inf_fpu: 0;
78    /// force normal voltage in sleep mode (RTC memory)
79    pub rtc_mem_inf_fpu, set_rtc_mem_inf_fpu: 1;
80    /// keep low voltage in sleep mode (even if ULP/touch is used)
81    pub rtc_mem_inf_follow_cpu, set_rtc_mem_inf_follow_cpu: 2;
82    /// power down RTC fast memory
83    pub rtc_fastmem_pd_en, set_rtc_fastmem_pd_en: 3;
84    /// power down RTC slow memory
85    pub rtc_slowmem_pd_en, set_rtc_slowmem_pd_en: 4;
86    /// power down RTC peripherals
87    pub rtc_peri_pd_en, set_rtc_peri_pd_en: 5;
88    /// power down WiFi
89    pub wifi_pd_en, set_wifi_pd_en: 6;
90    /// Powers down Internal 8M oscillator.
91    pub int_8m_pd_en, set_int_8m_pd_en: 7;
92    /// power down main RAM and ROM
93    pub rom_mem_pd_en, set_rom_mem_pd_en: 8;
94    /// power down digital domain
95    pub deep_slp, set_deep_slp: 9;
96    /// enable WDT flashboot mode
97    pub wdt_flashboot_mod_en, set_wdt_flashboot_mod_en: 10;
98    /// bias for digital domain, in active mode
99    pub u8, dig_dbias_wak, set_dig_dbias_wak: 13, 11;
100    /// bias for digital domain, in sleep mode
101    pub u8, dig_dbias_slp, set_dig_dbias_slp: 16, 14;
102    /// bias for RTC domain, in active mode
103    pub u8, rtc_dbias_wak, set_rtc_dbias_wak: 19, 17;
104    /// bias for RTC domain, in sleep mode
105    pub u8, rtc_dbias_slp, set_rtc_dbias_slp: 22, 20;
106    /// remove all peripheral force power up flags
107    pub lslp_meminf_pd, set_lslp_meminf_pd: 23;
108    /// power down VDDSDIO regulator
109    pub vddsdio_pd_en, set_vddsdio_pd_en: 24;
110    /// keep main XTAL powered up in sleep
111    pub xtal_fpu, set_xtal_fpu: 25;
112    /// enable deep sleep reject
113    pub deep_slp_reject, set_deep_slp_reject: 26;
114    /// enable light sleep reject
115    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    /// Configures the RTC for deep sleep mode.
134    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        // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/port/esp32/rtc_sleep.c#L110
139        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        // settings derived from esp-idf after basic boot
161        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        // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/port/esp32/rtc_init.c#L40
217        // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/port/esp32/rtc_init.c#L109-L110
218        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        // like esp-idf rtc_sleep_init()
226        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                // set rtc memory timer
232                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                // set rom&ram timer
238                w.rom_ram_powerup_timer().bits(ROM_RAM_POWERUP_CYCLES);
239                w.rom_ram_wait_timer().bits(ROM_RAM_WAIT_CYCLES);
240                // set wifi timer
241                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                // set rtc peri timer
247                w.powerup_timer().bits(RTC_POWERUP_CYCLES);
248                w.wait_timer().bits(RTC_WAIT_CYCLES);
249                // set digital wrap timer
250                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            // remove all peripheral force power up flags
259            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                // esp-idf also clears these:
270
271                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                // (&*esp32::FE::ptr()).gen_ctrl.modify(|_, w| w
289                //     .iq_est_force_pu().clear_bit()
290                // );
291                //
292                // (&*esp32::FE2::ptr()).tx_interp_ctrl.modify(|_, w| w
293                //     .inf_force_pu().clear_bit()
294                // );
295            }
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                // TODO: does this need to be optional based on if there is something stored in
302                // fastmem?
303                //.fastmem_pd_en().bit(self.rtc_fastmem_pd_en())
304
305                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            // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/port/esp32/rtc_sleep.c#L198-L204
316            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            // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/port/esp32/rtc_sleep.c#L111
360            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            // enable VDDSDIO control by state machine
378
379            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    /// Configures the wakeup and reject sources of the sleep.
399    ///
400    /// [`Self::enter_sleep`] requests the sleep after this call. The caller calls
401    /// [`SavedState::restore`] on the return value after [`Self::finish_sleep`].
402    #[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        // set bits for what can wake us up
411        LPWR::regs()
412            .wakeup_state()
413            .modify(|_, w| unsafe { w.wakeup_ena().bits(wakeup_mask as u16) });
414
415        // esp32 has no reject-source mask. GPIO and SDIO have one reject enable each, and no other
416        // source can reject a sleep. The reject bits that `apply` wrote arm these enables.
417        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        // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/port/esp32/rtc_sleep.c#L288-L289
424        // https://github.com/espressif/esp-idf/blob/fff9895c82d/components/esp_hw_support/port/esp32/rtc_sleep.c#L316-L317
425        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    /// Requests the sleep.
434    ///
435    /// The caller waits for the result of the request.
436    #[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        // In deep sleep mode, we never get here
444        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        // restore DBG_ATTEN to the default value
450
451        LPWR::regs()
452            .bias_conf()
453            .modify(|_, w| unsafe { w.dbg_atten().bits(RTC_CNTL_DBG_ATTEN_DEFAULT) });
454    }
455}
456
457/// The state that [`RtcSleepConfig::start_sleep`] changes for the length of the sleep.
458#[must_use]
459pub(crate) struct SavedState {
460    cpu_clk: super::SleepCpuClock,
461}
462
463impl SavedState {
464    /// Restores the state. Runs after a wakeup and after a rejected sleep.
465    pub(crate) fn restore(self) {
466        self.cpu_clk.restore();
467    }
468}