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.

Execute code from RAM: weird behavior

Other Parts Discussed in Thread: MSP430F5308

Hello all,

to reflash my MSP430F5308, I want to move the actual copying routine (do_reprogram()) to RAM and run it from there. Noting unusual so far. I am using CCS5.4.

I have tried two approaches:

  • Selecting an arbitrary RAM address, copying the code from flash there and then branching to the beginning of the code in RAM. No references to functions in flash are used.
    This will overwrite the existing RAM, but the reflashing routine terminates with a reset anyway, so this would not be a problem. Advantage: the function which is rarely called does not occupy any RAM.
  • As in the appnote: modify the linker command file. What I put there is:
    .flashcode   : load = FLASH, run > RAM
    and in the C file:
    #pragma CODE_SECTION(do_reprogram,".flashcode")

In both cases, the function code is copied to the correct location in RAM. The copy looks fine as far as I can tell (copied using memcpy()).

While the code runs fine when executed from flash, it gives me resets when executed from RAM. What makes thins worse: if I run the code with "Resume" in the debugger, the reset comes before even reaching the function in RAM (a breakpoint placed at the beginning of the function is never reached). In single step mode, everything looks fine.

Reset causes (as defined by SYSRSTIV) are either PMM access violations (which is plain nonsense since I do not use the PMM functionality) or flash password violations (which makes a little more sense, but as stated before, the function which does the flash access is not even reached).

Before I go into details and start posting tons of code: is there any caveat to be considered when running code from RAM? I am starting to get pretty frustrated.

Max

  • Is the code being copied to RAM position independent? Is the "arbitrary RAM address" in the stack area? Where the the stack may grow into? In the global or static variable area?

  • Are the binary copied code (result of memcpy) correct? Is there any jump/branch inside RAM function. How calling/returning RAM function and reset is is done in code?

    Try to do completely simple RAM function with just calling and returning to main (flash) program. If everything is working OK, add few new lines, and so on, with testing/debugging after each update.

**Attention** This is a public forum