-
Notifications
You must be signed in to change notification settings - Fork 5
Timing
wip (luni)
IntervalTimer uses interrupts to call a function at a precise timing interval.
4 independent 32bit timer modules. Each module has its own IRQ
Number of FTM/TPM modules and number of channels per module depends on the Model -> add table
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.
One timer module with four independent 32bit channels. All channels share one ISR
Two timer modules with one 32bit channel per module.
Teensy is a PJRC trademark. Notes here are for reference and will typically refer to the ARM variants unless noted.