Pepe
Pepe

Reputation: 213

Function pointer to specific memory

I'd like to ask for an advice. I am working with small embedded uP.

I'd like to assign my various functions to myfunctions struct. How to do that correctly? Then I want to place this myfunctions (struct of function pointers) to specific memory address (e.g. 0x1000). Whats is the best procedure to achieve this?

typedef void (*fptr)(void);

typedef struct FCN_IMAGE
{
    fptr fcn1;
    fptr fcn2;
    fptr fcn3;
} FUNC_T;

FUNC_T myfunctions;

Actually it should be sort of jump table.


Secondly I want to read this function pointers from within other program - directly from specified address location (e.g. 0x1000).

It means the first code should assign the struct of function pointers to specific memory location and other independent code should read this table from specific memory. Interconnection between those two programs should be

#define FCN_BASE_ADDRESS (0x1000)

Any ideas what is the best way to achieve it?

P.S. This will run on embedded processor - not PC.

Upvotes: 0

Views: 1844

Answers (3)

Lundin
Lundin

Reputation: 213711

This is the best way to solve it in a portable manner:

typedef void (*fptr)(void);

#define FCN_BASE_ADDRESS ((volatile fptr*)0x1000)

/* Make myfunctions an array, not a struct. 
   Structs can have padding and aren't portable.
   It doesn't look like you need a struct in this case.
*/
fptr myfunctions [N] =
{    
  fptr fcn1;
  fptr fcn2;
  fptr fcn3;
};

memcpy(&FCN_BASE_ADDRESS, myfunctions, sizeof(myfunctions));

Though if you are using Codewarrior, you could probably use a #pragma to allocate them where you want them. Here is an example assuming they are stored in read/write RAM and a 32-bit address bus.

// .prm file
SECTIONS
  MEM_FCN_BASE_ADDRESS = READ_WRITE 0x2000 TO 0x200C; 
END

PLACEMENT
  FCN_BASE_ADDRESS INTO MEM_FCN_BASE_ADDRESS;
END

// .c file
#pragma DATA_SEG FCN_BASE_ADDRESS
fptr myfunctions[N] = ...;
#pragma DATA_SEG DEFAULT

If they should be stored in ROM/flash, for example a vector table, then it must be done differently with READ_ONLY sections, #pragma CONST_SEG and const fptr. (Note that the const keyword in C behaves in irrational ways when combined with typedef:ed pointers. In this case I believe it would give a constant pointer to a non-constant function and thus it should end up in NVM as desired.)

Upvotes: 0

anatolyg
anatolyg

Reputation: 28241

Maybe the following will help you:

// assign my various functions to myfunctions struct
myfunctions.fcn1 = &YourFunction1;
myfunctions.fcn2 = &YourFunction2;
myfunctions.fcn3 = &YourFunction3;

// assign the struct of function pointers to specific memory location
memcpy((void*)FCN_BASE_ADDRESS, &myfunctions, sizeof(myfunctions));

// read this table from specific memory
memcpy(&myfunctions, (void*)FCN_BASE_ADDRESS, sizeof(myfunctions));

This is based on my guess on what you actually want to do.

Upvotes: 0

Clifford
Clifford

Reputation: 93476

Locating objects at specific locations is usually most easily performed by the use of compiler specific extension; there is no standard method defined by the language. It may also be possible to locate a global object at a specific location by modifying the linker script, but that will be specific to your particular tool-chain

What compiler/tool-chain are you using? Refer to its documentation.

Upvotes: 3

Related Questions