1
\$\begingroup\$

I'm facing a little problem with function pointers in a code for the microcontroller Microchip Pic 18 Series.

The code below shows the prototypes of the functions involved in the problem and a call to the function 'insert_task', who should receive the address of the function 'task1' by the parameter 'task'.

However, I'm debugging the code in Proteus Isis and the parameter 'task' doesn't receive the address of the function passed as argument. More specifically, the parameter 'task' doesn't receive anything.

Anyone can see an error in the code?

void insert_task(uint8 priority, type_t type, void (*task)());
void task1();

insert_task(0, 0, task1);

The printscreen below shows the field "value" of the pointer "task" ("tarefa" in portuguese) without a value during the call of "insert_task" ("insere_tarefa" in portuguese).

debug

\$\endgroup\$
15
  • \$\begingroup\$ I don't remember, but... I think you must specify void as the argument type of task pointer, i.e. void (*task)(void), if it doesn't take arguments. \$\endgroup\$ – Martin Petrei Sep 24 '14 at 12:19
  • \$\begingroup\$ No. Didn't work, but thank you. \$\endgroup\$ – user49894 Sep 24 '14 at 12:23
  • 4
    \$\begingroup\$ This question belongs on StackOverflow \$\endgroup\$ – Scott Seidman Sep 24 '14 at 12:28
  • 6
    \$\begingroup\$ @ScottSeidman: No, it doesn't. It actually relates to the specifics of programming in C on microcontrollers that have a Harvard architecture (separate memory spaces for instructions and data). Support for things like function pointers (if it exists at all) is going to be very dependent on the specific compiler being used. \$\endgroup\$ – Dave Tweed Sep 24 '14 at 12:34
  • 2
    \$\begingroup\$ @DaveTweed - If this counts as a hardware-specific problem because of the architecture then you probably need to migrate 75% of the contents of StackOverflow over to EE. It looks like pretty universal C code to me, should compile & run (when fixed, natch) on just about anything. \$\endgroup\$ – John U Sep 24 '14 at 13:26
2
\$\begingroup\$

If you're using the XC8 compiler, it handles pointers to functions by creating a jump table in code memory. The actual function pointer is then an offset value that's used to index the jump table.

In a debugger, you'll never see the actual code-space address of the function in the pointer. In fact, if you happen to be passing a pointer to the first function in the table, the pointer value will correctly be zero.

\$\endgroup\$
2
  • \$\begingroup\$ I'm using C18. The same is valid to it? \$\endgroup\$ – user49894 Sep 24 '14 at 13:21
  • \$\begingroup\$ I don't know. A quick Google didn't turn up anything that gave any hints about how it implements function pointers internally. But it does seem to have support for them. \$\endgroup\$ – Dave Tweed Sep 24 '14 at 13:48
2
\$\begingroup\$

The only error in your code is this line: void task1();

This is only function prototype. You need to write function defination.

Replace this line with following code :

void task1()
{
  /* Write some code here */
  Nop();//No operation
}

Just writing function prototype will not allocate memory.

\$\endgroup\$
2
  • \$\begingroup\$ No, no, no. Here I just write the prototype, but the definition is in the code. \$\endgroup\$ – user49894 Sep 24 '14 at 13:19
  • \$\begingroup\$ you can use PICKIT 3 or similar debugger with MPLAB to debug. Add task1 to watch or you can see deassembly code. \$\endgroup\$ – GOKU Sep 24 '14 at 13:27
0
\$\begingroup\$

I think the problem is a bug in Proteus. I tried to debug the code in MPLAB X and there it works. Thanks for your help!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Good catch. Many compiler-specific PIC sites will simply turn you away if you haven't tried it on the chip for such reasons. \$\endgroup\$ – Scott Seidman Sep 24 '14 at 20:21
0
\$\begingroup\$

Also beware that many uC compilers have optimization settings that cause problems with function pointers. The optimization routines apparently can't "see" that some code is actually used and it gets optimized away. If you have sufficient code space, I recommend turning off or turning down the optimization level until you've finished debugging, and then increase the optimization and see if it still works.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy