3

If I want to create an interrupt and have it do something, can this 'something' be written in a regular function that I also use outside of the interrupt?

2

Yes you may.

Yet keep the function in question short, you don't want to spend too much time in interruption.

Another way is to set up a variable state in the interruption, and call the function in question in the normal cycle when the variable is in the desired state.

| improve this answer | |
4

Yes, you can. You just have to make sure your function is reentrant. If it accesses no memory beyond its parameters and (non static) local variables, you should be fine. If it accesses global variables, hardware registers, or static locals, it is unlikely to be, and only detailed analysis will tell you for sure.

Beware also of the library functions you call: some of them are not themselves reentrant and should therefore be avoided. See for example the manual of strtok(), which explicitly addresses the reentrancy problem.

| improve this answer | |
  • 1
    In addition, if the function is only called by the one ISR, and it will always run to completion (won't be interrupted and run another time before the previous call finishes), it doesn't need to be re-entrant. – JRobert Apr 7 '17 at 16:15
  • @JRobert: The OP stated that the function would also be called from non-interrupt context. That other call would need to be inside a critical section if one wanted to ensure the function is never called while another call has not finished. – Edgar Bonet Apr 7 '17 at 17:01
  • True enough, making my comment irrelevant here. – JRobert Apr 7 '17 at 18:21
1

You can do that, but it will increase the complexity of your program. Often when entering an interrupt service routine, the first thing you do is disabling interrupts - the make sure the callback completes reliably. This is something you will have to consider if you want to use the same routine for a 'regular' scenario

| improve this answer | |
  • You wrote: “the first thing you do is disabling interrupts”: on AVRs at least, you don't. The hardware does it right before executing the interrupt vector. – Edgar Bonet Apr 7 '17 at 17:03
1

If I want to create an interrupt and have it do something, can this 'something' be written in a regular function that I also use outside of the interrupt?

The right answer is yes and no.

The issue here is reentrancy : whether a function can be interrupted while it's being called by another. Not all functions are re entrant so the safer assumption is that they are.

Another issue with calling a function from within the isr is overhead.

The typical solutions are:

  1. Make sure that the function called from the isr is re entrant.
  2. The simplest way to assure that is to a. Limit your calling of that from the isr. Or b. Create two versions of the same function. And call them separately.

Btw, I routinely call functions from within isr. In my case through function pointers.

| improve this answer | |
  • You left out not at the end of “Not all functions are re entrant so the safer assumption is that they are.” – James Waldby - jwpat7 Apr 8 '17 at 0:43

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.