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).
4 Answers 4
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.
-
\$\begingroup\$ I'm using C18. The same is valid to it? \$\endgroup\$user49894– user498942014年09月24日 13:21:07 +00:00Commented Sep 24, 2014 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– Dave Tweed2014年09月24日 13:48:47 +00:00Commented Sep 24, 2014 at 13:48
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.
-
\$\begingroup\$ No, no, no. Here I just write the prototype, but the definition is in the code. \$\endgroup\$user49894– user498942014年09月24日 13:19:39 +00:00Commented Sep 24, 2014 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– GOKU2014年09月24日 13:27:31 +00:00Commented Sep 24, 2014 at 13:27
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!
-
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– Scott Seidman2014年09月24日 20:21:14 +00:00Commented Sep 24, 2014 at 20:21
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.
void
as the argument type oftask
pointer, i.e.void (*task)(void)
, if it doesn't take arguments. \$\endgroup\$