C++ and interrupts

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

C++ and interrupts

Jump to solution
1,973 Views
Vasilakes
Contributor II

Not new to embedded systems, not new to C++.

 

New to embedded C++ however.

 

I have interrupts 'working'  by putting them in a C file and accessing global variables.

 

Is there a more C++ way of doing this?

 

Using CW 5.9 and MC9s12

 

Thanks

Keith

Labels (1)
Tags (1)
0 Kudos
1 Solution
583 Views
Lundin
Senior Contributor IV
If you mean private encapsulation, no that isn't possible. In both C and C++ you should communicate with the ISR through static variables (at file scope) and not global variables. All variables shared between an ISR and the rest of the program must also be declared as volatile.

View solution in original post

0 Kudos
4 Replies
584 Views
Lundin
Senior Contributor IV
If you mean private encapsulation, no that isn't possible. In both C and C++ you should communicate with the ISR through static variables (at file scope) and not global variables. All variables shared between an ISR and the rest of the program must also be declared as volatile.
0 Kudos
583 Views
Vasilakes
Contributor II

Thanks, thats what I was suspecting and pretty much how I'm implementing it.

I'm following your intterupt example in another thread as a guide.

0 Kudos
583 Views
Lundin
Senior Contributor IV
Actually, you could -in theory- create a C++ class as a "Singelton" design pattern, ie a class that only allows one instance. It should then be possible to make the ISR as a static member function if you can guarantee that it will be allocated inside non-banked memory. Then you must trick the linker to believe that this function is called from the program, so it won't get optimized away.

Even if this is possible in theory (I have no idea if it is possible in reality), you achieve very little with this. Implementing and testing the class will take lots of time and you will get various code overhead. Worst of all it adds obscure complexity to the program.

So the above would be the academic's choise, while my first post would be the cynical, experienced engineer's choise :smileyhappy:
0 Kudos
583 Views
Vasilakes
Contributor II

Oh I am MUCH more interested in the cynical experienced engineers choice!

 

And that was really the reason for this thread. I had 1 way to do it, but I wanted at least an opinion on the subject.

 

I did find an article about doing interrupts in C++ using aggreation and virtual base classes, but for my little processor it seemed way overkill.

 

I mean C++ my prove to be too much. But a guy has to try right?

0 Kudos