In this guide, we shall see how to add to linker script command to move the variable location from memory location to another (From RAM_D1 to RAM_D2 in this case on STM32H735 and can be applied to any ARM_GCC).
In this guide, we shall cover the following:
- Memory location before modification.
- Memory location after modification.
1. Memory Location Before the Modification:
We start from main.c file.
Within the main.c file, include the following:
#include "stdint.h" #include "stdlib.h"
Declare an array of size 1000 of uint32_t which will hold 4000 Bytes in the RAM:
uint32_t data[1000]
Get the size of the unsigned it as following:
#define dataSize sizeof(unsigned int)
in main code:
fill the array with random numbers to force the compiler to not optimize the array:
for(int i=0;i<1000;i++) { data[i]=rand()/dataSize; }
Add the while loop:
while(1) { }
Compile the code now.
After the compilation is done, check the memory region D1 which is the default for STM32H735:
There is 5.85KB of data in the RAM, 3.91 for the array and 1.5 for the stack.
Now, open Memory details, select RAM_D1 then open .bss section:
As you can see, the compiler with the default setup of the linkerscript allocate 3.91KB of data in RAM_D1.
Let say, we need to store this buffer into external RAM or another RAM region.
2. Memory Location After The Modification:
In order to move the variable to another location, we need to create a section, we can simply do this using attribute as following for the buffer:
uint32_t data[1000]__attribute__ ((section (".RAMD2_data")));
We named the section RAMD2_data, by adding this to any other variable, the linkerscript will put the data into RAM_D2 rather than the default D1
Now, open the linkerscript (STM32H735IGKX_FLASH.ld in this case) and add the following line:
.RAMD2_data : { KEEP(*(.RAMD2_data)) } >RAM_D2
Save the linkerscript and compile the code and check the memory region, you should see 3.91KB of RAM_D2 has been used as following:
By checking memory details, we can see the section we created:
We have successfully moved the data from RAM_D1 to RAM_D2. The same can be applied to external RAM to hold the variables.
Happy coding 🙂
2 Comments
What about initialisation of such variables in different memory locations?
This will enforce the compiler to not optimize the variable.
Add Comment