-
Notifications
You must be signed in to change notification settings - Fork 19
Supported Timers
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. Details can be found in the corresponding processor datasheets
Timer Module | Width | T-MM | T 4.1 | T4.0 | T3.6 | T3.5 | T3.2 | T-LC | |
---|---|---|---|---|---|---|---|---|---|
TMR1...TMR4 | QUAD Timer | 16bit | 4x4 | 4x4 | 4x4 | - | - | - | - |
GPT1...GPT2 | General Purpose Timer | 32bit | 2x1 | 2x1 | 2x1 | - | - | - | - |
PIT1...PITn | Periodic Timer | 32bit | 1x4 | 1x4 | 1x4 | (4x1) | (4x1) | (4x1) | (2x1) |
FTM1...FTMn | Flex Timer | 16bit | - | - | - | 20 | 20 | 12 | - |
RTC | Real Time Clock | n.a. | 1x1 | 1x1 | 1x1 | - | - | - | - |
TCK | Tick-Timer | 32bit | 1x20 | 1x20 | 1x20 | 1x20 | 1x20 | 1x20 | 1x20 |
TCK64 | 64 bit Tick-Timer | 64bit | 1x20 | 1x20 | 1x20 | 1x20 | 1x20 | 1x20 | 1x20 |
TCK_RTC | 64 bit Tick-Timer | 64bit | 1x20 | 1x20 | 1x20 | 1x20 | - | - | - |
The general purpose timer module (GPT) is a 32bit timer module with one timer channel. The controller of the T4.0 boards (IMXRT1062) implements two of those timer modules (GPT1 and GPT2). Currently the GPT modules are not used by the Teensyduino core libraries.
// allocate two timers using GPT1 and GPT2 respectively
PeriodicTimer t1(GPT1);
PeriodicTimer t2(GPT2);
The QUAD modules (TMR) are 16 bit timer modules with 4 timer channels per module. Teensy 4.0 controller has four TMR modules (TMR1 ... TMR4). The Teensyduino core uses TMR1-TMR3 for generating PWM signals. Using one of those will disable the PMW capability of the corresponding pins.
PeriodicTimer t1(TMR1); // first free channel of TMR1
OneShotTimer t2(TMR1); // next free channel of TMR1 (up to 4)
PeriodicTimer t3(TMR3); // first free channel of TMR3
...
The PIT timer modules differ between the T3.x and the T4.x boards.
The processor of the T4.x and Teensy MicroMod boards has one PIT module with four 32bit channels. All four channels share the same interrupt.
Usage:
PeriodicTimer t1(PIT), t2(PIT); // First and second channel of the PIT module
OneShotTimer t3(PIT); // Third channel
...
Note: The PIT module is clocked with either 24MHz (default) or FBUS (usually 150MHz). You can configure the used clock as described in the configuration chapter.
There are 4 dedicated PIT modules, each of them has one 32bit channel with its own interrupt and runs independently of the other channels. The T3.x PITs are not yet implemented. The stock IntervalTimer also uses the PIT modules and can be used instead.
The FTM modules are only available for the T3.x boards. The number of modules available depends on the board type. Each module has a various number of 16bit timer channels.
Usage:
PeriodicTimer t1(FTM0), t2(FTM0); // First and second channel of the FTM0 module
OneShotTimer t3(FTM1); // First channel of the FTM1 module.
...
The RTC module uses the built in real time clock to generate periodic interrupts.The interrupt periods are limited to 1s, 0.5s, 250ms, 125ms, ..., 30.51µs. The interrupts are synchronized with the clock. E.g. the 1s interrupt is invoked exactly at the second transitions of the clock.
...
PeriodicTimer t1(RTC);
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
t1.begin([] { digitalToggleFast(LED_BUILTIN); }, 125ms); // toggles LED every 125ms in sync with the RTC
}
...
The tick timer is a 32bit software timer. Instead of using one of the built in hardware timer modules it relies on calling a tick()
function as often as possible. The tick()
function checks the cycle counter to determine if the callback function needs to be called. In the default configuration, calling tick()
is automatically handled by TeensyTimerTool in the yield()
function. Thus, you can use the tick timer in exactly the same way as the hardware timers. You can configure the way how the tick()
function is called in the config file. See here for the corresponding description.
Usage:
PeriodicTimer t1(TCK), t2(TCK);
OneShotTimer t3(TCK);
void setup()
{
t1.begin(callback1, 200);
t2.begin(callback2, 20'000'000);
t3.begin(callback3);
}
Advantages:
- Since the tick timers don't need any hardware resources, you can allocate as many timers you need. However, to allow static memory allocation TeensyTimerTool currently limits the total number of tick timers (TCK, TCK64, TCK_RTC) to 20 (that will be settable later).
- Due to the weak coupling of peripheral to the ARM core the hardware timers of the Teensy 4.0 are not very efficient. (e.g. https://forum.pjrc.com/threads/57959-Teensy-4-IntervalTimer-Max-Speed). Given the very fast ARM core of the T4.0 controller the tick timers can be more effective and faster than the hardware timers on this board.
Caveats:
The tick timers only work if the TeensyTimerTool can call the tick function with a high frequency. This means, if you can not avoid long blocking tasks the tick timers might not work for you. (Please note: delay()
or other functions calling yield()
in the background are perfectly fine to use.)
The TCK64 timer is a 64bit software timer. It works exactly like the normal TCK timers but extends the internally used counter variable to 64bit. Thus, periods of more than one hour (depending on the clock frequency) are possible.
The TCK64_RTC timer is a 64bit software timer which uses the RTC module of the T4x boards as time base. Thus, the attached callback function will be invoked in sync with the RTC which might be useful in real time applications. Usage is identical to the other TCK timers.
TeensyTimerTool - Generic Interface to Teensy Timers