EmbDev.net

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


von Kenan Ö. (gladio)


Rate this post
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

von Karl H. (kbuchegg)


Rate this post
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
}

von Kenan Ö. (gladio)


Rate this post
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_ */

von Karl H. (kbuchegg)


Rate this post
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.

von Jörg W. (dl8dtl) (Moderator)


Rate this post
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).

von Kenan Ö. (gladio)


Rate this post
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?

von Karl H. (kbuchegg)


Rate this post
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.
1
template <typename _GlobalTimeType = unsigned long>
2
class TimeManager
3
{
4
private:
5
        _GlobalTimeType m_CurrentSuperloopTime;
6
        int x;
7
public:
8
  TimeManager()
9
  {
10
    m_CurrentSuperloopTime = 0;
11
        }
12
13
  void TimeTick()
14
  {
15
            x = 1;
16
  }
17
18
};
19
20
TimeManager theOneAndOnlyTimerManager;
21
22
extern "C"
23
{
24
  void TIM2_IRQHandler()
25
  {
26
     theOneAndOnlyTimerManager.TimeTick();
27
  }
28
}

von Kenan Ö. (gladio)


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

von escamoteur (Guest)


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

von Karl H. (kbuchegg)


Rate this post
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.

von Kenan Ö. (gladio)


Rate this post
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();
  }
}

von Kenan Ö. (gladio)


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

von Mark (Guest)


Rate this post
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.

von Rolf Magnus (Guest)


Rate this post
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.

von Mark (Guest)


Rate this post
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);

}

von Mark (Guest)


Rate this post
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.

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.