We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I am using STM32F4 Discovery board and Keil MDK-ARM version 4.53.0.0.I made some code which contains static variable.I have made two different .uvision projects in which project A works fine(static variable gets initialized to zero) whereas in project B this variable shows garbage value.The only difference in these two projects is startup file that is created automatically at the time of project creation.I want to know how this file differ for the same environment? The project scenarios are as given below:
The source code for both are same as follow. ********************************************* #include <stdio.h>; #define MAX 5
void sumIt(int);
void main() {
int i =0; int array[5]={1,2,3,4,5};
for(i = 0; i<MAX; ++i) sumIt(array[i]);
}
void sumIt(int num) {
static int sum = 0;
sum+=num;
********************************************* 1.Project/compiler settings and other environment settings are also same for both.
2.The only difference is in the start up file (That is loaded automatically by microvision while creating project)
PARAMETER ========= The difference in the start up file is as follow
Project A ============ IMPORT __main LDR R0, =SystemInit BLX R0 LDR R0, =__main BX R0 ENDP
Project B ============ IMPORT main LDR R0, =SystemInit BLX R0 LDR R0, =main BX R0 ENDP
3.As a result of this, change seen in the map file is as follow PARAMETER ========= MAP File
Project A ============ startup_stm32f4xx.o(.text) refers to __main.o(!!!main) for __main
Project B ============ startup_stm32f4xx.o(.text) refers to sample.o(.text) for main (sample.o is .object code of source file sample.c)
4.And as a result of these, variable gets initialized with values as follow
PARAMETER ========= variable value
Project A ============ sum = 0x00000000(initialized to zero)
Project B ============ sum=0x1C49D227(any non zero value)
Hi, will you please explain me what is the meaning of scatter loader and __main?
When your microcontroller comes out of 'power on' or 'reset', the ram is "empty". If you have an initialized static variable, someone needs to initialise the ram location of this variable. This is what the scatter loader does: It copies the initial values for these variables from flash to ram...
... if __main is called, the linker will link the scatter loader functions into your binary.
In your boot file B, the main() function of your c-code is directly called and the scatter loader functions are never executed. Therefore, your static variables are not initialized.
You can place a breakpoint at __main in your boot file A and single-step thorugh the execution path to see what it does exactly.