1 |
608 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
5 |
|
|
*
|
6 |
|
|
* All rights reserved.
|
7 |
|
|
*
|
8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
10 |
|
|
*
|
11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
12 |
|
|
* this list of conditions and the disclaimer below.
|
13 |
|
|
*
|
14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
15 |
|
|
* this software without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
* ----------------------------------------------------------------------------
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
/// Disable traces for this file
|
31 |
|
|
#ifndef NOTRACE
|
32 |
|
|
#define NOTRACE
|
33 |
|
|
#endif
|
34 |
|
|
|
35 |
|
|
//------------------------------------------------------------------------------
|
36 |
|
|
// Headers
|
37 |
|
|
//------------------------------------------------------------------------------
|
38 |
|
|
|
39 |
|
|
#include "pio_it.h"
|
40 |
|
|
#include "pio.h"
|
41 |
|
|
#include <aic/aic.h>
|
42 |
|
|
#include <board.h>
|
43 |
|
|
#include <utility/assert.h>
|
44 |
|
|
#include <utility/trace.h>
|
45 |
|
|
|
46 |
|
|
//------------------------------------------------------------------------------
|
47 |
|
|
// Local definitions
|
48 |
|
|
//------------------------------------------------------------------------------
|
49 |
|
|
|
50 |
|
|
/// Returns the current value of a register.
|
51 |
|
|
#define READ(peripheral, register) (peripheral->register)
|
52 |
|
|
/// Modifies the current value of a register.
|
53 |
|
|
#define WRITE(peripheral, register, value) (peripheral->register = value)
|
54 |
|
|
|
55 |
|
|
/// Maximum number of interrupt sources that can be defined.
|
56 |
|
|
#define MAX_INTERRUPT_SOURCES 7
|
57 |
|
|
|
58 |
|
|
//------------------------------------------------------------------------------
|
59 |
|
|
// Local types
|
60 |
|
|
//------------------------------------------------------------------------------
|
61 |
|
|
|
62 |
|
|
/// Describes a PIO interrupt source, including the PIO instance triggering the
|
63 |
|
|
/// interrupt and the associated interrupt handler.
|
64 |
|
|
typedef struct _InterruptSource {
|
65 |
|
|
|
66 |
|
|
/// Interrupt source pin.
|
67 |
|
|
const Pin *pPin;
|
68 |
|
|
|
69 |
|
|
/// Interrupt handler.
|
70 |
|
|
void (*handler)(const Pin *);
|
71 |
|
|
|
72 |
|
|
} InterruptSource;
|
73 |
|
|
|
74 |
|
|
//------------------------------------------------------------------------------
|
75 |
|
|
// Local variables
|
76 |
|
|
//------------------------------------------------------------------------------
|
77 |
|
|
|
78 |
|
|
/// List of interrupt sources.
|
79 |
|
|
static InterruptSource pSources[MAX_INTERRUPT_SOURCES];
|
80 |
|
|
|
81 |
|
|
/// Number of currently defined interrupt sources.
|
82 |
|
|
static unsigned int numSources;
|
83 |
|
|
|
84 |
|
|
//------------------------------------------------------------------------------
|
85 |
|
|
// Local functions
|
86 |
|
|
//------------------------------------------------------------------------------
|
87 |
|
|
|
88 |
|
|
//------------------------------------------------------------------------------
|
89 |
|
|
/// Handles all interrupts on the given PIO controller.
|
90 |
|
|
/// \param id PIO controller ID.
|
91 |
|
|
/// \param pBase PIO controller base address.
|
92 |
|
|
//------------------------------------------------------------------------------
|
93 |
|
|
void PioInterruptHandler(unsigned int id, AT91S_PIO *pBase)
|
94 |
|
|
{
|
95 |
|
|
unsigned int status;
|
96 |
|
|
unsigned int i;
|
97 |
|
|
|
98 |
|
|
// Check PIO controller status
|
99 |
|
|
status = pBase->PIO_ISR;
|
100 |
|
|
status &= pBase->PIO_IMR;
|
101 |
|
|
if (status != 0) {
|
102 |
|
|
|
103 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO interrupt on PIO controller #%d\n\r", id);
|
104 |
|
|
|
105 |
|
|
// Check all sources
|
106 |
|
|
i = 0;
|
107 |
|
|
while (status != 0) {
|
108 |
|
|
|
109 |
|
|
// There cannot be an unconfigured source enabled.
|
110 |
|
|
SANITY_CHECK(i < numSources);
|
111 |
|
|
|
112 |
|
|
// Source if configured on PIOA
|
113 |
|
|
if (pSources[i].pPin->id == id) {
|
114 |
|
|
|
115 |
|
|
// Source has PIOs which have changed
|
116 |
|
|
if ((status & pSources[i].pPin->mask) != 0) {
|
117 |
|
|
|
118 |
|
|
trace_LOG(trace_DEBUG, "-D- Interrupt source #%d triggered\n\r", i);
|
119 |
|
|
|
120 |
|
|
pSources[i].handler(pSources[i].pPin);
|
121 |
|
|
status &= ~(pSources[i].pPin->mask);
|
122 |
|
|
}
|
123 |
|
|
}
|
124 |
|
|
i++;
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
}
|
128 |
|
|
|
129 |
|
|
//------------------------------------------------------------------------------
|
130 |
|
|
/// Generic PIO interrupt handler. Single entry point for interrupts coming
|
131 |
|
|
/// from any PIO controller (PIO A, B, C ...). Dispatches the interrupt to
|
132 |
|
|
/// the user-configured handlers.
|
133 |
|
|
//------------------------------------------------------------------------------
|
134 |
|
|
void InterruptHandler()
|
135 |
|
|
{
|
136 |
|
|
#if defined(AT91C_ID_PIOA)
|
137 |
|
|
// Treat PIOA interrupts
|
138 |
|
|
PioInterruptHandler(AT91C_ID_PIOA, AT91C_BASE_PIOA);
|
139 |
|
|
#endif
|
140 |
|
|
|
141 |
|
|
#if defined(AT91C_ID_PIOB)
|
142 |
|
|
// Treat PIOB interrupts
|
143 |
|
|
PioInterruptHandler(AT91C_ID_PIOB, AT91C_BASE_PIOB);
|
144 |
|
|
#endif
|
145 |
|
|
|
146 |
|
|
#if defined(AT91C_ID_PIOC)
|
147 |
|
|
// Treat PIOC interrupts
|
148 |
|
|
PioInterruptHandler(AT91C_ID_PIOC, AT91C_BASE_PIOC);
|
149 |
|
|
#endif
|
150 |
|
|
|
151 |
|
|
#if defined(AT91C_ID_PIOD)
|
152 |
|
|
// Treat PIOD interrupts
|
153 |
|
|
PioInterruptHandler(AT91C_ID_PIOD, AT91C_BASE_PIOD);
|
154 |
|
|
#endif
|
155 |
|
|
|
156 |
|
|
#if defined(AT91C_ID_PIOE)
|
157 |
|
|
// Treat PIOE interrupts
|
158 |
|
|
PioInterruptHandler(AT91C_ID_PIOE, AT91C_BASE_PIOE);
|
159 |
|
|
#endif
|
160 |
|
|
|
161 |
|
|
#if defined(AT91C_ID_PIOABCD)
|
162 |
|
|
// Treat PIOABCD interrupts
|
163 |
|
|
#if !defined(AT91C_ID_PIOA)
|
164 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCD, AT91C_BASE_PIOA);
|
165 |
|
|
#endif
|
166 |
|
|
#if !defined(AT91C_ID_PIOB)
|
167 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCD, AT91C_BASE_PIOB);
|
168 |
|
|
#endif
|
169 |
|
|
#if !defined(AT91C_ID_PIOC)
|
170 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCD, AT91C_BASE_PIOC);
|
171 |
|
|
#endif
|
172 |
|
|
#if !defined(AT91C_ID_PIOD)
|
173 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCD, AT91C_BASE_PIOD);
|
174 |
|
|
#endif
|
175 |
|
|
#endif
|
176 |
|
|
|
177 |
|
|
#if defined(AT91C_ID_PIOABCDE)
|
178 |
|
|
// Treat PIOABCDE interrupts
|
179 |
|
|
#if !defined(AT91C_ID_PIOA)
|
180 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOA);
|
181 |
|
|
#endif
|
182 |
|
|
#if !defined(AT91C_ID_PIOB)
|
183 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOB);
|
184 |
|
|
#endif
|
185 |
|
|
#if !defined(AT91C_ID_PIOC)
|
186 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOC);
|
187 |
|
|
#endif
|
188 |
|
|
#if !defined(AT91C_ID_PIOD)
|
189 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOD);
|
190 |
|
|
#endif
|
191 |
|
|
#if !defined(AT91C_ID_PIOE)
|
192 |
|
|
PioInterruptHandler(AT91C_ID_PIOABCDE, AT91C_BASE_PIOE);
|
193 |
|
|
#endif
|
194 |
|
|
#endif
|
195 |
|
|
|
196 |
|
|
#if defined(AT91C_ID_PIOCDE)
|
197 |
|
|
// Treat PIOCDE interrupts
|
198 |
|
|
#if !defined(AT91C_ID_PIOC)
|
199 |
|
|
PioInterruptHandler(AT91C_ID_PIOCDE, AT91C_BASE_PIOC);
|
200 |
|
|
#endif
|
201 |
|
|
#if !defined(AT91C_ID_PIOD)
|
202 |
|
|
PioInterruptHandler(AT91C_ID_PIOCDE, AT91C_BASE_PIOD);
|
203 |
|
|
#endif
|
204 |
|
|
#if !defined(AT91C_ID_PIOE)
|
205 |
|
|
PioInterruptHandler(AT91C_ID_PIOCDE, AT91C_BASE_PIOE);
|
206 |
|
|
#endif
|
207 |
|
|
#endif
|
208 |
|
|
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
//------------------------------------------------------------------------------
|
212 |
|
|
// Global functions
|
213 |
|
|
//------------------------------------------------------------------------------
|
214 |
|
|
|
215 |
|
|
//------------------------------------------------------------------------------
|
216 |
|
|
/// Initializes the PIO interrupt management logic.
|
217 |
|
|
/// \param priority PIO controller interrupts priority.
|
218 |
|
|
//------------------------------------------------------------------------------
|
219 |
|
|
void PIO_InitializeInterrupts(unsigned int priority)
|
220 |
|
|
{
|
221 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize()\n\r");
|
222 |
|
|
|
223 |
|
|
SANITY_CHECK((priority & ~AT91C_AIC_PRIOR) == 0);
|
224 |
|
|
|
225 |
|
|
// Reset sources
|
226 |
|
|
numSources = 0;
|
227 |
|
|
|
228 |
|
|
#ifdef AT91C_ID_PIOA
|
229 |
|
|
// Configure PIO interrupt sources
|
230 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOA\n\r");
|
231 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;
|
232 |
|
|
AT91C_BASE_PIOA->PIO_ISR;
|
233 |
|
|
AT91C_BASE_PIOA->PIO_IDR = 0xFFFFFFFF;
|
234 |
|
|
AIC_ConfigureIT(AT91C_ID_PIOA, priority, InterruptHandler);
|
235 |
|
|
AIC_EnableIT(AT91C_ID_PIOA);
|
236 |
|
|
#endif
|
237 |
|
|
|
238 |
|
|
#ifdef AT91C_ID_PIOB
|
239 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOB\n\r");
|
240 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOB;
|
241 |
|
|
AT91C_BASE_PIOB->PIO_ISR;
|
242 |
|
|
AT91C_BASE_PIOB->PIO_IDR = 0xFFFFFFFF;
|
243 |
|
|
AIC_ConfigureIT(AT91C_ID_PIOB, priority, InterruptHandler);
|
244 |
|
|
AIC_EnableIT(AT91C_ID_PIOB);
|
245 |
|
|
#endif
|
246 |
|
|
|
247 |
|
|
#ifdef AT91C_ID_PIOC
|
248 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOC\n\r");
|
249 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOC;
|
250 |
|
|
AT91C_BASE_PIOC->PIO_ISR;
|
251 |
|
|
AT91C_BASE_PIOC->PIO_IDR = 0xFFFFFFFF;
|
252 |
|
|
AIC_ConfigureIT(AT91C_ID_PIOC, priority, InterruptHandler);
|
253 |
|
|
AIC_EnableIT(AT91C_ID_PIOC);
|
254 |
|
|
#endif
|
255 |
|
|
|
256 |
|
|
#ifdef AT91C_ID_PIOD
|
257 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOD\n\r");
|
258 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOD;
|
259 |
|
|
AT91C_BASE_PIOC->PIO_ISR;
|
260 |
|
|
AT91C_BASE_PIOC->PIO_IDR = 0xFFFFFFFF;
|
261 |
|
|
AIC_ConfigureIT(AT91C_ID_PIOD, priority, InterruptHandler);
|
262 |
|
|
AIC_EnableIT(AT91C_ID_PIOD);
|
263 |
|
|
#endif
|
264 |
|
|
|
265 |
|
|
#ifdef AT91C_ID_PIOE
|
266 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOE\n\r");
|
267 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOE;
|
268 |
|
|
AT91C_BASE_PIOC->PIO_ISR;
|
269 |
|
|
AT91C_BASE_PIOC->PIO_IDR = 0xFFFFFFFF;
|
270 |
|
|
AIC_ConfigureIT(AT91C_ID_PIOE, priority, InterruptHandler);
|
271 |
|
|
AIC_EnableIT(AT91C_ID_PIOE);
|
272 |
|
|
#endif
|
273 |
|
|
|
274 |
|
|
#if defined(AT91C_ID_PIOABCD)
|
275 |
|
|
// Treat PIOABCD interrupts
|
276 |
|
|
#if !defined(AT91C_ID_PIOA) \
|
277 |
|
|
&& !defined(AT91C_ID_PIOB) \
|
278 |
|
|
&& !defined(AT91C_ID_PIOC) \
|
279 |
|
|
&& !defined(AT91C_ID_PIOD)
|
280 |
|
|
|
281 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOABCD\n\r");
|
282 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOABCD;
|
283 |
|
|
AT91C_BASE_PIOA->PIO_ISR;
|
284 |
|
|
AT91C_BASE_PIOA->PIO_IDR = 0xFFFFFFFF;
|
285 |
|
|
AIC_ConfigureIT(AT91C_ID_PIOABCD, priority, InterruptHandler);
|
286 |
|
|
AIC_EnableIT(AT91C_ID_PIOABCD);
|
287 |
|
|
#endif
|
288 |
|
|
#endif
|
289 |
|
|
|
290 |
|
|
#if defined(AT91C_ID_PIOABCDE)
|
291 |
|
|
// Treat PIOABCDE interrupts
|
292 |
|
|
#if !defined(AT91C_ID_PIOA) \
|
293 |
|
|
&& !defined(AT91C_ID_PIOB) \
|
294 |
|
|
&& !defined(AT91C_ID_PIOC) \
|
295 |
|
|
&& !defined(AT91C_ID_PIOD) \
|
296 |
|
|
&& !defined(AT91C_ID_PIOE)
|
297 |
|
|
|
298 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOABCDE\n\r");
|
299 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOABCDE;
|
300 |
|
|
AT91C_BASE_PIOA->PIO_ISR;
|
301 |
|
|
AT91C_BASE_PIOA->PIO_IDR = 0xFFFFFFFF;
|
302 |
|
|
AIC_ConfigureIT(AT91C_ID_PIOABCDE, priority, InterruptHandler);
|
303 |
|
|
AIC_EnableIT(AT91C_ID_PIOABCDE);
|
304 |
|
|
#endif
|
305 |
|
|
#endif
|
306 |
|
|
|
307 |
|
|
#if defined(AT91C_ID_PIOCDE)
|
308 |
|
|
// Treat PIOCDE interrupts
|
309 |
|
|
#if !defined(AT91C_ID_PIOC) \
|
310 |
|
|
&& !defined(AT91C_ID_PIOD) \
|
311 |
|
|
&& !defined(AT91C_ID_PIOE)
|
312 |
|
|
|
313 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_Initialize: Configuring PIOC\n\r");
|
314 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOCDE;
|
315 |
|
|
AT91C_BASE_PIOC->PIO_ISR;
|
316 |
|
|
AT91C_BASE_PIOC->PIO_IDR = 0xFFFFFFFF;
|
317 |
|
|
AIC_ConfigureIT(AT91C_ID_PIOCDE, priority, InterruptHandler);
|
318 |
|
|
AIC_EnableIT(AT91C_ID_PIOCDE);
|
319 |
|
|
#endif
|
320 |
|
|
#endif
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
//------------------------------------------------------------------------------
|
324 |
|
|
/// Configures an interrupt source.
|
325 |
|
|
/// \param pPin Interrupt source.
|
326 |
|
|
/// \param handler Desired interrupt handler for the source.
|
327 |
|
|
//------------------------------------------------------------------------------
|
328 |
|
|
void PIO_ConfigureIt(const Pin *pPin, void (*handler)(const Pin *))
|
329 |
|
|
{
|
330 |
|
|
InterruptSource *pSource;
|
331 |
|
|
|
332 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_ConfigureIt()\n\r");
|
333 |
|
|
|
334 |
|
|
SANITY_CHECK(pPin);
|
335 |
|
|
ASSERT(numSources < MAX_INTERRUPT_SOURCES,
|
336 |
|
|
"-F- PIO_ConfigureIt: Increase MAX_INTERRUPT_SOURCES\n\r");
|
337 |
|
|
|
338 |
|
|
// Define new source
|
339 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_ConfigureIt: Defining new source #%d.\n\r", numSources);
|
340 |
|
|
|
341 |
|
|
pSource = &(pSources[numSources]);
|
342 |
|
|
pSource->pPin = pPin;
|
343 |
|
|
pSource->handler = handler;
|
344 |
|
|
numSources++;
|
345 |
|
|
}
|
346 |
|
|
|
347 |
|
|
//------------------------------------------------------------------------------
|
348 |
|
|
/// Enables the given interrupt source if it has been configured.
|
349 |
|
|
/// \param pPin Interrupt source to enable.
|
350 |
|
|
//------------------------------------------------------------------------------
|
351 |
|
|
void PIO_EnableIt(const Pin *pPin)
|
352 |
|
|
{
|
353 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_EnableIt()\n\r");
|
354 |
|
|
|
355 |
|
|
SANITY_CHECK(pPin);
|
356 |
|
|
|
357 |
|
|
#ifndef NOASSERT
|
358 |
|
|
unsigned int i = 0;
|
359 |
|
|
unsigned char found = 0;
|
360 |
|
|
while ((i < numSources) && !found) {
|
361 |
|
|
|
362 |
|
|
if (pSources[i].pPin == pPin) {
|
363 |
|
|
|
364 |
|
|
found = 1;
|
365 |
|
|
}
|
366 |
|
|
i++;
|
367 |
|
|
}
|
368 |
|
|
ASSERT(found, "-F- PIO_EnableIt: Interrupt source has not been configured\n\r");
|
369 |
|
|
#endif
|
370 |
|
|
|
371 |
|
|
pPin->pio->PIO_ISR;
|
372 |
|
|
pPin->pio->PIO_IER = pPin->mask;
|
373 |
|
|
}
|
374 |
|
|
|
375 |
|
|
//------------------------------------------------------------------------------
|
376 |
|
|
/// Disables a given interrupt source.
|
377 |
|
|
/// \param pPin Interrupt source to disable.
|
378 |
|
|
//------------------------------------------------------------------------------
|
379 |
|
|
void PIO_DisableIt(const Pin *pPin)
|
380 |
|
|
{
|
381 |
|
|
SANITY_CHECK(pPin);
|
382 |
|
|
|
383 |
|
|
trace_LOG(trace_DEBUG, "-D- PIO_DisableIt()\n\r");
|
384 |
|
|
|
385 |
|
|
pPin->pio->PIO_IDR = pPin->mask;
|
386 |
|
|
}
|
387 |
|
|
|