Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 14 of 14

Thread: Interrupt priority for attachInterrupt?

  1. #1
    Senior Member
    Join Date
    Nov 2012
    Posts
    271

    Interrupt priority for attachInterrupt?

    I am trying to get FreeRTOS 8.0.1 running on Teensy 3.x and Due.

    It appears that attachInterrupt is set to priority 128 for Teensy3. Is this true?

    Are any other hardware interrupts set to a numerical value less than 128 (higher priority since zero is the "highest" priority in Cortex M).

    On Due the attachInterrupt priority appears to be zero. If true, this seems like an unfortunate choice since zero is the "highest" priority.

    All PIO priorities are set like this in WInterrupts.c for Due.
    Code:
    	NVIC_SetPriority(PIOA_IRQn, 0);
    FreeRTOS 8.0.1 forbids all calls from an ISR at numerical priority zero so I need to decide how to handle Due.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,684
    Quote Originally Posted by Bill Greiman View Post
    It appears that attachInterrupt is set to priority 128 for Teensy3. Is this true?
    Yes, on Teensy all the interrupts default to priority 128.

    On my TO-DO list is an option for attachInterrupt to specify the priority.

    Are any other hardware interrupts set to a numerical value less than 128 (higher priority since zero is the "highest" priority in Cortex M).
    Yes. USB defaults to 112, the hardware serial ports default to 64, and systick defaults to 0.

    Some of the libraries raise or lower their interrupt priority.

    On Due the attachInterrupt priority appears to be zero. If true, this seems like an unfortunate choice since zero is the "highest" priority.
    Yes, there are many unfortunate choices in Arduino Due's core library. That stuff is all controlled by the Arduino Team.

  3. #3
    Senior Member
    Join Date
    Mar 2013
    Posts
    651
    So is setting attachInterrupt priority still not possible?

  4. #4
    it would be great if we could set the attachInterrupt priority.

    As mentioned here my DMX signal on the RX pin is sometimes preventing an attachInterrupt read on Pin2.

    A solution would be much appreciated.

  5. #5
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    Quote Originally Posted by stephanschulz View Post
    it would be great if we could set the attachInterrupt priority.

    As mentioned here my DMX signal on the RX pin is sometimes preventing an attachInterrupt read on Pin2.

    A solution would be much appreciated.
    For ARM-Teensy, there is a macro in kinetis.h : NVIC_SET_PRIORITY(irqnum, priority)
    For other Arduino-boards, this forum is the wrong place...

    Edit: Changing the priorities of existing interrupts (like usb for example) may result in unexpected side-effects. 128(="Medium setting" on T3.x) is a very good choice for the most cases.
    Last edited by Frank B; 04-29-2015 at 09:04 PM.

  6. #6
    thanks for the reply.
    I saw the NVIC_SET_PRIORITY but i did not know how to set it from within my sketch and how to set it for the right pin. in my case pin 2.

    right now i am doing this:
    Code:
      pinMode(zeroCrossPin, INPUT);
      attachInterrupt(zeroCrossPin, zero_crosss_interrupt, RISING);
    BTW this is for a teensy 3.1.

    thanks.

  7. #7
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    Ok, you have to find out on which port is your pin (this is NOT compatible to other Arduinos), it's visible on the schematics (if i remember correctly), or simply set the priority for all ports.
    Kinetis.h:
    Code:
    	IRQ_PORTA =		87,
    	IRQ_PORTB =		88,
    	IRQ_PORTC =		89,
    	IRQ_PORTD =		90,
    	IRQ_PORTE =		91,
    You have to add 16 to these numbers: NVIC_SET_PRIORITY(IRQ_PORTE+16, priority)
    Code:
    // 0 = highest priority
    // Cortex-M4: 0,16,32,48,64,80,96,112,128,144,160,176,192,208,224,240
    // Cortex-M0: 0,64,128,192
    Edit: The lower the value you choose, the faster should be your interrupt-code... :-)
    Last edited by Frank B; 04-29-2015 at 09:23 PM.

  8. #8
    i see.

    so if i follow this post for the teensy 3.0
    https://forum.pjrc.com/threads/23950...ll=1#post34158
    Pin2 = D2 -> PortD

    NVIC_SET_PRIORITY(IRQ_PORTD +16, 60)

    since:
    serial1 NVIC_SET_PRIORITY = 64
    usb NVIC_SET_PRIORITY = 112
    interrupt default priority = 128

    i will try this tomorrow.

    just wondering how this will effect the performance of other pins on this port.
    i am doing PWM on these other pins.

    thx.

  9. #9
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,679
    Quote Originally Posted by stephanschulz View Post
    NVIC_SET_PRIORITY(IRQ_PORTD +16, 60)
    One note: 60 is the same as 48,49..63 so for better readability i would choose 48 (= 3 << 4)

    Ooops... sorry, i'm not sure with this +16.. perhaps i should read the manual again.
    Try without "+16" :-)
    +16 was for the vectortable.. haha, i'm getting old... really, i'm not sure at the moment.
    Perhaps others know it better ...
    Last edited by Frank B; 04-29-2015 at 10:02 PM.

  10. #10
    Member Etienne's Avatar
    Join Date
    Mar 2016
    Location
    Ardeche, France
    Posts
    45
    Hi,

    I would like be able to set interrupt priority as well, and I was wondering if there as a table somewhere that would give me the port corresponding to a pin, or if I have to build it myself from the schematics ?

  11. #11
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,963
    There are tons of tables built into the headers and code for the Teensy processor in use.

    Cool thing is they work out in the compiler.

    just do :: attachinterrupt( Pin#, ...

  12. #12
    Member Etienne's Avatar
    Join Date
    Mar 2016
    Location
    Ardeche, France
    Posts
    45
    Thanks, I have noticed all these tables, but I could not find the one I'm talking about.
    I'm not trying to attach an interrupt, I already figured that out.
    I'm talking about setting the priority of any pin without having to figure out manually the port to which the pin is assigned.

  13. #13
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    546
    Here a quick sketch showing how to extract this information. One probably can find a more intelligent way but this is doing what you need.

    Code:
    #include "Arduino.h"
    
    const char *portName(volatile void *address)
    {
      static char result[50];
      char P;
      int irq;
    
      switch ((unsigned)address)
      {
      case (unsigned)&GPIOA_PDOR:
        P = 'A';
        irq = IRQ_PORTA;
        break;
      case (unsigned)&GPIOB_PDOR:
        P = 'B';
        irq = IRQ_PORTB;
        break;
      case (unsigned)&GPIOC_PDOR:
        P = 'C';
        irq = IRQ_PORTC;
        break;
      case (unsigned)&GPIOD_PDOR:
        P = 'D';
        irq = IRQ_PORTD;
        break;
      default:
        irq = 0;
        P = 'X';
      }
    
      sprintf(result, "Port-%c (%p) IRQ Nr: %d", P, address, irq);
      return result;
    }
    
    void setup()
    {
      while (!Serial);
        
      Serial.printf("Pin: 0, %s\n", portName(&CORE_PIN0_PORTREG));
      Serial.printf("Pin: 1, %s\n", portName(&CORE_PIN1_PORTREG));
      Serial.printf("Pin: 2, %s\n", portName(&CORE_PIN2_PORTREG));
      Serial.printf("Pin: 3, %s\n", portName(&CORE_PIN3_PORTREG));
      Serial.printf("Pin: 4, %s\n", portName(&CORE_PIN4_PORTREG));
      Serial.printf("Pin: 5, %s\n", portName(&CORE_PIN5_PORTREG));
      Serial.printf("Pin: 6, %s\n", portName(&CORE_PIN6_PORTREG));
      Serial.printf("Pin: 7, %s\n", portName(&CORE_PIN7_PORTREG));
      Serial.println("and so on...");   
    }
    
    void loop()
    {
    }
    Output:
    Code:
    Pin: 0, Port-B (0x400ff040) IRQ Nr: 88
    Pin: 1, Port-B (0x400ff040) IRQ Nr: 88
    Pin: 2, Port-D (0x400ff0c0) IRQ Nr: 90
    Pin: 3, Port-A (0x400ff000) IRQ Nr: 87
    Pin: 4, Port-A (0x400ff000) IRQ Nr: 87
    Pin: 5, Port-D (0x400ff0c0) IRQ Nr: 90
    Pin: 6, Port-D (0x400ff0c0) IRQ Nr: 90
    Pin: 7, Port-D (0x400ff0c0) IRQ Nr: 90
    and so on...

  14. #14
    Member Etienne's Avatar
    Join Date
    Mar 2016
    Location
    Ardeche, France
    Posts
    45
    Thank you very much luni, I'll use that to build a table.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •