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
Delay and Timing Functions
Up to 4 IntervalTimer objects may be active simultaneuously on Teensy 3.0 - 4.1. Teensy LC has only 2 timers for IntervalTimer.
🌐
GitHub
github.com › khoih-prog › Teensy_TimerInterrupt
GitHub - khoih-prog/Teensy_TimerInterrupt: This library enables you to use Interrupt from Hardware Timers on an 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 accuracy) than other software timers using millis() or micros(). That's mandatory if you need to measure some data requiring better accuracy. It now supports 16 ISR-base
January 29, 2023 - This library enables you to use Interrupt from Hardware Timers on an 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 accuracy) than other software timers using millis() or micros(). That's mandatory if you need to measure some data requiring better accuracy.
Starred by 12 users
Forked by 4 users
Languages   C++ 78.5% | C 21.3% | Shell 0.2%
🌐
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 › luni64 › TeensyTimerTool
GitHub - luni64/TeensyTimerTool: Generic Interface to Teensy Timers
The TeensyTimerTool is a library that provides a generic, easy to use interface to the hardware timers of the PJRC Teensy boards. In addition, it provides up to 20 highly efficient software timers that use the same interface.
Starred by 90 users
Forked by 19 users
Languages   C++ 96.7% | Python 3.0% | C 0.3% | C++ 96.7% | Python 3.0% | C 0.3%
🌐
Arduino Libraries
arduinolibraries.info › libraries › teensy-timer-tool
TeensyTimerTool - Arduino Libraries
August 8, 2023 - TeensyTimerTool is a library that provides a generic, easy to use interface to the hardware timers (FTM, GPT, QUAD, PIT) of the PJRC Teensy boards. In addition, it provides up to 20 highly efficient software timers based on the cycle counter or the RTC (32 and 64bit) that use the same interface.
🌐
PJRC
pjrc.com › teensy › td_libs_TimerOne.html
TimerOne & TimerThree Arduino Libraries
TimerOne and TimerThree let you use the built-in 16 bit timers.
🌐
Arduino
docs.arduino.cc › libraries › teensy_timerinterrupt
Teensy_TimerInterrupt
The Arduino environment can be extended through the use of libraries. Libraries provide extra functionality for use in sketches. To use a library in a sketch, select it from Sketch > Import Library.
🌐
Arduino
arduino.cc › reference › en › libraries › teensytimertool
TeensyTimerTool - Arduino Reference
November 25, 2020 - Generic Interface to Teensy Timers TeensyTimerTool is a library that provides a generic, easy to use interface to the hardware timers (FTM, GPT, QUAD, PIT) of the PJRC Teensy boards. In addition, it provides up to 20 highly efficient software timers based on the cycle counter or the RTC (32 ...
Find elsewhere
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 accuracy) than other software timers using millis() or micros().
🌐
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%
🌐
GitHub
github.com › luni64 › TeensyDelay
GitHub - luni64/TeensyDelay
Depending on the chosen timer it provides up to 8 independent delay channels. Depending on the selected timer module (see chapter Configuration below) and the processor frequency, the maximum delay time is limited to about 30ms. TeensyDelay is compatible to T Teensy 3.0, Teensy 3.1/3.2, Teensy 3.5 and Teensy 3.6.
Starred by 20 users
Forked by 4 users
Languages   C++ 100.0% | C++ 100.0%
🌐
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_libs_FrequencyTimer2.html
FrequencyTimer2 Library - Teensy
When running on Teensy 3.x, FrequencyTimer2 uses the CMT timer, to allow a zero-jitter frequency output separate from the PWM pins.
🌐
Tavisharmstrong
tavisharmstrong.com › timer1
A Simple and Interactive Explanation of the Teensy's 16-bit timer (Timer1)
In the Teensy++, there are three timers. The second of these, Timer 1, is a 16-bit timer. It can be used for periodic interrupts, which lets you run a piece of code every so often. It can also be used for pulse-width modulation (PWM), which is a way to simulate analogue signals using digital ...
🌐
Arduino
docs.arduino.cc › libraries › teensytimertool
TeensyTimerTool
July 22, 2024 - The Arduino environment can be extended through the use of libraries. Libraries provide extra functionality for use in sketches. To use a library in a sketch, select it from Sketch > Import Library.
🌐
PJRC
pjrc.com › teensy › interrupts.html
Using interrupts on Teensy, with C language
volatile unsigned int overflow_count=0; ISR(TIMER0_OVF_vect) { if (overflow_count < 0xFFFF) overflow_count++; } In this example, a 16 bit number counts the number of times Timer 0 has overflowed, which can be useful for measuring elapsed time.
🌐
Teensy Forum
forum.pjrc.com › home › forums › main category › technical support & questions
Timerinterrupt for Teensy 4.1 | Teensy Forum
December 24, 2020 - My goal with the Timer is: - Creating a I2C bus which may be faster than the standard Wire library. - Building a Special I2C parallel bus which can send data to multiple slaves at the same time: Signals: SCL ------> SCL slave1, slave2, slaveX SDA1 -----> SDA slave1 SDA2 -----> SDA slave2 SDAX -----> SDA slaveX Slaves are: 3x TLC59116 (LED Driver with 16 pwm outputs) I currently use 3 slaves and my bus is working fine (most of the time) -> sometimes the Teensy crashes because the timer works so fast I think.
🌐
Github-wiki-see
github-wiki-see.page › m › luni64 › TeensyTimerTool › wiki › Supported-Timers
Supported Timers - luni64/TeensyTimerTool GitHub Wiki
July 26, 2023 - The following table shows available timers for the Teensy boards. Entries show m x c where m is the number of modules and c is the number of channels per module. Modules with entries in parentheses are not yet implemented. Modules for entries with dashes are not available for the board.