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

Thread: Attach default Interupt Vector

  1. #1
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    531

    Attach default Interupt Vector

    I'm attaching an interrupt handler with attachInterruptVector(...). Does anyone know how to re-attach the original default handler? There is a "unused_interrupt_vector" in startup.c but this seems to be static, at least the linker complains about an undefined reference...


    Code:
    extern void unused_interrupt_vector(void);
    
    myClass::~myClass()
    {
       NVIC_DISABLE_IRQ(myIRQ);
       attachInterruptVector(myIRQ, unused_interrupt_vector); //<================
    
      // further cleanup...
    
    }

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    9,679
    The code for attach is : static inline void attachInterruptVector(IRQ_NUMBER_t irq, void (*function)(void)) { _VectorsRam[irq + 16] = function; asm volatile("": : :"memory"); }

    Was there something there before?

    Should work to store : OLD_function = _VectorsRam[irq + 16] and then put it back when done.

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    531
    Thanks, that might be even better than setting it to the default value;

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,554
    Quote Originally Posted by luni View Post
    Does anyone know how to re-attach the original default handler?
    You'd need to read the earlier value from the _VectorsRam array before writing your own. Then you could later write it back.

    But whether that would actually work also depends on whether you leave the peripheral hardware configured in the same (or similar enough) state that the original driver code was using. Even for seemingly simply peripherals like UARTs, there can be very difficult bugs if you're not very careful. For example, about a year ago a long-standing extremely obscure bug was fixed in the Teensy 3.x Serial1 & Serial2 code, where it would lock up if Serial1.end() was called while the UART's FIFO had captured an incoming byte with a framing error in its stop bit, but had not yet caused an interrupt before you called Serial1.begin(baud) with a new baud rate. After your code has used a peripheral, giving it back to the original driver in a state other than a clean reset is just asking for those sorts of very subtle bugs.

    But it "just because you can do a thing" resonates with you, then read the pointer from that array and store it before you write your own ISR function. Then later you can write the original value back.

  5. #5
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    531
    Thanks Paul, I already implemented that after defragsters post.

    Your remarks about the sensibility of that are true of course. Thinking of it, it might be better to not pretend to restore things but stick to the original idea and simply put in the pointer to the "unused_interrupt_vector()" in the destructor of my class. I meanwhile found out how to get hold of the function pointer of the default vector (forgot the extern "C"). Here a compiling example in case somebody else needs it:

    Code:
    #include "Arduino.h"
    
    extern "C"
    {
      void unused_interrupt_vector(void);
    }
    
    
    void setup()
    {
       attachInterruptVector(IRQ_GPT1, unused_interrupt_vector);
    }
    
    void loop(){}

Posting Permissions

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