AVR Function Attributes (Using the GNU Compiler Collection (GCC)) (original) (raw)
signal
¶
interrupt
The function is an interrupt service routine (ISR). The compiler generates function entry and exit sequences suitable for use in an interrupt handler when one of the attributes is present.
The AVR hardware globally disables interrupts when an interrupt is executed.
- ISRs with the
signal
attribute do not re-enable interrupts. It is save to enable interrupts in asignal
handler. This “save” only applies to the code generated by the compiler and not to the IRQ layout of the application which is responsibility of the application. - ISRs with the
interrupt
attribute re-enable interrupts. The first instruction of the routine is aSEI
instruction to globally enable interrupts.
The recommended way to use these attributes is by means of theISR
macro provided by avr/interrupt.h
fromAVR-LibC:
#include <avr/interrupt.h>
ISR (INT0_vect) // Uses the "signal" attribute. { // Code }
ISR (ADC_vect, ISR_NOBLOCK) // Uses the "interrupt" attribute. { // Code }
When both signal
and interrupt
are specified for the same function, then signal
is silently ignored.
signal(num)
¶
interrupt(num)
Similar to the signal
resp. interrupt
attribute without argument, but the IRQ number is supplied as an argument num to the attribute, rather than providing the ISR name itself as the function name:
attribute((signal(1))) static void my_handler (void) { // Code for __vector_1 }
Notice that the handler function needs not to be externally visible. The recommended way to use these attributes is by means of theISR_N
macro provided by avr/interrupt.h
fromAVR-LibC:
#include <avr/interrupt.h>
ISR_N (PCINT0_vect_num) static void my_pcint0_handler (void) { // Code }
ISR_N (ADC_vect_num, ISR_NOBLOCK) static void my_adc_handler (void) { // Code }
ISR_N
can be specified more than once, in which case several interrupt vectors are pointing to the same handler function. This is similar to the ISR_ALIASOF
macro provided by AVR-LibC, but without the overhead introduced by ISR_ALIASOF
.
noblock
¶
This attribute can be used together with the signal
attribute to indicate that an interrupt service routine should start with a SEI
instruction to globally re-enable interrupts. Using attributes signal
and noblock
together has the same effect like using the interrupt
attribute. Using the noblock
attribute without signal
has no effect.
naked
¶
This attribute allows the compiler to construct the requisite function declaration, while allowing the body of the function to be assembly code. The specified function will not have prologue/epilogue sequences generated by the compiler. Only basicasm
statements can safely be included in naked functions (see Basic Asm — Assembler Instructions Without Operands). While using extended asm
or a mixture of basic asm
and C code may appear to work, they cannot be depended upon to work reliably and are not supported.
no_gccisr
¶
Do not use the __gcc_isr
pseudo instructionin a function with the interrupt
or signal
attribute aka. interrupt service routine (ISR). Use this attribute if the preamble of the ISR prologue should always read
push zero_reg push tmp_reg in tmp_reg, SREG push tmp_reg clr zero_reg
and accordingly for the postamble of the epilogue — no matter whether the mentioned registers are actually used in the ISR or not. Situations where you might want to use this attribute include:
- Code that (effectively) clobbers bits of
SREG
other than theI
-flag by writing to the memory location ofSREG
. - Code that uses inline assembler to jump to a different function which expects (parts of) the prologue code as outlined above to be present.
To disable __gcc_isr
generation for the whole compilation unit, there is option -mno-gas-isr-prologues, see AVR Options.
OS_main
¶
OS_task
On AVR, functions with the OS_main
or OS_task
attribute do not save/restore any call-saved register in their prologue/epilogue.
The OS_main
attribute can be used when there is guarantee that interrupts are disabled at the time when the function is entered. This saves resources when the stack pointer has to be changed to set up a frame for local variables.
The OS_task
attribute can be used when there is no guarantee that interrupts are disabled at that time when the function is entered like for, e.g. task functions in a multi-threading operating system. In that case, changing the stack pointer register is guarded by save/clear/restore of the global interrupt enable flag.
The differences to the naked
function attribute are:
naked
functions do not have a return instruction whereasOS_main
andOS_task
functions have aRET
orRETI
return instruction.naked
functions do not set up a frame for local variables or a frame pointer whereasOS_main
andOS_task
do this as needed.