0
votes

I stumbled upon a this embedded system question, can we call an function inside an ISR ? Working ARM Cortex M4, Have called function many times from ISR without any fault.

I assume behavior will be same for other micro controller as well or am i wrong ?

Note : Please ignore calling an function in ISR would increase my ISR time in turn increasing the interrupt latency.

2
Is the function called only by the ISR? Is the function re-entrant? Are there other interrupt with higher priority that can interrupt your ISR? So: you should elaborate a little more to have an answer. Otherwise is too broad. - LPs
Is the function called only by the ISR? No Is the function re-entrant? Yes Are there other interrupt with higher priority that can interrupt your ISR? our code doesn't have knowledge about the complete system and if there are ISR of higher priority. - PartTimeEngineer
Based on those answers calling that function into ISR can be safe, as far as the worse case of race conditions grant that the next ISR call can be served. - LPs

2 Answers

3
votes

Generally, there is nothing stopping you from calling a function from an ISR. There are however some things to consider.

First of all, you should keep ISRs as short as possible. Even the function call overhead might be considered too much in some cases. So if you call functions from inside an ISR, it might be wise to inline those functions.

You must also ensure that the called function is either re-entrant or that it isn't called by other parts of the code except the ISR. If a non re-entrant function is called by the main program and your ISR both, then you'll get severe but subtle "race condition" bugs. (Just as you will if the main program and the ISR modify the same shared variable non-atomically, without semaphore guards.)

And finally, designing a system with interrupts, where you don't know if there are other interrupts in the system, is completely unprofessional. You must always consider the program's interrupt situation as whole when designing the individual interrupts. Otherwise the program will have non-existent real-time performance and no programmer involved in the project will actually know what the program is doing. And from the point where nobody knows what they are doing, bugs are guaranteed to follow.

0
votes

Some RTOS will enforce a policy of which of its macros can or can't be called from an ISR context, i.e. functions that will block on some shared resource. For example:

http://www.freertos.org/a00122.html