timer works properly well

This commit is contained in:
Priec
2025-10-15 11:17:03 +02:00
parent 200f37d794
commit 8af5dcbede
3 changed files with 51 additions and 23 deletions

View File

@@ -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"
'';
};
};

View File

@@ -7,3 +7,6 @@ build:
flash:
cargo espflash flash --release --monitor
clean:
cargo espflash erase-flash

View File

@@ -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
}