Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 5 months ago.
NVIC Set Vector in class
Hi. I need to put the line
NVIC_SetVector(TIMER2_IRQn,(uint32_t)&myIRQ);
into a class.
My first idea was
ClassA::ClassA() { NVIC_SetVector(TIMER2_IRQn, (uint32_t) &ClassA::myIRQ); NVIC_EnableIRQ(TIMER2_IRQn); } void ClassA::myIRQ() { //do something ]
But the error is: error: invalid cast from type 'void (ClassA::*)()' to type 'uint32_t {aka long unsigned int}
What is wrong in my code?
1 Answer
8 years, 5 months ago.
You can only attach static functions this way, not non-static member functions.
If you do need to use a member function, the way to go is as far as I know the one used in this lib: https://developer.mbed.org/users/Sissors/code/MODI2C/
Bit large to just browse through (and very old, not really that good :P), but the basic idea:
Make a static member function for each of the IRQs you want to use. (So if only this timer, one is sufficient). Also make a static pointer to an object of your class for each of those static irq functions. Use SetVector with your static function, and in your non-static code when the interrupt is set, make the static pointer point to your current class object (eg, the 'this' pointer). Then your interrupt code can use this pointer to call the correct function. In semi-pseudo code it looks like:
ClassA *myIRQ_objectpointer; ClassA::ClassA() { myIRQ_objectpointer = this; NVIC_SetVector(TIMER2_IRQn, (uint32_t) &ClassA::myIRQ); NVIC_EnableIRQ(TIMER2_IRQn); } void ClassA::irq_function() { //Your code here } static void ClassA::myIRQ() { myIRQ_objectpointer->irq_function() }