COSA
An Object-Oriented Platform for Arduino Programming
ExternalInterrupt.cpp
Go to the documentation of this file.
1 
22 
23 #if defined(BOARD_ATMEGA328P)
24 
27  InterruptMode mode,
28  bool pullup) :
29  IOPin((Board::DigitalPin) pin, INPUT_MODE, pullup),
30  m_ix(pin - Board::EXT0)
31 {
32  ext[m_ix] = this;
33  uint8_t ix = (m_ix << 1);
34  bit_field_set(EICRA, 0b11 << ix, mode << ix);
35 }
36 
37 #elif defined(BOARD_ATMEGA32U4) || defined(BOARD_AT90USB1286)
38 
41  InterruptMode mode,
42  bool pullup) :
43  IOPin((Board::DigitalPin) pin, INPUT_MODE, pullup),
44  m_ix(pin - Board::EXT0)
45 {
46  ext[m_ix] = this;
47  uint8_t ix = (m_ix << 1);
48  bit_field_set(EICRA, 0b11 << ix, mode << ix);
49 }
50 
51 #elif defined(BOARD_ATMEGA2560)
52 
55  InterruptMode mode,
56  bool pullup) :
57  IOPin((Board::DigitalPin) pin, INPUT_MODE, pullup),
58  m_ix(0)
59 {
60  if (pin <= Board::EXT5) {
61  m_ix = pin - Board::EXT4;
62  uint8_t ix = (m_ix << 1);
63  bit_field_set(EICRB, 0b11 << ix, mode << ix);
64  m_ix += 4;
65  }
66  else {
67  m_ix = pin - Board::EXT0;
68  uint8_t ix = (m_ix << 1);
69  bit_field_set(EICRA, 0b11 << ix, mode << ix);
70  }
71  ext[m_ix] = this;
72 }
73 
74 #elif defined(BOARD_ATMEGA1248P)
75 
78  InterruptMode mode,
79  bool pullup) :
80  IOPin((Board::DigitalPin) pin, INPUT_MODE, pullup),
81  m_ix(0)
82 {
83  if (pin == Board::EXT2) {
84  m_ix = 2;
85  } else {
86  m_ix = pin - Board::EXT0;
87  }
88  uint8_t ix = (m_ix << 1);
89  bit_field_set(EICRA, 0b11 << ix, mode << ix);
90  ext[m_ix] = this;
91 }
92 
93 #elif defined(BOARD_ATMEGA256RFR2)
94 
97  InterruptMode mode,
98  bool pullup) :
99  IOPin((Board::DigitalPin) pin, INPUT_MODE, pullup),
100  m_ix(0)
101 {
102  if (pin <= Board::EXT3) {
103  m_ix = pin - Board::EXT0;
104  uint8_t ix = (m_ix << 1);
105  bit_field_set(EICRA, 0b11 << ix, mode << ix);
106  }
107  else {
108  m_ix = pin - Board::EXT4;
109  uint8_t ix = (m_ix << 1);
110  bit_field_set(EICRB, 0b11 << ix, mode << ix);
111  m_ix += 4;
112  }
113  ext[m_ix] = this;
114 }
115 
116 #elif defined(BOARD_ATTINYX61)
117 
120  InterruptMode mode,
121  bool pullup) :
122  IOPin((Board::DigitalPin) pin, INPUT_MODE, pullup),
123  m_ix(pin == Board::EXT1)
124 {
125  ext[m_ix] = this;
126  uint8_t ix = (m_ix << 1);
127  bit_field_set(MCUCR, 0b11 << ix, mode << ix);
128 }
129 
130 void
132 {
133  synchronized {
134  bit_set(GIFR, INTF0 + m_ix);
135  bit_set(GIMSK, INT0 + m_ix);
136  }
137 }
138 
139 void
141 {
142  synchronized bit_clear(GIMSK, INT0 + m_ix);
143 }
144 
145 void
147 {
148  synchronized bit_set(GIFR, INTF0 + m_ix);
149 }
150 
151 #elif defined(BOARD_ATTINY)
152 
155  InterruptMode mode,
156  bool pullup) :
157  IOPin((Board::DigitalPin) pin, INPUT_MODE, pullup),
158  m_ix(0)
159 {
160  ext[m_ix] = this;
161  bit_field_set(MCUCR, 0b11, mode);
162 }
163 
164 void
166 {
167  synchronized {
168  bit_set(GIFR, INTF0);
169  bit_set(GIMSK, INT0);
170  }
171 }
172 
173 void
175 {
176  synchronized bit_clear(GIMSK, INT0);
177 }
178 
179 void
181 {
182  synchronized bit_set(GIFR, INTF0);
183 }
184 
185 #endif
186 
187 #if !defined(BOARD_ATTINY)
188 
189 void
191 {
192  synchronized {
193  bit_set(EIFR, m_ix);
194  bit_set(EIMSK, m_ix);
195  }
196 }
197 
198 void
200 {
201  synchronized bit_clear(EIMSK, m_ix);
202 }
203 
204 void
206 {
207  synchronized bit_clear(EIFR, m_ix);
208 }
209 #endif
210 
211 ExternalInterrupt* ExternalInterrupt::ext[Board::EXT_MAX] = { NULL };
212 
213 #define INT_ISR(nr) \
214 ISR(INT ## nr ## _vect) \
215 { \
216  if (ExternalInterrupt::ext[nr] != NULL) \
217  ExternalInterrupt::ext[nr]->on_interrupt(); \
218 }
219 
220 INT_ISR(0)
221 #if defined(INT1_vect)
222 INT_ISR(1)
223 #endif
224 #if defined(INT2_vect)
225 INT_ISR(2)
226 #endif
227 #if defined(INT3_vect)
228 INT_ISR(3)
229 #endif
230 #if defined(INT4_vect)
231 INT_ISR(4)
232 #endif
233 #if defined(INT5_vect)
234 INT_ISR(5)
235 #endif
236 #if defined(INT6_vect)
237 INT_ISR(6)
238 #endif
239 #if defined(INT7_vect)
240 INT_ISR(7)
241 #endif
#define bit_set(p, b)
Definition: Bits.h:35
#define NULL
Definition: Types.h:101
#define bit_clear(p, b)
Definition: Bits.h:36
virtual void disable()
#define INT_ISR(nr)
ExternalInterrupt(Board::ExternalInterruptPin pin, InterruptMode mode=ON_CHANGE_MODE, bool pullup=false)
ExternalInterruptPin
Definition: ATmega1284P.hh:190
Definition: IOPin.hh:29
#define bit_field_set(p, m, v)
Definition: Bits.h:40