You can use the TeensyTimerTool for this. It provides a generic interface to the hardware timers (TMR, GPT, PIT) and 20 software timers. Since the coupling of the timer peripherals to the ARM core is rather inefficient, the software timers might be a better fit for sub micro second interrupts. If you prefer the hardware timers I suggest to use a GPT timer (also supported by the lib). You can configure it to use a 150MHz clock instead of the standard 24MHz clock. So something like 2MHz interrupt rate should be feasible.

Documentation can be found here: https://github.com/luni64/TeensyTimerTool/wiki

Answer from luni64 on Stack Exchange
🌐
PJRC
pjrc.com › teensy › td_timing_IntervalTimer.html
IntervalTimer
Teensy LC has only 2 timers for IntervalTimer.
🌐
GitHub
github.com › loglow › IntervalTimer
GitHub - loglow/IntervalTimer: Timer library for Teensy 3.0
Timer library for Teensy 3.0. Contribute to loglow/IntervalTimer development by creating an account on GitHub.
Starred by 21 users
Forked by 8 users
Languages   C++ 90.1% | C++ 90.1%
Discussions

interrupt - Faster TimerOne with Teensy 4.0 (600MHz) - Arduino Stack Exchange
I have a teensy 4.0 which runs at 600MHz, and is double superscaler (i.e. can execute two instructions per clock cycle, sometimes). I need an interrupt to happen faster than every microsecond. I've More on arduino.stackexchange.com
🌐 arduino.stackexchange.com
February 10, 2020
using Teensy IntervalTimer library with Arduino
Hi, looking around in the forum I found a postage about IntervalTimer in a Teensy comment. There is a description of the methods of the IntervalTimer object which seemed to be suitable for my project. I tested it with a Board setting for a Teensy 3.2 and it worked without including a library. More on forum.arduino.cc
🌐 forum.arduino.cc
0
0
September 25, 2017
IntervalTimer on Teensy 4.0 versus 3.6
I have been working on a lot of low-jitter, fast interrupt applications (mostly precision digital synthesis) using the Teensy 3.6. It's been a great learning experience, and when I come across something that isn't easy to explain (usually needs the code gurus, as I'm mainly a hardware person), I... More on forum.pjrc.com
🌐 forum.pjrc.com
48
January 23, 2020
Teensy_TimerInterrupt Library
Teensy_TimerInterrupt Library How To Install Using Arduino Library Manager This library enables you to use Interrupt from Hardware Timers on an Teensy-based board, such as Teensy 4.x, 3.x, LC, Teensy++ 2.0 or Teensy 2.0. As Hardware Timers are rare, and very precious assets of any board, this ... More on forum.arduino.cc
🌐 forum.arduino.cc
1
0
November 6, 2020
🌐
PJRC
pjrc.com › teensy › td_timing.html
Delay and Timing Functions - Teensy
PJRC Store Teensy 4.1 Teensy 4.0 Chips for PCB Teensy Main Page Hardware Getting Started Tutorial How-To Tips Code Library Projects Teensyduino Main Download+Install Basic Usage Digital I/O PWM & Tone Timing Functions Delay elaspedMillis millis IntervalTimer Code Security Startup CrashReport ...
🌐
GitHub
github.com › PaulStoffregen › cores › blob › master › teensy4 › IntervalTimer.h
cores/teensy4/IntervalTimer.h at master · PaulStoffregen/cores
#define __INTERVALTIMER_H__ · #include <stddef.h> #include "imxrt.h" #if TEENSYDUINO >= 159 · #include "inplace_function.h" #endif · · // IntervalTimer provides access to hardware timers which can run an · // interrupt function a precise timing intervals.
Author   PaulStoffregen
🌐
GitHub
github.com › apmorton › teensy-template › blob › master › teensy3 › IntervalTimer.cpp
teensy-template/teensy3/IntervalTimer.cpp at master · apmorton/teensy-template
#include "IntervalTimer.h" · · // ------------------------------------------------------------ // static class variables need to be reiterated here before use · // ------------------------------------------------------------ bool IntervalTimer::PIT_enabled; bool IntervalTimer::PIT_used[]; IntervalTimer::ISR IntervalTimer::PIT_ISR[]; ·
Author   apmorton
Top answer
1 of 2
2

You can use the TeensyTimerTool for this. It provides a generic interface to the hardware timers (TMR, GPT, PIT) and 20 software timers. Since the coupling of the timer peripherals to the ARM core is rather inefficient, the software timers might be a better fit for sub micro second interrupts. If you prefer the hardware timers I suggest to use a GPT timer (also supported by the lib). You can configure it to use a 150MHz clock instead of the standard 24MHz clock. So something like 2MHz interrupt rate should be feasible.

Documentation can be found here: https://github.com/luni64/TeensyTimerTool/wiki

2 of 2
0

I had a similar problem with the Teensy 3.5. I found that using the "Periodic Interrupt Timer (PIT)" let me generate interrupt every 300 ns. I imagine that with a Teensy 4.0, it must be possible to generate an interrupt every 100 ns. Here is the code which works for me on my Teensy 3.5, with comments in french and some references in the comments. I test the signal on an oscilloscope.

// ex00305_Timer_pit0_isr_rapide.ino
/*

FONCTIONNE TRES BIEN

Le signal n'est pas très stable, mais acceptable.
J'imagine que d'autres routines d'interruption perturbe la précision, comme la routine millis()

L'instruction suivante améliore la stabilité. visiblement.
NVIC_SET_PRIORITY(IRQ_PIT_CH0, 0);

Le but est de tester l'utilisation du Timer1 .c.f
/home/bg/Arduino/Teensy/Teensy_3_5_manual_K64P144M120SF5RM.pdf
Page 1083, Chapter 41 : "Periodic Interrupt Timer (PIT)"

Pour la gestion des timers PIT, c.f.
====================================
https://github.com/loglow/PITimer/blob/master/PITimer.cpp

********************************************************************************** */

#define PINOUT1 34
#define LEDRED  24
#define LEDYELLOW  25
#define LEDGREEN  26

volatile byte bEtat = 0;

void setup() {
    //============
    pinMode(PINOUT1, OUTPUT);
    digitalWriteFast(PINOUT1, HIGH);

    pinMode(33, OUTPUT);
    pinMode(34, OUTPUT);
    pinMode(LEDRED, OUTPUT);  digitalWriteFast(LEDRED, LOW);
    pinMode(LEDYELLOW, OUTPUT);  digitalWriteFast(LEDYELLOW, LOW);
    pinMode(LEDGREEN, OUTPUT);  digitalWriteFast(LEDGREEN, LOW);

    //if (SIM_SCGC6_PIT == 0b100000000000000000000000) digitalWriteFast(LEDYELLOW, HIGH);
    SIM_SCGC6 |= SIM_SCGC6_PIT; // C.f. page 317.  PIT Clock Gate Control   SIM_SCGC6_PIT == 0b100000000000000000000000

    // C.f. : /home/bg/.arduino15/packages/teensy/hardware/avr/1.58.1/cores/teensy3/kinetis.h
    // #define NVIC_ENABLE_IRQ(n) (*((volatile uint32_t *)0xE000E100 + ((n) >> 5)) = (1 << ((n) & 31)))
    // enable IRQ Interrupt
    NVIC_SET_PRIORITY(IRQ_PIT_CH0, 0); // Haute priorité, pour ne pas être perturbé par d'autres interrupt. Amélioration visible !
    NVIC_ENABLE_IRQ(IRQ_PIT_CH0);
    PIT_TCTRL0 = 3; // 0b011;  // enable Timer 0 interrupts ; start Timer 0   IRQ_PIT_CH0 == 48  Dans #elif defined(__MK64FX512__)
    //if (IRQ_PIT_CH0 == 48) digitalWriteFast(LEDGREEN,  HIGH);

    // Efface le flag du timer, pour permettre d'autres interruptions, nécessaire au départ
    PIT_TFLG0 = 1;

    // L'ordre des instruction a de l'importance !
    //""""""""""""""""""""""""""""""""""""""""""""
    PIT_MCR = 0;  // turn on PIT
    //PIT_LDVAL0 = 600-1;  // == 10 micro secondes setup timer 0 for xxx cycles Fréqu = 60 MHz  dt = 16.666 ns
    //PIT_LDVAL0 = 120-1;  // == 2 micros second
    //PIT_LDVAL0 = 60-1;   // == 1 micros second
    //PIT_LDVAL0 = 30-1;   // == 500 ns
    //PIT_LDVAL0 = 24-1;   // == 400 ns
    //PIT_LDVAL0 = 21-1;   // == 350 ns
    //PIT_LDVAL0 = 20-1;   // == 333 ns
    //PIT_LDVAL0 = 18-1;   // == 300 ns
    PIT_LDVAL0 = 120-1;
} // setup

void loop() {
    //===========
} // loop

void pit0_isr() {  // C.f. mk20dx128.c
    //===============
    // J'espère pouvoir gérer un temps

    // Efface le flag du timer, pour permettre d'autres interruptions
    // Doit être mis en début de routine !!!
    PIT_TFLG0 = 1;

    if (bEtat) {
        digitalWriteFast(PINOUT1, HIGH);
        //digitalWriteFast(LEDRED,  HIGH);
        bEtat = 0;
    }
    else {
        bEtat = 1;
        digitalWriteFast(PINOUT1, LOW);
        //digitalWriteFast(LEDRED,  LOW);
    }

} // pit0_isr
🌐
Arduino
arduino.cc › reference › en › libraries › teensy_timerinterrupt
Teensy_TimerInterrupt - Arduino Reference
June 10, 2022 - This library enables you to use Interrupt from Hardware Timers on a Teensy-based board such as Teensy 4.x, 3.x, LC, 2.0, etc. These Teensy Hardware Timers, using Interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency ...
Find elsewhere
🌐
Arduino Forum
forum.arduino.cc › development tools › ide 1.x
using Teensy IntervalTimer library with Arduino - IDE 1.x - Arduino Forum
September 25, 2017 - Hi, looking around in the forum I found a postage about IntervalTimer in a Teensy comment. There is a description of the methods of the IntervalTimer object which seemed to be suitable for my project. I tested it with a Board setting for a Teensy 3.2 and it worked without including a library.
🌐
Teensy Forum
forum.pjrc.com › home › forums › main category › technical support & questions
IntervalTimer on Teensy 4.0 versus 3.6 | Teensy Forum
January 23, 2020 - I have been working on a lot of low-jitter, fast interrupt applications (mostly precision digital synthesis) using the Teensy 3.6. It's been a great learning experience, and when I come across something that isn't easy to explain (usually needs the code gurus, as I'm mainly a hardware person), I post it here. This is about the wonderful IntervalTimer function (https://www.pjrc.com/teensy/td_timing_IntervalTimer.html).
🌐
Arduino Forum
forum.arduino.cc › projects › libraries
Teensy_TimerInterrupt Library - Libraries - Arduino Forum
November 6, 2020 - Teensy_TimerInterrupt Library How To Install Using Arduino Library Manager This library enables you to use Interrupt from Hardware Timers on an Teensy-based board, such as Teensy 4.x, 3.x, LC, Teensy++ 2.0 or Teensy 2.0. As Hardware Timers are ...
🌐
PJRC
pjrc.com › teensy › td_timing_millis.html
Delay and Timing Functions
PJRC Store Teensy 4.1 Teensy 4.0 Chips for PCB Teensy Main Page Hardware Getting Started Tutorial How-To Tips Code Library Projects Teensyduino Main Download+Install Basic Usage Digital I/O PWM & Tone Timing Functions Delay elaspedMillis millis IntervalTimer Code Security Startup CrashReport ...
🌐
Astro-electronic
astro-electronic.de › Teensy_Book.pdf pdf
Teensy 2.0, Teensy LC, Teensy 4.0, 4.1 and Seeed XIAO SAMD21
Source code: IntervalTimer.h : https://github.com/PaulStoffregen/co...ntervalTimer.h ... pinMode(led, OUTPUT); //Teensy 4.0: maximal 4mA pro Ausgangspin, für eine rote LED 470 Ohm verwenden
🌐
University of Washington
courses.cs.washington.edu › courses › cse466 › 14au › labs › l2 › l2.html
CSE466 Lab 2: Timer Interrupts and Pulse-Width-Modulaton
Pulse Width Modulation Teensy reference · Pulse Width Modulation Arduino reference · IntervalTimer uses interrupts to call a function at a precise timing interval. Here is a reference for IntervalTimer, and sample code for it's use. Copy the sample code into a new Arduino Sketch, calling ...
🌐
Particle
community.particle.io › firmware › libraries
IntervalTimer library - Libraries - Particle
February 20, 2014 - Teensy has a library called IntervalTimer() that calls an ISR with user code at defined intervals. I also know that PJRC is working on a timer “pool” that automatically allocates a hw timer to the user.
🌐
Arduino Libraries
arduinolibraries.info › libraries › interval-timer-ex
IntervalTimerEx - Arduino Libraries
September 6, 2022 - IntervalTimerEx uses the underlying Teensyduino mechanisms and bookkeeping and can be mixed with the standard IntervalTimer.
🌐
GitHub
github.com › PaulStoffregen › cores › blob › master › teensy3 › IntervalTimer.cpp
cores/teensy3/IntervalTimer.cpp at master · PaulStoffregen/cores
February 10, 2013 - bool IntervalTimer::beginCycles(callback_t funct, uint32_t cycles) { if (channel) { channel->TCTRL = 0; channel->TFLG = 1; } else { SIM_SCGC6 |= SIM_SCGC6_PIT; __asm__ volatile("nop"); // solves timing problem on Teensy 3.5 ·
Author   PaulStoffregen
🌐
Teensy Forum
forum.pjrc.com › home › forums › main category › technical support & questions
Timerinterrupt for Teensy 4.1 | Teensy Forum
December 24, 2020 - Click to expand... Maybe you should just copy the IntervalTimer code and edit the parts you feel aren't efficient enough? Or use Luni's timer tool library, either as-is or a starting point. Those PIT timers really are the right way to run an interrupt at regular intervals.