9 years, 4 months ago.

What happens if I call wait in ISR?

I have called wait in the ISR of my program, and it seems to be working fine. So, what happens if I call wait in ISR?

Question relating to:

2 Answers

9 years, 4 months ago.

It waits :).

In principle it works fine, however the big downside is that while it waits nothing else can happen, and often you will use interrupts to make sure things can happen in parallel.

Lets say you have 4 buttons, and when you press one an LED should be turned on for one second. If you do this with 4 IRQs which have a wait statement each, then when one LED is turned on and you press another button, nothing will happen until that 1 second is gone, then the next LED will turn on corresponding to the button you pressed.

But for some applications it is perfectly fine to use wait statements.

Accepted Answer

What would happen if you specify another ISR (before the 'wait') within the ISR that is waiting and that interrupt then comes in before the wait time has expired, would it run the code and return to the 'waiting' ISR and finish the remaining waiting time? or would the wait time be broken and exit the ISR?

posted by Paul Staron 14 Dec 2014

All ISRs default to being the same priority. That means that until you exit the ISR you are in no other interrupts will be handled. Where you defined them doesn't matter.

You can set priorities for different interrupts but that means going outside the mbed libraries and so losing some cross platform support. In that situation a higher priority interrupt would still work if you called wait inside a lower priority ISR.

posted by Andy A 15 Dec 2014

In addition: If you do it with a higher priority, or just interrupting a wait in main(), the wait time will still be correct. So lets say you wait 1 second, and an interrupt happens which takes 0.2 seconds, then it will still wait 1 second total, and not 1.2 seconds. This is of course assuming the interrupt is finished before the end of the wait: If it only occurs after 0.9 seconds, then first the 0.2 seconds from the interrupt will have to finish, so after 1.1 seconds then the wait statement finishes.

posted by Erik - 15 Dec 2014
9 years, 4 months ago.

An excellent notebook about the use of wait in ISRs has been written by Andy Kirkham:
https://developer.mbed.org/users/AjK/notebook/regarding-interrupts-use-and-blocking/

Thanks ..the article was great and so is your suggestion

posted by Dheeraj M Pai 03 Jul 2015