InterruptManagement.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. LUFA Library
  3. Copyright (C) Dean Camera, 2018.
  4. dean [at] fourwalledcubicle [dot] com
  5. www.lufa-lib.org
  6. */
  7. /*
  8. Copyright 2018 Dean Camera (dean [at] fourwalledcubicle [dot] com)
  9. Permission to use, copy, modify, distribute, and sell this
  10. software and its documentation for any purpose is hereby granted
  11. without fee, provided that the above copyright notice appear in
  12. all copies and that both that the copyright notice and this
  13. permission notice and warranty disclaimer appear in supporting
  14. documentation, and that the name of the author not be used in
  15. advertising or publicity pertaining to distribution of the
  16. software without specific, written prior permission.
  17. The author disclaims all warranties with regard to this
  18. software, including all implied warranties of merchantability
  19. and fitness. In no event shall the author be liable for any
  20. special, indirect or consequential damages or any damages
  21. whatsoever resulting from loss of use, data or profits, whether
  22. in an action of contract, negligence or other tortious action,
  23. arising out of or in connection with the use or performance of
  24. this software.
  25. */
  26. /** \file
  27. * \brief Interrupt Controller Driver for the AVR32 UC3 microcontrollers.
  28. *
  29. * Interrupt controller driver for the AVR32 UC3 microcontrollers, for the configuration of interrupt
  30. * handlers within the device.
  31. */
  32. /** \ingroup Group_PlatformDrivers_UC3
  33. * \defgroup Group_PlatformDrivers_UC3Interrupts Interrupt Controller Driver - LUFA/Platform/UC3/InterruptManagement.h
  34. * \brief Interrupt Controller Driver for the AVR32 UC3 microcontrollers.
  35. *
  36. * \section Sec_PlatformDrivers_UC3Interrupts_Dependencies Module Source Dependencies
  37. * The following files must be built with any user project that uses this module:
  38. * - LUFA/Platform/UC3/InterruptManagement.c <i>(Makefile source module name: LUFA_SRC_PLATFORM)</i>
  39. * - LUFA/Platform/UC3/Exception.S <i>(Makefile source module name: LUFA_SRC_PLATFORM)</i>
  40. *
  41. * \section Sec_PlatformDrivers_UC3Interrupts_ModDescription Module Description
  42. * Interrupt controller driver for the AVR32 UC3 microcontrollers, for the configuration of interrupt
  43. * handlers within the device.
  44. *
  45. * Usage Example:
  46. * \code
  47. * #include <LUFA/Platform/UC3/InterruptManagement.h>
  48. *
  49. * ISR(USB_Group_IRQ_Handler)
  50. * {
  51. * // USB group handler code here
  52. * }
  53. *
  54. * void main(void)
  55. * {
  56. * INTC_Init();
  57. * INTC_RegisterGroupHandler(INTC_IRQ_GROUP(AVR32_USBB_IRQ), AVR32_INTC_INT0, USB_Group_IRQ_Handler);
  58. * }
  59. * \endcode
  60. *
  61. * @{
  62. */
  63. #ifndef _UC3_INTERRUPT_MANAGEMENT_H_
  64. #define _UC3_INTERRUPT_MANAGEMENT_H_
  65. /* Includes: */
  66. #include "../../Common/Common.h"
  67. /* Enable C linkage for C++ Compilers: */
  68. #if defined(__cplusplus)
  69. extern "C" {
  70. #endif
  71. /* Private Interface - For use in library only: */
  72. #if !defined(__DOXYGEN__)
  73. /* Type Defines: */
  74. typedef void (*InterruptHandlerPtr_t)(void);
  75. /* External Variables: */
  76. #if defined(__INCLUDE_FROM_INTMANAGEMENT_C)
  77. extern const void EVBA_Table;
  78. #endif
  79. extern InterruptHandlerPtr_t InterruptHandlers[AVR32_INTC_NUM_INT_GRPS];
  80. extern const uint32_t Autovector_Table[];
  81. #endif
  82. /* Public Interface - May be used in end-application: */
  83. /* Macros: */
  84. /** Converts a given interrupt index into its associated interrupt group.
  85. *
  86. * \param[in] IRQIndex Index of the interrupt request to convert.
  87. *
  88. * \return Interrupt group number associated with the interrupt index.
  89. */
  90. #define INTC_IRQ_GROUP(IRQIndex) (IRQIndex / 32)
  91. /** Converts a given interrupt index into its associated interrupt line.
  92. *
  93. * \param[in] IRQIndex Index of the interrupt request to convert.
  94. *
  95. * \return Interrupt line number associated with the interrupt index.
  96. */
  97. #define INTC_IRQ_LINE(IRQIndex) (IRQIndex % 32)
  98. /* Function Prototypes: */
  99. /** Initializes the interrupt controller ready to handle interrupts. This must be called at the
  100. * start of the user program before any interrupts are registered or enabled.
  101. */
  102. void INTC_Init(void);
  103. /** Retrieves the associated interrupt handler for the interrupt group currently being fired. This
  104. * is called directly from the exception handler routine before dispatching to the ISR.
  105. *
  106. * \param[in] InterruptLevel Priority level of the interrupt.
  107. *
  108. * \return Pointer to the associated interrupt handler function, or NULL if no handler set.
  109. */
  110. InterruptHandlerPtr_t INTC_GetInterruptHandler(const uint_reg_t InterruptLevel);
  111. /* Inline Functions: */
  112. /** Registers a handler for a given interrupt group. On the AVR32 UC3 devices, interrupts are grouped by
  113. * peripheral. To save on SRAM used, a single ISR handles all interrupt lines within a single group - to
  114. * determine the exact line that has interrupted within the group ISR handler, use \ref INTC_GetGroupInterrupts().
  115. *
  116. * If multiple interrupts with the same group are registered, the last registered handler will become the
  117. * handler called for interrupts raised within that group.
  118. *
  119. * To obtain the group number of a specific interrupt index, use the \ref INTC_IRQ_GROUP() macro.
  120. *
  121. * \param[in] GroupNumber Group number of the interrupt group to register a handler for.
  122. * \param[in] InterruptLevel Priority level for the specified interrupt, a \c AVR32_INTC_INT* mask.
  123. * \param[in] Handler Address of the ISR handler for the interrupt group.
  124. */
  125. static inline void INTC_RegisterGroupHandler(const uint16_t GroupNumber,
  126. const uint8_t InterruptLevel,
  127. const InterruptHandlerPtr_t Handler) ATTR_ALWAYS_INLINE;
  128. static inline void INTC_RegisterGroupHandler(const uint16_t GroupNumber,
  129. const uint8_t InterruptLevel,
  130. const InterruptHandlerPtr_t Handler)
  131. {
  132. InterruptHandlers[GroupNumber] = Handler;
  133. AVR32_INTC.ipr[GroupNumber] = Autovector_Table[InterruptLevel];
  134. }
  135. /** Retrieves the pending interrupts for a given interrupt group. The result of this function should be masked
  136. * against interrupt request indexes converted to a request line number via the \ref INTC_IRQ_LINE() macro. To
  137. * obtain the group number of a given interrupt request, use the \ref INTC_IRQ_GROUP() macro.
  138. *
  139. * \param[in] GroupNumber Group number of the interrupt group to check.
  140. *
  141. * \return Mask of pending interrupt lines for the given interrupt group.
  142. */
  143. static inline uint_reg_t INTC_GetGroupInterrupts(const uint16_t GroupNumber) ATTR_ALWAYS_INLINE;
  144. static inline uint_reg_t INTC_GetGroupInterrupts(const uint16_t GroupNumber)
  145. {
  146. return AVR32_INTC.irr[GroupNumber];
  147. }
  148. /* Disable C linkage for C++ Compilers: */
  149. #if defined(__cplusplus)
  150. }
  151. #endif
  152. #endif
  153. /** @} */