Skip to main content
Graduate II
September 18, 2024
Solved

Where are global arrays stored? Flash or SRAM or?

  • September 18, 2024
  • 3 replies
  • 1387 views

Suppose I define a global array called DATA in a .c file for an STM32F103 like so:

#include ...

uint32_t DATA[0x1000] = {
 // data goes here
};

int main(){
 // main
}

Questions.

0) Where does DATA get stored? Flash or SRAM? Or somewhere else? (Is there somewhere else?)

1) If it's stored in SRAM does that mean that SRAM is persistent storage?

2) How can I control where it is stored? Eg. how can I store it at position 0x08004000 in Flash or at position 0x20004000 is SRAM?

    This topic has been closed for replies.
    Best answer by mƎALLEm

    Hello,

    1- 

    uint32_t DATA[0x1000]

    This is stored in RAM.

    while this is stored in RO memory (it could be RAM or Flash according to your linker config) :

    const uint32_t DATA[0x1000]

    2- 


    @smoothmanifolds wrote:

    2) How can I control where it is stored? Eg. how can I store it at position 0x08004000 in Flash or at position 0x20004000 is SRAM?


    This is done with linker file or using attributes. See this link: https://www.openstm32.org/Using%2BCCM%2BMemory

    See also this thread: https://community.st.com/t5/stm32cubeide-mcus/how-to-use-a-variable-in-sram-to-communicate-between-bootloader/m-p/718106 

    Hope I answered your question.

    3 replies

    mƎALLEmAnswer
    Technical Moderator
    September 18, 2024

    Hello,

    1- 

    uint32_t DATA[0x1000]

    This is stored in RAM.

    while this is stored in RO memory (it could be RAM or Flash according to your linker config) :

    const uint32_t DATA[0x1000]

    2- 


    @smoothmanifolds wrote:

    2) How can I control where it is stored? Eg. how can I store it at position 0x08004000 in Flash or at position 0x20004000 is SRAM?


    This is done with linker file or using attributes. See this link: https://www.openstm32.org/Using%2BCCM%2BMemory

    See also this thread: https://community.st.com/t5/stm32cubeide-mcus/how-to-use-a-variable-in-sram-to-communicate-between-bootloader/m-p/718106 

    Hope I answered your question.

    Graduate II
    September 18, 2024

    It can be held in RAM, but that's not persistent. Statics are either zeroed out by startup code, or copied out of flash if they have initial values.

    If it's fixed it can be defined as such and held in-place in Flash.

    You can use Linker sections and attributes to place the data.

    You can use tags or signatures to find the structure within an object irrespective of address.

    You could also shrink the Flash the Linker sees/uses and use pointers to a structure you place beyond the end of its visible space. You'd probably want a signature and check value to confirm it is valid and correct. Perhaps use defaults if not.

    Super User
    September 18, 2024

    If the array has initializer like in your example, it will occupy both RAM and ROM spaces. The runtime location is in RAM, but the initialization data go into ROM and will be copied to its runtime address by the startup routine. IAR and Keil compilers can compress the initialization data so that image files are smaller.

    Uninitialized arrays in RAM are zeroed by startup routine and do not occupy extra ROM space.