Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ ISR using class method?

Tags:

c++

interrupt

isr

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.

like image 379
SPlatten Avatar asked Jul 28 '26 03:07

SPlatten


1 Answers

With the information given in the comments, this is a possible solution:

class SerialPort
{
public:
    // Unrelated functions...

    void isr();  // The serial interrupt handler

    // Other unrelated stuff...
};

std::array<SerialPort, 3> serial_ports;  // Or SerialPort serial_ports[3];

static void serial_isr(int const port_index)
{
    // Common code for all serial ports that for some reason
    // can't be in the class...

    serial_ports[port_index].isr();  // Call actual ISR
}

extern "C" void interrupt serial_isr_1()
{
    // Special code for only this serial port...

    serial_isr(0);
}

extern "C" void interrupt serial_isr_2()
{
    // Special code for only this serial port...

    serial_isr(1);
}

extern "C" void interrupt serial_isr_3()
{
    // Special code for only this serial port...

    serial_isr(2);
}
like image 200
Some programmer dude Avatar answered Jul 29 '26 18:07

Some programmer dude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!