]> git.sur5r.net Git - freertos/blob - Demo/AVR32_UC3A_GCC/Atmel_SW_Framework/DRIVERS/INTC/intc.c
03dd56bbef204a47f3330b8de6c528504df015d7
[freertos] / Demo / AVR32_UC3A_GCC / Atmel_SW_Framework / DRIVERS / INTC / intc.c
1 /* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */\r
2
3 /*This file is prepared for Doxygen automatic documentation generation.*/\r
4 /*! \file *********************************************************************\r
5  *\r
6  * \brief INTC driver for AVR32 UC3.\r
7  *\r
8  * AVR32 Interrupt Controller driver module.\r
9  *\r
10  * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32\r
11  * - Supported devices:  All AVR32 devices with an INTC module can be used.\r
12  * - AppNote:\r
13  *\r
14  * \author               Atmel Corporation: http://www.atmel.com \n\r
15  *                       Support and FAQ: http://support.atmel.no/\r
16  *\r
17  ******************************************************************************/\r
18 \r
19 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.\r
20  *\r
21  * Redistribution and use in source and binary forms, with or without\r
22  * modification, are permitted provided that the following conditions are met:\r
23  *\r
24  * 1. Redistributions of source code must retain the above copyright notice, this\r
25  * list of conditions and the following disclaimer.\r
26  *\r
27  * 2. Redistributions in binary form must reproduce the above copyright notice,\r
28  * this list of conditions and the following disclaimer in the documentation\r
29  * and/or other materials provided with the distribution.\r
30  *\r
31  * 3. The name of Atmel may not be used to endorse or promote products derived\r
32  * from this software without specific prior written permission.\r
33  *\r
34  * 4. This software may only be redistributed and used in connection with an Atmel\r
35  * AVR product.\r
36  *\r
37  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED\r
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
39  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
40  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR\r
41  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
42  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r
45  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r
46  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE\r
47  *\r
48  */\r
49 \r
50 #include <avr32/io.h>\r
51 #include "compiler.h"\r
52 #include "preprocessor.h"\r
53 #include "intc.h"\r
54 \r
55 // define _evba from exception.S\r
56 extern void _evba;\r
57 \r
58 //! Values to store in the interrupt priority registers for the various interrupt priority levels.\r
59 extern const unsigned int ipr_val[AVR32_INTC_NUM_INT_LEVELS];\r
60 \r
61 //! Creates a table of interrupt line handlers per interrupt group in order to optimize RAM space.\r
62 //! Each line handler table contains a set of pointers to interrupt handlers.\r
63 #if (defined __GNUC__)\r
64 #define DECL_INT_LINE_HANDLER_TABLE(GRP, unused) \\r
65 static volatile __int_handler _int_line_handler_table_##GRP[Max(AVR32_INTC_NUM_IRQS_PER_GRP##GRP, 1)];\r
66 #elif (defined __ICCAVR32__)\r
67 #define DECL_INT_LINE_HANDLER_TABLE(GRP, unused) \\r
68 static volatile __no_init __int_handler _int_line_handler_table_##GRP[Max(AVR32_INTC_NUM_IRQS_PER_GRP##GRP, 1)];\r
69 #endif\r
70 MREPEAT(AVR32_INTC_NUM_INT_GRPS, DECL_INT_LINE_HANDLER_TABLE, ~);\r
71 #undef DECL_INT_LINE_HANDLER_TABLE\r
72 \r
73 //! Table containing for each interrupt group the number of interrupt request\r
74 //! lines and a pointer to the table of interrupt line handlers.\r
75 static const struct\r
76 {\r
77   unsigned int num_irqs;\r
78   volatile __int_handler *_int_line_handler_table;\r
79 } _int_handler_table[AVR32_INTC_NUM_INT_GRPS] =\r
80 {\r
81 #define INSERT_INT_LINE_HANDLER_TABLE(GRP, unused) \\r
82   {AVR32_INTC_NUM_IRQS_PER_GRP##GRP, _int_line_handler_table_##GRP},\r
83   MREPEAT(AVR32_INTC_NUM_INT_GRPS, INSERT_INT_LINE_HANDLER_TABLE, ~)\r
84 #undef INSERT_INT_LINE_HANDLER_TABLE\r
85 };\r
86 \r
87 \r
88 /*! \brief Default interrupt handler.\r
89  *\r
90  * \note Taken and adapted from Newlib.\r
91  */\r
92 #if (defined __GNUC__)\r
93 __attribute__((__interrupt__))\r
94 #elif (defined __ICCAVR32__)\r
95 __interrupt\r
96 #endif\r
97 static void _unhandled_interrupt(void)\r
98 {\r
99   // Catch unregistered interrupts.\r
100   while (TRUE);\r
101 }\r
102 \r
103 \r
104 /*! \brief Gets the interrupt handler of the current event at the \a int_level\r
105  *         interrupt priority level (called from exception.S).\r
106  *\r
107  * \param int_level Interrupt priority level to handle.\r
108  *\r
109  * \return Interrupt handler to execute.\r
110  *\r
111  * \note Taken and adapted from Newlib.\r
112  */\r
113 __int_handler _get_interrupt_handler(unsigned int int_level)\r
114 {\r
115   // ICR3 is mapped first, ICR0 last.\r
116   // Code in exception.S puts int_level in R12 which is used by AVR32-GCC to\r
117   // pass a single argument to a function.\r
118   unsigned int int_grp = AVR32_INTC.icr[AVR32_INTC_INT3 - int_level];\r
119   unsigned int int_req = AVR32_INTC.irr[int_grp];\r
120 \r
121   // As an interrupt may disappear while it is being fetched by the CPU\r
122   // (spurious interrupt caused by a delayed response from an MCU peripheral to\r
123   // an interrupt flag clear or interrupt disable instruction), check if there\r
124   // are remaining interrupt lines to process.\r
125   // If a spurious interrupt occurs, the status register (SR) contains an\r
126   // execution mode and interrupt level masks corresponding to a level 0\r
127   // interrupt, whatever the interrupt priority level causing the spurious\r
128   // event. This behavior has been chosen because a spurious interrupt has not\r
129   // to be a priority one and because it may not cause any trouble to other\r
130   // interrupts.\r
131   // However, these spurious interrupts place the hardware in an unstable state\r
132   // and could give problems in other/future versions of the CPU, so the\r
133   // software has to be written so that they never occur. The only safe way of\r
134   // achieving this is to always clear or disable peripheral interrupts with the\r
135   // following sequence:\r
136   // 1: Mask the interrupt in the CPU by setting GM (or IxM) in SR.\r
137   // 2: Perform the bus access to the peripheral register that clears or\r
138   //    disables the interrupt.\r
139   // 3: Wait until the interrupt has actually been cleared or disabled by the\r
140   //    peripheral. This is usually performed by reading from a register in the\r
141   //    same peripheral (it DOES NOT have to be the same register that was\r
142   //    accessed in step 2, but it MUST be in the same peripheral), what takes\r
143   //    bus system latencies into account, but peripheral internal latencies\r
144   //    (generally 0 cycle) also have to be considered.\r
145   // 4: Unmask the interrupt in the CPU by clearing GM (or IxM) in SR.\r
146   // Note that steps 1 and 4 are useless inside interrupt handlers as the\r
147   // corresponding interrupt level is automatically masked by IxM (unless IxM is\r
148   // explicitly cleared by the software).\r
149   //\r
150   // Get the right IRQ handler.\r
151   //\r
152   // If several interrupt lines are active in the group, the interrupt line with\r
153   // the highest number is selected. This is to be coherent with the\r
154   // prioritization of interrupt groups performed by the hardware interrupt\r
155   // controller.\r
156   //\r
157   // If no handler has been registered for the pending interrupt,\r
158   // _unhandled_interrupt will be selected thanks to the initialization of\r
159   // _int_line_handler_table_x by INTC_init_interrupts.\r
160   //\r
161   // exception.S will provide the interrupt handler with a clean interrupt stack\r
162   // frame, with nothing more pushed onto the stack. The interrupt handler must\r
163   // manage the `rete' instruction, what can be done thanks to pure assembly,\r
164   // inline assembly or the `__attribute__((__interrupt__))' C function\r
165   // attribute.\r
166   return (int_req) ? _int_handler_table[int_grp]._int_line_handler_table[32 - clz(int_req) - 1] : NULL;\r
167 }\r
168 \r
169 //! Init EVBA address. This sequence might also be done in the UTILS/STARTUP/GCC/crt0.S\r
170 static __inline__ void INTC_init_evba(void)\r
171 {\r
172   Set_system_register(AVR32_EVBA, (int)&_evba );\r
173 }\r
174 \r
175 void INTC_init_interrupts(void)\r
176 {\r
177   unsigned int int_grp, int_req;\r
178 \r
179   INTC_init_evba();\r
180 \r
181   // For all interrupt groups,\r
182   for (int_grp = 0; int_grp < AVR32_INTC_NUM_INT_GRPS; int_grp++)\r
183   {\r
184     // For all interrupt request lines of each group,\r
185     for (int_req = 0; int_req < _int_handler_table[int_grp].num_irqs; int_req++)\r
186     {\r
187       // Assign _unhandled_interrupt as default interrupt handler.\r
188       _int_handler_table[int_grp]._int_line_handler_table[int_req] = &_unhandled_interrupt;\r
189     }\r
190 \r
191     // Set the interrupt group priority register to its default value.\r
192     // By default, all interrupt groups are linked to the interrupt priority\r
193     // level 0 and to the interrupt vector _int0.\r
194     AVR32_INTC.ipr[int_grp] = ipr_val[AVR32_INTC_INT0];\r
195   }\r
196 }\r
197 \r
198 \r
199 void INTC_register_interrupt(__int_handler handler, unsigned int irq, unsigned int int_level)\r
200 {\r
201   // Determine the group of the IRQ.\r
202   unsigned int int_grp = irq / AVR32_INTC_MAX_NUM_IRQS_PER_GRP;\r
203 \r
204   // Store in _int_line_handler_table_x the pointer to the interrupt handler, so\r
205   // that _get_interrupt_handler can retrieve it when the interrupt is vectored.\r
206   _int_handler_table[int_grp]._int_line_handler_table[irq % AVR32_INTC_MAX_NUM_IRQS_PER_GRP] = handler;\r
207 \r
208   // Program the corresponding IPRX register to set the interrupt priority level\r
209   // and the interrupt vector offset that will be fetched by the core interrupt\r
210   // system.\r
211   // NOTE: The _intx functions are intermediate assembly functions between the\r
212   // core interrupt system and the user interrupt handler.\r
213   AVR32_INTC.ipr[int_grp] = ipr_val[int_level & (AVR32_INTC_IPR_INTLEVEL_MASK >> AVR32_INTC_IPR_INTLEVEL_OFFSET)];\r
214 }\r