From 8af5dcbede0ae8bc4d23d71efe209935b1634cd7 Mon Sep 17 00:00:00 2001 From: Priec Date: Wed, 15 Oct 2025 11:17:03 +0200 Subject: [PATCH] timer works properly well --- flake.nix | 34 ++++++++++++++++---------- test_LED3_interrupt/Makefile | 3 +++ test_LED3_interrupt/src/bin/main.rs | 37 +++++++++++++++++++++-------- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/flake.nix b/flake.nix index 28c1717..569ebe6 100644 --- a/flake.nix +++ b/flake.nix @@ -21,29 +21,37 @@ gcc gnumake rustup + rust-analyzer cargo-espflash espup mosquitto ]; shellHook = '' - echo ">>> ESP32 DevShell (esp-hal only)" + echo ">>> ESP32 DevShell (esp-hal only)" - if [ ! -d "$HOME/.espup" ]; then - echo "Running espup install..." - espup install - fi + if [ ! -d "$HOME/.espup" ]; then + echo "Running espup install..." + espup install + fi - # Activate Xtensa Rust toolchain - export RUSTUP_TOOLCHAIN=esp + # Ensure the real rust-analyzer from Nix is used (not rustup's shim) + export PATH=${pkgs.rust-analyzer}/bin:$PATH + + # Keep ESP toolchain binaries on PATH for building/flashing + # (do NOT set RUSTUP_TOOLCHAIN=esp globally; that breaks rust-analyzer) export PATH=$HOME/.rustup/toolchains/esp/bin:$PATH + + # Add GCC/binutils path for xtensa-esp32-elf-gcc + export PATH=$HOME/.rustup/toolchains/esp/xtensa-esp-elf/esp-14.2.0_20240906/xtensa-esp-elf/bin:$PATH + + # Helpers that force the ESP toolchain explicitly when needed + alias cargo-esp="RUSTUP_TOOLCHAIN=esp cargo" + alias rustc-esp="RUSTUP_TOOLCHAIN=esp rustc" - # Add GCC/binutils path for xtensa-esp32-elf-gcc - export PATH=$HOME/.rustup/toolchains/esp/xtensa-esp-elf/esp-14.2.0_20240906/xtensa-esp-elf/bin:$PATH - - echo "Xtensa Rust toolchain ready." - rustc --version - which xtensa-esp32-elf-gcc || echo "⚠️ xtensa-esp32-elf-gcc not found in PATH" + echo "Xtensa Rust toolchain ready." + rustc --version + which xtensa-esp32-elf-gcc || echo "⚠️ xtensa-esp32-elf-gcc not found in PATH" ''; }; }; diff --git a/test_LED3_interrupt/Makefile b/test_LED3_interrupt/Makefile index 2845b25..1aa8ad2 100644 --- a/test_LED3_interrupt/Makefile +++ b/test_LED3_interrupt/Makefile @@ -7,3 +7,6 @@ build: flash: cargo espflash flash --release --monitor + +clean: + cargo espflash erase-flash diff --git a/test_LED3_interrupt/src/bin/main.rs b/test_LED3_interrupt/src/bin/main.rs index 0d1c8b5..106674d 100644 --- a/test_LED3_interrupt/src/bin/main.rs +++ b/test_LED3_interrupt/src/bin/main.rs @@ -6,11 +6,15 @@ holding buffers for the duration of a data transfer." )] use embassy_executor::Spawner; -use embassy_time::{Duration, Timer}; +// use embassy_time::{Duration, Timer}; use esp_backtrace as _; -use esp_hal::gpio::{Output, Level, OutputConfig}; -use esp_hal::clock::CpuClock; -use esp_hal::timer::timg::TimerGroup; +use esp_hal::{ + clock::CpuClock, + gpio::{Level, Output, OutputConfig}, + timer::Timer, + timer::timg::TimerGroup, + time::Duration, +}; use log::info; extern crate alloc; @@ -28,8 +32,8 @@ async fn main(spawner: Spawner) { esp_alloc::heap_allocator!(size: 64 * 1024); - let timer0 = TimerGroup::new(peripherals.TIMG1); - esp_hal_embassy::init(timer0.timer0); + let timer_group1 = TimerGroup::new(peripherals.TIMG1); + let timer0 = timer_group1.timer0; // Initialize GPIO4 as output (starts LOW) let mut gpio4 = Output::new(peripherals.GPIO4, Level::Low, OutputConfig::default()); @@ -38,12 +42,25 @@ async fn main(spawner: Spawner) { loop { gpio4.set_high(); info!("GPIO4 ON"); - Timer::after(Duration::from_secs(1)).await; + + // Start timer for 1 second and block until done + let duration = Duration::from_secs(1); + timer0.load_value(duration).unwrap(); + timer0.start(); + info!("{:?}", duration); + + // Wait until timer completes + while !timer0.is_interrupt_set() {} + timer0.clear_interrupt(); gpio4.set_low(); info!("GPIO4 OFF"); - Timer::after(Duration::from_secs(1)).await; + + // Start timer for 1 second and block until done + timer0.load_value(duration).unwrap(); + timer0.start(); + + while !timer0.is_interrupt_set() {} + timer0.clear_interrupt(); } - - // for inspiration have a look at the examples at https://github.com/esp-rs/esp-hal/tree/esp-hal-v1.0.0-rc.0/examples/src/bin }