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