cancel
Showing results for 
Search instead for 
Did you mean: 

Global variables not initialized converting a running project

Roberto C
Associate III

Hello,

converting a runnig project from IDE to VisualStudioCode, I have a strange problem:

all global variables are not initialized...

example:

uint32_t TestTemp1=0x1234;

When running after the main(), if i check the TestTemp1 value I don't found the expected value (0x1234), but a random value....

 

After checking I Found that the problem was in linker file:

This NOT INITIALIZE !

/* Specify the memory areas */
MEMORY
{
  FLASH (rx)     : ORIGIN = 0x08000000, LENGTH = 2048K
  DTCMRAM (xrw)  : ORIGIN = 0x20000000, LENGTH = 128K
  RAM_D1 (xrw)   : ORIGIN = 0x24000000, LENGTH = 512K
  RAM_D2 (xrw)   : ORIGIN = 0x30000000, LENGTH = 288K
  RAM_D3 (xrw)   : ORIGIN = 0x38000000, LENGTH = 64K
  ITCMRAM (xrw)  : ORIGIN = 0x00000000, LENGTH = 64K
}
  

/* Initialized data sections goes into RAM, load LMA copy after code */
  .data :
  {
    . = ALIGN(4);
    _sdata = .;        /* create a global symbol at data start */
    *(.data)           /* .data sections */
    *(.data*)          /* .data* sections */
    *(.RamFunc)        /* .RamFunc sections */
    *(.RamFunc*)       /* .RamFunc* sections */

    . = ALIGN(4);
  } >RAM_D1 AT> FLASH

 

 

THIS INITIALIZE CORRECTLY !

/* Specify the memory areas */
MEMORY
{
  FLASH (rx)     : ORIGIN = 0x08000000, LENGTH = 2048K
  DTCMRAM (xrw)  : ORIGIN = 0x20000000, LENGTH = 128K
  RAM_D1 (xrw)   : ORIGIN = 0x24000000, LENGTH = 512K
  RAM_D2 (xrw)   : ORIGIN = 0x30000000, LENGTH = 288K
  RAM_D3 (xrw)   : ORIGIN = 0x38000000, LENGTH = 64K
  ITCMRAM (xrw)  : ORIGIN = 0x00000000, LENGTH = 64K
}

 /* Initialized data sections goes into RAM, load LMA copy after code */
  .data :
  {
    . = ALIGN(4);
    _sdata = .;        /* create a global symbol at data start */
    *(.data)           /* .data sections */
    *(.data*)          /* .data* sections */
    *(.RamFunc)        /* .RamFunc sections */
    *(.RamFunc*)       /* .RamFunc* sections */

    . = ALIGN(4);
  } >DTCMRAM AT> FLASH

 

The only difference is that in the second one use DTCMRAM instead of RAM_D1

But, I repeat, with STM32_IDE the project runs well also with RAM_D1

 

 

 

 

3 REPLIES 3
Pavel A.
Super User

Where is your stack? Can it be that the stack collides with .data in RAM_D1 ?

 

I don't think that could collide, since I check TestTemp1 value in main() function and it's not 0x1234 as I could expect.

Btw, the same project compiled with STIDE run without problems.

Best Regards

Ozone
Principal III

Another toolchain usually means different startup code, amongst others.
I never tried a STM32 project in VisualStudio.

But I would review the startup code, or debug it.
At some point before main, this startup code is supposed to initialize global variables either to zero of from Flash constants. This doesn't happen in your code, it seems.