0

I'm programming STM32L432KC and I want to put it in standby mode which preserves SRAM2 content. To do that I created a separated section in memory by separating the SRAM1 from SRAM2.

In this chip, the SRAM1 and the SRAM2 can be treated as a continuous memory region, so originally the memory was mapped like that in the linker script:

MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 64K
FLASH (rx)     : ORIGIN = 0x8000000, LENGTH = 256K
}

I changed it to separate the SRAM1 and SRAM2:

MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 48K
FLASH (rx)     : ORIGIN = 0x8000000, LENGTH = 256K
SRAM2 (xrw)    : ORIGIN = 0x10000000, LENGTH = 16K
}

The region 0x2000C000 - 0x20010000 is mapped to 0x10000000 - 0x10004000 by default.

Then I mapped a section to the new memory region in the linker script:

  .sram2 :
  {
    . = ALIGN(4);
    _ssram2 = .;
    *(.sram2)
    *(.sram2*)

    . = ALIGN(4);
    _esram2 = .;
  } > SRAM2

And then put an initialized global variable in the new section:

static unsigned test_var __attribute__((section(".sram2")) = 10;

The problem is that I didn't edit the startup code to copy the initialized data into the SRAM2, but when I debug I can see that the variable gets initialized and I think it shouldn't.

The question is: why it is initialized?

I wonder if STLinkV2 apart from flashing the device it also initializes the RAM region.

K. Koovalsky
  • 596
  • 4
  • 17
  • It's either the startup code which can handle the copying of initialized data to their run-time location. *Or* the linker places the the data in the correct sections at the correct addresses, so the program loaded does it. – Some programmer dude Sep 13 '18 at 11:26
  • You need to check the startup code. Non L4 ones only initialize the one .data section. Post the startup code here otherwise I cant help. Place the breakpoint at the ResetHandler and see what is happening. – 0___________ Sep 13 '18 at 11:46
  • You probably will need to let linker to save the data into the flash `} >SRAM2 AT> FLASH` and add couple of C or asm lines in the startup. I usually call the C routine from the ASM startup for it (I am avoiding assembler, when I was younger I had to write a lots in it and have enough) – 0___________ Sep 13 '18 at 11:50
  • @P__J__ the startup code is automatically generated with Cube and it's the same like [here](https://github.com/stm32duino/Arduino_Core_STM32/blob/master/system/Drivers/CMSIS/Device/ST/STM32L4xx/Source/Templates/gcc/startup_stm32l432xx.s). Will that `} >SRAM2 AT> FLASH` treat uninitialized data as it was initialized? I mean I would like to preserve the same functionality of `bss` and `data` in the `SRAM2` as it works in "normal" RAM. – K. Koovalsky Sep 13 '18 at 12:16
  • Yes you need to have the same for all separate data and bss segments – 0___________ Sep 13 '18 at 12:59
  • When use CCM RAM I do the same – 0___________ Sep 13 '18 at 13:00
  • @P__J__ Ok, then I have one more [question](https://stackoverflow.com/questions/52314424/multiple-bss-and-data-segments) – K. Koovalsky Sep 13 '18 at 13:08

0 Answers0