How to tell if you are in an interrupt
How to tell if you are in an interrupt
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.
- 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
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
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
Re: How to tell if you are in an interrupt
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); };
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
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.
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
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
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
Project Engineer
Netburner, Inc
- 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
But is there a fence all around the trampoline function?
Chris

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