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.

How do I run code from RAM?

Other Parts Discussed in Thread: MSP430F2272

Hello,

I'm trying to figure out how to designate C code to be run in RAM, rather than from ROM.  How can I do this?  I suspect I may need to designate a specific address that the C function is stored in and then manually copy that to RAM and call the RAM address to have it run... but I do not know if this is correct, and if it is correct, I still don't know how to go about it.  Thank you to anyone who can help.  I am using an MSP430F2272 and the latest version of IAR kickstart.

Regards,

Dylan

  • That's pretty much it!

     

    A good place to start might be the example zip file Dung Dang has included in an answer on this thread:

    Program Execution from Ram?

    http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/p/72156/262251.aspx#262251

     

  • Dylan Jackson said:
    I suspect I may need to designate a specific address that the C function is stored in and then manually copy that to RAM and call the RAM address to have it run...

    It would work this way. You can get the memory location to copy from flash to ram by taking the function pointer and the function pointer of the function after it in the source code as start and end address, and then simply copy it.

    Alternatively, you can put the function in the initialized data segment. Thsi way it will be compiled into flash, but copied to RAm on startup (where it easily can be damaged, so beware). All function calling it will automatically call the correct location (just like they know the addresses of any pre-initialized global variables too)

  • Well it turns out that for what I was trying to do, I didn't need to do anything fancy...  simply running the code normally had the desired effect.  Looking at your post though, I'll definitely go there next if I really need to do something more.  Thanks!

  • If you are looking to copy and run code from stack/ RAM then have a look at this article

    http://raviyp.in/how-to-run-code-on-stackram-in-microcontroller/

  • Michael,

    How would I go about putting function in the initialized data segment, do I need to call a pragma on the function and add the function to linker command file?
  • Isn't the correct way is to use your regular code location and when you need to use it
    you copy that routine to ram (could to that through the stack) just make sure your code is of the relocatable type.

    Should use CALL #ram_address
    and then routine should end with RET as using a JMP for exit is not relocatable (as it use +-128),
    Could use BR # for entry and exit, if you construct the code correctly.

  • This "make sure your code is of the relocatable type" is the problem.
    However, if code is placed in initialized data segment, it is copied from flash to ram during startup, and all calls to it call the proper ram location. However, there is of course a risk that the code is damaged due to stack overflow or index-out-of-bouds writes. well, things like this shouldn't happen in a properly written program (and would crash a buggy program anyway - but if the function is a flash write function, it may damage the code then, so even a reset won't help)
    Sure, relocatable code that is copied when needed is the safest way. However, it is one of these "if you have to ask, then it is nothing for you" things. :)

**Attention** This is a public forum