1

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

Glorfindel
  • 1,245
  • 3
  • 15
  • 20
  • 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. – Martin Petrei Sep 24 '14 at 12:19
  • No. Didn't work, but thank you. –  Sep 24 '14 at 12:23
  • 4
    This question belongs on StackOverflow – Scott Seidman Sep 24 '14 at 12:28
  • 6
    @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. – Dave Tweed Sep 24 '14 at 12:34
  • Can you explain what exactly you mean by "the parameter 'task' doesn't receive anything." – user28910 Sep 24 '14 at 12:45
  • What compiler are you using? Are you using the Microchip XC8 compiler and then running in Proteus? – David Sep 24 '14 at 12:48
  • Possible duplicate of [C coding design - function pointers?](http://electronics.stackexchange.com/questions/56058/c-coding-design-function-pointers) –  Sep 24 '14 at 13:16
  • David, I'm using the Microchip C18 and then running and debugging in Proteus. –  Sep 24 '14 at 13:22
  • user28910, I am debugging the code in Proteus Isis and, when I call the function, the field value of the parameter 'task' receive nothing. There's nothing in this field, not even garbage. –  Sep 24 '14 at 13:25
  • 2
    @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. – John U Sep 24 '14 at 13:26
  • John U, I guess my problem is some C18 compiler's specificity. Because of this I posted it here instead of StackOverflow. –  Sep 24 '14 at 13:31
  • @JohnU: Yes, there is a considerable amount of overlap between the two sites. But this question started here, and it relates to the fact that some microcontroller compilers don't implement the full language (or put restrictions on certain details), so it makes sense to leave it here. – Dave Tweed Sep 24 '14 at 13:53
  • Please don't add "Solved" to question titles. This is not a forum, so that's not how it works here. – JYelton Sep 24 '14 at 16:35
  • I remember reading something about function pointer issues when the target function address is located above the 64k (2 byte pointer addressable) flash area. There are PIC18 controllers with 128k, what model do you use? How "full" is it? – Rev Sep 25 '14 at 14:14
  • It's a PIC18F4520. What do you mean by "full"? –  Sep 25 '14 at 23:22

4 Answers4

3

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.

Dave Tweed
  • 168,369
  • 17
  • 228
  • 393
  • I'm using C18. The same is valid to it? –  Sep 24 '14 at 13:21
  • 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. – Dave Tweed Sep 24 '14 at 13:48
2

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.

JYelton
  • 32,302
  • 33
  • 134
  • 249
GOKU
  • 322
  • 1
  • 6
  • No, no, no. Here I just write the prototype, but the definition is in the code. –  Sep 24 '14 at 13:19
  • you can use PICKIT 3 or similar debugger with MPLAB to debug. Add task1 to watch or you can see deassembly code. – GOKU Sep 24 '14 at 13:27
0

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
    Good catch. Many compiler-specific PIC sites will simply turn you away if you haven't tried it on the chip for such reasons. – Scott Seidman Sep 24 '14 at 20:21
0

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.

mixed_signal
  • 271
  • 1
  • 6