Search code examples
c++interruptisr

C++ ISR using class method?


Is it possible to use a class method as the Interrupt Service Routine?

I have an ISR written and working in C using a function:

    static void interrupt far ISR(...) {}

I've tried in C++ to create a method (prototype):

    void interrupt far ISR(...);

Then the implementation:

    #pragma interrupt
    void interrupt far MyClass::ISR(...) {
    ...
    }

But I get all sorts of errors when I try to use this with 'setvect':

    setvect(muint16Vector, &ISR);

I'm trying to write a class to service a serial port, the ISR would service the Rx of data from the port. The ISR would then use instance specific members.

I have 3 serial ports, so I would like to create 3 instances of the class.


Solution

  • The real answer is 'No' it is not possible in C++ to use a method as the ISR handler directly.

    You have to resort to standard C functions and then with glue in the ISR function, reference the required object.

    It ends up as quite a messy solution.