Move a function (not main()) from FLASH to RAM

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

Move a function (not main()) from FLASH to RAM

4,360 Views
Mr_void
Contributor I
Hello world!
 
I am rather new to programming microcontrollers, so this may be a trivial thing for you to help me with.
 
I'm going to erase the Flash on a NE64 (SPI master) and then be able to burn a new program from a DP256B (SPI slave). In order to do this I have to move the SPI function that I have written and place the whole function in RAM. I would really appreciate if I could get any hint. I'm thinking about using the #pragma directive but maybe there are easier ways to solve this problem.
 
Thanks in advance!
 
 
 
Labels (1)
0 Kudos
4 Replies

598 Views
Mr_void
Contributor I
Thank you very much for your replies, it partially solved the problems.
But as always when programing microcontrollers a new problem is lurking behind the next corner.:smileymad:
0 Kudos

598 Views
CrasyCat
Specialist III

Hello

Are you using CodeWarrior for HC12 V 2.0 or higher as development tool?

If this is the case there is a technical note (TN228) explaining how to achieve that. The Technical note should be available if you have done a full product install.

Anyway I have attached it to that thread for your convenience.

I hope this helps.

CrasyCat

0 Kudos

598 Views
pittbull
Contributor III
Hi,
You can make a project that is configured to run entirely from RAM. This project contains your flasher code and has a jump vector that points to its _Startup() routine. Build it and make from the .abs file an 'unsigned char array'. Put the array into your main project and call a little asm code that looks like this:

// Copy flasher code to RAM and invoke it
asm
{
SEI // Interrupts OFF
LDX #0 // Initialize loop counter
L1: LDAB flashercode,X // Loop: Get one byte from flasher code
STAB $2000,X // ...and store into RAM
INX // Go to next byte
CPX #$1F00 // 0x1f00 bytes transferred?
BLT L1 // No, continue
LDX #0 // Initialize for indirect jump
JMP [$2000,x] // Invoke flasher code, jump to vector at 0x2000
}

It should be assembly which doesn't need the stack because the stack will surely be overwritten...

With this method you can run big programs (depending only on how much RAM your MCU has). Such as my flasher code, which reads S-records from FAT-formatted compact flash memory cards and re-programs an HCS12XA256...

Hope that helps a little,
pittbull
0 Kudos

598 Views
Lundin
Senior Contributor IV
A rather daring solution is to reserve a block of RAM large enough to hold the largest function you want to copy (in my example below, 100 bytes).

Then in the flash, write

void myFunc (void)
{
...
}

void myFunc_end (void){}

Here I assume that the two functions will be placed adjacent in the memory. To be certain of it, you could use
#pragma CODE_SEG MYFUNC_SEG or something just for the two functions.


Then in main():

union myUnion
{
char dummySpace[100];
void (*myGenericFunction)(void);
};

memcpy(myUnion.dummySpace, (void*)myFunc, (int)(myFunc_end-myFunc));

myUnion.myGenericFunction();
0 Kudos