1 /* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2008, Atmel Corporation
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
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 * ----------------------------------------------------------------------------
30 //------------------------------------------------------------------------------
32 //------------------------------------------------------------------------------
36 #include "exceptions.h"
37 #include <utility/trace.h>
39 /// The index of IRQ handler in the exception table
40 #define NVIC_IRQ_HANDLER_INDEX 16
42 //------------------------------------------------------------------------------
44 //------------------------------------------------------------------------------
46 //------------------------------------------------------------------------------
47 /// Configures an interrupt in the NVIC. The interrupt is identified by its
48 /// source (AT91C_ID_xxx) and is configured to a specified priority and
49 /// interrupt handler function. priority is the value that will be put in NVIC_IPRx
50 /// and the function address will be set in "ExceptionTable". The parameter priority
51 /// will include the preemptionPriority and the subPriority, where the subPriority
52 /// defined in the B[7:0] of the parameter "priority", and the preemptionPriority defined
53 /// in the B[15:8] of the parameter "priority".
54 /// The interrupt is disabled before configuration, so it is useless
55 /// to do it before calling this function. When NVIC_ConfigureIT returns, the
56 /// interrupt will always be disabled and cleared; it must be enabled by a
57 /// call to NVIC_EnableIT().
58 /// \param source Interrupt source to configure.
59 /// \param priority Pre-emption priority (B[15:8] )+ subPriority (B[7:0])
60 /// \param handler Interrupt handler function.
61 //------------------------------------------------------------------------------
64 //unsigned int preemptionPriority,
65 //unsigned int subPriority,
66 unsigned int priority,
69 unsigned int priGroup = __NVIC_PRIO_BITS;
70 unsigned int nPre = 7 - priGroup;
71 unsigned int nSub = priGroup + 1;
72 unsigned int preemptionPriority;
73 unsigned int subPriority;
74 unsigned int IRQpriority;
76 preemptionPriority = (priority & 0xff00) >> 8;
77 subPriority = (priority & 0xff);
79 // Disable the interrupt first
80 NVIC_DisableIRQ((IRQn_Type)source);
82 // Clear any pending status
83 NVIC_ClearPendingIRQ((IRQn_Type)source);
85 // Configure interrupt handler
86 //if (handler == 0) handler = IrqHandlerNotUsed;
87 // GetExceptionTable()[NVIC_IRQ_HANDLER_INDEX + source] = handler;
89 if (subPriority >= (0x01 << nSub))
90 subPriority = (0x01 << nSub) - 1;
91 if (preemptionPriority >= (0x01 << nPre))
92 preemptionPriority = (0x01 << nPre) - 1;
94 IRQpriority = (subPriority | (preemptionPriority << nSub));
95 NVIC_SetPriority((IRQn_Type)source, IRQpriority);
98 //------------------------------------------------------------------------------
99 /// Enables interrupt coming from the given (unique) source (AT91C_ID_xxx).
100 /// \param source Interrupt source to enable.
101 //------------------------------------------------------------------------------
102 void IRQ_EnableIT(unsigned int source)
104 NVIC_EnableIRQ((IRQn_Type)source);
107 //------------------------------------------------------------------------------
108 /// Disables interrupt coming from the given (unique) source (AT91C_ID_xxx).
109 /// \param source Interrupt source to disable.
110 //------------------------------------------------------------------------------
111 void IRQ_DisableIT(unsigned int source)
113 NVIC_DisableIRQ((IRQn_Type)source);
116 //------------------------------------------------------------------------------
117 /// Set interrupt pending bit from the given (unique) source (AT91C_ID_xxx).
118 /// \param source Interrupt source to set.
119 //------------------------------------------------------------------------------
120 void NVIC_SetPending(unsigned int source)
122 NVIC_SetPendingIRQ((IRQn_Type)source);
125 //------------------------------------------------------------------------------
126 /// Clear interrupt pending bit from the given (unique) source (AT91C_ID_xxx).
127 /// \param source Interrupt source to clear.
128 //------------------------------------------------------------------------------
129 void NVIC_ClrPending(unsigned int source)
131 NVIC_ClearPendingIRQ((IRQn_Type)source);
134 #if !defined(USE_CMSIS_on)
135 //------------------------------------------------------------------------------
136 /// Use the Software Trigger Interrupt Register to pend an interrupt.
137 /// \param source Interrupt source to trigger.
138 //------------------------------------------------------------------------------
139 void NVIC_Swi(unsigned int source)
141 AT91C_BASE_NVIC->NVIC_STIR = source;