How to tell if you are in an interrupt

Discussion to talk about software related topics only.
Post Reply
sulliwk06
Posts: 118
Joined: Tue Sep 17, 2013 7:14 am

How to tell if you are in an interrupt

Post 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.
User avatar
Chris Ruff
Posts: 222
Joined: Thu Apr 24, 2008 4:09 pm
Location: topsail island, nc
Contact:

Re: How to tell if you are in an interrupt

Post 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
Real Programmers don't comment their code. If it was hard to write, it should be hard to understand
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: How to tell if you are in an interrupt

Post 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); };
sulliwk06
Posts: 118
Joined: Tue Sep 17, 2013 7:14 am

Re: How to tell if you are in an interrupt

Post 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.
User avatar
dciliske
Posts: 624
Joined: Mon Feb 06, 2012 9:37 am
Location: San Diego, CA
Contact:

Re: How to tell if you are in an interrupt

Post 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
Dan Ciliske
Project Engineer
Netburner, Inc
User avatar
Chris Ruff
Posts: 222
Joined: Thu Apr 24, 2008 4:09 pm
Location: topsail island, nc
Contact:

Re: How to tell if you are in an interrupt

Post by Chris Ruff »

But is there a fence all around the trampoline function? :lol:

Chris
Real Programmers don't comment their code. If it was hard to write, it should be hard to understand
Post Reply