esp_hal::timer

Module timg

Source
Available on crate feature unstable only.
Expand description

§Timer Group (TIMG)

§Overview

The Timer Group (TIMG) peripherals contain one or more general-purpose timers, plus one or more watchdog timers.

The general-purpose timers are based on a 16-bit pre-scaler and a 54-bit auto-reload-capable up-down counter.

§Configuration

The timers have configurable alarms, which are triggered when the internal counter of the timers reaches a specific target value. The timers are clocked using the APB clock source.

Typically, a general-purpose timer can be used in scenarios such as:

  • Generate period alarms; trigger events periodically
  • Generate one-shot alarms; trigger events once
  • Free-running; fetching a high-resolution timestamp on demand

§Examples

§General-purpose Timer

use esp_hal::timer::timg::TimerGroup;
use esp_hal::timer::Timer;

let timg0 = TimerGroup::new(peripherals.TIMG0);
let timer0 = timg0.timer0;

// Get the current timestamp, in microseconds:
let now = timer0.now();

// Wait for timeout:
timer0.load_value(Duration::from_secs(1));
timer0.start();

while !timer0.is_interrupt_set() {
    // Wait
}

timer0.clear_interrupt();

§Watchdog Timer

use esp_hal::timer::timg::TimerGroup;
use esp_hal::timer::timg::MwdtStage;
use esp_hal::timer::Timer;

let timg0 = TimerGroup::new(peripherals.TIMG0);
let mut wdt = timg0.wdt;

wdt.set_timeout(MwdtStage::Stage0, Duration::from_millis(5_000));
wdt.enable();

loop {
    wdt.feed();
}

Structs§

  • A timer within a Timer Group.
  • A timer group consisting of 2 timers and a watchdog timer.
  • Watchdog timer

Enums§