Question about Timers and Possible Teensy 4.0 Capabilities

Status
Not open for further replies.

madgrizzle

Active member
I'm working on a project that I'm adapting to run on a Teensy 3.5 and someone just released an entirely different version of code (based on something much better) but they did it based upon using an Arduino Due. I am contemplating sticking with a Teensy and porting their code over since my additions will be doing a great deal of floating point calculations. However, they used DueTimer.cpp and are using Timers0 through Timer6. Is there a way to get these six timers on a Teensy 3.5 and if not, can it be done on a Teensy 4.0?

Thanks
 
Maybe you could use IntervalTimer?

https://www.pjrc.com/teensy/td_timing_IntervalTimer.html

I'm not familiar with DueTimer, but a quick look seems all it does it call interrupt functions at regular intervals.

If you need more, maybe look at the TimerOne and TimerThree libs, which are ported to Teensy. Those use different timers, so you could get 2 more regular timer calls this way. They are based on the FTM timers, so if you dig into the code you could probably make even 2 more for the other FTM timers (Teensy 3.5 has 4 of those timers).

You might also consider whether using interrupt so much is really the best way to go about things. Many people have built projects this way, believing they've done everything needed with volatile variables and interrupt disable, only to find rare cases that crash are incredibly hard to debug. Interrupts are powerful, but they've a very sharp double-edge sword.

Often less timing critical stuff can be done quite well with elapsedMillis or elapsedMicros.

https://www.pjrc.com/teensy/td_timing_elaspedMillis.html

Highly recommend you use those instead of interrupts, if you can.
 
Thanks for the quick response. I'm really new to all of this and winging most of it. The timers are used for many functions (this is for a CNC machine firmware based on GRBL).. from the guy that did the firmware update when I asked which were being used:

"Timer1 for Spindle, Timer5 for PID, Timers 3 & 4 are used by GRBL stepper.c, and Arduino uses Timers 0, 2, 6 for PWMs, delay functions, etc."

Timing is critical on many of those functions, so I don't think elapsedmillis/micros will work. I saw TimerOne & TimerThree (and read where you indicated .. I think.. one could create TimerTwo and TimerFour) but I think I need TimerFive and TimerSix as well? There probably is a way to work around it, but I'm not knowledgeable enough to do so.
 
Timing is critical on many of those functions, so I don't think elapsedmillis/micros will work.

Perhaps not for the steppers, but you really should use these for most of that other stuff. On top of the problems you'll avoid from interrupt context, using fewer interrupt for other stuff means the really critical things (timing of pulses to the steppers) you do with interrupts will be more efficient.

Teensy 3.5 is significantly faster than Arduino Due, and massively faster if float variables are used, which gives you quite a lot of breathing room to do things in simpler ways.

Interrupts do have substantial overhead, because the processor has to save and restore context, so they're not automatically better and in some cases much worse if used poorly. Do yourself a huge favor and try to use elapsedMicros for the non-stepper timing stuff.
 
Status
Not open for further replies.
Back
Top