0
\$\begingroup\$

Does the CMSIS SysTick_Handler() have to be placed in the main file (i.e. same file where it is configured)?

If not, how would I go about placing it elsewhere? Is it just a matter of defining the function in another .cpp file, and including "LPC17xx.h"?

\$\endgroup\$
2
\$\begingroup\$

You can put it anywhere, as long as it has the right linkage spec the linker will take care of it for you.

You don't need any header included, just extern "C" void SysTick_Handler(){ ... } in an implementation file.

\$\endgroup\$
  • 1
    \$\begingroup\$ That extern "C" part is really important. I was hung up on that for quite a while when I first started out. You don't need it if you are in C since the compiler was specified the interrupt handler names in C. But if you are in C++, function name mangling will occur if you do not use those keywords causing the compiler to be unable to not recognize the function's name as the interrupt handler. You'll get no warnings if you don't. Your interrupt will just inexplicably not work since the compiler just treats it as a regular function and use the default interrupt dummy handler. \$\endgroup\$ – Toor Apr 3 at 18:48
  • \$\begingroup\$ @Toor Can Systick_Handler() be made a member of a class? If so, does it need to be static? Ideally I'd like it to have access to context data, without needing a "load" function in the same file, which initialises variables in an included header file. \$\endgroup\$ – 19172281 Apr 3 at 18:55
  • \$\begingroup\$ @19172281 Not easily, no. A member function has an inherent this pointer passed to it as the first argument, which can't happen if it's called from elsewhere. The easiest thing to do is have your SysTick_Handler call a function on an instance of your class. \$\endgroup\$ – Colin Apr 3 at 18:57
  • \$\begingroup\$ @Colin, that's what I wanted to do originally, but what's the best way to get the instance to the handler in the first place? \$\endgroup\$ – 19172281 Apr 3 at 19:00
  • \$\begingroup\$ I don't know of a way to make the handler a class member. I would not be surprised if you could not. I write public member functions if my interrupt handler needs to access them. You could try declaring the handler as a friend to your class rather than declaring it as a class member. That might work but I've never tried it. Let me know if it works. It could be convenient if it does. \$\endgroup\$ – Toor Apr 3 at 19:01
1
\$\begingroup\$

It does not. I have it in a separate .cpp file dedicated to SysTick code. It's not even declared in the .h file associated with that .cpp file. The compiler will search it out, wherever it is. This is true for all the interrupt handler function names.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.