This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Issue after copying my code from Flash to RAM

Other Parts Discussed in Thread: MSP430F5438A

Hi all,

I'm currently working on a project, using the MSP430F5438A. I've ever copied my code from Flash to RAM, paying attention to section command and working area.

My code is well located into Flash at the beginning, then is copied into RAM, it works as well.

However during execution (Watching Disassembly on CCSv6), there is an instruction "BRA" with an absolute address, which jumps directly into Flash, but I looked after watching whether the copy of my code had been well executed, and all the code has been correctly copied.

This instruction appears on a loop "While", but I don't know why.

Having the same code in RAM as in Falsh, according to you, what could be the reason of this leap (Unconditionnal jump) ?

If someone would have an idea,

Regards,

  • (1) Some of the instructions in the object code are position independent. For example, MOV R14,R15.

    (2) Some of the instructions in the object code are "relative". For example, JMP ???? or MOVB R12,&????.

    (3) While other instructions in the object code are "absolute". For example, BR #???? or MOVB R12,???? (seldom used).

    When you copy the object code intended for one place (e.g., beginning of Flash) to a different place (e.g., somewhere in RAM), the "absolute" instructions (3), may or may not cause execution "problems".

    In your case, the BR #???? will branch back to the original code in Flash instead of the corresponding location In RAM.
  • Thanks,

    In order to avoid that, I should write directly assembly instructions in my C program or there is another possibility to solve my problem ?

    Herebelow my instruction BRA viewed from CSS :

    00290c: 0380 429E BRA #C$L76

    During execution code, I follow some instructions from RAM, and when arrive the "While" loop, I go back into Flash
  • I think you can tell CCS or IAR compiler that you want to run a section of the code "there" (somewhere in RAM) but put the code "here" (somewhere in Flash) and, at run-time, copy that section from "here" to "there" before trying to execute it.

    This should be in the user instructions of the compiler. I do not know exactly how to do that. (I use assembler.)
  • I've ever done something like that.

    Firstly, I reserved two memory areas in order to store my real function (.ramcode1 : load = FLASH22) and after copying it with a "classic" function using memcpy(dest, src, size) and 2 pointers are on it,

    In fact, all my code is well copied in RAM at the right address, during execution code, I start from (dest) address but I go back in Flash due to the BRA#?? instruction.
  • Thanks,

    I've ever followed this tutorial for CCS, in fact, I would like to know how I can do, for controlling the assembly instruction, may I put an "inline assembler" directly in my C program in order to avoid this "absolute" behaviour generated from my compiler.

    Regards,
  • My function starts at the right address in RAM. Almost all my code is executed until a loop "While" on which I control any values. After that, I see in disassembly (TEST.W, JNE, CMP.W, JHS) some test useful for the "While", then there is a call with BRA instruction.
  • I told you earlier that:
    "I think you can tell CCS or IAR compiler that you want to run a section of the code "there" (somewhere in RAM) but put the code "here" (somewhere in Flash) and, at run-time, copy that section from "here" to "there" before trying to execute it."

    Had you done that successfully, the BR #???? instruction would branch to a location in in RAM ("there"), not in Flash ("here").
  • I did something like that. I've allocated 2 other sections in my .map file and created executing routing for them.
    .ramcode : load = FLASH21, run = RAM_CODE
    .ramcode1 : load = FLASH22

    I use an empty function which gets the code of my main function (stored in FLASH22) after copying it into an address in RAM.

    the BR#?? refers to an address in Flash, may be a label (I think) => BRA #C$L76
  • I do not use CCS and do not know how to use CCS. What follows is just my guess.

    Your ".ramcode : load = FLASH21, run = RAM_CODE" probable has no effect to your "BR #??" instruction unless that instruction is inside the ramcode section.

    Your ".ramcode1 : load = FLASH22" did not say "run = RAM_CODE" probably has no effect on anything.

    In order to make the "BR #??" instruction to branch to RAM, that instruction itself must be part of the code in a section that is "run = RAM_CODE.

**Attention** This is a public forum