Doesn't that get optimized away? Even when volatile? I'll have a look at the assembly...
callback();
uint8_t dummy __attribute__((unused));
dummy = regs->CSCTRL;
}
...
Timer period/delay now additionally accepts floating point values.
Maybe I'm missing something, is there an official way to install this on the Mac version of the Arduino software?
With the newer MAC OS releases, Teensyduino could no longer install like it used to. So install Paul has to ship a whole Arduino package with Teensyduino parts installed. Which you can install from the normal place: https://www.pjrc.com/teensy/td_download.html
TeensyTimerTool is a normal, Arduino compatible library and you can install it like any other library. In case you never did this, here some info https://www.arduino.cc/en/guide/libraries . Scroll down to the chapter about installing zip libraries or to the chapter describing manual installs. Basically, all you have to do is to copy the library to your libraries folder and restart the IDE.
Pre-Scaling the TMR channels
The TMR timers are clocked with 150MHz. Since these timers are only 16bit wide you'd get a maximum period of 1/150MHz * 2^16 = 437µs. For longer periods you can pre-scale the timer clock using the TMR_DEFAULT_PSC. Setting this to 0,1,2..7 leads to a pre-scale value of 1,2,4,...128 respectively. Using say 7 you'd get 128/150MHz = 0.853µs per tick and a maximum period of 128/150MHz * 2^16 = 55.9ms.
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
PeriodicTimer t1(GPT1);
void setup()
{
pinMode(0, OUTPUT);
t1.begin([] { digitalWriteFast(0, !digitalReadFast(0));}, 0.25);
}
void loop()
{}
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
PeriodicTimer event(TMR1);
int pin = 13;
volatile int state = 0;
void tRun(){
state = 1 - state;
digitalWriteFast(pin, state);
return;
}
void setup() {
// put your setup code here, to run once:
pinMode(pin,OUTPUT);
event.begin(tRun,2);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
}
Gives a pulse width of 1.70us (setting the interval to 2.5 has no effect on the pulse width, are you sure it can accept float values?)
That is strange and I never observed that, I'll have a look at it later. Did you try the code I posted above?Editing to use timer GPT1 give a pulse width of 170ns (actually ~168ns)!!
The TCK timer is a pure software timer without any interrupts. It relies on being called often enough in the background (yield()) For an empty loop you can expect some 4Mhz call frequency which will result in a precision of about 0.25µs which corresponds to what you observe. In real life applications you can not call it that often and the precision will decrease accordingly. So, better not use a TCK for high frequency applicationsUsing timer TCK gives a pulse width of 2.134us
The TMR timers run at a default prescaler of 128, this gives 128/150 = 0.85µs per tick. You need to set the prescaler to a smaller value to increase the granularity. See here https://github.com/luni64/TeensyTimerTool#configuration how to do this. Please note that the TMRs are 16bit only, having a too small prescaler will reduce the max period significantly.
That is strange and I never observed that, I'll have a look at it later. Did you try the code I posted above?
The TCK timer is a pure software timer without any interrupts. It relies on being called often enough in the background (yield()) For an empty loop you can expect some 4Mhz call frequency which will result in a precision of about 0.25µs which corresponds to what you observe. In real life applications you can not call it that often and the precision will decrease accordingly. So, better not use a TCK for high frequency applications
As mentioned in the documentation you need to do a clean rebuild (once) when you add a user config. Since the Arduino IDE doesn't provide a "clean" command, it is best is to simply delete the build folder. However, for a quick experiment it is probably is easier to change the settings in the DefaultConfig.h (located in the library folder).I have put userConfig.h in the sketch folder, tried each prescaler value from 0 to 7... with a interval value of 0.636 (which should be a good value for a prescaler of 0)... all had no effect... I even tried to set the USE_GPT_PIT_150MHz = true, also no effect... so I don't think the compiler is using this file.
This is the max value for a prescaler of 128. The timer defaults to max in case of a period error. I assume, the period you set was smaller than 1 tick?Curiously the pulse width seems to be defaulting to 56.4ms when using fractional interval values.
I just requested to add it to the Arduino Library repository. That usually takes a day or two.
Editing to use timer GPT1 give a pulse width of 170ns (actually ~168ns)!!
void tRun(){
state = 1 - state;
digitalWriteFast(pin, state);
asm volatile("dsb"); // <---------------------------------
return;
}
void tRun()
{
digitalWriteFast(pin, !digitalReadFast(pin));
}