Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
19 years ago

C++ & ISR

Dear all,

I develop my project with C++ language.

But I encounter a problem: how to register a ISR?

For example: I define a UART class

class UART

{

//Internal Data

protected:

const alt_u32 UART_BASE_ADDRESS;

const alt_u32 UART_IRQ_NUMBER;

alt_u8 bUARTReceiveData;

bool bReceiveNewData;

public:

UART(alt_u32 BaseAddress, alt_u32 IRQNumber):UART_BASE_ADDRESS(BaseAddress),UART_IRQ_NUMBER(IRQNumber)

{

alt_irq_register(UART_IRQ_NUMBER, this, UartIsr);

}

~UART() {;}

void UartIsr(void* pClass, alt_u32 id)

{

}

bool IsReceiveNewData(void)

{

return bReceiveNewData;

}

alt_u8 GetData(void)

{

return bUARTReceiveData;

}

};

But the compiler report that:

error: argument of type `void (UART:: )(void*, alt_u32)' does not match `void (*)(void*, alt_u32)'

I also try:

alt_irq_register(UART_IRQ_NUMBER, this, (void(*)(void*, alt_u32))UartIsr);

But the error still occurred.

Anybody help?

David

2 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    I usually use a private static member function in this case, static is necessary, the visibility does not matter. Inside the function you can reinterpret_cast the void pointer to a pointer to your object (assuming you passed 'this' when the ISR was registered).

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks for your reply. http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/tongue.gif

    I&#39;ll complete the answer(I got it from Scott McNutt), hope it will be useful for someone who encounter the same problem.

    Make the UartIsr() operation class static. E.g.:

    static void UartIsr(void* pClass, alt_u32 id);

    Remember, the &#39;this&#39; pointer is implicitly passed to non-static member functions -- so non-static member functions can never match a &#39;C&#39; language prototype.

    As long as UartIsr is a class member, it can always invoke other member functions regardless of their

    visibility:

    class UART {

    private:

    void doISR (alt_u32 id);

    protected:

    static void UartIsr (void *pClass, alt_u32 id)

    {

    UART *uart = (UART *)pClass;

    uart->doISR (id);

    // ... and other calls as needed ...

    }

    // ... and so on.

    };

    Thanks Scott McNutt again,

    David