The GCC development tools allow interrupts to be written in C.
A compare match event on the AVR timer 1 peripheral can be written using the following syntax.
void SIG_OUTPUT_COMPARE1A( void ) __attribute__ ( ( signal ) );
void SIG_OUTPUT_COMPARE1A( void )
/* ISR C code for RTOS tick. */
The ‘__attribute__ ( ( signal ) )’ directive on the function prototype informs the compiler that the function is an
ISR and results in two important changes in the compiler output.
The ‘signal’ attribute ensures that every processor register that gets modified during the ISR is restored to its original value when the ISR exits. This is required as the compiler cannot make any assumptions as to when the interrupt will execute, and therefore cannot optimize which processor registers require saving and which don’t.
The ‘signal’ attribute also forces a ‘return from interrupt’ instruction (RETI) to be used in place of the ‘return’ instruction (RET) that would otherwise be used. The AVR microcontroller disables interrupts upon entering an ISR and the RETI instruction is required to re-enable them on exiting.
Code output by the compiler:
;void SIG_OUTPUT_COMPARE1A( void )
; CODE GENERATED BY THE COMPILER TO SAVE
; THE REGISTERS THAT GET ALTERED BY THE
; APPLICATION CODE DURING THE ISR.