MPC560xB - Copy a code from flash to RAM for executing it in RAM

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

MPC560xB - Copy a code from flash to RAM for executing it in RAM

1,567 Views
romainbenet
Contributor III

Hi,

 

for my application I have to execute a specific code in RAM location.

My problem is: how can I store by default this specific code into flash memory and at the beginning (startup execution before main application) this code is automatically copying into RAM memory and execute from RAM location automatically when I call this function?

 

For example:

function to save in ROM and to copy in RAM: void ExecuteInRam(void)

memory section into the linker:

          SaveCodeInFlash:     org = 0x00003000, len = 0x00001000 /** section where is stored this function in flash */

          ExecuteCodeInRam: org = 0x40000000, len = 0x00001000 /** section where is copying this function for execution */

 

I read CodeWarrior Linker Command File (LCF) for Qorivva/PX [AN4497] / Kinetis [AN4498] and Relacating Code and Data using the CodeWarrior LCF for ColdFire Architecture [AN4329] documents but I need your help.

 

Best Regards,

Romain

Labels (1)
0 Kudos
4 Replies

1,085 Views
davidtosenovjan
NXP TechSupport
NXP TechSupport

Procedure is as follows:

1) To create new segment in linker command file (and shorten internal_ram segment respectively) - you have already created ExecuteCodeInRam.

2) To create new section in linker command file and place it after sections pointing into internal_ram. For instance

.my_ram :{} > ExecuteCodeInRam

3) To mark function or functions to be relocated into RAM in user code like this:

#pragma push

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

void test(void)

{

  test2();      

}

void test2(void)

{

  asm(nop);

  test();        

}

#pragma pop

That’s all, functions will be copied from flash to RAM during startup and executed from RAM.

0 Kudos

1,085 Views
romainbenet
Contributor III

Hi,

Thanks for your help.

But now I have another problem with this code: I program the microcontroller (in release mode) then I reset the board the program restarts and runs correctly but when I shut-down the power then I power-on the target the micro doesn't restart, as if the program enters in infini loop at the beginning when it recopies the code from flash to RAM before calling the main.

Best regards,

Romain

0 Kudos

1,085 Views
davidtosenovjan
NXP TechSupport
NXP TechSupport

Typically it could be due to watchdog. In general if code does not boot from flash but RAM target works, it means debugger script does something necessary for application running.

Try to add following code to your project:

static void DisableWatchdog(void)

{

    SWT.SR.R = 0x0000c520;     /* Write keys to clear soft lock bit */

    SWT.SR.R = 0x0000d928;

    SWT.CR.R = 0x8000010A;     /* Clear watchdog enable (WEN) */

}

0 Kudos

1,085 Views
romainbenet
Contributor III

The Watch Dog is disabled at startup, inside the INIT_Derivative function.

To sum up:

1. When the function is located in Flash without relocation to RAM at start-up, everything is OK:

- After a warm reset (pushing the Reset button on the TRK-MPC5606B demoboard), the boot occurs normally and the main() is reached

- After a cold reset (Unplugging the TRK-MPC5606B demoboard power cable, waiting a few seconds then plugging the power cable again), the boot occurs normally and the main() is reached

2. When the function is located in Flash with a relocation to RAM at start-up :

- After a warm reset (pushing the Reset button on the TRK-MPC5606B demoboard), the boot occurs normally and the main() is reached

- After a cold reset (Unplugging the TRK-MPC5606B demoboard power cable, waiting a few seconds then plugging the power cable again), the boot fails and the microcontroller resets indefinitely. The main() is never reached and the INIT_Derivative() function which disables the Watch Dog is called.

I do not understand why a cold reset causes the boot to fail in case of Flash code relocation to RAM.


You fill find attached the Ready-to use CodeWarrior 10.6 project for MPC5604B for a quick reproduction on the problem. The function we are trying to relocate from Flash to RAM is called "output_clock()" in driver folder files.

Best regards,

Romain 

0 Kudos