diff --git a/test_LED5_hard_interrupt/Cargo.toml b/test_LED5_hard_interrupt/Cargo.toml index 068ef44..872abb5 100644 --- a/test_LED5_hard_interrupt/Cargo.toml +++ b/test_LED5_hard_interrupt/Cargo.toml @@ -10,11 +10,7 @@ path = "./src/bin/main.rs" [dependencies] esp-bootloader-esp-idf = { version = "0.2.0", features = ["esp32"] } -esp-hal = { version = "=1.0.0-rc.0", features = [ - "esp32", - "log-04", - "unstable", -] } +esp-hal = { version = "=1.0.0-rc.0", features = ["esp32", "log-04", "rt", "unstable"] } log = "0.4.27" embedded-io = "0.6.1" embedded-io-async = "0.6.1" diff --git a/test_LED5_hard_interrupt/src/bin/main.rs b/test_LED5_hard_interrupt/src/bin/main.rs index 106674d..25435e3 100644 --- a/test_LED5_hard_interrupt/src/bin/main.rs +++ b/test_LED5_hard_interrupt/src/bin/main.rs @@ -12,9 +12,12 @@ use esp_hal::{ clock::CpuClock, gpio::{Level, Output, OutputConfig}, timer::Timer, + interrupt, + peripherals, timer::timg::TimerGroup, time::Duration, }; +use esp_hal::handler; use log::info; extern crate alloc; @@ -23,6 +26,8 @@ extern crate alloc; // For more information see: esp_bootloader_esp_idf::esp_app_desc!(); +static mut INTERRUPT_FIRED: bool = false; + #[esp_hal_embassy::main] async fn main(spawner: Spawner) { esp_println::logger::init_logger_from_env(); @@ -32,35 +37,53 @@ async fn main(spawner: Spawner) { esp_alloc::heap_allocator!(size: 64 * 1024); - let timer_group1 = TimerGroup::new(peripherals.TIMG1); - let timer0 = timer_group1.timer0; + let mut timer_group1 = TimerGroup::new(peripherals.TIMG1); + let mut timer0 = timer_group1.timer0; + + // make it periodic, 1 s per tick, enable interrupt + timer0.enable_interrupt(true); + timer0.enable_auto_reload(true); + timer0.load_value(Duration::from_secs(1)).unwrap(); + timer0.start(); + + // enable NVIC entry for this timer + unsafe { + interrupt::enable( + peripherals::Interrupt::TG1_T0_LEVEL, + interrupt::Priority::Priority2, // medium priority for now + ) + .unwrap(); + } // Initialize GPIO4 as output (starts LOW) let mut gpio4 = Output::new(peripherals.GPIO4, Level::Low, OutputConfig::default()); info!("Embassy initialized!"); loop { - gpio4.set_high(); - info!("GPIO4 ON"); - // Start timer for 1 second and block until done - let duration = Duration::from_secs(1); - timer0.load_value(duration).unwrap(); - timer0.start(); - info!("{:?}", duration); + let mut counter = 0; + while counter < 10000000 { + counter += 1; + } - // Wait until timer completes - while !timer0.is_interrupt_set() {} - timer0.clear_interrupt(); - - gpio4.set_low(); - info!("GPIO4 OFF"); - - // Start timer for 1 second and block until done - timer0.load_value(duration).unwrap(); - timer0.start(); - - while !timer0.is_interrupt_set() {} - timer0.clear_interrupt(); + unsafe { + if INTERRUPT_FIRED { + info!("Timer interrupt fired!"); + INTERRUPT_FIRED = false; + } + } } } + +#[handler] +fn TG1_T0_LEVEL() { + // Clear the interrupt + critical_section::with(|_cs| { + unsafe { + let mut tg1 = TimerGroup::new(peripherals::TIMG1::steal()); + tg1.timer0.clear_interrupt(); + + INTERRUPT_FIRED = true; + } + }); +}