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.

Flash self-programming

Other Parts Discussed in Thread: MSP430F5419

I am using MSP430F5419 microcontroller and using C++ code for it. i want to know how to execute the code in RAM and from where the function in RAM should be called ?. Please suggest me a way.

  • C++ has no such features. But various C++ compilers may have extensions or "intrisics" to do that. Consult the documents of the specific compiler you are using.

  • Hello!

    I did that a couple of times. I wrote a monitor on a 2274, and the monitor was simply taking a file
    made from TI's .txt output that I converted into a binary format.
    So here are a few hints (Supposing you are using IAR):

    1. - Create a configuration file (called nnn.xcl, where nnn is the name of your device). For nnn,
    you can put an arbitrary name, for instance my_device.xcl. The best is that you copy MSP430F5419.xcl
    to your my_device.xcl.
    2. - Now open this config, and modify the start of the program flash. Change it to one location in
    RAM to let the compiler think that this position is plain flash. But you will also have to change
    the RAM parameters so that your program area is not used as RAM anymore, at least during the ram program
    run time.

    3. - Copy MSP430F5419.menu to my_device.menu. Change the contents of my_device.menu so that it fits the
    configuration.
    4. - Compile  a small test program (e.g led blink), and transform TI's output .txt file so that you can either load it as
    a .h file containing your.

    Here is an actual example : 

     @03A4
     31 40 00 70 3C 40 A0 03 3D 40 04 00 B0 12 B8 07
     B0 12 28 05 B0 12 A0 07 F2 D2 1E 00 F2 D0 20 00
     1D 00 30 41 B0 12 BC 03 B0 12 00 04 B0 12 1C 05
     F2 D2 1D 00 B0 12 1C 05 4C 43 30 41 B0 12 1C 05
     F2 C2 1D 00 F7 3F F2 C0 10 00 1D 00 B0 12 BC 03
     F2 D2 1D 00 B0 12 00 04 30 40 E0 03 F2 D0 10 00
     1D 00 30 41 F2 C2 1E 00 F2 C0 20 00 1D 00 30 41
     B0 12 42 04 B0 12 00 04 5C 42 1C 00 7C F2 30 41 ... etc... 

    $03a4 is a location 2274's RAM. So it will not be the same value in your case. Check the position
    of the RAM in the 5419 to verify if it makes sense.

    Now transform the TI file to an array by adding commas and 0x. Now you have an array of bytes
    like this (taking the example above) :

    const uint8 MyProgram[] = {
        0xa4, 0x03, 0x00, 0c70, 0x3C, 0x40 etc...
    };

    5. - Now load the function to ram:
    copy your program MyProgram to the address specified in the Texas .txt file like this:

    uint8 * MyRamLocation = (uint8 *)03A4;
    for(i = - ; i < length ; ++i) MyRamLocation[i] = MyProgram[i];

    Careful, there might be more than 1 segment. A segment begins with a @ (like the @03A4 above).
    Everytime you have a @, you have to update the copy location.
    I also forgot to mention that you have to provide your ram program with its own stack. At the end of the
    RAM, you will have the global program stack (the normal stack), but you have to define one stack for
    your "small" program running in RAM.
    It should look like this (from the end of the ram):

    main program stack
    ram program stack
    ram program's work memory
    ram program's code space

    Now in all this mess you may have to think about one shared ram space that both programs can access.
    Might be a good idea to put it just under the ram program stack.

    6. - Now the magic part, run from RAM:
    You can do like this:

    void (*RamFunc)(void);            // This will be your function (or program) running from RAM
    RamFunc = (void(*)(void))((uint8 *) MyRamLocation);

    Now the execution instruction:

    (*RamFunc)();

    7. Wait for crashes and debug.
    The trouble here is that you have no seatbelts: you will not have access to the code because you have
    loaded an executable. The debugger can display mnemonics which is better than nothing, but it's really
    not simple. So be sure that your RAM progran runs perfectly before transforming it to a RAM program.

    Now for your C++ issue, old_cow_yellow is partly right. He's right because there is indeed no instruction in C++
    that allows to run code from RAM. But he's only partly right because there is no instruction in C or assembler
    either that would do all the background work to run something in RAM. Or you have to design a specific tool
    like those on PCs.

    Back to embedded programming.
    Basically what you can do in assembler can be done in C and therefore in C++ which includes C. The overhead
    on modern compilers is very small, and it's usually not worth bothering with assembler. I'm aware most of
    the embedded programmers may disagree on this one.
    The advantage of C++ is that you can encapsulate the above code inside of the object and make it absolutely
    transparent.
    Then you can even make a generic ram loader like this:

    class RamLoader {
    public:
       RamLoader();
        ~RamLoader();
        LoadRamFunction(uint8 * MyTIArrayTransformed);
        Run();
    private:
        uint32 mStartLocation;  // uint32 to be compatible with the new 20-bit address bus
    }

    In this case, the SetRAMFunction would look like this:

    RamLoader::LoadRamFunction(uint8 * MyTIArrayTransformed) {
       // Here you copy the program to RAM, and you set the value of mStartLocation
    }

    And finally the RUN function:

    RamLoader::Run() {
        void (*RamFunc)(void); // This will be your function (or program) running from
        RamFunc = (void(*)(void))((uint8 *)mStartLocation);
       (*RamFunc)();
    }

    As a conclusion, running a functon or program from RAM is not a trivial task, especially because there are
    no tools to help that kind of programming (no debugger for running suprograms, you have to do a lot by yourself
    But you can have something running provided that you work on it steadily. Not the kind of task you can do during
    coffee break.

    Have fun!

    Pascal
  • Thanks for the reply

**Attention** This is a public forum