esp_hal/rtc_cntl/rtc/
esp32c3.rs1use strum::FromRepr;
2
3use crate::{
4    clock::{RtcClock, RtcFastClock, RtcSlowClock},
5    peripherals::{APB_CTRL, EXTMEM, LPWR, SPI0, SPI1, SYSTEM},
6    rtc_cntl::RtcCalSel,
7    soc::regi2c,
8};
9
10pub(crate) fn init() {
11    let rtc_cntl = LPWR::regs();
12
13    regi2c::I2C_DIG_REG_XPD_DIG_REG.write_field(0);
14    regi2c::I2C_DIG_REG_XPD_RTC_REG.write_field(0);
15
16    rtc_cntl.ana_conf().modify(|_, w| w.pvtmon_pu().clear_bit());
17
18    unsafe {
19        rtc_cntl
20            .timer1()
21            .modify(|_, w| w.pll_buf_wait().bits(20u8).ck8m_wait().bits(20u8));
22        rtc_cntl.timer5().modify(|_, w| w.min_slp_val().bits(2u8));
23
24        rtc_cntl.timer3().modify(|_, w| {
26            w.wifi_powerup_timer().bits(1u8);
27            w.wifi_wait_timer().bits(1u16);
28            w.bt_powerup_timer().bits(1u8);
29            w.bt_wait_timer().bits(1u16)
30        });
31        rtc_cntl.timer4().modify(|_, w| {
32            w.cpu_top_powerup_timer().bits(1u8);
33            w.cpu_top_wait_timer().bits(1u16);
34            w.dg_wrap_powerup_timer().bits(1u8);
35            w.dg_wrap_wait_timer().bits(1u16)
36        });
37        rtc_cntl.timer6().modify(|_, w| {
38            w.dg_peri_powerup_timer().bits(1u8);
39            w.dg_peri_wait_timer().bits(1u16)
40        });
41    }
42
43    calibrate_ocode();
44
45    set_rtc_dig_dbias();
46
47    clock_control_init();
48
49    power_control_init();
50
51    unsafe {
52        rtc_cntl.int_ena().write(|w| w.bits(0));
53        rtc_cntl.int_clr().write(|w| w.bits(u32::MAX));
54    }
55
56    regi2c::I2C_ULP_IR_FORCE_XPD_CK.write_field(0);
57
58    rtc_cntl.clk_conf().modify(|_, w| w.enb_ck8m().clear_bit());
60    rtc_cntl
61        .timer1()
62        .modify(|_, w| unsafe { w.ck8m_wait().bits(5) });
63
64    crate::rom::ets_delay_us(50);
66    RtcClock::set_fast_freq(RtcFastClock::RcFast);
67    RtcClock::set_slow_freq(RtcSlowClock::RcSlow);
68}
69
70pub(crate) fn configure_clock() {
71    let cal_val = loop {
72        let res = RtcClock::calibrate(RtcCalSel::RtcMux, 1024);
73        if res != 0 {
74            break res;
75        }
76    };
77
78    LPWR::regs().store1().write(|w| unsafe { w.bits(cal_val) });
79}
80
81fn calibrate_ocode() {}
82
83fn set_rtc_dig_dbias() {}
84
85fn clock_control_init() {
87    let extmem = EXTMEM::regs();
88    let spi_mem_0 = SPI0::regs();
89    let spi_mem_1 = SPI1::regs();
90
91    extmem
93        .cache_mmu_power_ctrl()
94        .modify(|_, w| w.cache_mmu_mem_force_on().clear_bit());
95
96    extmem
98        .icache_tag_power_ctrl()
99        .modify(|_, w| w.icache_tag_mem_force_on().clear_bit());
100
101    spi_mem_0.clock_gate().modify(|_, w| w.clk_en().clear_bit());
103    spi_mem_1.clock_gate().modify(|_, w| w.clk_en().clear_bit());
104}
105
106fn power_control_init() {
108    let rtc_cntl = LPWR::regs();
109    let system = SYSTEM::regs();
110    rtc_cntl
111        .clk_conf()
112        .modify(|_, w| w.ck8m_force_pu().clear_bit());
113
114    rtc_cntl
117        .options0()
118        .modify(|_, w| w.xtl_force_pu().clear_bit());
119
120    rtc_cntl.ana_conf().modify(|_, w| {
122        w.plla_force_pu().clear_bit();
123        w.plla_force_pd().set_bit();
124        w.reset_por_force_pd().clear_bit()
127    });
128
129    rtc_cntl.options0().modify(|_, w| {
131        w.bbpll_force_pu().clear_bit();
132        w.bbpll_i2c_force_pu().clear_bit();
133        w.bb_i2c_force_pu().clear_bit()
134    });
135    rtc_cntl.rtc_cntl().modify(|_, w| {
136        w.regulator_force_pu().clear_bit();
137        w.dboost_force_pu().clear_bit();
138        w.dboost_force_pd().set_bit()
139    });
140
141    system
145        .mem_pd_mask()
146        .modify(|_, w| w.lslp_mem_pd_mask().clear_bit());
147
148    rtc_sleep_pu();
149
150    rtc_cntl.dig_pwc().modify(|_, w| {
151        w.dg_wrap_force_pu().clear_bit();
152        w.wifi_force_pu().clear_bit();
153        w.bt_force_pu().clear_bit();
154        w.cpu_top_force_pu().clear_bit();
155        w.dg_peri_force_pu().clear_bit()
156    });
157    rtc_cntl.dig_iso().modify(|_, w| {
158        w.dg_wrap_force_noiso().clear_bit();
159        w.wifi_force_noiso().clear_bit();
160        w.bt_force_noiso().clear_bit();
161        w.cpu_top_force_noiso().clear_bit();
162        w.dg_peri_force_noiso().clear_bit()
163    });
164
165    system
167        .cpu_per_conf()
168        .modify(|_, w| w.cpu_wait_mode_force_on().clear_bit());
169
170    rtc_cntl.dig_iso().modify(|_, w| {
173        w.dg_pad_force_unhold().clear_bit();
174        w.dg_pad_force_noiso().clear_bit()
175    });
176}
177
178fn rtc_sleep_pu() {
180    let rtc_cntl = LPWR::regs();
181    let apb_ctrl = APB_CTRL::regs();
182
183    rtc_cntl.dig_pwc().modify(|_, w| {
184        w.lslp_mem_force_pu().clear_bit();
185        w.fastmem_force_lpu().clear_bit()
186    });
187
188    apb_ctrl.front_end_mem_pd().modify(|_, w| {
189        w.dc_mem_force_pu().clear_bit();
190        w.pbus_mem_force_pu().clear_bit();
191        w.agc_mem_force_pu().clear_bit()
192    });
193    apb_ctrl.mem_power_up().modify(|_, w| unsafe {
194        w.sram_power_up().bits(0u8);
195        w.rom_power_up().bits(0u8)
196    });
197}
198
199#[derive(Debug, Clone, Copy, PartialEq, Eq, FromRepr)]
209pub enum SocResetReason {
210    ChipPowerOn   = 0x01,
216    CoreSw        = 0x03,
218    CoreDeepSleep = 0x05,
220    CoreMwdt0     = 0x07,
222    CoreMwdt1     = 0x08,
224    CoreRtcWdt    = 0x09,
226    Cpu0Mwdt0     = 0x0B,
228    Cpu0Sw        = 0x0C,
230    Cpu0RtcWdt    = 0x0D,
232    SysBrownOut   = 0x0F,
234    SysRtcWdt     = 0x10,
236    Cpu0Mwdt1     = 0x11,
238    SysSuperWdt   = 0x12,
240    SysClkGlitch  = 0x13,
242    CoreEfuseCrc  = 0x14,
244    CoreUsbUart   = 0x15,
246    CoreUsbJtag   = 0x16,
248    CorePwrGlitch = 0x17,
250}