Use Library TaskScheduler with Teensy (4.x)

Sam Halvoe

Active member
Hello,

I would like to use the library TaskScheduler in my project, but I think the library could support Teensyduino better.
I would like to make a PR for the library, but beforehand I would like to ask for advice:
  1. The library supports std::function, but only for specific platforms (e.g. ESP32). I would like to add Teensy as a supported platform.
  2. The library supports to call yield(), but only for specific platforms (e.g. ESP32). I would like to add Teensy as a supported platform.
Here are some of the relevant code sections:
  1. check for platform for std::function
  2. conditional call to yield()
Therefore my questions are:
  1. Do all MCUs which use Teensyduino support std::function and define yield()?
  2. Is it appropriate to use the define TEENSYDUINO in my PR for the library to check if a Teensy is used (and therefore std::function and yield() are supported)? (see code sections above)
 
I've used std::function (and lambdas) extensively with Teensy 4.x and Teensyduino 1.59+ (which enabled C++17 for Teensy 3.x/4.x), it should work fine as long as dynamic memory is supported.
AFAIK the Arduino platform requires yield(), so any toolchain that is compatible with it should have it present.
 
Thanks for your response!
I will make a PR for TaskScheduler with the define TEENSYDUINO. I think use of this define here is appropriate and the easiest opposed to defines like ARDUINO_TEENSY41 and so on...
 
Last edited:
The github page for TaskScheduler says it was tested with Teensy 3.5. The only reference in the source code is use of a macro CORE_TEENSY, which is defined for all Teensy models. The simplest example program (blink) builds with TD 1.59 and runs on T4.1. It generates output to the Serial Monitor, but doesn't blink the LED as expected.
 
It looks like it's using the wfi instruction (wait for interrupt) when idle to wait until the next timer tick, that's not going to work properly on Teensy 4.x since wfi stops the systick timer by default.
 
stops the systick timer by default.
@jmarsh - was it code you showed once that restored the sys_tick to a real interrupt? Indeed as it is it doesn't trigger a waking interrupt.

Alternative would be to make a millisecond intervaltimer interrupt to wake from the wfi?
 
The github page for TaskScheduler says it was tested with Teensy 3.5. The only reference in the source code is use of a macro CORE_TEENSY, which is defined for all Teensy models. The simplest example program (blink) builds with TD 1.59 and runs on T4.1. It generates output to the Serial Monitor, but doesn't blink the LED as expected.
I cannot test the library immediately but want to try it next week.
I have seen the define _TASK_SLEEP_ON_IDLE_RUN in the example code, you could comment out the line with this define and try the blink example again, if you want to.
 
It looks like it's using the wfi instruction (wait for interrupt) when idle to wait until the next timer tick, that's not going to work properly on Teensy 4.x since wfi stops the systick timer by default.
What would be a better alternative to wfi instruction on Teensy 4.x? Maybe just a delay for 1 ms: delay(1)?
 
I commented out the "wfi" statement used for CORE_TEENSY as shown below, and the Blink example now runs correctly. I assume this means the processor spins in an idle task rather than actually sleeping. With this change, the Blink example runs correctly.

Code:
#elif defined( CORE_TEENSY )
void SleepMethod( unsigned long aDuration ) {
  // JWP 09/13/25 comment out this line for T4
  //asm("wfi");
}
//CORE_TEENSY
 
Back
Top