Example Path: examples/PlatformSpecific/SOMRT1061/GPT
Supported Platforms: SOMRT1061
GPT Input Capture Demo (DEV-SOMRT1061)
Overview
A demonstration of the i.MX RT1061 General Purpose Timer (GPT) peripheral operating in input-capture mode. The application configures one GPT channel as a free-running 24 MHz counter and latches the counter value each time an external signal edge arrives on the capture pin. The captured value and the delta since the previous capture are printed to the serial console.
The example supports two modes, selected at compile time:
- IRQ mode (default) – a user-installed ISR services the capture-flag interrupt, snapshots the captured count into shared volatile state, and the foreground task prints the result when it changes.
- Polled mode – IRQ_MODE is undefined and the foreground task polls GetStatus(GPT_STATUS_IF1) directly, reading GetCapVal() to clear the latch.
A heartbeat line printed every loop iteration also shows the live free-running counter so the count rate can be sanity-checked against the configured 24 MHz clock.
- Note
- Toggle between IRQ mode and polled mode by commenting out #define IRQ_MODE (1) near the top of main.cpp.
Target Hardware
DEV-SOMRT1061.** The demo uses GPT1 capture channel 1 routed to pin 85** (PIN_85_GPT1_CAPTURE1). The IRQ pushbutton on the carrier board is wired to pin 30, so a jumper wire from pin 30 to pin 85 makes pressing the IRQ switch produce the capture edge used by the demo.
- Note
- The GPT count clock used by Init(1, true, false) is 24 MHz, so a capture-to-capture delta of 24000000 corresponds to one second between edges. Falling-edge capture is selected by default; change the third argument of SetCapEdge() to GPT_CAP_RISING or GPT_CAP_BOTH to alter polarity.
Required Setup
- Connect a serial terminal to the DEV-SOMRT1061 debug UART.
- Run a jumper from pin 30 (IRQ pushbutton) to pin 85 (GPT1_CAPTURE1).
- Build and load the application.
What You Will See
After the application banner the demo prints a heartbeat line on every iteration. Pressing the IRQ pushbutton produces a falling edge on the capture input and a corresponding Captured line is printed with the captured counter value and the delta since the previous capture.
Expected Serial Output
Captured from a live run (heartbeat once per second, then several button presses):
Running SOMRT1061 GPT
At 3524 count = 4151997932 delta=24000000
At 3544 count = 4175997932 delta=24000000
Captured 4180213465 delta 4180213465
At 3564 count = 4199997932 delta=24000000
Captured 4221388955 delta 41175490
At 3584 count = 4223997932 delta=24000000
Captured 4239347934 delta 17958979
At 3604 count = 4247997944 delta=24000012
Captured 4256832496 delta 17484562
At 3624 count = 4271997944 delta=24000000
At 3644 count = 1030636 delta=23999988
The exact counts vary; what matters is that:
- The heartbeat delta is ~24,000,000 every second – the free-running counter advancing at the 24 MHz count clock.
- A Captured line appears each time the IRQ pushbutton is pressed, and its delta is the count-clock ticks between successive presses (the sample above shows presses 0.7 to 1.7 seconds apart).
- The first Captured delta equals the captured value itself (there is no previous capture to subtract).
- The counter is 32 bits wide and wraps about every 179 seconds at 24 MHz – visible in the last line above, where the count rolls over. Deltas computed with unsigned arithmetic remain correct across the wrap.
Learning Objectives
This example teaches:
- How to mux a pad to a GPT capture function with Pins[n].function(...).
- How to initialize a GPTimer for free-running counting at the 24 MHz count clock via Init(prescaler, freerun, ...).
- How to select capture edge polarity with SetCapEdge().
- How to install a GPT capture ISR with SetIRQFn() and unmask the channel-1 capture interrupt with EnableIRQ(GPT_STATUS_IF1).
- The correct ISR discipline: clear the status flag with ClrIRQ() first, then read the captured value, then return quickly. Shared state observed by the foreground task must be volatile.
- The trade-off between IRQ-driven capture (low-latency, no missed edges if the ISR is short) and polled capture (simpler, but the foreground task must run faster than the capture rate to avoid missing edges).
- How to read the live free-running count via GetCurCount() while the timer continues to run – useful as a microsecond-resolution timestamp source.
Further Reading