interrupt_avr8.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * \file
  3. *
  4. * \brief Global interrupt management for 8-bit AVR
  5. *
  6. * Copyright (C) 2010-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef UTILS_INTERRUPT_INTERRUPT_H
  47. #define UTILS_INTERRUPT_INTERRUPT_H
  48. #include <compiler.h>
  49. #include <parts.h>
  50. /**
  51. * \weakgroup interrupt_group
  52. *
  53. * @{
  54. */
  55. #ifdef ISR_CUSTOM_H
  56. # include ISR_CUSTOM_H
  57. #else
  58. /**
  59. * \def ISR
  60. * \brief Define service routine for specified interrupt vector
  61. *
  62. * Usage:
  63. * \code
  64. ISR(FOO_vect)
  65. {
  66. ...
  67. }
  68. \endcode
  69. *
  70. * \param vect Interrupt vector name as found in the device header files.
  71. */
  72. #if defined(__DOXYGEN__)
  73. # define ISR(vect)
  74. #elif defined(__GNUC__)
  75. # include <avr/interrupt.h>
  76. #elif defined(__ICCAVR__)
  77. # define __ISR(x) _Pragma(#x)
  78. # define ISR(vect) __ISR(vector=vect) __interrupt void handler_##vect(void)
  79. #endif
  80. #endif // ISR_CUSTOM_H
  81. #if XMEGA
  82. /**
  83. * \brief Initialize interrupt vectors
  84. * Enables all interrupt levels, with vectors located in the application section
  85. * and fixed priority scheduling.
  86. */
  87. #define irq_initialize_vectors() \
  88. PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm | PMIC_HILVLEN_bm;
  89. #elif MEGA_RF
  90. #define irq_initialize_vectors()
  91. #endif
  92. #ifdef __GNUC__
  93. # define cpu_irq_enable() sei()
  94. # define cpu_irq_disable() cli()
  95. #else
  96. # define cpu_irq_enable() __enable_interrupt()
  97. # define cpu_irq_disable() __disable_interrupt()
  98. #endif
  99. typedef uint8_t irqflags_t;
  100. static inline irqflags_t cpu_irq_save(void)
  101. {
  102. volatile irqflags_t flags = SREG;
  103. cpu_irq_disable();
  104. return flags;
  105. }
  106. static inline void cpu_irq_restore(irqflags_t flags)
  107. {
  108. barrier();
  109. SREG = flags;
  110. }
  111. static inline bool cpu_irq_is_enabled_flags(irqflags_t flags)
  112. {
  113. #if XMEGA
  114. # ifdef __GNUC__
  115. return flags & CPU_I_bm;
  116. # else
  117. return flags & I_bm;
  118. # endif
  119. #elif MEGA || TINY
  120. return flags & (1 << SREG_I);
  121. #endif
  122. }
  123. #define cpu_irq_is_enabled() cpu_irq_is_enabled_flags(SREG)
  124. //! @}
  125. /**
  126. * \weakgroup interrupt_deprecated_group
  127. * @{
  128. */
  129. // Deprecated definitions.
  130. #define Enable_global_interrupt() cpu_irq_enable()
  131. #define Disable_global_interrupt() cpu_irq_disable()
  132. #define Is_global_interrupt_enabled() cpu_irq_is_enabled()
  133. //! @}
  134. #endif /* UTILS_INTERRUPT_INTERRUPT_H */