2
\$\begingroup\$

I often see something like this void UART2_IRQHandler(void) __irq, and I wonder why __irq is used when declaring interrupt handler, is it required or handler can work without it?

\$\endgroup\$
  • \$\begingroup\$ It is instruction to compiler. Have a look at Assembly code generated with and without __irq... Lot of interesting things happen due to __irq (which are explained in the answers) \$\endgroup\$ – Swanand Apr 23 '15 at 10:45
4
\$\begingroup\$

Basically, an interrupt service routine written in C is simply a function that takes no arguments and doesn't return anything void someKindOfIsr(void).

__irq is a C language extension for your compiler that is used to generate special entry and/or exit assembly instructions for handling interrupts. For instance, the return instruction may be different if you are returning from an interrupt rather than a simple function to clear some type of interrupt status register.

Whether it is necessary or not depends on what ARM architecture you are working with. A quick way to find out if it is necessary for your platform is to examine the assembly produced both with and without the __irq keyword.

Even if the listing file is exactly the same both with and without the __irq keyword, you may want to consider leaving the __irq keyword in place for clarity and for portability to some future platform.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer, now I know what is the purpose of __irq. \$\endgroup\$ – Lazar Apr 21 '15 at 6:27
5
\$\begingroup\$

Some microcontrollers require a different handling of the stack and return for interrupt handling functions as for normal functions.

For ARM controllers a normal entry to a subroutine uses a PUSH to save stuff on the stack, and on exit of the subroutine you would POP the saved state back and leave the subroutine. (ARM Assembly Reference page 65)

For an exception handler on modern ARM platforms the state is automatically pushed on the stack, so the compiler doesn't need to create a PUSH instruction on function entry. Also the exit from a exception handler can look different from a normal one, the ARMv7-M architecture will return from an exception if either POP/LDM, LDR or BX will load a value of 0xFXXXXXXX into the program counter (PC).

So the __irq tells the compiler that this functions entry and exit might require special treatment, and special depending on the set target (for 8051 I think there was RET for normal return and RETI for interrupt return).

\$\endgroup\$
-3
\$\begingroup\$

No, it cant work without _irq. it's a general format. you need to follow this. otherwise it wont work.

\$\endgroup\$
  • 7
    \$\begingroup\$ This is a bit thin for an answer. Can you explain exactly what __irq does in the compiler? \$\endgroup\$ – Dave Tweed Apr 20 '15 at 14:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.