~ubuntu-branches/ubuntu/precise/arduino/precise

« back to all changes in this revision

Viewing changes to hardware/arduino/cores/arduino/WInterrupts.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Howard
  • Date: 2010-04-13 22:32:24 UTC
  • Revision ID: james.westby@ubuntu.com-20100413223224-jduxnd0xxnkkda02
Tags: upstream-0018+dfsg
ImportĀ upstreamĀ versionĀ 0018+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
*/
 
25
 
 
26
#include <inttypes.h>
 
27
#include <avr/io.h>
 
28
#include <avr/interrupt.h>
 
29
#include <avr/pgmspace.h>
 
30
#include <stdio.h>
 
31
 
 
32
#include "WConstants.h"
 
33
#include "wiring_private.h"
 
34
 
 
35
volatile static voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
 
36
// volatile static voidFuncPtr twiIntFunc;
 
37
 
 
38
#if defined(__AVR_ATmega8__)
 
39
#define EICRA MCUCR
 
40
#define EIMSK GICR
 
41
#endif
 
42
 
 
43
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
 
44
  if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
 
45
    intFunc[interruptNum] = userFunc;
 
46
    
 
47
    // Configure the interrupt mode (trigger on low input, any change, rising
 
48
    // edge, or falling edge).  The mode constants were chosen to correspond
 
49
    // to the configuration bits in the hardware register, so we simply shift
 
50
    // the mode into place.
 
51
      
 
52
    // Enable the interrupt.
 
53
      
 
54
    switch (interruptNum) {
 
55
#if defined(__AVR_ATmega1280__)
 
56
    case 2:
 
57
      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
 
58
      EIMSK |= (1 << INT0);
 
59
      break;
 
60
    case 3:
 
61
      EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
 
62
      EIMSK |= (1 << INT1);
 
63
      break;
 
64
    case 4:
 
65
      EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
 
66
      EIMSK |= (1 << INT2);
 
67
      break;
 
68
    case 5:
 
69
      EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
 
70
      EIMSK |= (1 << INT3);
 
71
      break;
 
72
    case 0:
 
73
      EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
 
74
      EIMSK |= (1 << INT4);
 
75
      break;
 
76
    case 1:
 
77
      EICRB = (EICRB & ~((1 << ISC50) | (1 << ISC51))) | (mode << ISC50);
 
78
      EIMSK |= (1 << INT5);
 
79
      break;
 
80
    case 6:
 
81
      EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
 
82
      EIMSK |= (1 << INT6);
 
83
      break;
 
84
    case 7:
 
85
      EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
 
86
      EIMSK |= (1 << INT7);
 
87
      break;
 
88
#else
 
89
    case 0:
 
90
      EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
 
91
      EIMSK |= (1 << INT0);
 
92
      break;
 
93
    case 1:
 
94
      EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
 
95
      EIMSK |= (1 << INT1);
 
96
      break;
 
97
#endif
 
98
    }
 
99
  }
 
100
}
 
101
 
 
102
void detachInterrupt(uint8_t interruptNum) {
 
103
  if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
 
104
    // Disable the interrupt.  (We can't assume that interruptNum is equal
 
105
    // to the number of the EIMSK bit to clear, as this isn't true on the 
 
106
    // ATmega8.  There, INT0 is 6 and INT1 is 7.)
 
107
    switch (interruptNum) {
 
108
#if defined(__AVR_ATmega1280__)
 
109
    case 2:
 
110
      EIMSK &= ~(1 << INT0);
 
111
      break;
 
112
    case 3:
 
113
      EIMSK &= ~(1 << INT1);
 
114
      break;
 
115
    case 4:
 
116
      EIMSK &= ~(1 << INT2);
 
117
      break;
 
118
    case 5:
 
119
      EIMSK &= ~(1 << INT3);
 
120
      break;
 
121
    case 0:
 
122
      EIMSK &= ~(1 << INT4);
 
123
      break;
 
124
    case 1:
 
125
      EIMSK &= ~(1 << INT5);
 
126
      break;
 
127
    case 6:
 
128
      EIMSK &= ~(1 << INT6);
 
129
      break;
 
130
    case 7:
 
131
      EIMSK &= ~(1 << INT7);
 
132
      break;
 
133
#else
 
134
    case 0:
 
135
      EIMSK &= ~(1 << INT0);
 
136
      break;
 
137
    case 1:
 
138
      EIMSK &= ~(1 << INT1);
 
139
      break;
 
140
#endif
 
141
    }
 
142
      
 
143
    intFunc[interruptNum] = 0;
 
144
  }
 
145
}
 
146
 
 
147
/*
 
148
void attachInterruptTwi(void (*userFunc)(void) ) {
 
149
  twiIntFunc = userFunc;
 
150
}
 
151
*/
 
152
 
 
153
#if defined(__AVR_ATmega1280__)
 
154
 
 
155
SIGNAL(INT0_vect) {
 
156
  if(intFunc[EXTERNAL_INT_2])
 
157
    intFunc[EXTERNAL_INT_2]();
 
158
}
 
159
 
 
160
SIGNAL(INT1_vect) {
 
161
  if(intFunc[EXTERNAL_INT_3])
 
162
    intFunc[EXTERNAL_INT_3]();
 
163
}
 
164
 
 
165
SIGNAL(INT2_vect) {
 
166
  if(intFunc[EXTERNAL_INT_4])
 
167
    intFunc[EXTERNAL_INT_4]();
 
168
}
 
169
 
 
170
SIGNAL(INT3_vect) {
 
171
  if(intFunc[EXTERNAL_INT_5])
 
172
    intFunc[EXTERNAL_INT_5]();
 
173
}
 
174
 
 
175
SIGNAL(INT4_vect) {
 
176
  if(intFunc[EXTERNAL_INT_0])
 
177
    intFunc[EXTERNAL_INT_0]();
 
178
}
 
179
 
 
180
SIGNAL(INT5_vect) {
 
181
  if(intFunc[EXTERNAL_INT_1])
 
182
    intFunc[EXTERNAL_INT_1]();
 
183
}
 
184
 
 
185
SIGNAL(INT6_vect) {
 
186
  if(intFunc[EXTERNAL_INT_6])
 
187
    intFunc[EXTERNAL_INT_6]();
 
188
}
 
189
 
 
190
SIGNAL(INT7_vect) {
 
191
  if(intFunc[EXTERNAL_INT_7])
 
192
    intFunc[EXTERNAL_INT_7]();
 
193
}
 
194
 
 
195
#else
 
196
 
 
197
SIGNAL(INT0_vect) {
 
198
  if(intFunc[EXTERNAL_INT_0])
 
199
    intFunc[EXTERNAL_INT_0]();
 
200
}
 
201
 
 
202
SIGNAL(INT1_vect) {
 
203
  if(intFunc[EXTERNAL_INT_1])
 
204
    intFunc[EXTERNAL_INT_1]();
 
205
}
 
206
 
 
207
#endif
 
208
 
 
209
/*
 
210
SIGNAL(SIG_2WIRE_SERIAL) {
 
211
  if(twiIntFunc)
 
212
    twiIntFunc();
 
213
}
 
214
*/
 
215