1 //*****************************************************************************
3 // interrupt.c - Driver for the NVIC Interrupt Controller.
5 // Copyright (c) 2005,2006 Luminary Micro, Inc. All rights reserved.
7 // Software License Agreement
9 // Luminary Micro, Inc. (LMI) is supplying this software for use solely and
10 // exclusively on LMI's Stellaris Family of microcontroller products.
12 // The software is owned by LMI and/or its suppliers, and is protected under
13 // applicable copyright laws. All rights are reserved. Any use in violation
14 // of the foregoing restrictions may subject the user to criminal sanctions
15 // under applicable laws, as well as to civil liability for the breach of the
16 // terms and conditions of this license.
18 // THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
19 // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
20 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
21 // LMI SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
22 // CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
24 // This is part of revision 991 of the Stellaris Driver Library.
26 //*****************************************************************************
28 //*****************************************************************************
30 //! \addtogroup interrupt_api
33 //*****************************************************************************
35 #include "../hw_ints.h"
36 #include "../hw_nvic.h"
37 #include "../hw_types.h"
40 #include "interrupt.h"
42 //*****************************************************************************
44 // This is a mapping between priority grouping encodings and the number of
45 // preemption priority bits.
47 //*****************************************************************************
48 #if defined(GROUP_pulpriority) || defined(BUILD_ALL)
49 const unsigned long g_pulPriority[] =
51 NVIC_APINT_PRIGROUP_0_8, NVIC_APINT_PRIGROUP_1_7, NVIC_APINT_PRIGROUP_2_6,
52 NVIC_APINT_PRIGROUP_3_5, NVIC_APINT_PRIGROUP_4_4, NVIC_APINT_PRIGROUP_5_3,
53 NVIC_APINT_PRIGROUP_6_2, NVIC_APINT_PRIGROUP_7_1
56 extern const unsigned long g_pulPriority[];
59 //*****************************************************************************
61 // This is a mapping between interrupt number and the register that contains
62 // the priority encoding for that interrupt.
64 //*****************************************************************************
65 #if defined(GROUP_pulregs) || defined(BUILD_ALL)
66 const unsigned long g_pulRegs[12] =
68 0, NVIC_SYS_PRI1, NVIC_SYS_PRI2, NVIC_SYS_PRI3, NVIC_PRI0, NVIC_PRI1,
69 NVIC_PRI2, NVIC_PRI3, NVIC_PRI4, NVIC_PRI5, NVIC_PRI6, NVIC_PRI7
72 extern const unsigned long g_pulRegs[12];
75 //*****************************************************************************
78 //! The default interrupt handler.
80 //! This is the default interrupt handler for all interrupts. It simply loops
81 //! forever so that the system state is preserved for observation by a
82 //! debugger. Since interrupts should be disabled before unregistering the
83 //! corresponding handler, this should never be called.
87 //*****************************************************************************
88 #if defined(GROUP_defaulthandler) || defined(BUILD_ALL)
90 IntDefaultHandler(void)
93 // Go into an infinite loop.
100 extern void IntDefaultHandler(void);
103 //*****************************************************************************
105 // The processor vector table.
107 // This contains a list of the handlers for the various interrupt sources in
108 // the system. The layout of this list is defined by the hardware; assertion
109 // of an interrupt causes the processor to start executing directly at the
110 // address given in the corresponding location in this list.
112 //*****************************************************************************
113 #if defined(GROUP_vtable) || defined(BUILD_ALL)
115 __no_init void (*g_pfnRAMVectors[NUM_INTERRUPTS])(void) @ "VTABLE";
117 __attribute__((section("vtable")))
118 void (*g_pfnRAMVectors[NUM_INTERRUPTS])(void);
121 extern void (*g_pfnRAMVectors[NUM_INTERRUPTS])(void);
124 //*****************************************************************************
126 //! Enables the processor interrupt.
128 //! Allows the processor to respond to interrupts. This does not affect the
129 //! set of interrupts enabled in the interrupt controller; it just gates the
130 //! single interrupt from the controller to the processor.
134 //*****************************************************************************
135 #if defined(GROUP_masterenable) || defined(BUILD_ALL) || defined(DOXYGEN)
137 IntMasterEnable(void)
140 // Enable processor interrupts.
146 //*****************************************************************************
148 //! Disables the processor interrupt.
150 //! Prevents the processor from receiving interrupts. This does not affect the
151 //! set of interrupts enabled in the interrupt controller; it just gates the
152 //! single interrupt from the controller to the processor.
156 //*****************************************************************************
157 #if defined(GROUP_masterdisable) || defined(BUILD_ALL) || defined(DOXYGEN)
159 IntMasterDisable(void)
162 // Disable processor interrupts.
168 //*****************************************************************************
170 //! Registers a function to be called when an interrupt occurs.
172 //! \param ulInterrupt specifies the interrupt in question.
173 //! \param pfnHandler is a pointer to the function to be called.
175 //! This function is used to specify the handler function to be called when the
176 //! given interrupt is asserted to the processor. When the interrupt occurs,
177 //! if it is enabled (via IntEnable()), the handler function will be called in
178 //! interrupt context. Since the handler function can preempt other code, care
179 //! must be taken to protect memory or peripherals that are accessed by the
180 //! handler and other non-handler code.
182 //! \note The use of this function (directly or indirectly via a peripheral
183 //! driver interrupt register function) moves the interrupt vector table from
184 //! flash to SRAM. Therefore, care must be taken when linking the application
185 //! to ensure that the SRAM vector table is located at the beginning of SRAM;
186 //! otherwise NVIC will not look in the correct portion of memory for the
187 //! vector table (it requires the vector table be on a 1 kB memory alignment).
188 //! Normally, the SRAM vector table is so placed via the use of linker scripts;
189 //! some tool chains, such as the evaluation version of RV-MDK, do not support
190 //! linker scripts and therefore will not produce a valid executable. See the
191 //! discussion of compile-time versus run-time interrupt handler registration
192 //! in the introduction to this chapter.
196 //*****************************************************************************
197 #if defined(GROUP_register) || defined(BUILD_ALL) || defined(DOXYGEN)
199 IntRegister(unsigned long ulInterrupt, void (*pfnHandler)(void))
204 // Check the arguments.
206 ASSERT(ulInterrupt < NUM_INTERRUPTS);
209 // Make sure that the RAM vector table is correctly aligned.
211 ASSERT(((unsigned long)g_pfnRAMVectors & 0x000003ff) == 0);
214 // See if the RAM vector table has been initialized.
216 if(HWREG(NVIC_VTABLE) != (unsigned long)g_pfnRAMVectors)
219 // Copy the vector table from the beginning of FLASH to the RAM vector
222 for(ulIdx = 0; ulIdx < NUM_INTERRUPTS; ulIdx++)
224 g_pfnRAMVectors[ulIdx] = (void (*)(void))HWREG(ulIdx * 4);
228 // Point NVIC at the RAM vector table.
230 HWREG(NVIC_VTABLE) = (unsigned long)g_pfnRAMVectors;
234 // Save the interrupt handler.
236 g_pfnRAMVectors[ulInterrupt] = pfnHandler;
240 //*****************************************************************************
242 //! Unregisters the function to be called when an interrupt occurs.
244 //! \param ulInterrupt specifies the interrupt in question.
246 //! This function is used to indicate that no handler should be called when the
247 //! given interrupt is asserted to the processor. The interrupt source will be
248 //! automatically disabled (via IntDisable()) if necessary.
250 //! \sa IntRegister() for important information about registering interrupt
255 //*****************************************************************************
256 #if defined(GROUP_unregister) || defined(BUILD_ALL) || defined(DOXYGEN)
258 IntUnregister(unsigned long ulInterrupt)
261 // Check the arguments.
263 ASSERT(ulInterrupt < NUM_INTERRUPTS);
266 // Reset the interrupt handler.
268 g_pfnRAMVectors[ulInterrupt] = IntDefaultHandler;
272 //*****************************************************************************
274 //! Sets the priority grouping of the interrupt controller.
276 //! \param ulBits specifies the number of bits of preemptable priority.
278 //! This function specifies the split between preemptable priority levels and
279 //! subpriority levels in the interrupt priority specification. The range of
280 //! the grouping values are dependent upon the hardware implementation; on
281 //! the Stellaris family it can range from 0 to 3.
285 //*****************************************************************************
286 #if defined(GROUP_prioritygroupingset) || defined(BUILD_ALL) || \
289 IntPriorityGroupingSet(unsigned long ulBits)
292 // Check the arguments.
294 ASSERT(ulBits < NUM_PRIORITY_BITS);
297 // Set the priority grouping.
299 HWREG(NVIC_APINT) = NVIC_APINT_VECTKEY | g_pulPriority[ulBits];
303 //*****************************************************************************
305 //! Gets the priority grouping of the interrupt controller.
307 //! This function returns the split between preemptable priority levels and
308 //! subpriority levels in the interrupt priority specification.
310 //! \return The number of bits of preemptable priority.
312 //*****************************************************************************
313 #if defined(GROUP_prioritygroupingget) || defined(BUILD_ALL) || \
316 IntPriorityGroupingGet(void)
318 unsigned long ulLoop, ulValue;
321 // Read the priority grouping.
323 ulValue = HWREG(NVIC_APINT) & NVIC_APINT_PRIGROUP_M;
326 // Loop through the priority grouping values.
328 for(ulLoop = 0; ulLoop < 8; ulLoop++)
331 // Stop looping if this value matches.
333 if(ulValue == g_pulPriority[ulLoop])
340 // Return the number of priority bits.
346 //*****************************************************************************
348 //! Sets the priority of an interrupt.
350 //! \param ulInterrupt specifies the interrupt in question.
351 //! \param ucPriority specifies the priority of the interrupt.
353 //! This function is used to set the priority of an interrupt. When multiple
354 //! interrupts are asserted simultaneously, the ones with the highest priority
355 //! are processed before the lower priority interrupts. Smaller numbers
356 //! correspond to higher interrupt priorities; priority 0 is the highest
357 //! interrupt priority.
359 //! The hardware priority mechanism will only look at the upper N bits of the
360 //! priority level (where N is 3 for the Stellaris family), so any
361 //! prioritization must be performed in those bits. The remaining bits can be
362 //! used to sub-prioritize the interrupt sources, and may be used by the
363 //! hardware priority mechanism on a future part. This arrangement allows
364 //! priorities to migrate to different NVIC implementations without changing
365 //! the gross prioritization of the interrupts.
369 //*****************************************************************************
370 #if defined(GROUP_priorityset) || defined(BUILD_ALL) || defined(DOXYGEN)
372 IntPrioritySet(unsigned long ulInterrupt, unsigned char ucPriority)
374 unsigned long ulTemp;
377 // Check the arguments.
379 ASSERT((ulInterrupt >= 4) && (ulInterrupt < NUM_INTERRUPTS));
382 // Set the interrupt priority.
384 ulTemp = HWREG(g_pulRegs[ulInterrupt >> 2]);
385 ulTemp &= ~(0xFF << (8 * (ulInterrupt & 3)));
386 ulTemp |= ucPriority << (8 * (ulInterrupt & 3));
387 HWREG(g_pulRegs[ulInterrupt >> 2]) = ulTemp;
391 //*****************************************************************************
393 //! Gets the priority of an interrupt.
395 //! \param ulInterrupt specifies the interrupt in question.
397 //! This function gets the priority of an interrupt. See IntPrioritySet() for
398 //! a definition of the priority value.
400 //! \return Returns the interrupt priority, or -1 if an invalid interrupt was
403 //*****************************************************************************
404 #if defined(GROUP_priorityget) || defined(BUILD_ALL) || defined(DOXYGEN)
406 IntPriorityGet(unsigned long ulInterrupt)
409 // Check the arguments.
411 ASSERT((ulInterrupt >= 4) && (ulInterrupt < NUM_INTERRUPTS));
414 // Return the interrupt priority.
416 return((HWREG(g_pulRegs[ulInterrupt >> 2]) >> (8 * (ulInterrupt & 3))) &
421 //*****************************************************************************
423 //! Enables an interrupt.
425 //! \param ulInterrupt specifies the interrupt to be enabled.
427 //! The specified interrupt is enabled in the interrupt controller. Other
428 //! enables for the interrupt (such as at the peripheral level) are unaffected
429 //! by this function.
433 //*****************************************************************************
434 #if defined(GROUP_enable) || defined(BUILD_ALL) || defined(DOXYGEN)
436 IntEnable(unsigned long ulInterrupt)
439 // Check the arguments.
441 ASSERT(ulInterrupt < NUM_INTERRUPTS);
444 // Determine the interrupt to enable.
446 if(ulInterrupt == FAULT_MPU)
449 // Enable the MemManage interrupt.
451 HWREG(NVIC_SYS_HND_CTRL) |= NVIC_SYS_HND_CTRL_MEM;
453 else if(ulInterrupt == FAULT_BUS)
456 // Enable the bus fault interrupt.
458 HWREG(NVIC_SYS_HND_CTRL) |= NVIC_SYS_HND_CTRL_BUS;
460 else if(ulInterrupt == FAULT_USAGE)
463 // Enable the usage fault interrupt.
465 HWREG(NVIC_SYS_HND_CTRL) |= NVIC_SYS_HND_CTRL_USAGE;
467 else if(ulInterrupt == FAULT_SYSTICK)
470 // Enable the System Tick interrupt.
472 HWREG(NVIC_ST_CTRL) |= NVIC_ST_CTRL_INTEN;
474 else if(ulInterrupt >= INT_GPIOA)
477 // Enable the general interrupt.
479 HWREG(NVIC_EN0) = 1 << (ulInterrupt - INT_GPIOA);
484 //*****************************************************************************
486 //! Disables an interrupt.
488 //! \param ulInterrupt specifies the interrupt to be disabled.
490 //! The specified interrupt is disabled in the interrupt controller. Other
491 //! enables for the interrupt (such as at the peripheral level) are unaffected
492 //! by this function.
496 //*****************************************************************************
497 #if defined(GROUP_disable) || defined(BUILD_ALL) || defined(DOXYGEN)
499 IntDisable(unsigned long ulInterrupt)
502 // Check the arguments.
504 ASSERT(ulInterrupt < NUM_INTERRUPTS);
507 // Determine the interrupt to disable.
509 if(ulInterrupt == FAULT_MPU)
512 // Disable the MemManage interrupt.
514 HWREG(NVIC_SYS_HND_CTRL) &= ~(NVIC_SYS_HND_CTRL_MEM);
516 else if(ulInterrupt == FAULT_BUS)
519 // Disable the bus fault interrupt.
521 HWREG(NVIC_SYS_HND_CTRL) &= ~(NVIC_SYS_HND_CTRL_BUS);
523 else if(ulInterrupt == FAULT_USAGE)
526 // Disable the usage fault interrupt.
528 HWREG(NVIC_SYS_HND_CTRL) &= ~(NVIC_SYS_HND_CTRL_USAGE);
530 else if(ulInterrupt == FAULT_SYSTICK)
533 // Disable the System Tick interrupt.
535 HWREG(NVIC_ST_CTRL) &= ~(NVIC_ST_CTRL_INTEN);
537 else if(ulInterrupt >= INT_GPIOA)
540 // Disable the general interrupt.
542 HWREG(NVIC_DIS0) = 1 << (ulInterrupt - INT_GPIOA);
547 //*****************************************************************************
549 // Close the Doxygen group.
552 //*****************************************************************************