Hi : I use S32 Design Studio compiler,my MCU is S9KEAZ128AMLH。How can I put the const variables into flash area by default?

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Hi : I use S32 Design Studio compiler,my MCU is S9KEAZ128AMLH。How can I put the const variables into flash area by default?

跳至解决方案
798 次查看
chenaimei15
Contributor I

Hi : I use S32 Design Studio compiler,my MCU is S9KEAZ128AMLH。How can I put the const variables into flash area by default?

标记 (4)
0 项奖励
1 解答
676 次查看
stanish
NXP Employee
NXP Employee

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
Now the constant is located in Flash (.rodata)

Note: 

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

在原帖中查看解决方案

2 回复数
677 次查看
stanish
NXP Employee
NXP Employee

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
Now the constant is located in Flash (.rodata)

Note: 

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

676 次查看
chenaimei15
Contributor I

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。

0 项奖励