Debian Codegraph @

cores/arduino/WInterrupts.c

raw
1 /* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
3 /*
4 Part of the Wiring project - http://wiring.uniandes.edu.co
5
6 Copyright (c) 2004-05 Hernando Barragan
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General
19 Public License along with this library; if not, write to the
20 Free Software Foundation, Inc., 59 Temple Place, Suite 330,
21 Boston, MA 02111-1307 USA
22
23 Modified 24 November 2006 by David A. Mellis
24 Modified 1 August 2010 by Mark Sproul
25 */
26
27 #include <inttypes.h>
28 #include <avr/io.h>
29 #include <avr/interrupt.h>
30 #include <avr/pgmspace.h>
31 #include <stdio.h>
32
33 #include "wiring_private.h"
34
35 static void nothing(void) {
36 }
37
38 static volatile voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS] = {
39 #if EXTERNAL_NUM_INTERRUPTS > 8
40 #warning There are more than 8 external interrupts. Some callbacks may not be initialized.
41 nothing,
42 #endif
43 #if EXTERNAL_NUM_INTERRUPTS > 7
44 nothing,
45 #endif
46 #if EXTERNAL_NUM_INTERRUPTS > 6
47 nothing,
48 #endif
49 #if EXTERNAL_NUM_INTERRUPTS > 5
50 nothing,
51 #endif
52 #if EXTERNAL_NUM_INTERRUPTS > 4
53 nothing,
54 #endif
55 #if EXTERNAL_NUM_INTERRUPTS > 3
56 nothing,
57 #endif
58 #if EXTERNAL_NUM_INTERRUPTS > 2
59 nothing,
60 #endif
61 #if EXTERNAL_NUM_INTERRUPTS > 1
62 nothing,
63 #endif
64 #if EXTERNAL_NUM_INTERRUPTS > 0
65 nothing,
66 #endif
67 };
68
69 void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
70 if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
71 intFunc[interruptNum] = userFunc;
72
73 // Configure the interrupt mode (trigger on low input, any change, rising
74 // edge, or falling edge). The mode constants were chosen to correspond
75 // to the configuration bits in the hardware register, so we simply shift
76 // the mode into place.
77
78 // Enable the interrupt.
79
80 switch (interruptNum) {
81 #if defined(__AVR_ATmega32U4__)
82 // I hate doing this, but the register assignment differs between the 1280/2560
83 // and the 32U4. Since avrlib defines registers PCMSK1 and PCMSK2 that aren't
84 // even present on the 32U4 this is the only way to distinguish between them.
85 case 0:
86 EICRA = (EICRA & ~((1<<ISC00) | (1<<ISC01))) | (mode << ISC00);
87 EIMSK |= (1<<INT0);
88 break;
89 case 1:
90 EICRA = (EICRA & ~((1<<ISC10) | (1<<ISC11))) | (mode << ISC10);
91 EIMSK |= (1<<INT1);
92 break;
93 case 2:
94 EICRA = (EICRA & ~((1<<ISC20) | (1<<ISC21))) | (mode << ISC20);
95 EIMSK |= (1<<INT2);
96 break;
97 case 3:
98 EICRA = (EICRA & ~((1<<ISC30) | (1<<ISC31))) | (mode << ISC30);
99 EIMSK |= (1<<INT3);
100 break;
101 case 4:
102 EICRB = (EICRB & ~((1<<ISC60) | (1<<ISC61))) | (mode << ISC60);
103 EIMSK |= (1<<INT6);
104 break;
105 #elif defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__)
106 case 0:
107 EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
108 EIMSK |= (1 << INT0);
109 break;
110 case 1:
111 EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
112 EIMSK |= (1 << INT1);
113 break;
114 case 2:
115 EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
116 EIMSK |= (1 << INT2);
117 break;
118 case 3:
119 EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
120 EIMSK |= (1 << INT3);
121 break;
122 case 4:
123 EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
124 EIMSK |= (1 << INT4);
125 break;
126 case 5:
127 EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
128 EIMSK |= (1 << INT5);
129 break;
130 case 6:
131 EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
132 EIMSK |= (1 << INT6);
133 break;
134 case 7:
135 EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
136 EIMSK |= (1 << INT7);
137 break;
138 #elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
139 case 2:
140 EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
141 EIMSK |= (1 << INT0);
142 break;
143 case 3:
144 EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
145 EIMSK |= (1 << INT1);
146 break;
147 case 4:
148 EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
149 EIMSK |= (1 << INT2);
150 break;
151 case 5:
152 EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
153 EIMSK |= (1 << INT3);
154 break;
155 case 0:
156 EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
157 EIMSK |= (1 << INT4);
158 break;
159 case 1:
160 EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
161 EIMSK |= (1 << INT5);
162 break;
163 case 6:
164 EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
165 EIMSK |= (1 << INT6);
166 break;
167 case 7:
168 EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
169 EIMSK |= (1 << INT7);
170 break;
171 #else
172 case 0:
173 #if defined(EICRA) && defined(ISC00) && defined(EIMSK)
174 EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
175 EIMSK |= (1 << INT0);
176 #elif defined(MCUCR) && defined(ISC00) && defined(GICR)
177 MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
178 GICR |= (1 << INT0);
179 #elif defined(MCUCR) && defined(ISC00) && defined(GIMSK)
180 MCUCR = (MCUCR & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
181 GIMSK |= (1 << INT0);
182 #else
183 #error attachInterrupt not finished for this CPU (case 0)
184 #endif
185 break;
186
187 case 1:
188 #if defined(EICRA) && defined(ISC10) && defined(ISC11) && defined(EIMSK)
189 EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
190 EIMSK |= (1 << INT1);
191 #elif defined(MCUCR) && defined(ISC10) && defined(ISC11) && defined(GICR)
192 MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
193 GICR |= (1 << INT1);
194 #elif defined(MCUCR) && defined(ISC10) && defined(GIMSK) && defined(GIMSK)
195 MCUCR = (MCUCR & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
196 GIMSK |= (1 << INT1);
197 #else
198 #warning attachInterrupt may need some more work for this cpu (case 1)
199 #endif
200 break;
201
202 case 2:
203 #if defined(EICRA) && defined(ISC20) && defined(ISC21) && defined(EIMSK)
204 EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
205 EIMSK |= (1 << INT2);
206 #elif defined(MCUCR) && defined(ISC20) && defined(ISC21) && defined(GICR)
207 MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
208 GICR |= (1 << INT2);
209 #elif defined(MCUCR) && defined(ISC20) && defined(GIMSK) && defined(GIMSK)
210 MCUCR = (MCUCR & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
211 GIMSK |= (1 << INT2);
212 #endif
213 break;
214 #endif
215 }
216 }
217 }
218
219 void detachInterrupt(uint8_t interruptNum) {
220 if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
221 // Disable the interrupt. (We can't assume that interruptNum is equal
222 // to the number of the EIMSK bit to clear, as this isn't true on the
223 // ATmega8. There, INT0 is 6 and INT1 is 7.)
224 switch (interruptNum) {
225 #if defined(__AVR_ATmega32U4__)
226 case 0:
227 EIMSK &= ~(1<<INT0);
228 break;
229 case 1:
230 EIMSK &= ~(1<<INT1);
231 break;
232 case 2:
233 EIMSK &= ~(1<<INT2);
234 break;
235 case 3:
236 EIMSK &= ~(1<<INT3);
237 break;
238 case 4:
239 EIMSK &= ~(1<<INT6);
240 break;
241 #elif defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__)
242 case 0:
243 EIMSK &= ~(1 << INT0);
244 break;
245 case 1:
246 EIMSK &= ~(1 << INT1);
247 break;
248 case 2:
249 EIMSK &= ~(1 << INT2);
250 break;
251 case 3:
252 EIMSK &= ~(1 << INT3);
253 break;
254 case 4:
255 EIMSK &= ~(1 << INT4);
256 break;
257 case 5:
258 EIMSK &= ~(1 << INT5);
259 break;
260 case 6:
261 EIMSK &= ~(1 << INT6);
262 break;
263 case 7:
264 EIMSK &= ~(1 << INT7);
265 break;
266 #elif defined(EICRA) && defined(EICRB) && defined(EIMSK)
267 case 2:
268 EIMSK &= ~(1 << INT0);
269 break;
270 case 3:
271 EIMSK &= ~(1 << INT1);
272 break;
273 case 4:
274 EIMSK &= ~(1 << INT2);
275 break;
276 case 5:
277 EIMSK &= ~(1 << INT3);
278 break;
279 case 0:
280 EIMSK &= ~(1 << INT4);
281 break;
282 case 1:
283 EIMSK &= ~(1 << INT5);
284 break;
285 case 6:
286 EIMSK &= ~(1 << INT6);
287 break;
288 case 7:
289 EIMSK &= ~(1 << INT7);
290 break;
291 #else
292 case 0:
293 #if defined(EIMSK) && defined(INT0)
294 EIMSK &= ~(1 << INT0);
295 #elif defined(GICR) && defined(ISC00)
296 GICR &= ~(1 << INT0); // atmega32
297 #elif defined(GIMSK) && defined(INT0)
298 GIMSK &= ~(1 << INT0);
299 #else
300 #error detachInterrupt not finished for this cpu
301 #endif
302 break;
303
304 case 1:
305 #if defined(EIMSK) && defined(INT1)
306 EIMSK &= ~(1 << INT1);
307 #elif defined(GICR) && defined(INT1)
308 GICR &= ~(1 << INT1); // atmega32
309 #elif defined(GIMSK) && defined(INT1)
310 GIMSK &= ~(1 << INT1);
311 #else
312 #warning detachInterrupt may need some more work for this cpu (case 1)
313 #endif
314 break;
315
316 case 2:
317 #if defined(EIMSK) && defined(INT2)
318 EIMSK &= ~(1 << INT2);
319 #elif defined(GICR) && defined(INT2)
320 GICR &= ~(1 << INT2); // atmega32
321 #elif defined(GIMSK) && defined(INT2)
322 GIMSK &= ~(1 << INT2);
323 #elif defined(INT2)
324 #warning detachInterrupt may need some more work for this cpu (case 2)
325 #endif
326 break;
327 #endif
328 }
329
330 intFunc[interruptNum] = nothing;
331 }
332 }
333
334
335 #define IMPLEMENT_ISR(vect, interrupt) \
336 ISR(vect) { \
337 intFunc[interrupt](); \
338 }
339
340 #if defined(__AVR_ATmega32U4__)
341
342 IMPLEMENT_ISR(INT0_vect, EXTERNAL_INT_0)
343 IMPLEMENT_ISR(INT1_vect, EXTERNAL_INT_1)
344 IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_2)
345 IMPLEMENT_ISR(INT3_vect, EXTERNAL_INT_3)
346 IMPLEMENT_ISR(INT6_vect, EXTERNAL_INT_4)
347
348 #elif defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega8U2__)
349
350 IMPLEMENT_ISR(INT0_vect, EXTERNAL_INT_0)
351 IMPLEMENT_ISR(INT1_vect, EXTERNAL_INT_1)
352 IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_2)
353 IMPLEMENT_ISR(INT3_vect, EXTERNAL_INT_3)
354 IMPLEMENT_ISR(INT4_vect, EXTERNAL_INT_4)
355 IMPLEMENT_ISR(INT5_vect, EXTERNAL_INT_5)
356 IMPLEMENT_ISR(INT6_vect, EXTERNAL_INT_6)
357 IMPLEMENT_ISR(INT7_vect, EXTERNAL_INT_7)
358
359 #elif defined(EICRA) && defined(EICRB)
360
361 IMPLEMENT_ISR(INT0_vect, EXTERNAL_INT_2)
362 IMPLEMENT_ISR(INT1_vect, EXTERNAL_INT_3)
363 IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_4)
364 IMPLEMENT_ISR(INT3_vect, EXTERNAL_INT_5)
365 IMPLEMENT_ISR(INT4_vect, EXTERNAL_INT_0)
366 IMPLEMENT_ISR(INT5_vect, EXTERNAL_INT_1)
367 IMPLEMENT_ISR(INT6_vect, EXTERNAL_INT_6)
368 IMPLEMENT_ISR(INT7_vect, EXTERNAL_INT_7)
369
370 #else
371
372 IMPLEMENT_ISR(INT0_vect, EXTERNAL_INT_0)
373 IMPLEMENT_ISR(INT1_vect, EXTERNAL_INT_1)
374
375 #if defined(EICRA) && defined(ISC20)
376 IMPLEMENT_ISR(INT2_vect, EXTERNAL_INT_2)
377 #endif
378
379 #endif