]> begriffs open source - cmsis-freertos/blob - Demo/CORTEX_AT91SAM3U256_IAR/AT91Lib/peripherals/irq/nvic.c
Initial commit
[cmsis-freertos] / Demo / CORTEX_AT91SAM3U256_IAR / AT91Lib / peripherals / irq / nvic.c
1 /* ----------------------------------------------------------------------------
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 //------------------------------------------------------------------------------
31 //         Headers
32 //------------------------------------------------------------------------------
33
34 #include "board.h"
35 #include "irq.h"
36 #include "exceptions.h"
37 #include <utility/trace.h>
38
39 /// The index of IRQ handler in the exception table
40 #define NVIC_IRQ_HANDLER_INDEX     16
41
42 //------------------------------------------------------------------------------
43 //         Global functions
44 //------------------------------------------------------------------------------
45
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 //------------------------------------------------------------------------------
62 void IRQ_ConfigureIT(
63     unsigned int source,
64     //unsigned int preemptionPriority,
65     //unsigned int subPriority,
66     unsigned int priority,
67     IntFunc handler)
68 {
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;
75
76     preemptionPriority = (priority & 0xff00) >> 8;
77     subPriority = (priority & 0xff);
78
79     // Disable the interrupt first
80     NVIC_DisableIRQ((IRQn_Type)source);
81
82     // Clear any pending status
83     NVIC_ClearPendingIRQ((IRQn_Type)source);
84
85     // Configure interrupt handler
86     //if (handler == 0) handler = IrqHandlerNotUsed;
87       //  GetExceptionTable()[NVIC_IRQ_HANDLER_INDEX + source] = handler;
88
89     if (subPriority >= (0x01 << nSub))
90       subPriority = (0x01 << nSub) - 1;
91     if (preemptionPriority >= (0x01 << nPre))
92       preemptionPriority = (0x01 << nPre) - 1;
93
94     IRQpriority = (subPriority | (preemptionPriority << nSub));
95     NVIC_SetPriority((IRQn_Type)source, IRQpriority);
96 }
97
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)
103 {
104     NVIC_EnableIRQ((IRQn_Type)source);
105 }
106
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)
112 {
113     NVIC_DisableIRQ((IRQn_Type)source);
114 }
115
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)
121 {
122     NVIC_SetPendingIRQ((IRQn_Type)source);
123 }
124
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)
130 {
131     NVIC_ClearPendingIRQ((IRQn_Type)source);
132 }
133
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)
140 {
141     AT91C_BASE_NVIC->NVIC_STIR = source;
142 }
143 #endif
144