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

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