Embedded Systems & Firmware
What should be avoided inside an interrupt handler?
- AWriting 1 to a flag variable to notify the main code
- BClearing the bit that indicates the interrupt source
- CTime-consuming work such as printing long strings with printf or loops that wait for something to happen
Show answer and explanation
Answer: C. Time-consuming work such as printing long strings with printf or loops that wait for something to happen
While an interrupt handler runs long, other interrupts cannot be serviced and the main code stalls. printf takes time to finish and may itself rely on interrupts, so it is especially risky. A handler should do only the minimum, such as moving received data into a buffer or setting a flag, and leave heavy work to the main loop or a task. It must also remember to clear the interrupt source bit.
- A. Notifying main with a short action is good practice.
- B. Without clearing it, the same interrupt fires repeatedly.
- C. Correct. It holds up other interrupts and the main code for a long time.