Hi : I use S32 Design Studio compiler,my MCU is S9KEAZ128AMLH。How can I put the const variables into flash area by default?
Solved! Go to Solution.
Hi,
GCC ARM compiler should place the const variable into .rodata section by default.
Be aware that if it's possible then compiler may optimize the constant access (constant folding, constant propagation) to generate faster and more compact code. In such case you cannot find the address related to the constant in .map file.
e.g:
source file:
const int myIntConst = 10;
int main(void)
{
volatile int counter = 0;
counter = myIntConst;
...
}
generated code:
counter = myIntConst;
a: 230a movs r3, #10
c: 603b str r3, [r7, #0]
map.file:
.rodata.myIntConst
0x00000000 0x4 ./src/main.o
Now the same experiment with array constant.
source file:
const int myIntConstArray[] = {1,2,3,4,5};
int main(void)
{
int i = 0;
for(;;) {
counter++;
i = myIntConstArray[counter];
if(counter > 5) {
counter = 0;
}
}
}
map file:
*(.rodata)
*(.rodata*)
.rodata.myIntConstArray
0x000005e0 0x14 ./src/main.o
0x000005e0 myIntConstArray
I'd recommend you do not use volatile keyword modifier to constant since the compiler then treat the constant as a variable and places it into .data (RAM) section.
Hope it helps.
Stan
Hi,
GCC ARM compiler should place the const variable into .rodata section by default.
Be aware that if it's possible then compiler may optimize the constant access (constant folding, constant propagation) to generate faster and more compact code. In such case you cannot find the address related to the constant in .map file.
e.g:
source file:
const int myIntConst = 10;
int main(void)
{
volatile int counter = 0;
counter = myIntConst;
...
}
generated code:
counter = myIntConst;
a: 230a movs r3, #10
c: 603b str r3, [r7, #0]
map.file:
.rodata.myIntConst
0x00000000 0x4 ./src/main.o
Now the same experiment with array constant.
source file:
const int myIntConstArray[] = {1,2,3,4,5};
int main(void)
{
int i = 0;
for(;;) {
counter++;
i = myIntConstArray[counter];
if(counter > 5) {
counter = 0;
}
}
}
map file:
*(.rodata)
*(.rodata*)
.rodata.myIntConstArray
0x000005e0 0x14 ./src/main.o
0x000005e0 myIntConstArray
I'd recommend you do not use volatile keyword modifier to constant since the compiler then treat the constant as a variable and places it into .data (RAM) section.
Hope it helps.
Stan
Hi stan: thank you for your reply。you solved my problem。I used codewarrior compiler before,and i add volatile modifier before every const variables, and the codewarrior compiler placed the volatile const variables into flash area。the S32 Design Studio is different from codewarrior compiler。