2024-09-18 01:39 AM
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?
Solved! Go to Solution.
2024-09-18 01:53 AM
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.
2024-09-18 01:53 AM
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.
2024-09-18 04:58 AM - edited 2024-09-18 05:02 AM
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.
2024-09-18 07:41 AM - edited 2024-09-18 07:44 AM
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.