use embassy_executor::SendSpawner;
use esp_hal::interrupt::Priority;
use esp_hal::peripherals::FROM_CPU_INTR1;
use esp_rtos::embassy::InterruptExecutor;
use static_cell::StaticCell;
#[macro_export]
macro_rules! init_heap {
() => {
#[cfg(feature = "esp32s2")]
$crate::esp_alloc::heap_allocator!(#[$crate::esp_hal::ram(reclaimed)] size: $crate::ssh_stamp::settings::HEAP_SIZE);
#[cfg(not(feature = "esp32s2"))]
$crate::esp_alloc::heap_allocator!(size: $crate::ssh_stamp::settings::HEAP_SIZE);
};
}
#[macro_export]
macro_rules! start_rtos {
($peripherals:ident) => {{
#[cfg(feature = "esp32")]
$crate::esp_rtos::start(
$crate::esp_hal::timer::timg::TimerGroup::new($peripherals.TIMG1).timer0,
$peripherals.FROM_CPU_INTR0,
);
#[cfg(not(feature = "esp32"))]
$crate::esp_rtos::start(
$crate::esp_hal::timer::systimer::SystemTimer::new($peripherals.SYSTIMER).alarm0,
$peripherals.FROM_CPU_INTR0,
);
$peripherals.FROM_CPU_INTR1
}};
}
#[macro_export]
macro_rules! boot {
($peripherals:ident, $rng:ident, $entropy_source:ident, $sw_int1:ident) => {
$crate::init_heap!();
$crate::esp_bootloader_esp_idf::esp_app_desc!();
$crate::esp_println::logger::init_logger_from_env();
$crate::bench::log_heap("boot");
$crate::log::debug!("HSM: initialising peripherals");
let $peripherals = $crate::esp_hal::init($crate::esp_hal::Config::default());
let ($rng, $entropy_source) = $crate::init_entropy!($peripherals);
$crate::flash_init($peripherals.FLASH);
let $sw_int1 = $crate::start_rtos!($peripherals);
};
}
pub fn start_interrupt_executor(sw_int1: FROM_CPU_INTR1<'static>) -> SendSpawner {
static INT_EXECUTOR: StaticCell<InterruptExecutor<1>> = StaticCell::new();
let interrupt_executor = INT_EXECUTOR.init_with(|| InterruptExecutor::new(sw_int1));
cfg_if::cfg_if! {
if #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))] {
let interrupt_spawner = interrupt_executor.start(Priority::Priority3);
} else if #[cfg(feature = "esp32c6")] {
let interrupt_spawner = interrupt_executor.start(Priority::Priority10);
} else {
let interrupt_spawner = interrupt_executor.start(Priority::Priority1);
}
}
interrupt_spawner
}