AnsweredAssumed Answered

Pointers to Functions in a structure.

Question asked by John Dammeyer on Nov 19, 2012
Latest reply on Nov 19, 2012 by John Dammeyer

I'm porting some code onto the 9S12 for handling CANOpen Object Dictionaries.  One of the fields is either a pointer to a rom constant, eerom location, a ram variable or a function.  Depending on one of the other fields the application either reads from the ram, rom or calls the eerom code with the eerom address or calls the function.


I'm having trouble getting the compiler to generate what I want (rather than what I tell i.)


// First the function that returns nothing with no parameters.

void MyFunc(void);


typedef union _DICT_PTRS     /* Pointers to objects */


unsigned char * pRam;

const unsigned char * pRom;

unsigned int EEAdr;

void __far * (* pFunc)(void);



// Then the variable that will hold the data.  The initializer flags a compiler error

// that the sizes don't match. 

DICT_OBJECT_TEMPLATE _db_device[3] = {

  {0x1000,0x00,CONST,4,(unsigned char *)&rCO_DevType},

  {0x1001,0x00,RO,1,(unsigned char *)&uCO_DevErrReg},

{0x1002,0x00,RO,4,(unsigned char *)&uCO_DevManufacturerStatReg}    

// {0x1005,0x00,FUNC | RW,6,(void __far  *)&_CO_COMM_SYNC_COBIDAccessEvent} 



// So for now I try to load the variable directly.


     _db_device[0].p.pFunc = (void *)MyFunc;




Breaking at the code where the function is assigned it's always the same.  The wrong value is stored in the stucture and the call picks yet another location relative to the PC.  I can work with the non function variables without any problems.  I've tried the various permuations of __far and * etc. but the code generated is the same every time and the funtion is not called because what's stored in the structure isn't correct.