esp_rom_sys/rom/
mod.rs

1//! # Wrappers for selected ROM functions
2//!
3//! ## Overview
4//!
5//! For some selected ROM functions safe wrappers are provided for convenience.
6
7#[cfg(any(rom_crc_be, rom_crc_le))]
8pub mod crc;
9#[cfg(any(rom_md5_bsd, rom_md5_mbedtls))]
10pub mod md5;
11pub mod spiflash;
12
13/// Busy-loop CPU for the given amount of microseconds.
14#[inline(always)]
15pub fn ets_delay_us(us: u32) {
16    unsafe extern "C" {
17        fn ets_delay_us(us: u32);
18    }
19
20    unsafe { ets_delay_us(us) };
21}
22
23/// Set the real CPU ticks per us to the ets, so that ets_delay_us will be
24/// accurate. Call this function when CPU frequency is changed.
25#[inline(always)]
26pub fn ets_update_cpu_frequency_rom(ticks_per_us: u32) {
27    unsafe extern "C" {
28        fn ets_update_cpu_frequency(ticks_per_us: u32);
29    }
30
31    unsafe { ets_update_cpu_frequency(ticks_per_us) };
32}
33
34/// Get the reset reason for CPU.
35#[inline(always)]
36pub fn rtc_get_reset_reason(cpu_num: u32) -> u32 {
37    unsafe extern "C" {
38        fn rtc_get_reset_reason(cpu_num: u32) -> u32;
39    }
40
41    unsafe { rtc_get_reset_reason(cpu_num) }
42}
43
44/// Software Reset digital core.
45#[inline(always)]
46pub fn software_reset_cpu(cpu_num: u32) {
47    unsafe extern "C" {
48        fn software_reset_cpu(cpu_num: u32);
49    }
50
51    unsafe { software_reset_cpu(cpu_num) };
52}
53
54/// Software Reset digital core.
55#[inline(always)]
56pub fn software_reset() -> ! {
57    unsafe extern "C" {
58        fn software_reset() -> !;
59    }
60
61    unsafe { software_reset() }
62}
63
64/// Set App cpu Entry code, code can be called in PRO CPU.
65#[cfg(esp32s3)]
66#[inline(always)]
67pub fn ets_set_appcpu_boot_addr(boot_addr: u32) {
68    unsafe extern "C" {
69        fn ets_set_appcpu_boot_addr(boot_addr: u32);
70    }
71
72    unsafe { ets_set_appcpu_boot_addr(boot_addr) };
73}
74
75// libphy.a can pull this in on some chips, we provide it here
76// so that either ieee or esp-radio gets it for free without duplicating in both
77#[unsafe(no_mangle)]
78extern "C" fn rtc_clk_xtal_freq_get() -> i32 {
79    cfg_if::cfg_if! {
80        if #[cfg(any(esp32c6, esp32h2))] {
81            unsafe extern "C" {
82                fn ets_clk_get_xtal_freq() -> i32;
83            }
84            (unsafe { ets_clk_get_xtal_freq() }) / 1_000_000
85        } else if #[cfg(any(esp32s2, esp32s3, esp32c3))] {
86            unsafe extern "C" {
87                fn ets_get_xtal_freq() -> i32;
88            }
89            (unsafe { ets_get_xtal_freq() }) / 1_000_000
90        } else if #[cfg(any(esp32, esp32c2))] {
91            // just rely on RTC_CNTL_STORE4
92            regs!(RTC_CNTL).store4().read().bits() as i32
93        } else if #[cfg(any(esp32c5, esp32c61))]  {
94            // PCR_CLK_XTAL_FREQ updates its value based on EFUSE_XTAL_48M_SEL.
95            regs!(PCR).sysclk_conf().read().clk_xtal_freq().bits() as i32
96        } else {
97            compile_error!("rtc_clk_xtal_freq_get not implemented for this chip");
98        }
99    }
100}