This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Declaring a Class member function as an interrupt handler

Hi.

I'm trying to use a member function of a static class as an interupt handler. I can handle interrupts in C++ file by creating a function with the same name as in startupxx.s file and declare it with extern"C" but i'm unable to do the same in class member function. Does anyone knows a solution for this problem.

  • There is a bit of information missing.

    "I am unable to", doesn't really tell much. Unable in which way? You don't tell us if you have an issue with symbol names, or stack frames or something else.

    Further, you haven't told us what processor you have, so we don't know what special requirements there may be for stack handling of the ISR.

    But note also that that C++ has different rules for external naming of symbols, to support type-safe linking and multiple functions with same name but taking different types of parameters. extern "C" informs the C++ compiler that a function need to be compatible with C code.

    Always try to give as full explanation as possible, when asking questions.

  • I'm trying to use a member function of a static class as an interupt handler.

    That begs the question: why? You already know how to do it without that additional requirement. So why make like more difficult for no tangible benefit?

    There's absolutely no way to do it for a normal member function, because interrupt handlers can't pass any arguments, so there's no way to pass the hidden "this" argument. So at most it could be a class method, a.k.a. "static" member function. But a class method has virtually no advantage over a stand-alone extern "C" function.

    If all else breaks, you can always call your C++ class member directly from an extern "C" interrupt handler and be done with it, at the price of a small run-time overhead which the compiler can probably optimize away.

    function with the same name as in startupxx.s file and declare it with extern"C" but i'm unable to do the same in class member function.

    That'll be because the actual name of a C++ method is rather different from what you typed in your source. I suggest you look up "name mangling" in your C++ textbooks for further explanation.

  • Thanks alot. I hope to access private member variables and call private functions of the class inside an interrupt hadler function. Thats why i tried to declare the member function as an interrupt handler. I see that it is impossible to pass hiden "this" parameter to interrupt handler. I had missed this issue. Thanks for help.