How to Copy Function from Flash to RAM & execute it

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to Copy Function from Flash to RAM & execute it

6,595 Views
MCF52233
Contributor III

Hi

 

We are using MPC560xx (PowerPC)

our compiler is 4.3 mwcceppc.exe

Codewarrior IDE is 5.9

 

We need to copy some function (from flash) to RAM and execute from RAM itself.

 

We refered

https://community.freescale.com/message/15640#15640

 

"In order to execute portion of your application from RAM in a PowerPC application, you need to create a ROM image of your application, and then copy this image into RAM"

 

So what is the correct  method to do this in case of PowerPC?

 

Regards,

Abhijit Khadtare

Labels (1)
0 Kudos
5 Replies

1,726 Views
stanish
NXP Employee
NXP Employee

Hi MCF52233,

 

See below a simple example of how this could be achieved:

 

lcf file:

---------

MEMORY

{

...

internal_flash: org = 0x00002000, len = 0x001FD000

internal_ram: org = 0x40000000, len = 0x00009000

my_ram_code: org = 0x40009000, len = 0x00001000 // <-- custom memory segment for custome code executed from RAM

...

}

 

SECTIONS

{

...

GROUP : {

.__uninitialized_intc_handlertable ALIGN(0x10) : {}

.data : {}

.sdata : {}

.sbss : {}

.sdata2 : {}

.sbss2 : {}

.bss : {}

} > internal_ram

 

.my_ram_code (VLECODE) :{} > my_ram_code // <-- place all .my_ram_code section VLE code into custom memory segment.

// Remove (VLECODE) if you do not use VLE

...

}

 

main.c

----------

 

#pragma push // save current pragma context

#pragma section code_type ".my_ram_code" ".my_ram_code" code_mode=far_abs

// function prototypes below should be placed into .my_ram_code section far absolute address mode

void test(void); // declare prototype

 #pragma pop //restore original pragma context

 

 

void test(void)

{

asm(nop);

}

 

int main(void) {

 volatile int i = 0;

 test();

}

 

The startup automatically copies-down the appropriate function(s) into RAM. The ROM image generation must be enabled (default setting for the "Internal_flash" build target if project is generated by the Wizard).

 

You can check the .map file to see where the functions are placed...see the example snippet below:

 

...

.my_ram_code section layout

Starting Virtual File

address Size address offset

---------------------------------

00000000 000006 4003b000 00002808 1 .my_ram_code     main.o

00000000 000006 4003b000 00002808 2 test     main.o

 

...

 

Memory map:

Starting Size File ROM RAM Buffer S-Record

address Offset Address Address Line

...

.bss 40000548 0000000c 00002808 00031808 00031808 0

.my_ram_code 4003b000 00000008 00002808 00031808 00031808 239

 

 

Where 0x4003b000 is the address in RAM and 0x31808 is the address in ROM which is copied by the startup to RAM.

 

 

I hope it will help you.

Stanish

 

0 Kudos

1,726 Views
MCF52233
Contributor III

Hello Stanish,

 

Thanks for ur prompt answer.

You provided solution includes

 

/***

pragma push // save current pragma context

#pragma section code_type ".my_ram_code" ".my_ram_code" code_mode=far_abs

// function prototypes below should be placed into .my_ram_code section far absolute address mode

void test(void); // declare prototype

 #pragma pop //restore original pragma context

 

**/

 

Your solution is correct. absolutely no doubt.

 

But our scenerio is as follows:

 

a>Code running in flash.

if(certain condition occured)

{

   Copy some flash code to RAM, then start executing from RAM

}

 

We are looking for how to "Copy some flash code to RAM, then start executing from RAM"

 

Thanks for ur time Stanish.

We hope scenerio is now clear to u.

 

Regards,

 

 

0 Kudos

1,726 Views
MCF52233
Contributor III

Here i'm going do this as follows:

 

Create section say MyCodeFlash in flash n put corresponding function in that section.

 

/*********** Appl.c     **********/

if(certain condition occured){

    Copy code from MyCodeFlash section to RAM n execute it

}

 

/*******************************  test.c  ****** put Test <fn> in MyCodeFlash*****/

#pragma push // save current settings

#pragma section code_type ".MyCodeFlash"

 asm void Test(void){

  e_lis r7,0x23;

   nop; 

}

#pragma pop //retrieve previous settings

/*****************************************************/

//in LCF file

.MyCodeFlash(VLECODE) ALIGN(0x20) : {} > Internal_flash

 

/*******************************************/

but still my .map file shows

 

 .MyCodeFlashsection layout

Starting Virtual File

address Size address offset

---------------------------------

/***********************************************/

It creates section in .map file, but does not shows any size occupied by the code in it.

 

Does any hv idea about what is missing ?

 

 

0 Kudos

1,726 Views
stanish
NXP Employee
NXP Employee

Hi Abhijit,

 

This issue can be caused by the linker dead-stripping. If the function is not referenced it's considered as a dead code and removed by the linker from executable file.

You can avoid this linker optimization either in the source file:

 

#pragma push
#pragma section code_type ".my_sec"                 

__attribute__((force_export))  asm void test(void);

asm void Test(void)
{
  ...   
}
#pragma pop

 

Or in .lcf file:

 

FORCEACTIVE { ... "Test" }

 

Stanish

0 Kudos

1,726 Views
MCF52233
Contributor III

Hi Stanish,

 

Thanks for your help.

Now it is displayed in .Map file.

 

 

.TestSection   section layout

Starting                     Virtual            File

address      Size      address        offset

---------------------------------

00000000 00000e 00029280 00027de0 1 .TestSection  TestFile.o

00000000 00000e 00029280 00027de0 16 TestFn         TestFile.o

 

When we used "TestFn" in assembly line, it gives its address.

But is this possible to get size of the Function?

 

eg. Below code snippets load address of the "TestFn" in R6 Reg.

e_lis r6,TestFn@h

e_or2i r6,TestFn@l

 

 

We can get the size of the function, by seeing END of the section, but this permits if only single funtion exist in the section.

We hope, scenerio is clear now.

 

is this possible to get size of the Function?

0 Kudos