Since you tagged it Teensy: You could also reduce the priority of the interrupt, so that it can be interrupted by higher priortypriority interrupts. Thus you can implement interrupts with longer durations whithoutwithout blocking important system functions.
Here is an example using the GPT1 and GPT2 timers running at different priorities:
This shows that the high priopriority timer (running at 50ms intervall50 ms interval) intercepts the low prio isrpriority ISR which runs every 500ms500 ms for about 100ms100 ms.
Since you tagged Teensy: You could also reduce the priority of the interrupt, so that it can be interrupted by higher priorty interrupts. Thus you can implement interrupts with longer durations whithout blocking important system functions.
Here an example using the GPT1 and GPT2 timers running at different priorities:
This shows that the high prio timer (running at 50ms intervall) intercepts the low prio isr which runs every 500ms for about 100ms.
Since you tagged it Teensy: You could also reduce the priority of the interrupt, so that it can be interrupted by higher priority interrupts. Thus you can implement interrupts with longer durations without blocking important system functions.
Here is an example using the GPT1 and GPT2 timers running at different priorities:
This shows that the high priority timer (running at 50 ms interval) intercepts the low priority ISR which runs every 500 ms for about 100 ms.
Here an example using the GPT1 and GPT2 timers running at different priorities:
#include "Arduino.h"
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
PeriodicTimer loPrioTimer(GPT1);
PeriodicTimer hiPrioTimer(GPT2);
void onLowPrioTimer() // some long task running from interrupt with low prio
{
Serial.printf(" Start low prio isr @t=%d ms\n", millis());
for (int i = 0; i < 10; i++)
{
Serial.printf(" lowPriority task %d\n",i);
delay(10);
}
Serial.println(" Stop low prio isr");
}
void onHiPrioTimer() // high prio, will interrupt the low prio isr
{
Serial.printf("high priority @t=%d ms\n", millis());
}
void setup()
{
while (!Serial) {}
loPrioTimer.begin(onLowPrioTimer, 500ms); // GPT1
hiPrioTimer.begin(onHiPrioTimer, 50ms); // GPT2
NVIC_SET_PRIORITY(IRQ_GPT1, 128); // lowest prio
NVIC_SET_PRIORITY(IRQ_GPT2, 16); // high prio
}
void loop()
{
}
which prints:
high priority @t=1057 ms
high priority @t=1107 ms
high priority @t=1157 ms
high priority @t=1207 ms
high priority @t=1257 ms
high priority @t=1307 ms
high priority @t=1357 ms
high priority @t=1407 ms
high priority @t=1457 ms
high priority @t=1507 ms
Start low prio isr @t=1507 ms
lowPriority task 0
lowPriority task 1
lowPriority task 2
lowPriority task 3
lowPriority task 4
high priority @t=1557 ms
lowPriority task 5
lowPriority task 6
lowPriority task 7
lowPriority task 8
lowPriority task 9
high priority @t=1607 ms
Stop low prio isr
high priority @t=1657 ms
high priority @t=1707 ms
high priority @t=1757 ms
high priority @t=1807 ms
high priority @t=1857 ms
high priority @t=1907 ms
high priority @t=1957 ms
high priority @t=2007 ms
Start low prio isr @t=2007 ms
lowPriority task 0
lowPriority task 1
lowPriority task 2
lowPriority task 3
lowPriority task 4
high priority @t=2057 ms
lowPriority task 5
lowPriority task 6
lowPriority task 7
lowPriority task 8
lowPriority task 9
high priority @t=2107 ms
Stop low prio isr
high priority @t=2157 ms
high priority @t=2207 ms
high priority @t=2257 ms
high priority @t=2307 ms
This shows that the high prio timer (running at 50ms intervall) intercepts the low prio isr which runs every 500ms for about 100ms.
Here an example using the GPT1 and GPT2 timers running at different priorities:
#include "Arduino.h"
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;
PeriodicTimer loPrioTimer(GPT1);
PeriodicTimer hiPrioTimer(GPT2);
void onLowPrioTimer() // some long task running from interrupt with low prio
{
Serial.printf(" Start low prio isr @t=%d ms\n", millis());
for (int i = 0; i < 10; i++)
{
Serial.printf(" lowPriority task %d\n",i);
delay(10);
}
Serial.println(" Stop low prio isr");
}
void onHiPrioTimer() // high prio, will interrupt the low prio isr
{
Serial.printf("high priority @t=%d ms\n", millis());
}
void setup()
{
while (!Serial) {}
loPrioTimer.begin(onLowPrioTimer, 500ms); // GPT1
hiPrioTimer.begin(onHiPrioTimer, 50ms); // GPT2
NVIC_SET_PRIORITY(IRQ_GPT1, 128); // lowest prio
NVIC_SET_PRIORITY(IRQ_GPT2, 16); // high prio
}
void loop()
{
}
which prints:
high priority @t=1057 ms
high priority @t=1107 ms
high priority @t=1157 ms
high priority @t=1207 ms
high priority @t=1257 ms
high priority @t=1307 ms
high priority @t=1357 ms
high priority @t=1407 ms
high priority @t=1457 ms
high priority @t=1507 ms
Start low prio isr @t=1507 ms
lowPriority task 0
lowPriority task 1
lowPriority task 2
lowPriority task 3
lowPriority task 4
high priority @t=1557 ms
lowPriority task 5
lowPriority task 6
lowPriority task 7
lowPriority task 8
lowPriority task 9
high priority @t=1607 ms
Stop low prio isr
high priority @t=1657 ms
high priority @t=1707 ms
high priority @t=1757 ms
high priority @t=1807 ms
high priority @t=1857 ms
high priority @t=1907 ms
high priority @t=1957 ms
high priority @t=2007 ms
Start low prio isr @t=2007 ms
lowPriority task 0
lowPriority task 1
lowPriority task 2
lowPriority task 3
lowPriority task 4
high priority @t=2057 ms
lowPriority task 5
lowPriority task 6
lowPriority task 7
lowPriority task 8
lowPriority task 9
high priority @t=2107 ms
Stop low prio isr
high priority @t=2157 ms
high priority @t=2207 ms
high priority @t=2257 ms
high priority @t=2307 ms
This shows that the high prio timer (running at 50ms intervall) intercepts the low prio isr which runs every 500ms for about 100ms.