Critique this ISR. What is wrong with it?
0 upvotes
Practice with AISoon
c
__interrupt double compute_area(double radius) {double area = PI * radius * radius;printf("Area = %f", area);return area;}
This ISR has four fundamental problems:
- ISRs cannot return values — the hardware invokes the ISR via the interrupt vector; there is no caller to receive a return value.
- ISRs cannot take parameters — they are triggered by hardware, not called with arguments.
- Floating-point in an ISR — FP operations use shared FPU registers that may not be saved/restored, and they are slow. ISRs should be fast and minimal.
printf()in an ISR — it is slow, typically not reentrant, and may rely on interrupts itself (UART TX), causing deadlocks.
A proper ISR: takes no parameters, returns void, does minimal work (set a flag, copy data to a buffer), and defers heavy processing to the main loop or an RTOS task.
Source: C / C++ Concepts Q&A
