attachInterrupt priority (Teensy 4.0)

Status
Not open for further replies.

Kuba0040

Well-known member
Hello,
I am working on a project using the audio library that has 2 running interrupts in it.
The first one is a IntervalTimer which triggers an interrupt at a fixed rate. The other is a rising pin interrupt (attachInterrupt). I would like to be able to set the priority for these interrups so the CPU time is shared like this:

(Most important, highest CPU priority)
1. Audio library
2. IntervalTimer
3. Pin interrupt
4. Main arduino code (void loop, ect...)
(Least important, lowest CPU priority)

So here are my questions:
How can I set the priority for the IntervalTimer and attachInterrupt?
What priority does the Audio Library and main arduino code run at?

If anybody knows, please let me know. Thanks!
 
You can use the NVIC_SET_PRIORITY(irqnum, value)
calls to control the priority of the interrupts. The lower the number the higher the priority.
I believe all default to 128. You can set any value 0-255 by this call, HOWEVER
there are only so many actual priorities:
From imxrt.h
Code:
// 0 = highest priority
// Cortex-M7: 0,16,32,48,64,80,96,112,128,144,160,176,192,208,224,240
#define NVIC_SET_PRIORITY(irqnum, priority)  (*((volatile uint8_t *)0xE000E400 + (irqnum)) = (uint8_t)(priority))
#define NVIC_GET_PRIORITY(irqnum) (*((uint8_t *)0xE000E400 + (irqnum)))

Note: when you change the interrupt priority, you need to understand what the underlying interrupt is and that all things that interrupt services will be set to that level.

For example if you wish to change the interrupt priority of attachInterrupt(pin, fn);
call, you need to understand that on T4.x all of these are serviced by one interrupt: IRQ_GPIO6789
There are ways to maybe speed up for specific pin/pins which is covered in some other threads recently.

The IntervalTimer code has a method priority(n)
Which helps to prioritize processing of IntervalTimer instances... but they still boil down to IRQ_PIT

Good luck
 
Don't use the highest priorities as they are needed to keep the time (micros() etc)
Also, if you take too much time in a priority > USB priority, you'll slow down communication.

It's ok for the Audio priority to have some lag. The lib can work with am time-lag of 1/2 audio block (maximum). So, it is not critical.
 
Status
Not open for further replies.
Back
Top