What happens with a premature 'return' in an ISR?
Asked Answered
L

1

11

I'm using AVR-GCC 4.9.2, and I would like to know what happens if I do a premature return in an ISR on an AVR?

ISR(USART_RXC_vect)
{
    ...
    if(idx == BUFSIZE)
        return;
    ...
 }

Will the return be translated to a reti instruction? Or do I need to include a reti() myself?

I'm looking for a detailed explanation of what goes on behind the scenes.

Loader answered 4/8, 2015 at 20:18 Comment(0)
S
9

Just as
ISR(USART_RXC_vect)
is not literally just
USART_INTERUPT_VECTOR:
in Assembler,

return;
is not literally just
ret
or
reti
in Assembler.

Both instructions in C/C++ will be translated into multiple Assembler statements and in both cases it will depend on context. The context for an ISR(){} is quite one-track in this case, but it will most likely also include a few pushes and storing the SREG. But the number of pushes to the stack will depend on what happens in the function.

So will any return; be interpreted in context. At the end of a normal subroutine that actually gets built into a subroutine (many subroutines of limited use get "in-lined" by a compiler for code efficiency reasons) it will become a ret instruction (after handling any needed POPs and other low-level clean-up). At the end of an interrupt it will actually mean "pop everything you pushed before (and also restore the SREG) and then reti".

When you return a type that will get compiled into transferring that value through the platform's system, before including the ret instruction. So return; is a very context sensitive statement that you can assume will be interpreted correctly, unless you do very weird things. But those weird things will be things a good compiler (like AVR-GCC) will at the least warn for.

Salian answered 4/8, 2015 at 22:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.