Keil Logo

C51: Locating Function Tables in Code Memory


Information in this article applies to:

  • C51

QUESTION

I have defined a structure that contains an array of function pointers. I then use this to declare a variable. I'm trying to locate the function pointers in code memory, however, I get syntax errors. What am I doing wrong?

typedef struct {
  const code void (*dsfHardwareFunc[2])(long x);
} HARDWARE_TASKLIST;

HARDWARE_TASKLIST functable = {foo, bar};

ANSWER

Individual struct members can't be assigned to different memories. This only works for complete objects/variables.
The following code defines a type based on a structure, the same as your example. Because the typedef is simply defining a type, we do not need to specify where the structure goes in memory. It is used as a 'template' for subsequent variable declarations.

The variable functable is then defined using the typedef. This is a variable declaration, so we specify that it is 'const code'. This ensures that the function pointers are located in code memory.

We also have to initialize the table right away because it cannot be initialized later.

The rest of the code simply calls two functions indirectly to prove all the code is correct and functional.

void foo(long x);
void bar(long x);

typedef struct {
  void (*dsfHardwareFunc[2])(long x);
} HARDWARE_TASKLIST;

HARDWARE_TASKLIST const code functable = {foo, bar};

void foo(long x)
{
  x++;
}

void bar(long x)
{
  x++;
}

void main(void)
{
  long x = 5;
  functable.dsfHardwareFunc[0](x);
  functable.dsfHardwareFunc[1](x);
  while(1);
}

If you now look at the Symbol Table of Module in the generated .m51 map file, you see that the functions foo and bar are located at, for example, the following locations:

foo: 50H bar: 70H

You also see that functable is located, in this case, at 9CH in code memory.

If you now debug the program in µVision (can be the Simulator) you can double-check that the function pointers really are stored in code memory.

Type

D C:0x9C, 0x9C+5

into the Command Windows command line to display code memory from location 9CH onwards. You will see the following data:

9CH: FF  00  50  FF  00  70

These are the function pointers. The first byte of each pointer is FFH, which means that the pointer points to code memory. The remaining two bytes are the location of the function. You can see that 0050H and 0070H are the locations of the functions foo() and bar().

Now that you can see that the structure is located in code memory, the function pointers are also located in code memory.

If you look through the .m51 file, you may not be able to find the structure name. That is because it was never located in memory. It was simply used as a 'template' for the functable variable.

Using these methods, it is easy to double-check where your function pointers are being located, whether they are declared on their own or as part of a structure.

MORE INFORMATION


Last Reviewed: Thursday, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.