async hard interrupt

This commit is contained in:
Priec
2025-10-15 19:25:59 +02:00
parent 4f21d03822
commit e6e22818e4
10 changed files with 1619 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
use embassy_executor::Spawner;
use embassy_sync::signal::Signal;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
clock::CpuClock,
gpio::{Level, Output, OutputConfig},
timer::Timer as HalTimer,
interrupt,
peripherals,
timer::timg::TimerGroup,
time::Duration as HalDuration,
};
use core::cell::RefCell;
use critical_section::Mutex;
use esp_hal::handler;
use log::info;
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
static SIGNAL: Signal<()> = Signal::new();
static TIMER0: Mutex<RefCell<Option<esp_hal::timer::timg::Timer<'static>>>> = Mutex::new(RefCell::new(None));
#[esp_hal_embassy::main]
async fn main(spawner: Spawner) {
esp_println::logger::init_logger_from_env();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(size: 64 * 1024);
let timer_group1 = TimerGroup::new(peripherals.TIMG1);
let timer0 = timer_group1.timer0;
timer0.set_interrupt_handler(TG1_T0_LEVEL);
timer0.clear_interrupt();
timer0.load_value(HalDuration::from_secs(1)).unwrap();
timer0.enable_interrupt(true);
timer0.enable_auto_reload(true);
timer0.start();
// Store timer in the static safely
critical_section::with(|cs| {
TIMER0.borrow_ref_mut(cs).replace(timer0);
});
// enable NVIC entry for this timer
interrupt::enable(
peripherals::Interrupt::TG1_T0_LEVEL,
interrupt::Priority::Priority2, // medium priority
)
.unwrap();
// Initialize GPIO4 as output (starts LOW)
let mut gpio4 = Output::new(peripherals.GPIO4, Level::Low, OutputConfig::default());
spawner.spawn(led_task(gpio4)).unwrap();
info!("Embassy initialized!");
loop {
embassy_time::Timer::after(Duration::from_secs(5)).await;
info!("x");
}
}
#[embassy_executor::task]
async fn led_task(mut gpio4: Output<'static>) {
loop {
SIGNAL.wait().await;
gpio4.toggle();
info!("tick");
}
}
#[handler]
fn TG1_T0_LEVEL() {
critical_section::with(|cs| {
if let Some(timer) = TIMER0.borrow_ref_mut(cs).as_mut() {
timer.clear_interrupt();
SIGNAL.signal(());
}
});
}

View File

@@ -0,0 +1 @@
#![no_std]