-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWInterrupts.c
More file actions
148 lines (123 loc) · 4.23 KB
/
WInterrupts.c
File metadata and controls
148 lines (123 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* -*- mode: jde; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Wiring project - http://wiring.uniandes.edu.co
Copyright (c) 2004-05 Hernando Barragan
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
Modified 24 November 2006 by David A. Mellis
Modified 28-08-2009 for attiny84 R.Wiersma
Modified 09-10-2009 for attiny45 A.Saporetti
Modified 20-11-2010 - B.Cook - Correct a minor bug in attachInterrupt
*/
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include "WConstants.h"
#include "wiring_private.h"
volatile static voidFuncPtr intFunc[NUMBER_EXTERNAL_INTERRUPTS];
#if defined( MCUCR ) && ! defined( EICRA )
#define EICRA MCUCR
#endif
#if defined( GIMSK ) && ! defined( EIMSK )
#define EIMSK GIMSK
#endif
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode)
{
if ( interruptNum < NUMBER_EXTERNAL_INTERRUPTS )
{
/*
If attachInterrupt is called in succession for the same
interruptNum but a different userFunc then the following line
is not safe. Changing intFunc is not atomic.
intFunc[interruptNum] = userFunc;
*/
{
// save interrupt flag
uint8_t SaveSREG = SREG;
// disable interrupts
cli();
// access the shared data
intFunc[interruptNum] = userFunc;
// restore the interrupt flag
SREG = SaveSREG;
}
// Configure the interrupt mode (trigger on low input, any change, rising
// edge, or falling edge). The mode constants were chosen to correspond
// to the configuration bits in the hardware register, so we simply shift
// the mode into place.
// Enable the interrupt.
switch ( interruptNum )
{
#if NUMBER_EXTERNAL_INTERRUPTS >= 1
case EXTERNAL_INTERRUPT_0:
EICRA = (EICRA & ~((1 << ISC00) | (1 << ISC01))) | (mode << ISC00);
EIMSK |= (1 << INT0);
break;
#endif
#if NUMBER_EXTERNAL_INTERRUPTS >= 2
case EXTERNAL_INTERRUPT_1:
EICRA = (EICRA & ~((1 << ISC10) | (1 << ISC11))) | (mode << ISC10);
EIMSK |= (1 << INT1);
break;
#endif
#if NUMBER_EXTERNAL_INTERRUPTS > 2
#error Add handlers for the additional interrupts.
#endif
}
}
}
void detachInterrupt(uint8_t interruptNum)
{
if ( interruptNum < NUMBER_EXTERNAL_INTERRUPTS )
{
// Disable the interrupt. (We can't assume that interruptNum is equal
// to the number of the EIMSK bit to clear, as this isn't true on the
// ATmega8. There, INT0 is 6 and INT1 is 7.)
switch (interruptNum)
{
#if NUMBER_EXTERNAL_INTERRUPTS >= 1
case EXTERNAL_INTERRUPT_0:
EIMSK &= ~(1 << INT0);
break;;
#endif
#if NUMBER_EXTERNAL_INTERRUPTS >= 2
case EXTERNAL_INTERRUPT_1:
EIMSK &= ~(1 << INT1);
break;;
#endif
#if NUMBER_EXTERNAL_INTERRUPTS > 2
#error Add handlers for the additional interrupts.
#endif
}
intFunc[interruptNum] = 0;
}
}
#if NUMBER_EXTERNAL_INTERRUPTS >= 1
ISR(EXTERNAL_INTERRUPT_0_vect)
{
if(intFunc[EXTERNAL_INTERRUPT_0])
intFunc[EXTERNAL_INTERRUPT_0]();
}
#endif
#if NUMBER_EXTERNAL_INTERRUPTS >= 2
ISR(EXTERNAL_INTERRUPT_1_vect)
{
if(intFunc[EXTERNAL_INTERRUPT_1])
intFunc[EXTERNAL_INTERRUPT_1]();
}
#endif
#if NUMBER_EXTERNAL_INTERRUPTS > 2
#error Add handlers for the additional interrupts.
#endif