00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include <inttypes.h>
00031 #include <avr/io.h>
00032 #include <avr/interrupt.h>
00033 #include <avr/signal.h>
00034 #include <avr/pgmspace.h>
00035 #include <stdio.h>
00036
00037 #include "WaspVariables.h"
00038 #include "WConstants.h"
00039 #ifndef __WASPCONSTANTS_H__
00040 #include "WaspConstants.h"
00041 #endif
00042 #include "wiring_private.h"
00043
00044
00045 volatile static voidFuncPtr intFunc[EXTERNAL_NUM_INTERRUPTS];
00046 volatile static voidFuncPtr twiIntFunc;
00047
00048
00049 #if defined(__AVR_ATmega168__)
00050 #define MCUCR EICRA
00051 #define GICR EIMSK
00052 #endif
00053
00054
00055
00056
00057
00058
00059
00060 void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
00061 if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
00062 intFunc[interruptNum] = userFunc;
00063
00064 switch (interruptNum) {
00065 case 0:
00066
00067
00068
00069
00070
00071 EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
00072
00073
00074 EIMSK |= (1 << INT0);
00075 break;
00076 case 1:
00077
00078 EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
00079 EIMSK |= (1 << INT1);
00080 break;
00081 case 2:
00082
00083
00084 EICRA = (EICRA & ~((1 << ISC20) | (1 << ISC21))) | (mode << ISC20);
00085 EIMSK |= (1 << INT2);
00086 break;
00087 case 3:
00088
00089
00090 EICRA = (EICRA & ~((1 << ISC30) | (1 << ISC31))) | (mode << ISC30);
00091 EIMSK |= (1 << INT3);
00092 break;
00093 case 4:
00094
00095
00096
00097 EICRB = (EICRB & ~((1 << ISC40) | (1 << ISC41))) | (mode << ISC40);
00098 EIMSK |= (1 << INT4);
00099 break;
00100 case 5:
00101
00102
00103
00104
00105
00106 break;
00107 case 6:
00108
00109
00110
00111 EICRB = (EICRB & ~((1 << ISC60) | (1 << ISC61))) | (mode << ISC60);
00112 EIMSK |= (1 << INT6);
00113 break;
00114 case 7:
00115
00116
00117
00118 EICRB = (EICRB & ~((1 << ISC70) | (1 << ISC71))) | (mode << ISC70);
00119 EIMSK |= (1 << INT7);
00120 break;
00121 }
00122 }
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132 void detachInterrupt(uint8_t interruptNum) {
00133 cli();
00134 if(interruptNum < EXTERNAL_NUM_INTERRUPTS) {
00135 switch (interruptNum) {
00136 case 0:
00137
00138 EIMSK &= ~(1 << INT0);
00139 break;
00140 case 1:
00141 EIMSK &= ~(1 << INT1);
00142 break;
00143 case 2:
00144 EIMSK &= ~(1 << INT2);
00145 break;
00146 case 3:
00147 EIMSK &= ~(1 << INT3);
00148 break;
00149 case 4:
00150 EIMSK &= ~(1 << INT4);
00151 break;
00152 case 5:
00153
00154
00155
00156
00157 break;
00158 case 6:
00159 EIMSK &= ~(1 << INT6);
00160 break;
00161 case 7:
00162 EIMSK &= ~(1 << INT7);
00163 break;
00164 }
00165
00166 intFunc[interruptNum] = 0;
00167 }
00168 sei();
00169 }
00170
00171 void attachInterruptTwi(void (*userFunc)(void) ) {
00172 twiIntFunc = userFunc;
00173 }
00174
00175
00176 SIGNAL(SIG_INTERRUPT0) {
00177 if(intFunc[EXTERNAL_INT_0])
00178 intFunc[EXTERNAL_INT_0]();
00179 }
00180
00181 SIGNAL(SIG_INTERRUPT1) {
00182 if(intFunc[EXTERNAL_INT_1])
00183 intFunc[EXTERNAL_INT_1]();
00184 }
00185
00186 SIGNAL(SIG_INTERRUPT2) {
00187 if(intFunc[EXTERNAL_INT_2])
00188 intFunc[EXTERNAL_INT_2]();
00189 }
00190
00191 SIGNAL(SIG_INTERRUPT3) {
00192 if(intFunc[EXTERNAL_INT_3])
00193 intFunc[EXTERNAL_INT_3]();
00194 }
00195
00196 SIGNAL(SIG_INTERRUPT4) {
00197 if(intFunc[EXTERNAL_INT_4])
00198 intFunc[EXTERNAL_INT_4]();
00199 }
00200
00201 SIGNAL(SIG_INTERRUPT5) {
00202 if(intFunc[EXTERNAL_INT_5])
00203 intFunc[EXTERNAL_INT_5]();
00204 }
00205
00206 SIGNAL(SIG_INTERRUPT6) {
00207 if(intFunc[EXTERNAL_INT_6])
00208 intFunc[EXTERNAL_INT_6]();
00209 }
00210
00211 SIGNAL(SIG_INTERRUPT7) {
00212 if(intFunc[EXTERNAL_INT_7])
00213 intFunc[EXTERNAL_INT_7]();
00214 }
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 void onHAIwakeUP(void)
00237 {
00238 cli();
00239
00240 if( intConf & ACC_INT )
00241 {
00242 if( digitalRead(ACC_INT_PIN_MON) )
00243 {
00244 intCounter++;
00245 intFlag |= ACC_INT;
00246 intArray[ACC_POS]++;
00247 }
00248 }
00249
00250 if( intConf & RTC_INT )
00251 {
00252 if( digitalRead(RTC_INT_PIN_MON) )
00253 {
00254 intCounter++;
00255 intFlag |= RTC_INT;
00256 intArray[RTC_POS]++;
00257 }
00258 }
00259
00260 if( intConf & UART1_INT )
00261 {
00262 if( digitalRead(UART1_INT_PIN_MON) )
00263 {
00264 intCounter++;
00265 intFlag |= UART1_INT;
00266 intArray[UART1_POS]++;
00267 }
00268 }
00269
00270 if( intConf & HAI_INT )
00271 {
00272 if( digitalRead(HAI_INT_PIN_MON) )
00273 {
00274 intCounter++;
00275 intFlag |= HAI_INT;
00276 intArray[HAI_POS]++;
00277 }
00278 }
00279
00280 if( intConf & SENS_INT )
00281 {
00282 if( digitalRead(SENS_INT_PIN_MON) )
00283 {
00284 intCounter++;
00285 digitalWrite(SENS_INT_ENABLE,LOW);
00286 intFlag |= SENS_INT;
00287 intArray[SENS_POS]++;
00288 }
00289 }
00290
00291 if( intConf & ANE_INT )
00292 {
00293 if( !( (intConf & ACC_INT) && digitalRead(ACC_INT_PIN_MON) ) && !( (intConf & RTC_INT) && digitalRead(RTC_INT_PIN_MON) ) && !( (intConf & SENS_INT) && digitalRead(SENS_INT_PIN_MON) ) )
00294 {
00295 intCounter++;
00296 intFlag |= ANE_INT;
00297 intArray[SENS2_POS]++;
00298 }
00299 }
00300
00301 sei();
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318 void onLAIwakeUP(void)
00319 {
00320 cli();
00321
00322
00323 if( intConf & BAT_INT )
00324 {
00325 if( !digitalRead(BAT_INT_PIN_MON) )
00326 {
00327 intCounter++;
00328 intFlag |= BAT_INT;
00329 intArray[BAT_POS]++;
00330 disableInterrupts(BAT_INT);
00331 }
00332 }
00333
00334 if( intConf & WTD_INT )
00335 {
00336 if( !digitalRead(WTD_INT_PIN_MON) )
00337 {
00338 intCounter++;
00339 intFlag |= WTD_INT;
00340 intArray[WTD_POS]++;
00341 digitalWrite(WTD_INT_PIN_MON,HIGH);
00342 }
00343 }
00344
00345 if( intConf & LAI_INT )
00346 {
00347 if( !digitalRead(LAI_INT_PIN_MON) )
00348 {
00349 intCounter++;
00350 intFlag |= LAI_INT;
00351 intArray[LAI_POS]++;
00352 }
00353 }
00354
00355 if( intConf & PLV_INT )
00356 {
00357 if( ( !(intConf & BAT_INT) || !digitalRead(BAT_INT_PIN_MON) ) && ( !(intConf & WTD_INT) || !digitalRead(WTD_INT_PIN_MON) ) )
00358 {
00359 intCounter++;
00360 intFlag |= PLV_INT;
00361 intArray[SENS2_POS]++;
00362 }
00363 }
00364
00365 sei();
00366 }
00367
00368
00369
00370
00371 void clearIntFlag()
00372 {
00373 intFlag=0;
00374 }
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 void enableInterrupts(uint16_t conf)
00385 {
00386 intConf |= conf;
00387
00388
00389 if( conf & HAI_INT )
00390 {
00391 pinMode(MUX_RX,INPUT);
00392 attachInterrupt(HAI_INT_ACT, onHAIwakeUP, HIGH);
00393 }
00394 if( conf & LAI_INT )
00395 {
00396 pinMode(MUX_TX, INPUT);
00397 attachInterrupt(LAI_INT_ACT, onLAIwakeUP, LOW);
00398 }
00399 if( conf & ACC_INT )
00400 {
00401 pinMode(MUX_RX,INPUT);
00402 pinMode(ACC_INT_PIN_MON, INPUT);
00403 attachInterrupt(ACC_INT_ACT, onHAIwakeUP, HIGH);
00404 }
00405 if( conf & BAT_INT )
00406 {
00407 pinMode(MUX_TX, INPUT);
00408 pinMode(BAT_INT_PIN_MON,INPUT);
00409 digitalWrite(MUX_TX, HIGH);
00410 attachInterrupt(BAT_INT_ACT, onLAIwakeUP, LOW);
00411 }
00412 if( conf & RTC_INT )
00413 {
00414 pinMode(MUX_RX, INPUT);
00415 pinMode(RTC_INT_PIN_MON,INPUT);
00416 attachInterrupt(RTC_INT_ACT, onHAIwakeUP, HIGH);
00417 }
00418 if( conf & WTD_INT )
00419 {
00420 pinMode(WTD_INT_PIN_MON,OUTPUT);
00421 digitalWrite(WTD_INT_PIN_MON,HIGH);
00422 attachInterrupt(WTD_INT_ACT, onLAIwakeUP, LOW);
00423 }
00424 if( conf & UART1_INT )
00425 {
00426 pinMode(MUX_RX, INPUT);
00427 pinMode(UART1_INT_PIN_MON,INPUT);
00428 attachInterrupt(UART1_INT_ACT, onHAIwakeUP, HIGH);
00429 }
00430 if( conf & SENS_INT )
00431 {
00432 pinMode(MUX_RX, INPUT);
00433 pinMode(SENS_INT_PIN_MON,INPUT);
00434 attachInterrupt(SENS_INT_ACT, onHAIwakeUP, HIGH);
00435 }
00436 if( conf & ANE_INT )
00437 {
00438 pinMode(MUX_RX, INPUT);
00439 pinMode(MUX_TX, INPUT);
00440 pinMode(SENS2_INT_PIN_MON,INPUT);
00441 pinMode(SENS2_INT_PIN2_MON,INPUT);
00442 attachInterrupt(ANE_INT_ACT, onHAIwakeUP, HIGH);
00443 }
00444 if( conf & PLV_INT )
00445 {
00446 pinMode(MUX_RX, INPUT);
00447 pinMode(MUX_TX, INPUT);
00448 pinMode(SENS2_INT_PIN_MON,INPUT);
00449 pinMode(SENS2_INT_PIN2_MON,INPUT);
00450 attachInterrupt(PLV_INT_ACT, onLAIwakeUP, LOW);
00451 }
00452 }
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462 void disableInterrupts(uint16_t conf)
00463 {
00464 cli();
00465 if( conf & HAI_INT )
00466 {
00467 detachInterrupt(HAI_INT_ACT);
00468 }
00469 if( conf & LAI_INT )
00470 {
00471 detachInterrupt(LAI_INT_ACT);
00472 }
00473 if( conf & ACC_INT )
00474 {
00475 detachInterrupt(ACC_INT_ACT);
00476 }
00477 if( conf & BAT_INT )
00478 {
00479 detachInterrupt(BAT_INT_ACT);
00480 }
00481 if( conf & RTC_INT )
00482 {
00483 detachInterrupt(RTC_INT_ACT);
00484 }
00485 if( conf & UART1_INT )
00486 {
00487 detachInterrupt(UART1_INT_ACT);
00488 }
00489 if( conf & WTD_INT )
00490 {
00491 detachInterrupt(WTD_INT_ACT);
00492 }
00493 if( conf & SENS_INT )
00494 {
00495 detachInterrupt(SENS_INT_ACT);
00496 }
00497 if( conf & ANE_INT )
00498 {
00499 detachInterrupt(ANE_INT_ACT);
00500 }
00501 if( conf & PLV_INT )
00502 {
00503 detachInterrupt(PLV_INT_ACT);
00504 }
00505 intConf &= ~(conf);
00506 sei();
00507 }