intc.c 9.1 KB

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