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.