Bengt-Olov Andersson

Function pointer

Discussion created by Bengt-Olov Andersson on Oct 14, 2008
Latest reply on Oct 15, 2008 by Bengt-Olov Andersson
Hello!
 
I have some problem when trying to copy a small piece of code (a function) from ROM to RAM.
 
To do this I'm passing the source and destination address to a copy function. Previously the source address has been hordcoded into SW but I'm trying to make this more flexible by passing the address to this function instead.
 

bool (* funcP)();

funcP =&ProgFlash;
CopyString((byte
*
)funcP, (byte *)RAM_FUNCS_START, RAM_FUNCS_SIZE);

The address to progFlash() was in this case 0x40A9. The resulting part in the .lst file looks like:

 

226: funcP = &ProgFlash;
002a cc0000 [2] LDD #LOW_PAGE(ProgFlash)
002d 6c8c [2] STD 12,SP
002f c600 [1] LDAB #HIGH(ProgFlash)
0031 6b8b [2] STAB 11,SP
227:
228: CopyString((byte*)funcP, (byte *)RAM_FUNCS_START, RAM_FUNCS_SIZE);
0033 ec8c [3] LDD 12,SP
0035 160000 [4] JSR WriteEeprom:0x01a2

.

01a2 3b [2] PSHD
01a3 cc3fd0 [2] LDD #16336
01a6 3b [2] PSHD
01a7 c62b [1] LDAB #43
01a9 4a000000 [7] CALL CopyString,PAGE(CopyString)

The address to the function is stored relative to SP and then loaded back to D. But since two bytes are copied from SP + 12 the address A900 will finally be stored in D and pushed as argument to the CopyString function. LDD 11,SP would have been a better choice from my point of view.

Something is wrong in the C-code when trying to convert the functionpointer to a byte pointer but i can't figure out what. Can anyone help me??

 

//Bengt

Outcomes