AnsweredHot!Change a function pointer inside an interrupt.

Author
vibemac.1
Starting Member
  • Total Posts : 77
  • Reward points : 0
  • Joined: 2016/05/31 07:39:32
  • Location: 0
  • Status: offline
2018/10/12 01:05:11 (permalink)
0

Change a function pointer inside an interrupt.

Goodmorning,
I'm using XC16 version 1.31 with PIC33EP512MU810, and I have a doubt. I'm doing a project where a motor works throuh three different function:
- PWM interrupt for counting impulses (Priority: 7)
- 1ms timer interrupt for updating quote (Priority: 5)
- a while function for calculation (main while)
Besides the motor can work in different ways (following an analog signal, linear movement, etc.).
For changing the mode, I've made three function pointers for PWM, timer and while that point to the desired mode functions.
So far, I had to change mode inside the while loop, so I was sure that none of interrupt  and while functions were called while I was doing the change.
 
Now I need to make the change inside an interrupt with 6 as priority and I have some doubts. What happen if I change the address of the function pointed, while it has been calling?
Please is very important.
 
Thank you.
#1
crosland
Super Member
  • Total Posts : 1290
  • Reward points : 0
  • Joined: 2005/05/10 10:55:05
  • Location: Bucks, UK
  • Status: offline
Re: Change a function pointer inside an interrupt. 2018/10/12 01:08:43 (permalink)
0
vibemac.1
Now I need to make the change inside an interrupt with 6 as priority and I have some doubts. What happen if I change the address of the function pointed, while it has been calling?
Please is very important.

 
You are not changing the address of any functions (the location of the code) you are changing the value of a pointer.
 
The code stays exactly where it is and will continue to execute.
#2
vibemac.1
Starting Member
  • Total Posts : 77
  • Reward points : 0
  • Joined: 2016/05/31 07:39:32
  • Location: 0
  • Status: offline
Re: Change a function pointer inside an interrupt. 2018/10/12 01:34:26 (permalink)
0
Hello crossland, Thank you for your answer.
Excuse me, I didn't write correctly since I'm not english mothertongue. For better understanding, I meant the following scenario:
- The while function pointer points to "Linear Movement while function".
- The while loop is calling exactly this while function pointer and in that very moment, the priority level 6 is triggered.
- Inside the interrupt, I change mode and set the while function pointer to point to "Analog Movement while Function".
 
Once that the priority level 6 finishes, the code will complete "Linear Movement function" and the next time, the code will call the "analog Movement function". Is it correct?
Besides, if inside the priority 6 interrupt I make the function pointer points to 0, is it correct as well?
 
Thank you very much. I highly appreciate.
 
Regards,
Giordano
 
 
#3
Chris A
Super Member
  • Total Posts : 806
  • Reward points : 0
  • Joined: 2010/07/20 04:37:07
  • Location: 0
  • Status: offline
Re: Change a function pointer inside an interrupt. 2018/10/12 02:21:14 (permalink) ☼ Best Answerby vibemac.1 2018/10/12 02:56:43
+1 (1)
Your issue here is not really the changing the function pointers but more that you are changing the 'Mode' in the interrupt while the "other mode" code could still running.
 
You could disable (delay) the interrupt when the 'function' is running, or the interrupt could detect that the 'function' is running and set a flag so that the mainline code adjusts the mode at the end.
 
Or maybe it really does not matter with the way the code is written!  But the good thing is that you are aware of the issue so will guard against it :-)
 
#4
vibemac.1
Starting Member
  • Total Posts : 77
  • Reward points : 0
  • Joined: 2016/05/31 07:39:32
  • Location: 0
  • Status: offline
Re: Change a function pointer inside an interrupt. 2018/10/12 02:57:47 (permalink)
0
Thank you Chris,
since I cannot delay the interrupt, I'll make sure that no function pointer is called through flags, before making the mode change or set the function pointer to 0. It's not the answer that I hoped, but I understand.
 
Thank you again :).
Gio
post edited by vibemac.1 - 2018/10/12 02:58:48
#5
Chris A
Super Member
  • Total Posts : 806
  • Reward points : 0
  • Joined: 2010/07/20 04:37:07
  • Location: 0
  • Status: offline
Re: Change a function pointer inside an interrupt. 2018/10/12 03:17:27 (permalink)
+1 (1)
Watch out for the setting the pointer to zero.  The following code is not atomic:
if (pDoFunction)
  pDoFunction();

If the interrupt changed the function pointer to null between the two lines, the code could call 0 and reset the CPU!  You would need to disable the interrupt for a couple of instructions.  You can use the __builtin_disi( n) to do that.
 
Also not sure of the reason for setting to 0.  It might be better to have a 'do nothing' mode function that you set the function pointer to so that you do not require the above test in the code and can safely just call the function at any time.  Changing the function pointer should be atomic as it is only a 16bit value.
 
Also if you have only a few modes, a switch(mode) may be a better solution.
post edited by Chris A - 2018/10/12 04:29:40
#6
vibemac.1
Starting Member
  • Total Posts : 77
  • Reward points : 0
  • Joined: 2016/05/31 07:39:32
  • Location: 0
  • Status: offline
Re: Change a function pointer inside an interrupt. 2018/10/12 04:52:46 (permalink)
0
Hello Chris,
thank you for your message, I'll use the if-else for choosing the mode (I have four modes overall).
By now, I have a motor file that calls the chosen function pointers (while, timer, PWM) according to the selected mode, using the if-else I'll loose a little the separation between the files (in motor there was no trace of specific mode function called) but it's acceptable.
I used the function pointer pointing to 0 as an insurance for stopping the motor, aside setting DUTY to 0.
Please if you have any other suggestion let me know, I thank you.
 
Regards,
Gio
post edited by vibemac.1 - 2018/10/12 04:56:24
#7
qhb
Superb Member
  • Total Posts : 7535
  • Reward points : 0
  • Joined: 2016/06/05 14:55:32
  • Location: One step ahead...
  • Status: online
Re: Change a function pointer inside an interrupt. 2018/10/12 14:08:51 (permalink)
0
vibemac.1
I used the function pointer pointing to 0 as an insurance for stopping the motor, aside setting DUTY to 0.

But that relies on you separately testing for zero, which as mentioned, is non-atomic.
It would be far safer to point it to a dummy function instead.
 
#8
Jump to:
© 2018 APG vNext Commercial Version 4.5