cancel
Showing results for 
Search instead for 
Did you mean: 

Global variables losing their contents, using Keil and STM32l031k6

ADSB
Associate III

Dear Community,

I am encountering a strange behavior, where some of my global variables in main.c file are losing their contents (even when I declare them as static). They show the initial values which are 0. I am using Keil with optimization level O3, I used also level O1 , the problem is still occurring. My MCU is STM32l031k6. I want to know if this is related to optimization issues, has somebody encountered the same problem before? your help will be really appreciated.

Thank you very much

11 REPLIES 11
edogr.1
Associate

I have an similar problem when I create a global array in main c like

GPIO_TypeDef* x[NO] ={A,B,..} after HAL_Init and RCC_Init command executes the first member content become zero.. I changed optimization settings and this situation solved. but I don't want to change optimization due to other issues what can be the cause of problem.

>GPIO_TypeDef* x[NO] ={A,B,..}

The definition places x in a data section.

> I changed optimization settings and this situation solved.

What can changing the optimization change? A quick Google finds https://scienceprog.com/function-calls-and-stacking-of-in-embedded-systems/, which explains it pretty well. Increasing optimization may reduce function stack frames and their sizes. As mentioned in an earlier comment, if the stack is too small and above the data section, then it will overflow as functions execute, culminating in the stack trashing variables in the data section.