/
WInterrupts.c
242 lines (209 loc) · 6.61 KB
/
WInterrupts.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//************************************************************************
//* WInterrupts.c
//*
//* Arduino core files for PIC32
//* Copyright (c) 2010, 2011 by Mark Sproul
//*
//*
//************************************************************************
//* this code is based on code 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
//*
//*
//************************************************************************
//* Edit History
//************************************************************************
//* Oct 15, 2010 <MLS> Started on WInterrupts.c for Pic32
//* Aug 8, 2011 <GeneApperson> completely rewritten (issue #75)
//* Aug 30, 2011 <GeneApperson> clear interrupt flag after return from
//* user interrupt function (issue #109)
//* Jul 26, 2012 <GeneApperson> Added PPS support for PIC32MX1xx/MX2xx devices
// Feb 6, 2012 <GeneApperson> Removed dependencies on the Microchip plib library
//************************************************************************
#include <p32xxxx.h>
#include <sys/attribs.h>
#include <inttypes.h>
#include <stdio.h>
#define OPT_SYSTEM_INTERNAL
#define OPT_BOARD_INTERNAL //pull in internal symbol definitons
#include "p32_defs.h"
#include "pins_arduino.h"
#include "WConstants.h"
#include "wiring_private.h"
volatile static voidFuncPtr intFunc[NUM_EXTERNAL_INTERRUPTS];
//************************************************************************
// PIC32 devices only support rising and falling edge triggered interrupts
// on the external interrupt pins. Only the RISING and FALLING modes are
// supported.
//************************************************************************
void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode)
{
int edge;
if ((interruptNum < NUM_EXTERNAL_INTERRUPTS) && ((mode == FALLING)||(mode == RISING)))
{
intFunc[interruptNum] = userFunc;
#if defined(__PIC32MX1XX__) || defined(__PIC32MX2XX__)
/* For devices with peripheral pin select (PPS), it is necessary to
** map the input function to the pin. This is done by loading the
** PPS input select register for the specific interrupt with the value
** to select the pin that the interrupt is mapped to as defined by the
** board variant file.
*/
volatile uint32_t * pps;
uint8_t pin;
uint8_t sel;
if ((sel = externalIntToInputSelect(interruptNum)) != NOT_PPS_PIN)
{
pin = externalIntToDigitalPin(interruptNum);
pps = ppsInputRegister(sel);
*pps = ppsInputSelect(pin);
}
#endif
// The active edge is selected via the INTxEP bits in the INTCON register.
// A '0' bit selects falling edge, and a '1' bit select rising edge.
if (mode == FALLING)
{
edge = 0;
}
else
{
edge = 1;
}
// Select the active edge, set the privilege and sub-privilege levels,
// and enable the interrupt.
switch (interruptNum)
{
case EXT_INT0:
IEC0bits.INT0IE = 0;
IFS0bits.INT0IF = 0;
INTCONbits.INT0EP = edge;
IPC0bits.INT0IP = _INT0_IPL_IPC;
IPC0bits.INT0IS = _INT0_SPL_IPC;
IEC0bits.INT0IE = 1;
break;
case EXT_INT1:
IEC0bits.INT1IE = 0;
IFS0bits.INT1IF = 0;
INTCONbits.INT1EP = edge;
IPC1bits.INT1IP = _INT1_IPL_IPC;
IPC1bits.INT1IS = _INT1_SPL_IPC;
IEC0bits.INT1IE = 1;
break;
case EXT_INT2:
IEC0bits.INT2IE = 0;
IFS0bits.INT2IF = 0;
INTCONbits.INT2EP = edge;
IPC2bits.INT2IP = _INT2_IPL_IPC;
IPC2bits.INT2IS = _INT2_SPL_IPC;
IEC0bits.INT2IE = 1;
break;
case EXT_INT3:
IEC0bits.INT3IE = 0;
IFS0bits.INT3IF = 0;
INTCONbits.INT3EP = edge;
IPC3bits.INT3IP = _INT3_IPL_IPC;
IPC3bits.INT3IS = _INT3_SPL_IPC;
IEC0bits.INT3IE = 1;
break;
case EXT_INT4:
IEC0bits.INT4IE = 0;
IFS0bits.INT4IF = 0;
INTCONbits.INT4EP = edge;
IPC4bits.INT4IP = _INT4_IPL_IPC;
IPC4bits.INT4IS = _INT4_SPL_IPC;
IEC0bits.INT4IE = 1;
break;
}
}
}
//************************************************************************
void detachInterrupt(uint8_t interruptNum)
{
if (interruptNum < EXTERNAL_NUM_INTERRUPTS)
{
switch (interruptNum)
{
case EXT_INT0:
IEC0bits.INT0IE = 0;
break;
case EXT_INT1:
IEC0bits.INT1IE = 0;
break;
case EXT_INT2:
IEC0bits.INT2IE = 0;
break;
case EXT_INT3:
IEC0bits.INT3IE = 0;
break;
case EXT_INT4:
IEC0bits.INT4IE = 0;
break;
}
intFunc[interruptNum] = 0;
}
}
//************************************************************************
// INT0 ISR
void __ISR(_EXTERNAL_0_VECTOR, _INT0_IPL_ISR) ExtInt0Handler(void)
{
if (intFunc[EXT_INT0] != 0)
{
(*intFunc[EXT_INT0])();
}
IFS0bits.INT0IF = 0;
}
//************************************************************************
// INT1 ISR
void __ISR(_EXTERNAL_1_VECTOR, _INT1_IPL_ISR) ExtInt1Handler(void)
{
if (intFunc[EXT_INT1] != 0)
{
(*intFunc[EXT_INT1])();
}
IFS0bits.INT1IF = 0;
}
//************************************************************************
// INT2 ISR
void __ISR(_EXTERNAL_2_VECTOR, _INT2_IPL_ISR) ExtInt2Handler(void)
{
if (intFunc[EXT_INT2] != 0)
{
(*intFunc[EXT_INT2])();
}
IFS0bits.INT2IF = 0;
}
//************************************************************************
// INT3 ISR
void __ISR(_EXTERNAL_3_VECTOR, _INT3_IPL_ISR) ExtInt3Handler(void)
{
if (intFunc[EXT_INT3] != 0)
{
(*intFunc[EXT_INT3])();
}
IFS0bits.INT3IF = 0;
}
//************************************************************************
// INT4 ISR
void __ISR(_EXTERNAL_4_VECTOR, _INT4_IPL_ISR) ExtInt4Handler(void)
{
if (intFunc[EXT_INT4] != 0)
{
(*intFunc[EXT_INT4])();
}
IFS0bits.INT4IF = 0;
}
//************************************************************************