Topic : Execute Code in Ram

Forum : ST7/STM8

Original Post
Post Information Post
April 7, 2009 - 11:00am
Guest

Is it possible to copy a code of Flash Area to RAM in RKit-stm8 like fctcpy function of COSMIC Compiler.

Replies
Post Information Post
+1
0
-1
April 7, 2009 - 11:34am
Raisonance Support Team

Hi Laffer,

Yes of course. You can use the standard memcpy() function for this.
If you have plans to do in-application programming of your ROM, you have to ensure that ALL the necessary code for flashing is in RAM before launching its execution. Once all your necessary code is in RAM, you can just execute it and you are done.

Here is a piece of code which is pure standard C (except for the //-style comments) which performs code execution in RAM.

#include 

// This is a counter that will be incremented by our RAM function
unsigned int val = 0;

// To ease code readability, we define a "pointer to function" type
//typedef (void ((*)fctptr)(void));
typedef void (*fctptr)(void);

// This is the buffer where we will copy the function code.
// We have to ensure that it is large enough to hold the whole code
// This can be checked from the linker map file.
char buffer[50];    

// this function is in ROM, but will be copied to RAM before being executed
void executeFromRam(void)
{
    val++;
}

void main(void)
{
    // Copy the function to RAM
    memcpy(buffer, executeFromRam, sizeof(buffer));

    // Now execute it. We cast "buffer" into a function pointer
    (*(fctptr)buffer)();

    // We can check in the debugger that "val" has been incremented
    if(val != 1)
        while(1);   // This should NOT happen

    // Program end
    while(1);
    }

I hope this helps,
Bruno