1 | /* Copyright (c) 2002,2005,2007 Marek Michalkiewicz |
---|
2 | Copyright (c) 2007, Dean Camera |
---|
3 | |
---|
4 | All rights reserved. |
---|
5 | |
---|
6 | Redistribution and use in source and binary forms, with or without |
---|
7 | modification, are permitted provided that the following conditions are met: |
---|
8 | |
---|
9 | * Redistributions of source code must retain the above copyright |
---|
10 | notice, this list of conditions and the following disclaimer. |
---|
11 | |
---|
12 | * Redistributions in binary form must reproduce the above copyright |
---|
13 | notice, this list of conditions and the following disclaimer in |
---|
14 | the documentation and/or other materials provided with the |
---|
15 | distribution. |
---|
16 | |
---|
17 | * Neither the name of the copyright holders nor the names of |
---|
18 | contributors may be used to endorse or promote products derived |
---|
19 | from this software without specific prior written permission. |
---|
20 | |
---|
21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
22 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
23 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
25 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
31 | POSSIBILITY OF SUCH DAMAGE. */ |
---|
32 | |
---|
33 | /* $Id$ */ |
---|
34 | |
---|
35 | #ifndef _AVR_INTERRUPT_H_ |
---|
36 | #define _AVR_INTERRUPT_H_ |
---|
37 | |
---|
38 | #include <avr/io.h> |
---|
39 | |
---|
40 | #if !defined(__DOXYGEN__) && !defined(__STRINGIFY) |
---|
41 | /* Auxiliary macro for ISR_ALIAS(). */ |
---|
42 | #define __STRINGIFY(x) #x |
---|
43 | #endif /* !defined(__DOXYGEN__) */ |
---|
44 | |
---|
45 | /** |
---|
46 | \file |
---|
47 | \@{ |
---|
48 | */ |
---|
49 | |
---|
50 | |
---|
51 | /** \name Global manipulation of the interrupt flag |
---|
52 | |
---|
53 | The global interrupt flag is maintained in the I bit of the status |
---|
54 | register (SREG). |
---|
55 | */ |
---|
56 | |
---|
57 | #if defined(__DOXYGEN__) |
---|
58 | /** \def sei() |
---|
59 | \ingroup avr_interrupts |
---|
60 | |
---|
61 | \code #include <avr/interrupt.h> \endcode |
---|
62 | |
---|
63 | Enables interrupts by setting the global interrupt mask. This function |
---|
64 | actually compiles into a single line of assembly, so there is no function |
---|
65 | call overhead. */ |
---|
66 | #define sei() |
---|
67 | #else /* !DOXYGEN */ |
---|
68 | # define sei() __asm__ __volatile__ ("sei" ::) |
---|
69 | #endif /* DOXYGEN */ |
---|
70 | |
---|
71 | #if defined(__DOXYGEN__) |
---|
72 | /** \def cli() |
---|
73 | \ingroup avr_interrupts |
---|
74 | |
---|
75 | \code #include <avr/interrupt.h> \endcode |
---|
76 | |
---|
77 | Disables all interrupts by clearing the global interrupt mask. This function |
---|
78 | actually compiles into a single line of assembly, so there is no function |
---|
79 | call overhead. */ |
---|
80 | #define cli() |
---|
81 | #else /* !DOXYGEN */ |
---|
82 | # define cli() __asm__ __volatile__ ("cli" ::) |
---|
83 | #endif /* DOXYGEN */ |
---|
84 | |
---|
85 | |
---|
86 | /** \name Macros for writing interrupt handler functions */ |
---|
87 | |
---|
88 | |
---|
89 | #if defined(__DOXYGEN__) |
---|
90 | /** \def ISR(vector [, attributes]) |
---|
91 | \ingroup avr_interrupts |
---|
92 | |
---|
93 | \code #include <avr/interrupt.h> \endcode |
---|
94 | |
---|
95 | Introduces an interrupt handler function (interrupt service |
---|
96 | routine) that runs with global interrupts initially disabled |
---|
97 | by default with no attributes specified. |
---|
98 | |
---|
99 | The attributes are optional and alter the behaviour and resultant |
---|
100 | generated code of the interrupt routine. Multiple attributes may |
---|
101 | be used for a single function, with a space seperating each |
---|
102 | attribute. |
---|
103 | |
---|
104 | Valid attributes are ISR_BLOCK, ISR_NOBLOCK, ISR_NAKED and |
---|
105 | ISR_ALIASOF(vect). |
---|
106 | |
---|
107 | \c vector must be one of the interrupt vector names that are |
---|
108 | valid for the particular MCU type. |
---|
109 | */ |
---|
110 | # define ISR(vector, [attributes]) |
---|
111 | #else /* real code */ |
---|
112 | |
---|
113 | #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 1) || (__GNUC__ > 4) |
---|
114 | # define __INTR_ATTRS used, externally_visible |
---|
115 | #else /* GCC < 4.1 */ |
---|
116 | # define __INTR_ATTRS used |
---|
117 | #endif |
---|
118 | |
---|
119 | #ifdef __cplusplus |
---|
120 | # define ISR(vector, ...) \ |
---|
121 | extern "C" void vector (void) __attribute__ ((signal,__INTR_ATTRS)) __VA_ARGS__; \ |
---|
122 | void vector (void) |
---|
123 | #else |
---|
124 | # define ISR(vector, ...) \ |
---|
125 | void vector (void) __attribute__ ((signal,__INTR_ATTRS)) __VA_ARGS__; \ |
---|
126 | void vector (void) |
---|
127 | #endif |
---|
128 | |
---|
129 | #endif /* DOXYGEN */ |
---|
130 | |
---|
131 | #if defined(__DOXYGEN__) |
---|
132 | /** \def SIGNAL(vector) |
---|
133 | \ingroup avr_interrupts |
---|
134 | |
---|
135 | \code #include <avr/interrupt.h> \endcode |
---|
136 | |
---|
137 | Introduces an interrupt handler function that runs with global interrupts |
---|
138 | initially disabled. |
---|
139 | |
---|
140 | This is the same as the ISR macro without optional attributes. |
---|
141 | \deprecated Do not use SIGNAL() in new code. Use ISR() instead. |
---|
142 | */ |
---|
143 | # define SIGNAL(vector) |
---|
144 | #else /* real code */ |
---|
145 | |
---|
146 | #ifdef __cplusplus |
---|
147 | # define SIGNAL(vector) \ |
---|
148 | extern "C" void vector(void) __attribute__ ((signal, __INTR_ATTRS)); \ |
---|
149 | void vector (void) |
---|
150 | #else |
---|
151 | # define SIGNAL(vector) \ |
---|
152 | void vector (void) __attribute__ ((signal, __INTR_ATTRS)); \ |
---|
153 | void vector (void) |
---|
154 | #endif |
---|
155 | |
---|
156 | #endif /* DOXYGEN */ |
---|
157 | |
---|
158 | #if defined(__DOXYGEN__) |
---|
159 | /** \def EMPTY_INTERRUPT(vector) |
---|
160 | \ingroup avr_interrupts |
---|
161 | |
---|
162 | \code #include <avr/interrupt.h> \endcode |
---|
163 | |
---|
164 | Defines an empty interrupt handler function. This will not generate |
---|
165 | any prolog or epilog code and will only return from the ISR. Do not |
---|
166 | define a function body as this will define it for you. |
---|
167 | Example: |
---|
168 | \code EMPTY_INTERRUPT(ADC_vect);\endcode */ |
---|
169 | # define EMPTY_INTERRUPT(vector) |
---|
170 | #else /* real code */ |
---|
171 | |
---|
172 | #ifdef __cplusplus |
---|
173 | # define EMPTY_INTERRUPT(vector) \ |
---|
174 | extern "C" void vector(void) __attribute__ ((signal,naked,__INTR_ATTRS)); \ |
---|
175 | void vector (void) { __asm__ __volatile__ ("reti" ::); } |
---|
176 | #else |
---|
177 | # define EMPTY_INTERRUPT(vector) \ |
---|
178 | void vector (void) __attribute__ ((signal,naked,__INTR_ATTRS)); \ |
---|
179 | void vector (void) { __asm__ __volatile__ ("reti" ::); } |
---|
180 | #endif |
---|
181 | |
---|
182 | #endif /* DOXYGEN */ |
---|
183 | |
---|
184 | #if defined(__DOXYGEN__) |
---|
185 | /** \def ISR_ALIAS(vector, target_vector) |
---|
186 | \ingroup avr_interrupts |
---|
187 | |
---|
188 | \code #include <avr/interrupt.h> \endcode |
---|
189 | |
---|
190 | Aliases a given vector to another one in the same manner as the |
---|
191 | ISR_ALIASOF attribute for the ISR() macro. Unlike the ISR_ALIASOF |
---|
192 | attribute macro however, this is compatible for all versions of |
---|
193 | GCC rather than just GCC version 4.2 onwards. |
---|
194 | |
---|
195 | \note This macro creates a trampoline function for the aliased |
---|
196 | macro. This will result in a two cycle penalty for the aliased |
---|
197 | vector compared to the ISR the vector is aliased to, due to the |
---|
198 | JMP/RJMP opcode used. |
---|
199 | |
---|
200 | \deprecated |
---|
201 | For new code, the use of ISR(..., ISR_ALIASOF(...)) is |
---|
202 | recommended. |
---|
203 | |
---|
204 | Example: |
---|
205 | \code |
---|
206 | ISR(INT0_vect) |
---|
207 | { |
---|
208 | PORTB = 42; |
---|
209 | } |
---|
210 | |
---|
211 | ISR_ALIAS(INT1_vect, INT0_vect); |
---|
212 | \endcode |
---|
213 | */ |
---|
214 | # define ISR_ALIAS(vector, target_vector) |
---|
215 | #else /* real code */ |
---|
216 | |
---|
217 | #ifdef __cplusplus |
---|
218 | # if defined(__AVR_MEGA__) && __AVR_MEGA__ |
---|
219 | # define ISR_ALIAS(vector, tgt) extern "C" void vector (void) \ |
---|
220 | __attribute__((signal, naked, __INTR_ATTRS)); \ |
---|
221 | void vector (void) { asm volatile ("jmp " __STRINGIFY(tgt) ::); } |
---|
222 | # else /* !__AVR_MEGA */ |
---|
223 | # define ISR_ALIAS(vector, tgt) extern "C" void vector (void) \ |
---|
224 | __attribute__((signal, naked, __INTR_ATTRS)); \ |
---|
225 | void vector (void) { asm volatile ("rjmp " __STRINGIFY(tgt) ::); } |
---|
226 | # endif /* __AVR_MEGA__ */ |
---|
227 | #else /* !__cplusplus */ |
---|
228 | # if defined(__AVR_MEGA__) && __AVR_MEGA__ |
---|
229 | # define ISR_ALIAS(vector, tgt) void vector (void) \ |
---|
230 | __attribute__((signal, naked, __INTR_ATTRS)); \ |
---|
231 | void vector (void) { asm volatile ("jmp " __STRINGIFY(tgt) ::); } |
---|
232 | # else /* !__AVR_MEGA */ |
---|
233 | # define ISR_ALIAS(vector, tgt) void vector (void) \ |
---|
234 | __attribute__((signal, naked, __INTR_ATTRS)); \ |
---|
235 | void vector (void) { asm volatile ("rjmp " __STRINGIFY(tgt) ::); } |
---|
236 | # endif /* __AVR_MEGA__ */ |
---|
237 | #endif /* __cplusplus */ |
---|
238 | |
---|
239 | #endif /* DOXYGEN */ |
---|
240 | |
---|
241 | #if defined(__DOXYGEN__) |
---|
242 | /** \def reti() |
---|
243 | \ingroup avr_interrupts |
---|
244 | |
---|
245 | \code #include <avr/interrupt.h> \endcode |
---|
246 | |
---|
247 | Returns from an interrupt routine, enabling global interrupts. This should |
---|
248 | be the last command executed before leaving an ISR defined with the ISR_NAKED |
---|
249 | attribute. |
---|
250 | |
---|
251 | This macro actually compiles into a single line of assembly, so there is |
---|
252 | no function call overhead. |
---|
253 | */ |
---|
254 | # define reti() |
---|
255 | #else /* !DOXYGEN */ |
---|
256 | # define reti() __asm__ __volatile__ ("reti" ::) |
---|
257 | #endif /* DOXYGEN */ |
---|
258 | |
---|
259 | #if defined(__DOXYGEN__) |
---|
260 | /** \def BADISR_vect |
---|
261 | \ingroup avr_interrupts |
---|
262 | |
---|
263 | \code #include <avr/interrupt.h> \endcode |
---|
264 | |
---|
265 | This is a vector which is aliased to __vector_default, the vector |
---|
266 | executed when an ISR fires with no accompanying ISR handler. This |
---|
267 | may be used along with the ISR() macro to create a catch-all for |
---|
268 | undefined but used ISRs for debugging purposes. |
---|
269 | */ |
---|
270 | # define BADISR_vect |
---|
271 | #else /* !DOXYGEN */ |
---|
272 | # define BADISR_vect __vector_default |
---|
273 | #endif /* DOXYGEN */ |
---|
274 | |
---|
275 | /** \name ISR attributes */ |
---|
276 | |
---|
277 | #if defined(__DOXYGEN__) |
---|
278 | /** \def ISR_BLOCK |
---|
279 | \ingroup avr_interrupts |
---|
280 | |
---|
281 | \code# include <avr/interrupt.h> \endcode |
---|
282 | |
---|
283 | Identical to an ISR with no attributes specified. Global |
---|
284 | interrupts are initially disabled by the AVR hardware when |
---|
285 | entering the ISR, without the compiler modifying this state. |
---|
286 | |
---|
287 | Use this attribute in the attributes parameter of the ISR macro. |
---|
288 | */ |
---|
289 | # define ISR_BLOCK |
---|
290 | |
---|
291 | /** \def ISR_NOBLOCK |
---|
292 | \ingroup avr_interrupts |
---|
293 | |
---|
294 | \code# include <avr/interrupt.h> \endcode |
---|
295 | |
---|
296 | ISR runs with global interrupts initially enabled. The interrupt |
---|
297 | enable flag is activated by the compiler as early as possible |
---|
298 | within the ISR to ensure minimal processing delay for nested |
---|
299 | interrupts. |
---|
300 | |
---|
301 | This may be used to create nested ISRs, however care should be |
---|
302 | taken to avoid stack overflows, or to avoid infinitely entering |
---|
303 | the ISR for those cases where the AVR hardware does not clear the |
---|
304 | respective interrupt flag before entering the ISR. |
---|
305 | |
---|
306 | Use this attribute in the attributes parameter of the ISR macro. |
---|
307 | */ |
---|
308 | # define ISR_NOBLOCK |
---|
309 | |
---|
310 | /** \def ISR_NAKED |
---|
311 | \ingroup avr_interrupts |
---|
312 | |
---|
313 | \code# include <avr/interrupt.h> \endcode |
---|
314 | |
---|
315 | ISR is created with no prologue or epilogue code. The user code is |
---|
316 | responsible for preservation of the machine state including the |
---|
317 | SREG register, as well as placing a reti() at the end of the |
---|
318 | interrupt routine. |
---|
319 | |
---|
320 | Use this attribute in the attributes parameter of the ISR macro. |
---|
321 | */ |
---|
322 | # define ISR_NAKED |
---|
323 | |
---|
324 | /** \def ISR_ALIASOF(target_vector) |
---|
325 | \ingroup avr_interrupts |
---|
326 | |
---|
327 | \code#include <avr/interrupt.h>\endcode |
---|
328 | |
---|
329 | The ISR is linked to another ISR, specified by the vect parameter. |
---|
330 | This is compatible with GCC 4.2 and greater only. |
---|
331 | |
---|
332 | Use this attribute in the attributes parameter of the ISR macro. |
---|
333 | */ |
---|
334 | # define ISR_ALIASOF(target_vector) |
---|
335 | #else /* !DOXYGEN */ |
---|
336 | # define ISR_BLOCK |
---|
337 | # define ISR_NOBLOCK __attribute__((interrupt)) |
---|
338 | # define ISR_NAKED __attribute__((naked)) |
---|
339 | # define ISR_ALIASOF(v) __attribute__((alias(__STRINGIFY(v)))) |
---|
340 | #endif /* DOXYGEN */ |
---|
341 | |
---|
342 | /* \@} */ |
---|
343 | |
---|
344 | #endif |
---|