How to put code in RAM area?

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
winnie_zhan
Level 1
Level 1
Distributor - Zenitron(GC)
10 sign-ins First reply posted 5 sign-ins

Hello, I am using XMC1302-T038X0032 to do a FOC motor control application. I found that the code execution speed is relatively slow, so I want to put the code into RAM to run, I can use the __attribute__ keyword according to the reference material, But the code execution speed has not changed, it seems that it does not take effect! How can I solve it!

0 Likes
1 Solution
Owen_Su
Moderator
Moderator
Moderator
250 solutions authored 500 replies posted 50 likes received

Hi, @winnie_zhan ,

Here are some methods to put the codes in RAM area:

 1) If you need to run a function in RAM, you can use'__ attribute__ 'Keywords, such as:

void __attribute__((section (".ram_code"))) CalSumInRAM(void)
{

}

 'ram_code' is a segment in RAM used to place RAM code, It has been defined in the ld file in advance, and it does not need to be defined by the user.

If you need to run a file in RAM, you need to modify the ld file, first exclude the file from the 'text' section, and then add it to 'ram_code' section. For example, run the file test1. C in RAM, you can perform the following operations:

.text :
{

*(EXCLUDE_FILE( *test1.o) .text.* .gnu.linkonce.t.*);

} > FLASH
.ram_code : AT(DataLoadAddr + __data_size)
{
__ram_code_start = .;
/* functions with __attribute__ ((section (".ram_code")))*/
*(.ram_code)
/* list the object files running from PSRAM here*/
*test1.o(*text.*);

. = ALIGN(4);
__ram_code_end = .;
} > SRAM

2) How to put variables in the specified location in RAM?

Define your own data area in the ld file, and then use '__ attribute__' keyword in the source file to put the variable at the first address of this area. Here is an example to put a variable at the address 0x20002000:

.myDataBlock 0x20002000:
{
*(.myDataSection)
}> SRAM

In the source file, put the variable 'sum' at the address 0x20002000:

uint8_t __attribute__ ((section (".myDataSection"))) sum;

Please let us know if you have any other questions, hope this can help you.

Regards,

Owen_Su

View solution in original post

1 Reply
Owen_Su
Moderator
Moderator
Moderator
250 solutions authored 500 replies posted 50 likes received

Hi, @winnie_zhan ,

Here are some methods to put the codes in RAM area:

 1) If you need to run a function in RAM, you can use'__ attribute__ 'Keywords, such as:

void __attribute__((section (".ram_code"))) CalSumInRAM(void)
{

}

 'ram_code' is a segment in RAM used to place RAM code, It has been defined in the ld file in advance, and it does not need to be defined by the user.

If you need to run a file in RAM, you need to modify the ld file, first exclude the file from the 'text' section, and then add it to 'ram_code' section. For example, run the file test1. C in RAM, you can perform the following operations:

.text :
{

*(EXCLUDE_FILE( *test1.o) .text.* .gnu.linkonce.t.*);

} > FLASH
.ram_code : AT(DataLoadAddr + __data_size)
{
__ram_code_start = .;
/* functions with __attribute__ ((section (".ram_code")))*/
*(.ram_code)
/* list the object files running from PSRAM here*/
*test1.o(*text.*);

. = ALIGN(4);
__ram_code_end = .;
} > SRAM

2) How to put variables in the specified location in RAM?

Define your own data area in the ld file, and then use '__ attribute__' keyword in the source file to put the variable at the first address of this area. Here is an example to put a variable at the address 0x20002000:

.myDataBlock 0x20002000:
{
*(.myDataSection)
}> SRAM

In the source file, put the variable 'sum' at the address 0x20002000:

uint8_t __attribute__ ((section (".myDataSection"))) sum;

Please let us know if you have any other questions, hope this can help you.

Regards,

Owen_Su