crate::unstable_module! {
pub mod clocks;
}
pub(crate) mod cpu_control;
pub(crate) mod regi2c;
pub(crate) use esp32s31 as pac;
#[cfg(i2s_driver_supported)]
#[cfg_attr(not(feature = "unstable"), allow(unused))]
pub(crate) fn i2s_sclk_frequency() -> u32 {
clocks::pll_f160m_frequency()
}
pub(crate) fn enable_branch_predictor() {
const MHCR_RS: u32 = 1 << 4; const MHCR_BFE: u32 = 1 << 5; const MHCR_BTB: u32 = 1 << 12; unsafe {
core::arch::asm!("csrrs x0, 0x7c1, {0}", in(reg) MHCR_RS | MHCR_BFE | MHCR_BTB);
}
}
#[cfg(feature = "rt")]
pub(crate) fn riscv_preinit() {}
pub(crate) fn pre_init() {
if crate::system::Cpu::current() == crate::system::Cpu::ProCpu {
unsafe { cpu_control::internal_park_core(crate::system::Cpu::AppCpu, true) };
cpu_control::disable_core1();
}
crate::peripherals::LP_APM::regs()
.func_ctrl()
.write(|w| unsafe { w.bits(0) });
crate::peripherals::HP_APM::regs()
.func_ctrl()
.write(|w| unsafe { w.bits(0) });
crate::peripherals::HP_MEM_APM::regs()
.func_ctrl()
.write(|w| unsafe { w.bits(0) });
open_peri_pms();
let systimer = crate::peripherals::HP_SYS_CLKRST::regs().systimer_ctrl0();
systimer.modify(|_, w| w.apb_clk_en().set_bit());
systimer.modify(|_, w| w.rst_en().set_bit());
systimer.modify(|_, w| w.rst_en().clear_bit());
systimer.modify(|_, w| w.clk_en().set_bit());
}
fn open_peri_pms() {
write_pms_ctrl_range(pac::LP_PERI_PMS::PTR as usize, 0x00, 0x70);
write_pms_ctrl_range(pac::HP_PERI0_PMS::PTR as usize, 0x00, 0x78);
write_pms_ctrl_range(pac::HP_PERI1_PMS::PTR as usize, 0x00, 0x98);
}
fn write_pms_ctrl_range(base: usize, start: usize, end: usize) {
const OPEN: u32 = 0xFF;
let mut offset = start;
while offset <= end {
unsafe {
(base as *mut u32).add(offset / 4).write_volatile(OPEN);
}
offset += 4;
}
}
const CACHE_MAP_L1_DCACHE: u32 = 1 << 4;
#[doc(hidden)]
#[crate::ram]
pub unsafe fn cache_writeback_addr(addr: u32, size: u32) {
unsafe extern "C" {
fn Cache_WriteBack_Addr(map: u32, addr: u32, size: u32);
}
unsafe {
Cache_WriteBack_Addr(CACHE_MAP_L1_DCACHE, addr, size);
}
}
#[doc(hidden)]
#[crate::ram]
pub unsafe fn cache_invalidate_addr(addr: u32, size: u32) {
unsafe extern "C" {
fn Cache_Invalidate_Addr(map: u32, addr: u32, size: u32);
}
unsafe {
Cache_Invalidate_Addr(CACHE_MAP_L1_DCACHE, addr, size);
}
}