Interrupt handler in C++ File?!

OP #1868861
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
OP
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_ */
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.
Moderator
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).
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
}
OP
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();
  }
}
Guest
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.
Guest
> 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.
Guest
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);

}
Guest
>>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

A question of your own? This thread is over 16 years old. Ask it in a thread of its own – more people read along there, and it gets answered more often and sooner.

Start a new thread

A fix, an addition or feedback on this thread? Then reply here.

Formatting
  • [c]C-Code[/c]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]
  • [[Title]] – link to an article
  • Link to another post: right-click its post ID (#8078628), copy the address, paste it into the text

More about formatting

Post long source code as an attachment, not in the text.

Already registered? Log in

Not shown to anyone. We send you a link; your post appears once you confirm it there.

By submitting you create a free account and accept the terms of use.