cancel
Showing results for 
Search instead for 
Did you mean: 

Loosing scope in global variables

edmunoz
Associate II
Posted on January 08, 2015 at 14:22

Hello!

I'm using STM32 CubeMX 4.3.1 generating code in Keil. 

I experienced extrange behavior with Global variables. It seems as if at some point within a function a local variable loses its contents...

But debugging the code I could see it's not that the global variable had lost its content ... (that was my first thought)

It seems like loses its scope. It is strange in a global variable right?

At the beginning of the function I can manage it properly, but later in the same function can not even compare it to a value or assign its value to another variable. Strangely, if I halt there the debug and pass the mouse over the global variable, popup values are correct. But making the next step in debug takes no effect. 

I know that the variable retains its value because if I create a dummy function call it from where it fails and doing the assignment or comparison there, works fine...

In dissasembly I can see that one of the register involved has not the correct offset, so I'm focusing in optimization levels to see if i can find out where is the problem.

Please, if anyone can help... It's a known issue??

Thank you very much in advance!

2 REPLIES 2
Posted on January 08, 2015 at 15:19

Optimization can move local variable from the stack to registers, change those choices, and reduce the scope.

Watch for stack overrun, or corruption, and things that are volatile or passed as pointers, or used for DMA.

Keil generally has a very small stack allocation, you could make that bigger, or understand/confirm its current utilization.

Turn optimization off, confirm what version of Keil you're using.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
edmunoz
Associate II
Posted on January 08, 2015 at 15:43

Thank you very much for you quick help, Clive1!!

I'm not using Heap and the stack was increased to 4400 in startup_stm32f205xx.s.

One of the places i saw the ''no-scope'' fault was the main function (where the stack is not fulled by jumps). 

Anyway, lowering the optimization from 3 to 2 seems to solve the problem!! (turn optimization to 0 crash the program in hard fault)  I'll check if everything else is working fine...

Thanks again!