cancel
Showing results for 
Search instead for 
Did you mean: 

Where are global arrays stored? Flash or SRAM or?

smoothmanifolds
Associate III

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?

1 ACCEPTED SOLUTION

Accepted Solutions
SofLit
ST Employee

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

View solution in original post

3 REPLIES 3
SofLit
ST Employee

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.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Pavel A.
Evangelist III

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.