Nut/OS  4.10.3
API Reference
ihndlr.c
Go to the documentation of this file.
00001 /* This source file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
00002 
00003 /*This file is prepared for Doxygen automatic documentation generation.*/
00019 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
00020  *
00021  * Redistribution and use in source and binary forms, with or without
00022  * modification, are permitted provided that the following conditions are met:
00023  *
00024  * 1. Redistributions of source code must retain the above copyright notice, this
00025  * list of conditions and the following disclaimer.
00026  *
00027  * 2. Redistributions in binary form must reproduce the above copyright notice,
00028  * this list of conditions and the following disclaimer in the documentation
00029  * and/or other materials provided with the distribution.
00030  *
00031  * 3. The name of Atmel may not be used to endorse or promote products derived
00032  * from this software without specific prior written permission.
00033  *
00034  * 4. This software may only be redistributed and used in connection with an Atmel
00035  * AVR product.
00036  *
00037  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
00038  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00039  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00040  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
00041  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00042  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00043  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00044  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00045  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00046  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00047  *
00048  */
00049 
00050 #include <arch/avr32.h>
00051 #include <dev/irqreg.h>
00052 #include <avr32/io.h>
00053 #include <arch/avr32/preprocessor.h>
00054 
00055 /**************************** Added for NutOS ****************************/
00056 
00058 #define AVR32_INTC_MAX_NUM_IRQS_PER_GRP             32
00059 
00061 #define AVR32_INTC_NUM_INT_LEVELS                   (1 << AVR32_INTC_IPR_INTLEVEL_SIZE)
00062 
00064 typedef void (*__int_handler)(void);
00065 
00066 #define Max(a, b) (((a) > (b)) ? (a) : (b))
00067 
00068 /*************************************************************************/
00069 
00070 // define _evba from exception.S
00071 extern void _evba;
00072 
00074 extern const unsigned int ipr_val[AVR32_INTC_NUM_INT_LEVELS];
00075 
00078 #if (defined __GNUC__)
00079 #define DECL_INT_LINE_HANDLER_TABLE(GRP, unused) \
00080 static volatile __int_handler _int_line_handler_table_##GRP[Max(AVR32_INTC_NUM_IRQS_PER_GRP##GRP, 1)];
00081 #elif (defined __ICCAVR32__)
00082 #define DECL_INT_LINE_HANDLER_TABLE(GRP, unused) \
00083 static volatile __no_init __int_handler _int_line_handler_table_##GRP[Max(AVR32_INTC_NUM_IRQS_PER_GRP##GRP, 1)];
00084 #endif
00085 MREPEAT(AVR32_INTC_NUM_INT_GRPS, DECL_INT_LINE_HANDLER_TABLE, ~);
00086 #undef DECL_INT_LINE_HANDLER_TABLE
00087 
00090 static const struct
00091 {
00092   unsigned int num_irqs;
00093   volatile __int_handler *_int_line_handler_table;
00094 } _int_handler_table[AVR32_INTC_NUM_INT_GRPS] =
00095 {
00096 #define INSERT_INT_LINE_HANDLER_TABLE(GRP, unused) \
00097   {AVR32_INTC_NUM_IRQS_PER_GRP##GRP, _int_line_handler_table_##GRP},
00098   MREPEAT(AVR32_INTC_NUM_INT_GRPS, INSERT_INT_LINE_HANDLER_TABLE, ~)
00099 #undef INSERT_INT_LINE_HANDLER_TABLE
00100 };
00105 static SIGNAL(_unhandled_interrupt)
00106 {
00107   // Catch unregistered interrupts.
00108   while (1);
00109 }
00110 
00111 
00121 __int_handler _get_interrupt_handler(unsigned int int_level)
00122 {
00123   // ICR3 is mapped first, ICR0 last.
00124   // Code in exception.S puts int_level in R12 which is used by AVR32-GCC to
00125   // pass a single argument to a function.
00126   unsigned int int_grp = AVR32_INTC.icr[AVR32_INTC_INT3 - int_level];
00127   unsigned int int_req = AVR32_INTC.irr[int_grp];
00128 
00129   // As an interrupt may disappear while it is being fetched by the CPU
00130   // (spurious interrupt caused by a delayed response from an MCU peripheral to
00131   // an interrupt flag clear or interrupt disable instruction), check if there
00132   // are remaining interrupt lines to process.
00133   // If a spurious interrupt occurs, the status register (SR) contains an
00134   // execution mode and interrupt level masks corresponding to a level 0
00135   // interrupt, whatever the interrupt priority level causing the spurious
00136   // event. This behavior has been chosen because a spurious interrupt has not
00137   // to be a priority one and because it may not cause any trouble to other
00138   // interrupts.
00139   // However, these spurious interrupts place the hardware in an unstable state
00140   // and could give problems in other/future versions of the CPU, so the
00141   // software has to be written so that they never occur. The only safe way of
00142   // achieving this is to always clear or disable peripheral interrupts with the
00143   // following sequence:
00144   // 1: Mask the interrupt in the CPU by setting GM (or IxM) in SR.
00145   // 2: Perform the bus access to the peripheral register that clears or
00146   //    disables the interrupt.
00147   // 3: Wait until the interrupt has actually been cleared or disabled by the
00148   //    peripheral. This is usually performed by reading from a register in the
00149   //    same peripheral (it DOES NOT have to be the same register that was
00150   //    accessed in step 2, but it MUST be in the same peripheral), what takes
00151   //    bus system latencies into account, but peripheral internal latencies
00152   //    (generally 0 cycle) also have to be considered.
00153   // 4: Unmask the interrupt in the CPU by clearing GM (or IxM) in SR.
00154   // Note that steps 1 and 4 are useless inside interrupt handlers as the
00155   // corresponding interrupt level is automatically masked by IxM (unless IxM is
00156   // explicitly cleared by the software).
00157   //
00158   // Get the right IRQ handler.
00159   //
00160   // If several interrupt lines are active in the group, the interrupt line with
00161   // the highest number is selected. This is to be coherent with the
00162   // prioritization of interrupt groups performed by the hardware interrupt
00163   // controller.
00164   //
00165   // If no handler has been registered for the pending interrupt,
00166   // _unhandled_interrupt will be selected thanks to the initialization of
00167   // _int_line_handler_table_x by INTC_init_interrupts.
00168   //
00169   // exception.S will provide the interrupt handler with a clean interrupt stack
00170   // frame, with nothing more pushed onto the stack. The interrupt handler must
00171   // manage the `rete' instruction, what can be done thanks to pure assembly,
00172   // inline assembly or the `__attribute__((__interrupt__))' C function
00173   // attribute.
00174   return (int_req) ? _int_handler_table[int_grp]._int_line_handler_table[32 - __builtin_clz(int_req) - 1] : NULL;
00175 }
00176 
00178 static __inline__ void INTC_init_evba(void)
00179 {
00180   Set_system_register(AVR32_EVBA, (int)&_evba );
00181 }
00182 
00183 void init_interrupts(void)
00184 {
00185   unsigned int int_grp, int_req;
00186 
00187   INTC_init_evba();
00188 
00189   // For all interrupt groups,
00190   for (int_grp = 0; int_grp < AVR32_INTC_NUM_INT_GRPS; int_grp++)
00191   {
00192     // For all interrupt request lines of each group,
00193     for (int_req = 0; int_req < _int_handler_table[int_grp].num_irqs; int_req++)
00194     {
00195       // Assign _unhandled_interrupt as default interrupt handler.
00196       _int_handler_table[int_grp]._int_line_handler_table[int_req] = &_unhandled_interrupt;
00197     }
00198 
00199     // Set the interrupt group priority register to its default value.
00200     // By default, all interrupt groups are linked to the interrupt priority
00201     // level 0 and to the interrupt vector _int0.
00202     AVR32_INTC.ipr[int_grp] = ipr_val[AVR32_INTC_INT0];
00203   }
00204 }
00205 
00206 
00207 void register_interrupt(__int_handler handler, unsigned int irq, unsigned int int_level)
00208 {
00209   // Determine the group of the IRQ.
00210   unsigned int int_grp = irq / AVR32_INTC_MAX_NUM_IRQS_PER_GRP;
00211 
00212   // Store in _int_line_handler_table_x the pointer to the interrupt handler, so
00213   // that _get_interrupt_handler can retrieve it when the interrupt is vectored.
00214   _int_handler_table[int_grp]._int_line_handler_table[irq % AVR32_INTC_MAX_NUM_IRQS_PER_GRP] = handler;
00215 
00216   // Program the corresponding IPRX register to set the interrupt priority level
00217   // and the interrupt vector offset that will be fetched by the core interrupt
00218   // system.
00219   // NOTE: The _intx functions are intermediate assembly functions between the
00220   // core interrupt system and the user interrupt handler.
00221   AVR32_INTC.ipr[int_grp] = ipr_val[int_level & (AVR32_INTC_IPR_INTLEVEL_MASK >> AVR32_INTC_IPR_INTLEVEL_OFFSET)];
00222 }