AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Reference FAQ Example Projects

interrupt.h

Go to the documentation of this file.
01	1 Copyright (c) 2002,2005,2007 Marek Michalkiewicz
	022Copyright (c) 2007, Dean Camera
033 044All rights reserved. 055 066Redistribution and use in source and binary forms, with or without 077modification, are permitted provided that the following conditions are met: 088 099* Redistributions of source code must retain the above copyright 1010notice, this list of conditions and the following disclaimer. 1111 1212* Redistributions in binary form must reproduce the above copyright 1313notice, this list of conditions and the following disclaimer in 1414the documentation and/or other materials provided with the 1515distribution. 1616 1717* Neither the name of the copyright holders nor the names of 1818contributors may be used to endorse or promote products derived 1919from this software without specific prior written permission. 2020 2121THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2222AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2323IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2424ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 2525LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2626CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2727SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2828INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2929CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3030ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3131POSSIBILITY OF SUCH DAMAGE. */ 3232 3333/* $Id: interrupt.h 2220 2011-02-22 21:08:58Z arcanum $ */ 3434 3535#ifndef _AVR_INTERRUPT_H_ 3636#define _AVR_INTERRUPT_H_ 3737 3838#include <avr/io.h> 3939 4040#if !defined(__DOXYGEN__) && !defined(__STRINGIFY) 4141/* Auxiliary macro for ISR_ALIAS(). */ 4242#define __STRINGIFY(x) #x 4343#endif /* !defined(__DOXYGEN__) */ 4444 4545/** 4646\file 4747\@{ 4848*/ 4949 5050 5151/** \name Global manipulation of the interrupt flag 5252 5353The global interrupt flag is maintained in the I bit of the status 5454register (SREG). 5555 5656Handling interrupts frequently requires attention regarding atomic 5757access to objects that could be altered by code running within an 5858interrupt context, see <util/atomic.h>. 5959 6060Frequently, interrupts are being disabled for periods of time in 6161order to perform certain operations without being disturbed; see 6262\ref optim_code_reorder for things to be taken into account with 6363respect to compiler optimizations. 6464*/ 6565 6666#if defined(__DOXYGEN__) 6767/** \def sei() 6868\ingroup avr_interrupts 6969 7070Enables interrupts by setting the global interrupt mask. This function 7171actually compiles into a single line of assembly, so there is no function 7272call overhead. However, the macro also implies a <i>memory barrier</i> 7373which can cause additional loss of optimization. 7474 7575In order to implement atomic access to multi-byte objects, 7676consider using the macros from <util/atomic.h>, rather than 7777implementing them manually with cli() and sei(). 7878*/ 79 79#define sei() 8080#else /* !DOXYGEN */ 8181# define sei() __asm__ __volatile__ ("sei" ::: "memory") 8282#endif /* DOXYGEN */ 8383 8484#if defined(__DOXYGEN__) 8585/** \def cli() 8686\ingroup avr_interrupts 8787 8888Disables all interrupts by clearing the global interrupt mask. This function 8989actually compiles into a single line of assembly, so there is no function 9090call overhead. However, the macro also implies a <i>memory barrier</i> 9191which can cause additional loss of optimization. 9292 9393In order to implement atomic access to multi-byte objects, 9494consider using the macros from <util/atomic.h>, rather than 9595implementing them manually with cli() and sei(). 9696*/ 97 97#define cli() 9898#else /* !DOXYGEN */ 9999# define cli() __asm__ __volatile__ ("cli" ::: "memory") 100100#endif /* DOXYGEN */ 101101 102102 103103/** \name Macros for writing interrupt handler functions */ 104104 105105 106106#if defined(__DOXYGEN__) 107107/** \def ISR(vector [, attributes]) 108108\ingroup avr_interrupts 109109 110110Introduces an interrupt handler function (interrupt service 111111routine) that runs with global interrupts initially disabled 112112by default with no attributes specified. 113113 114114The attributes are optional and alter the behaviour and resultant 115115generated code of the interrupt routine. Multiple attributes may 116116be used for a single function, with a space seperating each 117117attribute. 118118 119119Valid attributes are ISR_BLOCK, ISR_NOBLOCK, ISR_NAKED and 120120ISR_ALIASOF(vect). 121121 122122\c vector must be one of the interrupt vector names that are 123123valid for the particular MCU type. 124124*/ 125 125# define ISR(vector, [attributes]) 126126#else /* real code */ 127127 128128#if (__GNUC__ == 4 && __GNUC_MINOR__ >> = 1) || (__GNUC__ >> 4) 129129# define __INTR_ATTRS used, externally_visible 130130#else /* GCC < 4.1 */ 131131# define __INTR_ATTRS used 132132#endif 133133 134134#ifdef __cplusplus 135135# define ISR(vector, ...) \ 136136extern "C" void vector (void) __attribute__ ((signal,__INTR_ATTRS)) __VA_ARGS__; \ 137137void vector (void) 138138#else 139139# define ISR(vector, ...) \ 140140void vector (void) __attribute__ ((signal,__INTR_ATTRS)) __VA_ARGS__; \ 141141void vector (void) 142142#endif 143143 144144#endif /* DOXYGEN */ 145145 146146#if defined(__DOXYGEN__) 147147/** \def SIGNAL(vector) 148148\ingroup avr_interrupts 149149 150150Introduces an interrupt handler function that runs with global interrupts 151151initially disabled. 152152 153153This is the same as the ISR macro without optional attributes. 154154\deprecated Do not use SIGNAL() in new code. Use ISR() instead. 155155*/ 156 156# define SIGNAL(vector) 157157#else /* real code */ 158158 159159#ifdef __cplusplus 160160# define SIGNAL(vector) \ 161161extern "C" void vector(void) __attribute__ ((signal, __INTR_ATTRS)); \ 162162void vector (void) 163163#else 164164# define SIGNAL(vector) \ 165165void vector (void) __attribute__ ((signal, __INTR_ATTRS)); \ 166166void vector (void) 167167#endif 168168 169169#endif /* DOXYGEN */ 170170 171171#if defined(__DOXYGEN__) 172172/** \def EMPTY_INTERRUPT(vector) 173173\ingroup avr_interrupts 174174 175175Defines an empty interrupt handler function. This will not generate 176176any prolog or epilog code and will only return from the ISR. Do not 177177define a function body as this will define it for you. 178178Example: 179179\code EMPTY_INTERRUPT(ADC_vect);\endcode */ 180 180# define EMPTY_INTERRUPT(vector) 181181#else /* real code */ 182182 183183#ifdef __cplusplus 184184# define EMPTY_INTERRUPT(vector) \ 185185extern "C" void vector(void) __attribute__ ((signal,naked,__INTR_ATTRS)); \ 186186void vector (void) { __asm__ __volatile__ ("reti" ::); } 187187#else 188188# define EMPTY_INTERRUPT(vector) \ 189189void vector (void) __attribute__ ((signal,naked,__INTR_ATTRS)); \ 190190void vector (void) { __asm__ __volatile__ ("reti" ::); } 191191#endif 192192 193193#endif /* DOXYGEN */ 194194 195195#if defined(__DOXYGEN__) 196196/** \def ISR_ALIAS(vector, target_vector) 197197\ingroup avr_interrupts 198198 199199Aliases a given vector to another one in the same manner as the 200200ISR_ALIASOF attribute for the ISR() macro. Unlike the ISR_ALIASOF 201201attribute macro however, this is compatible for all versions of 202202GCC rather than just GCC version 4.2 onwards. 203203 204204\note This macro creates a trampoline function for the aliased 205205macro. This will result in a two cycle penalty for the aliased 206206vector compared to the ISR the vector is aliased to, due to the 207207JMP/RJMP opcode used. 208208 209209\deprecated 210210For new code, the use of ISR(..., ISR_ALIASOF(...)) is 211211recommended. 212212 213213Example: 214214\code 215215ISR(INT0_vect) 216216{ 217217PORTB = 42; 218218} 219219 220220ISR_ALIAS(INT1_vect, INT0_vect); 221221\endcode 222222223223*/ 224 224# define ISR_ALIAS(vector, target_vector) 225225#else /* real code */ 226226 227227#ifdef __cplusplus 228228# if defined(__AVR_MEGA__) && __AVR_MEGA__ 229229# define ISR_ALIAS(vector, tgt) extern "C" void vector (void) \ 230230__attribute__((signal, naked, __INTR_ATTRS)); \ 231231void vector (void) { asm volatile ("jmp " __STRINGIFY(tgt) ::); } 232232# else /* !__AVR_MEGA */ 233233# define ISR_ALIAS(vector, tgt) extern "C" void vector (void) \ 234234__attribute__((signal, naked, __INTR_ATTRS)); \ 235235void vector (void) { asm volatile ("rjmp " __STRINGIFY(tgt) ::); } 236236# endif /* __AVR_MEGA__ */ 237237#else /* !__cplusplus */ 238238# if defined(__AVR_MEGA__) && __AVR_MEGA__ 239239# define ISR_ALIAS(vector, tgt) void vector (void) \ 240240__attribute__((signal, naked, __INTR_ATTRS)); \ 241241void vector (void) { asm volatile ("jmp " __STRINGIFY(tgt) ::); } 242242# else /* !__AVR_MEGA */ 243243# define ISR_ALIAS(vector, tgt) void vector (void) \ 244244__attribute__((signal, naked, __INTR_ATTRS)); \ 245245void vector (void) { asm volatile ("rjmp " __STRINGIFY(tgt) ::); } 246246# endif /* __AVR_MEGA__ */ 247247#endif /* __cplusplus */ 248248 249249#endif /* DOXYGEN */ 250250 251251#if defined(__DOXYGEN__) 252252/** \def reti() 253253\ingroup avr_interrupts 254254 255255Returns from an interrupt routine, enabling global interrupts. This should 256256be the last command executed before leaving an ISR defined with the ISR_NAKED 257257attribute. 258258 259259This macro actually compiles into a single line of assembly, so there is 260260no function call overhead. 261261*/ 262 262# define reti() 263263#else /* !DOXYGEN */ 264264# define reti() __asm__ __volatile__ ("reti" ::) 265265#endif /* DOXYGEN */ 266266 267267#if defined(__DOXYGEN__) 268268/** \def BADISR_vect 269269\ingroup avr_interrupts 270270 271271\code #include <avr/interrupt.h> \endcode 272272 273273This is a vector which is aliased to __vector_default, the vector 274274executed when an ISR fires with no accompanying ISR handler. This 275275may be used along with the ISR() macro to create a catch-all for 276276undefined but used ISRs for debugging purposes. 277277*/ 278 278# define BADISR_vect 279279#else /* !DOXYGEN */ 280280# define BADISR_vect __vector_default 281281#endif /* DOXYGEN */ 282282 283283/** \name ISR attributes */ 284284 285285#if defined(__DOXYGEN__) 286286/** \def ISR_BLOCK 287287\ingroup avr_interrupts 288288 289289Identical to an ISR with no attributes specified. Global 290290interrupts are initially disabled by the AVR hardware when 291291entering the ISR, without the compiler modifying this state. 292292 293293Use this attribute in the attributes parameter of the ISR macro. 294294*/ 295 295# define ISR_BLOCK 296296 297297/** \def ISR_NOBLOCK 298298\ingroup avr_interrupts 299299 300300ISR runs with global interrupts initially enabled. The interrupt 301301enable flag is activated by the compiler as early as possible 302302within the ISR to ensure minimal processing delay for nested 303303interrupts. 304304 305305This may be used to create nested ISRs, however care should be 306306taken to avoid stack overflows, or to avoid infinitely entering 307307the ISR for those cases where the AVR hardware does not clear the 308308respective interrupt flag before entering the ISR. 309309 310310Use this attribute in the attributes parameter of the ISR macro. 311311*/ 312 312# define ISR_NOBLOCK 313313 314314/** \def ISR_NAKED 315315\ingroup avr_interrupts 316316 317317ISR is created with no prologue or epilogue code. The user code is 318318responsible for preservation of the machine state including the 319319SREG register, as well as placing a reti() at the end of the 320320interrupt routine. 321321 322322Use this attribute in the attributes parameter of the ISR macro. 323323*/ 324 324# define ISR_NAKED 325325 326326/** \def ISR_ALIASOF(target_vector) 327327\ingroup avr_interrupts 328328 329329The ISR is linked to another ISR, specified by the vect parameter. 330330This is compatible with GCC 4.2 and greater only. 331331 332332Use this attribute in the attributes parameter of the ISR macro. 333333*/ 334 334# define ISR_ALIASOF(target_vector) 335335#else /* !DOXYGEN */ 336336# define ISR_BLOCK 337337# define ISR_NOBLOCK __attribute__((interrupt)) 338338# define ISR_NAKED __attribute__((naked)) 339339# define ISR_ALIASOF(v) __attribute__((alias(__STRINGIFY(v)))) 340340#endif /* DOXYGEN */ 341341 342342/* \@} */ 343343 344344#endif

io.h