#![cfg_attr(esp32s31, allow(dead_code))]
use esp_sync::NonReentrantMutex;
cfg_select! {
all(soc_multi_core_enabled, feature = "unstable") => {
pub(crate) mod multi_core;
pub use multi_core::*;
}
_ => {}
}
implement_peripheral_clocks!();
impl Peripheral {
pub const fn try_from(value: u8) -> Option<Peripheral> {
if value >= Peripheral::COUNT as u8 {
return None;
}
Some(unsafe { core::mem::transmute::<u8, Peripheral>(value) })
}
}
struct RefCounts {
counts: [usize; Peripheral::COUNT],
}
impl RefCounts {
pub const fn new() -> Self {
Self {
counts: [0; Peripheral::COUNT],
}
}
}
static PERIPHERAL_REF_COUNT: NonReentrantMutex<RefCounts> =
NonReentrantMutex::new(RefCounts::new());
#[cfg_attr(not(feature = "rt"), expect(dead_code))]
pub(crate) fn disable_peripherals() {
PERIPHERAL_REF_COUNT.with(|refcounts| {
for p in Peripheral::KEEP_ENABLED {
refcounts.counts[*p as usize] += 1;
}
for p in Peripheral::ALL {
let ref_count = refcounts.counts[*p as usize];
if ref_count == 0 {
PeripheralClockControl::enable_forced_with_counts(*p, false, true, refcounts);
}
}
})
}
#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub(crate) struct PeripheralGuard {
peripheral: Peripheral,
}
impl PeripheralGuard {
pub(crate) fn new_with(p: Peripheral, init: fn()) -> Self {
PeripheralClockControl::request_peripheral(p, init);
Self { peripheral: p }
}
pub(crate) fn new(p: Peripheral) -> Self {
Self::new_with(p, || {})
}
}
impl Clone for PeripheralGuard {
fn clone(&self) -> Self {
Self::new(self.peripheral)
}
fn clone_from(&mut self, _source: &Self) {
}
}
impl Drop for PeripheralGuard {
fn drop(&mut self) {
PeripheralClockControl::disable(self.peripheral);
}
}
#[derive(Debug)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub(crate) struct GenericPeripheralGuard<const P: u8> {}
impl<const P: u8> GenericPeripheralGuard<P> {
pub(crate) fn new_with(init: fn()) -> Self {
let p = const { Peripheral::try_from(P).unwrap() };
PeripheralClockControl::request_peripheral(p, init);
Self {}
}
#[cfg_attr(esp32p4, allow(unused))]
#[cfg_attr(not(feature = "unstable"), allow(unused))]
pub(crate) fn new() -> Self {
Self::new_with(|| {})
}
}
impl<const P: u8> Clone for GenericPeripheralGuard<P> {
fn clone(&self) -> Self {
Self::new()
}
fn clone_from(&mut self, _source: &Self) {
}
}
impl<const P: u8> Drop for GenericPeripheralGuard<P> {
fn drop(&mut self) {
let peripheral = const { Peripheral::try_from(P).unwrap() };
PeripheralClockControl::disable(peripheral);
}
}
pub(crate) struct PeripheralClockControl;
impl PeripheralClockControl {
fn request_peripheral(p: Peripheral, init: fn()) {
PERIPHERAL_REF_COUNT.with(|ref_counts| {
if Self::enable_with_counts(p, ref_counts) {
unsafe { Self::reset_racey(p) };
init();
}
});
}
pub(crate) fn enable(peripheral: Peripheral) -> bool {
PERIPHERAL_REF_COUNT.with(|ref_counts| Self::enable_with_counts(peripheral, ref_counts))
}
fn enable_with_counts(peripheral: Peripheral, ref_counts: &mut RefCounts) -> bool {
Self::enable_forced_with_counts(peripheral, true, false, ref_counts)
}
pub(crate) fn disable(peripheral: Peripheral) -> bool {
PERIPHERAL_REF_COUNT.with(|ref_counts| {
Self::enable_forced_with_counts(peripheral, false, false, ref_counts)
})
}
fn enable_forced_with_counts(
peripheral: Peripheral,
enable: bool,
force: bool,
ref_counts: &mut RefCounts,
) -> bool {
let ref_count = &mut ref_counts.counts[peripheral as usize];
if !force {
let prev = *ref_count;
if enable {
*ref_count += 1;
trace!("Enable {:?} {} -> {}", peripheral, prev, *ref_count);
if prev > 0 {
return false;
}
} else {
assert!(prev != 0);
*ref_count -= 1;
trace!("Disable {:?} {} -> {}", peripheral, prev, *ref_count);
if prev > 1 {
return false;
}
};
} else if !enable {
assert!(*ref_count == 0);
}
debug!("Enable {:?} {}", peripheral, enable);
unsafe { enable_internal_racey(peripheral, enable) };
true
}
pub(crate) unsafe fn reset_racey(peripheral: Peripheral) {
debug!("Reset {:?}", peripheral);
unsafe {
assert_peri_reset_racey(peripheral, true);
assert_peri_reset_racey(peripheral, false);
}
}
pub(crate) fn reset(peripheral: Peripheral) {
PERIPHERAL_REF_COUNT.with(|_| unsafe { Self::reset_racey(peripheral) })
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, strum::FromRepr)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(C)]
pub enum Cpu {
ProCpu = 0,
#[cfg(multi_core)]
AppCpu = 1,
}
impl Cpu {
pub const COUNT: usize = 1 + cfg!(multi_core) as usize;
#[procmacros::doc_replace]
#[inline(always)]
pub fn current() -> Self {
match raw_core() {
0 => Cpu::ProCpu,
#[cfg(all(multi_core, riscv))]
1 => Cpu::AppCpu,
#[cfg(all(multi_core, xtensa))]
0x2000 => Cpu::AppCpu,
other => unreachable!("unknown core id: {}", other),
}
}
#[inline(always)]
#[instability::unstable]
pub fn other() -> impl Iterator<Item = Self> {
cfg_select! {
multi_core => match Self::current() {
Cpu::ProCpu => [Cpu::AppCpu].into_iter(),
Cpu::AppCpu => [Cpu::ProCpu].into_iter(),
},
_ => [].into_iter(),
}
}
#[inline(always)]
pub fn all() -> impl Iterator<Item = Self> {
cfg_select! {
multi_core => [Cpu::ProCpu, Cpu::AppCpu].into_iter(),
_ => [Cpu::ProCpu].into_iter(),
}
}
}
#[inline(always)]
pub(crate) fn raw_core() -> usize {
cfg_select! {
all(multi_core, riscv) => riscv::register::mhartid::read(),
all(multi_core, xtensa) => (xtensa_lx::get_processor_id() & 0x2000) as usize,
_ => 0,
}
}
use crate::rtc_cntl::SocResetReason;
#[procmacros::doc_replace]
#[inline]
pub fn software_reset() -> ! {
let _uart0_sclk_guard = ensure_uart0_sclk_enabled();
#[cfg(any(esp32p4, esp32s31))]
crate::soc::cpu_control::pre_system_reset();
crate::rom::software_reset()
}
#[instability::unstable]
#[inline]
pub fn software_reset_cpu(cpu: Cpu) {
let _uart0_sclk_guard = ensure_uart0_sclk_enabled();
crate::rom::software_reset_cpu(cpu as u32)
}
#[must_use = "dropping the guard releases the UART0 source clock"]
pub(crate) struct Uart0SclkGuard {
release: bool,
}
impl Drop for Uart0SclkGuard {
fn drop(&mut self) {
if self.release {
release_uart0_sclk();
}
}
}
#[inline(always)]
pub(crate) fn ensure_uart0_sclk_enabled() -> Uart0SclkGuard {
Uart0SclkGuard {
release: request_uart0_sclk(),
}
}
#[cfg(soc_has_clock_node_uart_function_clock)]
fn request_uart0_sclk() -> bool {
crate::soc::clocks::ClockTree::with(|clocks| {
let uart = crate::soc::clocks::UartInstance::Uart0;
if uart.function_clock_config(clocks).is_some() {
uart.request_function_clock(clocks);
true
} else {
false
}
})
}
#[cfg(not(soc_has_clock_node_uart_function_clock))]
fn request_uart0_sclk() -> bool {
false
}
#[cfg(soc_has_clock_node_uart_function_clock)]
fn release_uart0_sclk() {
crate::soc::clocks::ClockTree::with(|clocks| {
crate::soc::clocks::UartInstance::Uart0.release_function_clock(clocks);
});
}
#[cfg(not(soc_has_clock_node_uart_function_clock))]
fn release_uart0_sclk() {}
#[instability::unstable]
#[inline]
pub fn reset_reason() -> Option<SocResetReason> {
crate::rtc_cntl::reset_reason(Cpu::current())
}
#[cfg(sleep_driver_supported)]
#[instability::unstable]
#[inline]
pub fn wakeup_cause() -> crate::rtc_cntl::WakeupReason {
crate::rtc_cntl::wakeup_cause()
}