Page 1 of 1

How to tell if you are in an interrupt

Posted: Fri May 30, 2014 5:31 am
by sulliwk06
I've been wondering for a while if there is a way to tell if you are in an interrupt or not. Is there some central flag or something that I can check to see if I'm in one? I think it would be nice to be able to handle certain functions differently while in an interrupt. Is there a way to do this, or do I need to set a flag myself every time I enter and leave an interrupt.

Re: How to tell if you are in an interrupt

Posted: Fri May 30, 2014 6:25 am
by Chris Ruff
That's interesting..it sounds like you are calling the same function(s) from both task(s) and interrupt(s).

I don't design my code in such a manner that a function will be called from essentially any context. Doesn't seem like a good idea.

I'm not saying that you can't or shouldn't. If the function is atomic and any side effects are not global then I don't see any major danger . Except that I try not to call any of my functions from any interrupts period.

And, yes, I would let my function know who called it by a handed param of my manufacture.

my 2c

Chris

Re: How to tell if you are in an interrupt

Posted: Fri May 30, 2014 8:11 am
by pbreed
I agree with chris, interrupts should be really simple.
If you need to do complex things in an interrupt, create a high priority task that waits on a semaphore and have the interrupt do no more than post to the semaphore...


given that
this global variable will be non zero when in an interrupt...

extern volatile DWORD OSIntNesting;

So


inline bool Am_I_in_an_ISR() {return (OSIntNesting!=0); };

Re: How to tell if you are in an interrupt

Posted: Fri May 30, 2014 8:59 am
by sulliwk06
Thank you very much.

I realize that interrupts should be used carefully, I'm just writing some convenience functions/classes and I'm trying to idiot proof them for future use.

Re: How to tell if you are in an interrupt

Posted: Fri May 30, 2014 10:56 am
by dciliske
Chris,

Sometimes when writing drivers, you want to do the same/similar things on the initial setup as to what actually occurs in the ISR. In this case, you would want to write a trampoline ISR which calls a main processing function (This is how the SPI and I2C drivers work). Since this main processing function can be called from either path, maybe certain bits need to be changed/cleared due to being in the IRQ. That said, it might be better suited to being handled in the trampoline function.

-Dan

Re: How to tell if you are in an interrupt

Posted: Fri May 30, 2014 11:08 am
by Chris Ruff
But is there a fence all around the trampoline function? :lol:

Chris