esp_hal/rtc_cntl/rtc/esp32s2.rs
1use strum::FromRepr;
2
3use crate::{
4 clock::XtalClock,
5 peripherals::LPWR,
6 rtc_cntl::{RtcCalSel, RtcClock, RtcFastClock, RtcSlowClock},
7};
8
9pub(crate) fn init() {}
10
11pub(crate) fn configure_clock() {
12 assert!(matches!(RtcClock::xtal_freq(), XtalClock::_40M));
13
14 RtcClock::set_fast_freq(RtcFastClock::RtcFastClock8m);
15
16 let cal_val = loop {
17 RtcClock::set_slow_freq(RtcSlowClock::RtcSlowClockRtc);
18
19 let res = RtcClock::calibrate(RtcCalSel::RtcCalRtcMux, 1024);
20 if res != 0 {
21 break res;
22 }
23 };
24
25 LPWR::regs().store1().write(|w| unsafe { w.bits(cal_val) });
26}
27
28// Terminology:
29//
30// CPU Reset: Reset CPU core only, once reset done, CPU will execute from
31// reset vector
32// Core Reset: Reset the whole digital system except RTC sub-system
33// System Reset: Reset the whole digital system, including RTC sub-system
34// Chip Reset: Reset the whole chip, including the analog part
35
36/// SOC Reset Reason.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, FromRepr)]
38pub enum SocResetReason {
39 /// Power on reset
40 ///
41 /// In ESP-IDF this value (0x01) can *also* be `ChipBrownOut` or
42 /// `ChipSuperWdt`, however that is not really compatible with Rust-style
43 /// enums.
44 ChipPowerOn = 0x01,
45 /// Software resets the digital core by RTC_CNTL_SW_SYS_RST
46 CoreSw = 0x03,
47 /// Deep sleep reset the digital core
48 CoreDeepSleep = 0x05,
49 /// Main watch dog 0 resets digital core
50 CoreMwdt0 = 0x07,
51 /// Main watch dog 1 resets digital core
52 CoreMwdt1 = 0x08,
53 /// RTC watch dog resets digital core
54 CoreRtcWdt = 0x09,
55 /// Main watch dog 0 resets CPU 0
56 Cpu0Mwdt0 = 0x0B,
57 /// Software resets CPU 0 by RTC_CNTL_SW_PROCPU_RST
58 Cpu0Sw = 0x0C,
59 /// RTC watch dog resets CPU 0
60 Cpu0RtcWdt = 0x0D,
61 /// VDD voltage is not stable and resets the digital core
62 SysBrownOut = 0x0F,
63 /// RTC watch dog resets digital core and rtc module
64 SysRtcWdt = 0x10,
65 /// Main watch dog 1 resets CPU 0
66 Cpu0Mwdt1 = 0x11,
67 /// Super watch dog resets the digital core and rtc module
68 SysSuperWdt = 0x12,
69 /// Glitch on clock resets the digital core and rtc module
70 SysClkGlitch = 0x13,
71 /// eFuse CRC error resets the digital core
72 CoreEfuseCrc = 0x14,
73}