Module imxrt_hal::gpt[][src]

General Purpose Timer (GPT)

Features

The GPTs are count-up, wrapping timers that run off of the IPG clock or the crystal oscillator (24MHz). Each GPT has three compare registers, called output comparison registers (OCR). When the counter reaches a value in an OCR, the GPT signals the comparison through status flags. A comparison can generate an interrupt.

Use GPTs to

  • detect if a timing interval has elapsed
  • trigger an interrupt when an interval elapses
  • measure code execution

GPT modes

The table below summarizes the effects of an output comparison operation when in restart mode, and when in free-running mode. See the subsequent sections for a discussion of the two modes.

GPT ModeOCR1OCR2OCR3
RestartResets the counter to zeroNo effect; counter continues incrementingNo effect…
Free-runningNo effect; counter continues incrementingNo effect …No effect…

In summary, OCR1 is special in restart mode, as it will reset the value in the GPT counter.

Select a mode with set_mode().

Restart mode

The GPTs default to ‘restart mode.’ In restart mode, a compare on channel 1 will reset the GPT counter to zero. Compare events on channels 2 and 3 will not reset the GPT counter. If you would rather have the GPT counter continue no matter the comparison event, set the GPT to free-running mode.

Free-running mode

The GPTs may be in free-running mode. When a comparion event occurs in free-running mode, the counter continues to increment, eventually wrapping around. Free-running mode treats all channels as equal; that is, channel 1 is no different than channel 2 or 3.

Reset on enable

Reset on enable is a complementary feature to the two modes. When reset on enable is active, the GPT counter will reset to zero each time the timer is enabled. By default, the counter will restart at whatever value is currently in the counter. The default behavior lets a user ‘pause’ the counter by disabling the GPT. On the other hand, reset on enable lets users reset the counter by disabling and re-enabling the GPT.

The table below summarizes the ‘reset on enable’ behaviors. Use set_reset_on_enable() to configure the reset on enable behavior.

StateBehavior
falseWhen the GPT is disabled, it maintains its counter value
trueWhen the GPT is disabled, the counter resets to zero

GPTs and system WAIT / STOP

By default, GPTs do not run when the process is in in wait mode. Use set_wait_mode_enable(true) to enable GPTs in wait mode.

If the GPT stops counting in WAIT / STOP system states, the counter freezes its counter. When the processor transitions into RUNNING, the counter increments from its previously-frozen value (provided the GPT was enabled).

embedded_hal implementations

The module provides adapters that pair a GPT with an OCR. The adapters implement the embedded_hal timers using the GPT and the OCR Users must ensure that the selected mode is suitable for the behaviors of the adapters.

The two adapters include

  • CountDown, which implements the CountDown trait
  • Periodic, which implements the Periodic trait

Although the adapters work on a single OCR, the timer may continue to monitor the other two OCRs.

Example

use imxrt_hal;

let mut peripherals = imxrt_hal::Peripherals::take().unwrap();

let (_, ipg_hz) = peripherals.ccm.pll1.set_arm_clock(
    imxrt_hal::ccm::PLL1::ARM_HZ,
    &mut peripherals.ccm.handle,
    &mut peripherals.dcdc,
);

let mut cfg = peripherals.ccm.perclk.configure(
    &mut peripherals.ccm.handle,
    imxrt_hal::ccm::perclk::PODF::DIVIDE_3,
    imxrt_hal::ccm::perclk::CLKSEL::IPG(ipg_hz),
);

let mut gpt1 = peripherals.gpt1.clock(&mut cfg);

gpt1.set_output_interrupt_on_compare(
    imxrt_hal::gpt::OutputCompareRegister::Three,
    true,
);
gpt1.set_wait_mode_enable(true);
gpt1.set_mode(imxrt_hal::gpt::Mode::FreeRunning);

gpt1.set_output_compare_duration(
    imxrt_hal::gpt::OutputCompareRegister::Three,
    core::time::Duration::from_micros(765),
);

TODO

  • Input capture. Each GPT can capture the value of the counter when a pin state changes. When the pin state changes, the GPT can generate an interrupt.
  • Output generation. When one of the three comparison registers match the counter, the GPT can generate a signal on an output pin.

Structs

CountDown

Adapter that implements the CountDown trait.

GPT

A general purpose timer

OutputCompareStatus

A handle to evaluate and modify the output compare status

Periodic

Adapter that implements ther Periodic trait.

Unclocked

An unclocked GPT

Enums

Mode

Possible modes of the GPT

OutputCompareRegister

An output compare register (OCR)