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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
|
/****************************************************************************
* arch/mips/src/pic32mx/pic32mx-irq.c
* arch/mips/src/chip/pic32mx-irq.c
*
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdint.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/irq.h>
#include <nuttx/arch.h>
#include <arch/irq.h>
#include <arch/pic32mx/cp0.h>
#include "up_arch.h"
#include "os_internal.h"
#include "up_internal.h"
#include "pic32mx-int.h"
#include "pic32mx-internal.h"
/****************************************************************************
* Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifdef CONFIG_PIC32MX_MVEC
# error "Multi-vectors not supported"
#endif
/****************************************************************************
* Public Data
****************************************************************************/
volatile uint32_t *current_regs;
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_irqinitialize
****************************************************************************/
void up_irqinitialize(void)
{
uint32_t regval;
int irq;
/* Disable all interrupts */
putreg32(0xffff, PIC32MX_INT_IEC0CLR);
putreg32(0xffff, PIC32MX_INT_IEC1CLR);
#ifdef PIC32MX_INT_IEC2CLR
putreg32(0xffff, PIC32MX_INT_IEC2CLR);
#endif
/* Set all interrupts to the default (middle) priority */
for (irq = 0; irq < NR_IRQS; irq++)
{
(void)up_prioritize_irq(irq, (INT_ICP_MID_PRIORITY << 2));
}
/* Set the BEV bit in the STATUS register */
regval = cp0_getstatus();
regval |= CP0_STATUS_BEV;
cp0_putstatus(regval);
/* Set the EBASE value to the beginning of boot FLASH */
cp0_putebase(0xbfc00000);
/* Set the INTCTL vector spacing to non-zero */
cp0_putintctl(0x00000020);
/* Set the IV bit in the CAUSE register */
regval = cp0_getcause();
regval |= CP0_CAUSE_IV;
cp0_putcause(regval);
/* Clear the EXL bit in the STATUS register */
regval = cp0_getstatus();
//regval &= ~CP0_STATUS_BEV;
regval &= ~CP0_STATUS_EXL;
cp0_putstatus(regval);
/* Configure multi- or single- vector interrupts */
#ifdef CONFIG_PIC32MX_MVEC
putreg32(INT_INTCON_MVEC, PIC32MX_INT_INTCONSET);
#else
putreg32(INT_INTCON_MVEC, PIC32MX_INT_INTCONCLR);
#endif
/* Initialize GPIO change notification handling */
#ifdef CONFIG_GPIO_IRQ
pic32mx_gpioirqinitialize();
#endif
/* Attach and enable software interrupts */
irq_attach(PIC32MX_IRQ_CS0, up_swint0);
up_enable_irq(PIC32MX_IRQSRC_CS0);
/* currents_regs is non-NULL only while processing an interrupt */
current_regs = NULL;
/* And finally, enable interrupts */
/* Interrupts are enabled by setting the IE bit in the CP0 status register */
regval = 0;
asm volatile("ei %0" : "=r"(regval));
#ifndef CONFIG_SUPPRESS_INTERRUPTS
/* Then enable all interrupt levels */
irqrestore(CP0_STATUS_IM_ALL);
#else
/* Enable only software interrupts */
irqrestore(CP0_STATUS_IM_SWINTS);
#endif
}
/****************************************************************************
* Name: up_disable_irq
*
* Description:
* Disable the IRQ specified by 'irq'
*
****************************************************************************/
void up_disable_irq(int irq)
{
uint32_t regaddr;
int bitno;
/* Disable the interrupt by clearing the associated bit in the IEC register */
DEBUGASSERT(irq >= PIC32MX_IRQSRC_FIRST && irq <= PIC32MX_IRQSRC_LAST)
if (irq >= PIC32MX_IRQSRC_FIRST)
{
if (irq <= PIC32MX_IRQSRC0_LAST)
{
/* Use IEC0 */
regaddr = PIC32MX_INT_IEC0CLR;
bitno = irq - PIC32MX_IRQSRC0_FIRST;
}
else if (irq <= PIC32MX_IRQSRC1_LAST)
{
/* Use IEC1 */
regaddr = PIC32MX_INT_IEC1CLR;
bitno = irq - PIC32MX_IRQSRC1_FIRST;
}
#ifdef PIC32MX_IRQSRC2_FIRST
else if (irq <= PIC32MX_IRQSRC2_LAST)
{
/* Use IEC2 */
regaddr = PIC32MX_INT_IEC2CLR;
bitno = irq - PIC32MX_IRQSRC2_FIRST;
}
#endif
else
{
/* Value out of range.. just ignore */
return;
}
/* Disable the interrupt */
putreg32((1 << bitno), regaddr);
}
}
/****************************************************************************
* Name: up_enable_irq
*
* Description:
* Enable the IRQ specified by 'irq'
*
****************************************************************************/
void up_enable_irq(int irq)
{
uint32_t regaddr;
int bitno;
/* Enable the interrupt by setting the associated bit in the IEC register */
DEBUGASSERT(irq >= PIC32MX_IRQSRC_FIRST && irq <= PIC32MX_IRQSRC_LAST)
if (irq >= PIC32MX_IRQSRC_FIRST)
{
if (irq <= PIC32MX_IRQSRC0_LAST)
{
/* Use IEC0 */
regaddr = PIC32MX_INT_IEC0SET;
bitno = irq - PIC32MX_IRQSRC0_FIRST;
}
else if (irq <= PIC32MX_IRQSRC1_LAST)
{
/* Use IEC1 */
regaddr = PIC32MX_INT_IEC1SET;
bitno = irq - PIC32MX_IRQSRC1_FIRST;
}
#ifdef PIC32MX_IRQSRC2_FIRST
else if (irq <= PIC32MX_IRQSRC2_LAST)
{
/* Use IEC2 */
regaddr = PIC32MX_INT_IEC2SET;
bitno = irq - PIC32MX_IRQSRC2_FIRST;
}
#endif
else
{
/* Value out of range.. just ignore */
return;
}
/* Disable the interrupt */
putreg32((1 << bitno), regaddr);
}
}
/****************************************************************************
* Name: up_pending_irq
*
* Description:
* Return true if the interrupt is pending and unmasked.
*
****************************************************************************/
bool up_pending_irq(int irq)
{
uint32_t ifsaddr;
uint32_t iecaddr;
uint32_t regval;
int bitno;
/* Disable the interrupt by clearing the associated bit in the IEC and then
* acknowledge the interrupt by clearing the associated bit in the IFS
* register. It is necessary to do this BEFORE lowering the interrupt
* priority level otherwise recursive interrupts would occur.
*/
DEBUGASSERT(irq >= PIC32MX_IRQSRC_FIRST && irq <= PIC32MX_IRQSRC_LAST)
if (irq >= PIC32MX_IRQSRC_FIRST)
{
if (irq <= PIC32MX_IRQSRC0_LAST)
{
/* Use IFS0 */
ifsaddr = PIC32MX_INT_IFS0;
iecaddr = PIC32MX_INT_IEC0;
bitno = irq - PIC32MX_IRQSRC0_FIRST;
}
else if (irq <= PIC32MX_IRQSRC1_LAST)
{
/* Use IFS1 */
ifsaddr = PIC32MX_INT_IFS1;
iecaddr = PIC32MX_INT_IEC1;
bitno = irq - PIC32MX_IRQSRC1_FIRST;
}
#ifdef PIC32MX_IRQSRC2_FIRST
else if (irq <= PIC32MX_IRQSRC2_LAST)
{
/* Use IFS2 */
ifsaddr = PIC32MX_INT_IFS2;
iecaddr = PIC32MX_INT_IEC2;
bitno = irq - PIC32MX_IRQSRC2_FIRST;
}
#endif
else
{
/* Value out of range.. just ignore */
return false;
}
/* Get the set of unmasked, pending interrupts. Return true if the
* interrupt is pending and unmask.
*/
regval = getreg32(ifsaddr) & getreg32(iecaddr);
return (regval & (1 << bitno)) != 0;
}
return false;
}
/****************************************************************************
* Name: up_clrpend_irq
*
* Description:
* Clear any pending interrupt
*
****************************************************************************/
void up_clrpend_irq(int irq)
{
uint32_t regaddr;
int bitno;
/* Acknowledge the interrupt by clearing the associated bit in the IFS
* register. It is necessary to do this BEFORE lowering the interrupt
* priority level otherwise recursive interrupts would occur.
*/
DEBUGASSERT(irq >= PIC32MX_IRQSRC_FIRST && irq <= PIC32MX_IRQSRC_LAST)
if (irq >= PIC32MX_IRQSRC_FIRST)
{
if (irq <= PIC32MX_IRQSRC0_LAST)
{
/* Use IFS0 */
regaddr = PIC32MX_INT_IFS0CLR;
bitno = irq - PIC32MX_IRQSRC0_FIRST;
}
else if (irq <= PIC32MX_IRQSRC1_LAST)
{
/* Use IFS1 */
regaddr = PIC32MX_INT_IFS1CLR;
bitno = irq - PIC32MX_IRQSRC1_FIRST;
}
#ifdef PIC32MX_IRQSRC2_FIRST
else if (irq <= PIC32MX_IRQSRC2_LAST)
{
/* Use IFS2 */
regaddr = PIC32MX_INT_IFS2CLR;
bitno = irq - PIC32MX_IRQSRC2_FIRST;
}
#endif
else
{
/* Value out of range.. just ignore */
return;
}
/* Disable then acknowledge interrupt */
putreg32((1 << bitno), regaddr);
}
}
/****************************************************************************
* Name: up_prioritize_irq
*
* Description:
* Set the priority of an IRQ by setting the priority and sub-priority
* fields in the PIC32MX IPC registers. There are 12 IPC registers, IPC0
* through IPC11. Each has sub-priority fields for 8 interrupts for a
* total of 96 interrupts max.
*
* Each interrupt priority is represent by a group of 5 bits: a 3-bit
* priority and a 2-bit sub-priority. These have different meanings to
* the hardware. The priority is the priority level that is enabled
* or masked by the IPL field of the CAUSE register. The sub-priority
* only mediates ties when two interrupts with the same priority pend
* simultaneously.
*
* In this function, we just treat this as a single 5-bit priority.
* (MS 3-bits=priority; LS 2-bits=sub-priority).
*
* The 5-bit priority/sub-priority fields are arranged at byte boundaries
* within each IPC register:
*
* xxxP PPSS xxxP PPSS xxxP PPSS xxxP PPSS
*
****************************************************************************/
#ifdef CONFIG_ARCH_IRQPRIO
int up_prioritize_irq(int irq, int priority)
{
int regndx;
int shift;
/* Don't allow this function to be used for disabling interrupts. There is
* no good reason for this restriction other than I want to make sure that
* the 5-bit priority values passed to this function are *not* confused with
* the 3-bit hardware priority values.
*/
DEBUGASSERT((unsigned)irq < NR_IRQS && (unsigned)(priority >> 2) > 0);
if (irq < NR_IRQS)
{
/* Get the index to the IPC register and the shift to the 5-bit priority
* field for this IRQ.
*/
regndx = irq >> 2; /* Range: 0-11 */
shift = (irq & 3) << 3; /* {0, 8, 16, 24 } */
/* Set the new interrupt priority (momentarily disabling interrupts) */
putreg32(0x1f << shift, PIC32MX_INT_IPCCLR(regndx));
putreg32(priority << shift, PIC32MX_INT_IPCSET(regndx));
return OK;
}
return -EINVAL;
}
#endif
|