STM32 - SysTick_Handler

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,762
Why does the location of the handler within the source file, matter?

Consider:

1707066096466.png

When the two function prototypes (set and Reset) are repositioned to appear before the handler, I get issues, exceptions etc, why?
 

MrChips

Joined Oct 2, 2009
34,809
I don't quite understand the problem.

Normally, function prototypes are placed at the start of the source file or in a header file. They must be declared before the function is called.

Can you state which function call is giving you problems?
Set( ) and Reset( ) are not library functions. Where are they implemented?

What IDE platform are you using?
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,762
I don't quite understand the problem.

Normally, function prototypes are placed at the start of the source file or in a header file. They must be declared before the function is called.

Can you state which function call is giving you problems?
Set( ) and Reset( ) are not library functions. Where are they implemented?

What IDE platform are you using?
I've just discovered this is an odd side effect of how the src is compiled. When the project is created the files are by default "compiled as C++" even though the code is pure C. Changing that build to "compile as C" resolves the problem.

The problem was (before I tweaked the "Build as" setting) that positioning those two prototypes before the handler, causes a runtime exception, like it doesn't "see" the handler, but moving them to after the handler, it runs fine.

I use Visual Studio with VisualGDB.
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,762
Here's the crash when debugging:

1707066834669.png

Simply making that handler the very first function (even before any prototypes) it all runs fine.
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,762
Ahh, its making a bit more sense. I've pretty much never used C++, so didn't grasp the semantics of this:

1707067254690.png

It wasn't clear to me that that ifdef applies the extern "C" to the very next thing in the file, I saw it and interpreted it as some global directive for the whole file, but it isn't. So it was nothing whatsoever to do with it being "first" but with whether the func had that extern "C" prefix.
 

Thread Starter

ApacheKid

Joined Jan 12, 2015
1,762
Being a bit OCD about these things, I'd have written this in these samples as:


C++:
#ifdef __cplusplus
extern "C" void SysTick_Handler(void)
{
    HAL_IncTick();
    HAL_SYSTICK_IRQHandler();
}
#else
void SysTick_Handler(void)
{
    HAL_IncTick();
    HAL_SYSTICK_IRQHandler();
}
#endif
Perhaps overkill but certainly clearer and better for beginners. Anyway, I did learn something so that's a plus!
 
Top