Skip to content
luni64 edited this page Dec 26, 2020 · 9 revisions

wip (luni)

Libraries

IntervalTimer

IntervalTimer uses interrupts to call a function at a precise timing interval.

TeensyTimerTool

The TeensyTimerTool is a library that provides a generic, easy to use interface to the hardware timers of the PJRC Teensy boards

Low level access to the timers

Teensy 3.x timers

PIT- Periodic Interval Timer

4 independent 32bit timer modules. Each module has its own IRQ

FTM & TPM - Flex Timer Module

Number of FTM/TPM modules and number of channels per module depends on the Model -> add table

Teensy 4.x timers

TMR (QUAD)

4 TMR modules, each module contains 4 independent timers which share one IRQ

Here a simple example showing how to use channel 2 from TMR module 3 to generate a periodic interrupt.

IMXRT_TMR_t& module = IMXRT_TMR3;
IMXRT_TMR_CH_t& channel = module.CH[2];

void ISR(){
    channel.CSCTRL &= ~TMR_CSCTRL_TCF1;                          // we do not need to check if channel flag is set since we disabled all other channels
    digitalToggleFast(LED_BUILTIN);
    asm volatile("dsb");
}

void TMR_start(u_int16_t reload)
{
    for (unsigned i = 0; i < 4; i++) module.CH[i].CTRL = 0x0000; // disable all channels

    attachInterruptVector(IRQ_QTIMER3, ISR);                     // attach ISR to module interrupt (note: ISR must handle all 4 channels)
    NVIC_ENABLE_IRQ(IRQ_QTIMER3);

    channel.LOAD = 0x0000;                                       // load on compare -> 0
    channel.COMP1 = reload;                                      // comapare values
    channel.CMPLD1 = reload;
    channel.CNTR = 0x0000;                                       // set counter to zero
    channel.CSCTRL |= TMR_CSCTRL_TCF1EN;                         // enable interrupt

    channel.CTRL = TMR_CTRL_CM(1) | TMR_CTRL_PCS(0b1111) | TMR_CTRL_LENGTH; // start timer using prescaler: 7->1/128; internal clock source

}

void setup(){
    pinMode(LED_BUILTIN, OUTPUT);
    TMR_start(0xFFFF);                                            // max reload value
}

void loop(){
}

Please note that the TMR timer modules are also used for PWM generation. Using them will disable PWM on the corresponding pins.

PIT - Periodic interval timer)

One timer module with four independent 32bit channels. All channels share one ISR

GPT - General purpose timer

Two timer modules with one 32bit channel per module.

Clone this wiki locally