forked from arduino/ArduinoCore-sam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWInterrupts.c
201 lines (173 loc) · 5.37 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
/*
Copyright (c) 2011-2012 Arduino. All right reserved.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "WInterrupts.h"
typedef void (*interruptCB)(void*);
static interruptCB callbacksPioA[32];
static interruptCB callbacksPioB[32];
static interruptCB callbacksPioC[32];
static interruptCB callbacksPioD[32];
static void* callbackParamsPioA[32];
static void* callbackParamsPioB[32];
static void* callbackParamsPioC[32];
static void* callbackParamsPioD[32];
/* Configure PIO interrupt sources */
static void __initialize() {
int i;
for (i=0; i<32; i++) {
callbacksPioA[i] = NULL;
callbacksPioB[i] = NULL;
callbacksPioC[i] = NULL;
callbacksPioD[i] = NULL;
}
pmc_enable_periph_clk(ID_PIOA);
NVIC_DisableIRQ(PIOA_IRQn);
NVIC_ClearPendingIRQ(PIOA_IRQn);
NVIC_SetPriority(PIOA_IRQn, 0);
NVIC_EnableIRQ(PIOA_IRQn);
pmc_enable_periph_clk(ID_PIOB);
NVIC_DisableIRQ(PIOB_IRQn);
NVIC_ClearPendingIRQ(PIOB_IRQn);
NVIC_SetPriority(PIOB_IRQn, 0);
NVIC_EnableIRQ(PIOB_IRQn);
pmc_enable_periph_clk(ID_PIOC);
NVIC_DisableIRQ(PIOC_IRQn);
NVIC_ClearPendingIRQ(PIOC_IRQn);
NVIC_SetPriority(PIOC_IRQn, 0);
NVIC_EnableIRQ(PIOC_IRQn);
pmc_enable_periph_clk(ID_PIOD);
NVIC_DisableIRQ(PIOD_IRQn);
NVIC_ClearPendingIRQ(PIOD_IRQn);
NVIC_SetPriority(PIOD_IRQn, 0);
NVIC_EnableIRQ(PIOD_IRQn);
}
void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode) {
// THIS IMPLEMENTATION IS NOT PORTABLE!
//
// On ARM's calling convention, calling a function with more arguments than it declares
// is OK - The extra parameter is ignored and causes no harm
//
// This implementation takes advantage of it to support callbacks with and without parameters with minimum overhead.
attachInterruptParam(pin, (void (*)(void*))callback, mode, NULL);
}
void attachInterruptParam(uint32_t pin, void (*callback)(void*), uint32_t mode, void* param)
{
static int enabled = 0;
if (!enabled) {
__initialize();
enabled = 1;
}
// Retrieve pin information
Pio *pio = g_APinDescription[pin].pPort;
uint32_t mask = g_APinDescription[pin].ulPin;
uint32_t pos = 0;
uint32_t t;
for (t = mask; t>1; t>>=1, pos++)
;
// Set callback function
if (pio == PIOA) {
callbacksPioA[pos] = callback;
callbackParamsPioA[pos] = param;
} else if (pio == PIOB) {
callbacksPioB[pos] = callback;
callbackParamsPioB[pos] = param;
} else if (pio == PIOC) {
callbacksPioC[pos] = callback;
callbackParamsPioC[pos] = param;
} else if (pio == PIOD) {
callbacksPioD[pos] = callback;
callbackParamsPioD[pos] = param;
}
// Configure the interrupt mode
if (mode == CHANGE) {
// Disable additional interrupt mode (detects both rising and falling edges)
pio->PIO_AIMDR = mask;
} else {
// Enable additional interrupt mode
pio->PIO_AIMER = mask;
// Select mode of operation
if (mode == LOW) {
pio->PIO_LSR = mask; // "Level" Select Register
pio->PIO_FELLSR = mask; // "Falling Edge / Low Level" Select Register
}
if (mode == HIGH) {
pio->PIO_LSR = mask; // "Level" Select Register
pio->PIO_REHLSR = mask; // "Rising Edge / High Level" Select Register
}
if (mode == FALLING) {
pio->PIO_ESR = mask; // "Edge" Select Register
pio->PIO_FELLSR = mask; // "Falling Edge / Low Level" Select Register
}
if (mode == RISING) {
pio->PIO_ESR = mask; // "Edge" Select Register
pio->PIO_REHLSR = mask; // "Rising Edge / High Level" Select Register
}
}
// Enable interrupt
pio->PIO_IER = mask;
}
void detachInterrupt(uint32_t pin)
{
// Retrieve pin information
Pio *pio = g_APinDescription[pin].pPort;
uint32_t mask = g_APinDescription[pin].ulPin;
// Disable interrupt
pio->PIO_IDR = mask;
}
#ifdef __cplusplus
extern "C" {
#endif
void PIOA_Handler(void) {
uint32_t isr = PIOA->PIO_ISR;
uint8_t leading_zeros;
while((leading_zeros=__CLZ(isr))<32)
{
uint8_t pin=32-leading_zeros-1;
if(callbacksPioA[pin]) callbacksPioA[pin](callbackParamsPioA[pin]);
isr=isr&(~(1<<pin));
}
}
void PIOB_Handler(void) {
uint32_t isr = PIOB->PIO_ISR;
uint8_t leading_zeros;
while((leading_zeros=__CLZ(isr))<32)
{
uint8_t pin=32-leading_zeros-1;
if(callbacksPioB[pin]) callbacksPioB[pin](callbackParamsPioB[pin]);
isr=isr&(~(1<<pin));
}
}
void PIOC_Handler(void) {
uint32_t isr = PIOC->PIO_ISR;
uint8_t leading_zeros;
while((leading_zeros=__CLZ(isr))<32)
{
uint8_t pin=32-leading_zeros-1;
if(callbacksPioC[pin]) callbacksPioC[pin](callbackParamsPioC[pin]);
isr=isr&(~(1<<pin));
}
}
void PIOD_Handler(void) {
uint32_t isr = PIOD->PIO_ISR;
uint8_t leading_zeros;
while((leading_zeros=__CLZ(isr))<32)
{
uint8_t pin=32-leading_zeros-1;
if(callbacksPioD[pin]) callbacksPioD[pin](callbackParamsPioD[pin]);
isr=isr&(~(1<<pin));
}
}
#ifdef __cplusplus
}
#endif