EmbDev.net

Forum: ARM programming with GCC/GNU tools Interrupt handler in C++ File?!


Author: Kenan Özdemir (gladio)
Posted on:

Rate this post
0 useful
not useful
Hi guy's

I have the following problem. I'm using the STM32F103RB and Sourcery G++ 
Lite 2008q1-126 and I try to implement the Interrupt Handlers in a C++ 
File.

I'm using C and C++ Code in my project and if I implement the handlers 
in a C File everything is working fine, when I get an Interrupt the 
handler is getting called.

But when I want to do the same with an C++ File the handler is not 
getting called.

Anyone out there with some suggestions?

Thx

Author: Karl Heinz (kbuchegg) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Try to enclose your handler in an extern "C" block to shut down name 
mangling for that function.


extern "C"
{
   your interrupt handler goes here, just as you would write it
   in C
}

Author: Kenan Özdemir (gladio)
Posted on:

Rate this post
0 useful
not useful
I tried that an I got the following error message:

error: expected unqualified-id at end of input


#ifndef TIMEMANGER_H_
#define TIMEMANGER_H_

template <typename _GlobalTimeType = unsigned long>
class TimeManager
{
private:
        _GlobalTimeType m_CurrentSuperloopTime;
        int x;
public:
  TimeManager()
  {
    m_CurrentSuperloopTime = 0;
        }

extern "C"
{
  void TIM2_IRQHandler()
  {
            x = 1;
  }
}
};

#endif /* TIMEMANGER_H_ */

Author: Karl Heinz (kbuchegg) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Well.
An Interrupt Handler cannot be part of an object.
Just as a quick thought: What is the object, the handler gets called 
for?You don't have one resp. nobody knows but you.
An Interrupt Handler is always a freestanding function and you need to 
route the execution to an object by yourself, since only you, the 
programmer, know what object should be used for eventually handeling the 
interrupt.

Author: Jörg Wunsch (dl8dtl) (Moderator)
Posted on:

Rate this post
0 useful
not useful
You cannot do that as part of a class.

I don't know the STM32, but my guess is that interrupt vectors are
known by their global name, so they cannot be established using C++
name mangling.  This also implies they cannot be hidden with in a
class (class members don't establish a global name).

Author: Kenan Özdemir (gladio)
Posted on:

Rate this post
0 useful
not useful
So that means an ISR can't be implemented in a Class and I have to do it 
in a function outside a Class right?

Author: Karl Heinz (kbuchegg) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Kenan Özdemir wrote:
> So that means an ISR can't be implemented in a Class and I have to do it
> in a function outside a Class right?

Exactly.
But of course nobody is hindering you in calling a member function of an 
object from the interrupt handler function.
template <typename _GlobalTimeType = unsigned long>
class TimeManager
{
private:
        _GlobalTimeType m_CurrentSuperloopTime;
        int x;
public:
  TimeManager()
  {
    m_CurrentSuperloopTime = 0;
        }

  void TimeTick()
  {
            x = 1;
  }

};

TimeManager theOneAndOnlyTimerManager;

extern "C"
{
  void TIM2_IRQHandler()
  {
     theOneAndOnlyTimerManager.TimeTick();
  }
}

Author: Kenan Özdemir (gladio)
Posted on:

Rate this post
0 useful
not useful
Yepp that was that what I needed.. thx for your help!

Author: escamoteur (Guest)
Posted on:

Rate this post
0 useful
not useful
It should be possible as a class static function.
Tom

Author: Karl Heinz (kbuchegg) (Moderator)
Posted on:

Rate this post
0 useful
not useful
escamoteur wrote:
> It should be possible as a class static function.

I haven't tried it, but i don't think it will work. Name mangling gets 
in your way and you can't get rid of it with a static member function.

Author: Kenan Özdemir (gladio)
Posted on:

Rate this post
0 useful
not useful
I'm back with another question.

I have my main.cpp where i create an object of TimeManager. To use 
TIM2_IRQHandler it must be a static function. But when I try to access 
the static variable of TimeManager over TimeTick I get an error message:

main.o: In function `TimeManager<unsigned long>::TimeTick()':
...SourceCodes/TimeManager.h:196: undefined reference to 
`TimeManager<unsigned long>::var'

template <typename _GlobalTimeType = unsigned long>
class TimeManager
{
private:
static int var;

public:
  TimeManager(){}

  static void TimeTick()
  {
      var++;
  }

};

extern "C"
{
  static void TIM2_IRQHandler()
  {
     TimerManager<>::TimeTick();
  }
}

Author: Kenan Özdemir (gladio)
Posted on:

Rate this post
0 useful
not useful
I also tried it with a static class function but it doesn't worked

Author: Mark (Guest)
Posted on:

Rate this post
0 useful
not useful
The right answer was given above... the IRQ_Handler must be a static 
member of the class.  Also, If any class variables are accessed inside 
this static ISR then they also need to be static.  Stick to this and all 
will be good.

A class is just a namespace.

Author: Rolf Magnus (Guest)
Posted on:

Rate this post
0 useful
not useful
> The right answer was given above... the IRQ_Handler must be a static
> member of the class.

No. Aan interrupt handler must be extern "C", and that is only possible 
for non-member functions.

The actual problem is that the variable is only declared, but not 
defined anywhere.

Author: Mark (Guest)
Posted on:

Rate this post
0 useful
not useful
I have this style of code everywhere.  I compile with yagarto.


class C_ADC {
public:
  C_ADC(){}
  virtual ~C_ADC();

  void Init(uint32_t ADC_Clk = DEF_ADC_CLK);

private:
  static void IRQ_Handler();

};


irq void C_ADC::IRQ_Handler()
{

}


void C_ADC::Init(uint32_t ADC_Clk)
{
    installVector(VIC_CH18_ADC0, (pfunction_t)&C_ADC::IRQ_Handler,  0);

}

Author: Mark (Guest)
Posted on:

Rate this post
0 useful
not useful
>>I have my main.cpp where i create an object of TimeManager. To use
>>TIM2_IRQHandler it must be a static function. But when I try to access
>>the static variable of TimeManager over TimeTick I get an error message:

>>main.o: In function `TimeManager<unsigned long>::TimeTick()':
>>...SourceCodes/TimeManager.h:196: undefined reference to
>>`TimeManager<unsigned long>::var'




>The actual problem is that the variable is only declared, but not
>defined anywhere.


Yes -- this is the answer to the above. But that is a problem unrelated 
to the IRQ handler being in the class, as the error would show for all 
uninitialized static variables.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.