intc.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /**
  2. * \file
  3. *
  4. * \brief INTC software driver for AVR UC3 devices.
  5. *
  6. * Copyright (c) 2009-2018 Microchip Technology Inc. and its subsidiaries.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Subject to your compliance with these terms, you may use Microchip
  13. * software and any derivatives exclusively with Microchip products.
  14. * It is your responsibility to comply with third party license terms applicable
  15. * to your use of third party software (including open source software) that
  16. * may accompany Microchip software.
  17. *
  18. * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
  19. * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
  20. * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
  21. * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
  22. * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
  23. * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
  24. * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
  25. * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT
  26. * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
  27. * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
  28. * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
  29. *
  30. * \asf_license_stop
  31. *
  32. */
  33. /*
  34. * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
  35. */
  36. #include <avr32/io.h>
  37. #include "compiler.h"
  38. #include "preprocessor.h"
  39. #include "intc.h"
  40. /**
  41. * \internal
  42. * \brief Import the _evba symbol from exception.S
  43. */
  44. extern void _evba;
  45. /**
  46. * \internal
  47. * \brief Import the symbols _int0, _int1, _int2, _int3 defined in exception.S
  48. */
  49. extern void _int0, _int1, _int2, _int3;
  50. /**
  51. * \internal
  52. * \brief Values to store in the interrupt priority registers for the various
  53. * interrupt priority levels.
  54. */
  55. #define IPR_INT0 ((AVR32_INTC_INT0 << AVR32_INTC_IPR_INTLEVEL_OFFSET) \
  56. | ((int)&_int0 - (int)&_evba))
  57. #define IPR_INT1 ((AVR32_INTC_INT1 << AVR32_INTC_IPR_INTLEVEL_OFFSET) \
  58. | ((int)&_int1 - (int)&_evba))
  59. #define IPR_INT2 ((AVR32_INTC_INT2 << AVR32_INTC_IPR_INTLEVEL_OFFSET) \
  60. | ((int)&_int2 - (int)&_evba))
  61. #define IPR_INT3 ((AVR32_INTC_INT3 << AVR32_INTC_IPR_INTLEVEL_OFFSET) \
  62. | ((int)&_int3 - (int)&_evba))
  63. /**
  64. * \internal
  65. * \brief Table of interrupt line handlers per interrupt group in order to
  66. * optimize RAM space. Each line handler table contains a set of pointers to
  67. * interrupt handlers.
  68. */
  69. #if (defined __GNUC__)
  70. # define DECL_INT_LINE_HANDLER_TABLE(GRP, unused) \
  71. static volatile __int_handler \
  72. _int_line_handler_table_##GRP[Max(AVR32_INTC_NUM_IRQS_PER_GRP##GRP, 1)];
  73. #elif (defined __ICCAVR32__)
  74. # define DECL_INT_LINE_HANDLER_TABLE(GRP, unused) \
  75. static volatile __no_init __int_handler \
  76. _int_line_handler_table_##GRP[Max(AVR32_INTC_NUM_IRQS_PER_GRP##GRP, 1)];
  77. #endif
  78. MREPEAT(AVR32_INTC_NUM_INT_GRPS, DECL_INT_LINE_HANDLER_TABLE, ~);
  79. #undef DECL_INT_LINE_HANDLER_TABLE
  80. /**
  81. * \internal
  82. * \brief Table containing for each interrupt group the number of interrupt
  83. * request lines and a pointer to the table of interrupt line handlers.
  84. */
  85. static const struct
  86. {
  87. unsigned int num_irqs;
  88. volatile __int_handler *_int_line_handler_table;
  89. } _int_handler_table[AVR32_INTC_NUM_INT_GRPS] =
  90. {
  91. #define INSERT_INT_LINE_HANDLER_TABLE(GRP, unused) \
  92. {AVR32_INTC_NUM_IRQS_PER_GRP##GRP, _int_line_handler_table_##GRP},
  93. MREPEAT(AVR32_INTC_NUM_INT_GRPS, INSERT_INT_LINE_HANDLER_TABLE, ~)
  94. #undef INSERT_INT_LINE_HANDLER_TABLE
  95. };
  96. /**
  97. * \internal
  98. * \brief Default interrupt handler.
  99. */
  100. #if (defined __GNUC__)
  101. __attribute__((__interrupt__))
  102. #elif (defined __ICCAVR32__)
  103. __interrupt
  104. #endif
  105. static void _unhandled_interrupt(void)
  106. {
  107. // Catch unregistered interrupts.
  108. while (true);
  109. }
  110. /**
  111. * \brief Gets the interrupt handler of the current event at the \a int_level
  112. * interrupt priority level (called from exception.S).
  113. *
  114. * \param int_level Interrupt priority level to handle.
  115. *
  116. * \return Interrupt handler to execute.
  117. */
  118. __int_handler _get_interrupt_handler(uint32_t int_level);
  119. __int_handler _get_interrupt_handler(uint32_t int_level)
  120. {
  121. /* ICR3 is mapped first, ICR0 last.
  122. Code in exception.S puts int_level in R12 which is used by the compiler
  123. to pass a single argument to a function. */
  124. uint32_t int_grp = AVR32_INTC.icr[AVR32_INTC_INT3 - int_level];
  125. uint32_t int_req = AVR32_INTC.irr[int_grp];
  126. /* As an interrupt may disappear while it is being fetched by the CPU
  127. (spurious interrupt caused by a delayed response from an MCU peripheral
  128. to an interrupt flag clear or interrupt disable instruction), check if
  129. there are remaining interrupt lines to process.
  130. If a spurious interrupt occurs, the status register (SR) contains an
  131. execution mode and interrupt level masks corresponding to a level 0
  132. interrupt, whatever the interrupt priority level causing the spurious
  133. event. This behavior has been chosen because a spurious interrupt has
  134. not to be a priority one and because it may not cause any trouble to
  135. other interrupts.
  136. However, these spurious interrupts place the hardware in an unstable
  137. state and could give problems in other/future versions of the CPU, so
  138. the software has to be written so that they never occur. The only safe
  139. way of achieving this is to always clear or disable peripheral
  140. interrupts with the following sequence:
  141. 1: Mask the interrupt in the CPU by setting GM (or IxM) in SR.
  142. 2: Perform the bus access to the peripheral register that clears or
  143. disables the interrupt.
  144. 3: Wait until the interrupt has actually been cleared or disabled by the
  145. peripheral. This is usually performed by reading from a register in the
  146. same peripheral (it DOES NOT have to be the same register that was
  147. accessed in step 2, but it MUST be in the same peripheral), what takes
  148. bus system latencies into account, but peripheral internal latencies
  149. (generally 0 cycle) also have to be considered.
  150. 4: Unmask the interrupt in the CPU by clearing GM (or IxM) in SR.
  151. Note that steps 1 and 4 are useless inside interrupt handlers as the
  152. corresponding interrupt level is automatically masked by IxM (unless IxM
  153. is explicitly cleared by the software).*/
  154. /* Get the right IRQ handler.
  155. If several interrupt lines are active in the group, the interrupt line
  156. with the highest number is selected. This is to be coherent with the
  157. prioritization of interrupt groups performed by the hardware interrupt
  158. controller.
  159. If no handler has been registered for the pending interrupt,
  160. _unhandled_interrupt will be selected thanks to the initialization of
  161. _int_line_handler_table_x by INTC_init_interrupts.
  162. exception.S will provide the interrupt handler with a clean interrupt
  163. stack frame, with nothing more pushed onto the stack. The interrupt
  164. handler must manage the `rete' instruction, which can be done using
  165. pure assembly, inline assembly or the `__attribute__((__interrupt__))'
  166. C function attribute.*/
  167. return (int_req)
  168. ? _int_handler_table[int_grp]._int_line_handler_table[32
  169. - clz(int_req) - 1]
  170. : NULL;
  171. }
  172. /**
  173. * \internal
  174. * \brief Init EVBA address. This operation may or may not have been done by the
  175. * C startup process.
  176. */
  177. static __inline__ void INTC_init_evba(void)
  178. {
  179. Set_system_register(AVR32_EVBA, (int32_t)&_evba );
  180. }
  181. /**
  182. * \brief Initializes the hardware interrupt controller driver.
  183. *
  184. */
  185. void INTC_init_interrupts(void)
  186. {
  187. uint32_t int_grp, int_req;
  188. INTC_init_evba();
  189. // For all interrupt groups,
  190. for (int_grp = 0; int_grp < AVR32_INTC_NUM_INT_GRPS; int_grp++)
  191. {
  192. // For all interrupt request lines of each group,
  193. for (int_req = 0;
  194. int_req < _int_handler_table[int_grp].num_irqs;
  195. int_req++)
  196. {
  197. /* Assign _unhandled_interrupt as the default interrupt
  198. handler. */
  199. _int_handler_table[int_grp]
  200. ._int_line_handler_table[int_req]
  201. = &_unhandled_interrupt;
  202. }
  203. /* Set the interrupt group priority register to its default
  204. value.
  205. By default, all interrupt groups are linked to the interrupt
  206. priority level 0 and to the interrupt vector _int0. */
  207. AVR32_INTC.ipr[int_grp] = IPR_INT0;
  208. }
  209. }
  210. /**
  211. * \brief Registers an interrupt handler.
  212. *
  213. * \param handler Interrupt handler to register.
  214. * \param irq IRQ of the interrupt handler to register.
  215. * \param int_level Interrupt priority level to assign to the group of this IRQ.
  216. *
  217. * \warning The interrupt handler must manage the `rete' instruction, which can
  218. * be done using pure assembly, inline assembly or the
  219. * `__attribute__((__interrupt__))' C function attribute.
  220. *
  221. * \warning If several interrupt handlers of a same group are registered with
  222. * different priority levels, only the latest priority level set will
  223. * be effective.
  224. *
  225. */
  226. void INTC_register_interrupt(__int_handler handler, uint32_t irq,
  227. uint32_t int_level)
  228. {
  229. // Determine the group of the IRQ.
  230. uint32_t int_grp = irq / AVR32_INTC_MAX_NUM_IRQS_PER_GRP;
  231. /* Store in _int_line_handler_table_x the pointer to the interrupt
  232. handler, so that _get_interrupt_handler can retrieve it when the
  233. interrupt is vectored. */
  234. _int_handler_table[int_grp]
  235. ._int_line_handler_table[irq % AVR32_INTC_MAX_NUM_IRQS_PER_GRP]
  236. = handler;
  237. /* Program the corresponding IPRX register to set the interrupt priority
  238. level and the interrupt vector offset that will be fetched by the core
  239. interrupt system.
  240. NOTE: The _intx functions are intermediate assembly functions between
  241. the core interrupt system and the user interrupt handler. */
  242. if (int_level == AVR32_INTC_INT0) {
  243. AVR32_INTC.ipr[int_grp] = IPR_INT0;
  244. } else if (int_level == AVR32_INTC_INT1) {
  245. AVR32_INTC.ipr[int_grp] = IPR_INT1;
  246. } else if (int_level == AVR32_INTC_INT2) {
  247. AVR32_INTC.ipr[int_grp] = IPR_INT2;
  248. } else {
  249. AVR32_INTC.ipr[int_grp] = IPR_INT3;
  250. }
  251. }