Duration of timer interrupts

mkoch

Well-known member
In this example for Teensy 4.0 I'm using a 200kHz timer interrupt to generate a short pulse at pin D1.
In the main loop pin D0 is toggled as fast as possible, so that I can see on the scope the duration of the interrupt.
Code:
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;

#define repeat_10(a)     a;a;a;a;a;a;a;a;a;a
#define repeat_100(a)    repeat_10(repeat_10(a))

PeriodicTimer timer1(GPT1);   // uses GPT1
//PeriodicTimer timer1(PIT);    // uses PIT

void setup()
{
  pinMode(0, OUTPUT);  // Main loop
  pinMode(1, OUTPUT);  // Interrupt
 
  timer1.begin(isr, 5);        // 200 kHz 
  NVIC_SET_PRIORITY(IRQ_GPT1, 32);  // set high priority
  NVIC_SET_PRIORITY(IRQ_PIT, 32);   // set high priority
}

void isr(void)
{
  digitalWriteFast(1, HIGH);
  digitalWriteFast(1, LOW);
}

void loop()           // toggle D0 as fast as possible
{
  repeat_100(
    digitalWriteFast(0, HIGH);   
    digitalWriteFast(0, LOW);
  );
}
On the scope yellow is D0 (main loop) and cyan is D1 (pulse in interrupt):
SCR05.PNG

When the main loop is interrupted, it takes about 180ns until the pulse appears at D1, and additional 40ns until the main loop continues.

Now I change the interrupt source in lines 7 and 8, using PIT instead of GPT1. Everything else is the same as before.
SCR06.PNG

Now the durations have increased to 330ns and 540ns.
I'd like to understand why. What's the microcontroller doing during this time?
 
Last edited:
Look at the source code of TeensyTimerTool to see how the interrupts are handled differently for PIT and GPT.
 
It seems the interrupt vector doesn't point to my ISR, but to some other code that's executed before and after my ISR. Is it possible to avoid this extra code? I think I must also clear the interrupt bit in my ISR then. Is there any example for making a timer interrupt as fast as possible? It doesn't care which interrupt source is used (GPT or PIT or something else). I just want the interrupt duration as short as possible.
 
Back
Top