Copy function to stack

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Copy function to stack

1,563 Views
UDP
Contributor I
Hello
 
Does someone has an example code how to load function to stack and run it there (i need it to program/erase flash)?
Do i need to run from the memory only the command to the flash or more (in the program command flow)?
 
Thanks
 
 
 
Labels (1)
0 Kudos
1 Reply

423 Views
mvincent
Contributor I
You only need to copy the code that does the actual programming or erase. For example:

#define IPSFLASH(x) (*(long *)(0x44000000 + (x)))
int
runInRam(char cmd, long *addr, long val) {
CFMSTAT = PVIOL | ACCERR;
IPSFLASH(addr) = val;
CFMCMD = cmd;
CFMSTAT = CBEIF;
while (!(CFMSTAT & (CCIF | PVIOL | ACCERR)));
return CFMSTAT & (PVIOL | ACCERR);
}
void runEnd(void){} // leave this immediately after runInRam

The function to program an array of longs, for instance:

int
flashProg(long *dest, long *src, int n) {
char buf[120];
int (*ramCopy)(char,long*,long) = (int(*)(char,long*,long))buf;
memcpy(buf, (void*)runInRam, (char*)runEnd - (char*)runInRam);
while (n--) {
if (ramCopy(CMDPROG, dest++, *src++)) return -1;
}
return 0;
}

You could also copy 'runInRam' to a static buffer just large enough (chech the map file) during initialization.

Hope this helps.
0 Kudos