interrupt.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2017 XRADIO TECHNOLOGY CO., LTD. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. Neither the name of XRADIO TECHNOLOGY CO., LTD. nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef _SYS_INTERRUPT_H_
  30. #define _SYS_INTERRUPT_H_
  31. #include "compiler.h"
  32. #if defined(__CC_ARM)
  33. /* ARM Compiler */
  34. /*
  35. * CPU interrupt mask handling.
  36. */
  37. #define IRQMASK_REG_NAME_R primask
  38. #define IRQMASK_REG_NAME_W primask
  39. /*
  40. * Save the current interrupt enable state & disable IRQs
  41. */
  42. static __always_inline unsigned long arch_irq_save(void)
  43. {
  44. unsigned long flags;
  45. __asm {
  46. mrs flags, IRQMASK_REG_NAME_R
  47. cpsid i
  48. }
  49. return flags;
  50. }
  51. /*
  52. * restore saved IRQ state
  53. */
  54. static __always_inline void arch_irq_restore(unsigned long flags)
  55. {
  56. __asm { msr IRQMASK_REG_NAME_W, flags }
  57. }
  58. /*
  59. * Enable IRQs
  60. */
  61. #define arch_irq_enable() __enable_irq()
  62. /*
  63. * Disable IRQs
  64. */
  65. #define arch_irq_disable() __disable_irq()
  66. /*
  67. * Enable FIQs
  68. */
  69. #define arch_fiq_enable() __enable_fiq()
  70. /*
  71. * Disable FIQs
  72. */
  73. #define arch_fiq_disable() __disable_fiq()
  74. #elif defined(__GNUC__)
  75. /* GNU Compiler */
  76. #ifdef __CONFIG_OS_RTTHREAD
  77. /*
  78. * Save the current interrupt enable state & disable IRQs
  79. */
  80. #define arch_irq_save(void) rt_hw_interrupt_disable()
  81. /*
  82. * restore saved IRQ state
  83. */
  84. #define arch_irq_restore(flags) rt_hw_interrupt_enable(flags)
  85. /*
  86. * Enable IRQs
  87. */
  88. #define arch_irq_enable() rt_hw_interrupt_enable(__irq_level)
  89. /*
  90. * Disable IRQs
  91. */
  92. #define arch_irq_disable() rt_base_t __irq_level = rt_hw_interrupt_disable()
  93. #else /* __CONFIG_OS_RTTHREAD */
  94. /*
  95. * CPU interrupt mask handling.
  96. */
  97. #define IRQMASK_REG_NAME_R "primask"
  98. #define IRQMASK_REG_NAME_W "primask"
  99. /*
  100. * Save the current interrupt enable state & disable IRQs
  101. */
  102. static __always_inline unsigned long arch_irq_save(void)
  103. {
  104. unsigned long flags;
  105. __asm volatile(
  106. "mrs %0, " IRQMASK_REG_NAME_R "\n"
  107. "cpsid i"
  108. : "=r" (flags) : : "memory", "cc");
  109. return flags;
  110. }
  111. /*
  112. * restore saved IRQ state
  113. */
  114. static __always_inline void arch_irq_restore(unsigned long flags)
  115. {
  116. __asm volatile(
  117. "msr " IRQMASK_REG_NAME_W ", %0"
  118. :
  119. : "r" (flags)
  120. : "memory", "cc");
  121. }
  122. /*
  123. * Save the current interrupt enable state.
  124. */
  125. static __always_inline unsigned long arch_irq_get_flags(void)
  126. {
  127. unsigned long flags;
  128. __asm volatile(
  129. "mrs %0, " IRQMASK_REG_NAME_R "\n"
  130. : "=r" (flags) : : "memory", "cc");
  131. return flags;
  132. }
  133. /*
  134. * Enable IRQs
  135. */
  136. #define arch_irq_enable() __asm volatile("cpsie i" : : : "memory", "cc")
  137. /*
  138. * Disable IRQs
  139. */
  140. #define arch_irq_disable() __asm volatile("cpsid i" : : : "memory", "cc")
  141. /*
  142. * Enable FIQs
  143. */
  144. #define arch_fiq_enable() __asm volatile("cpsie f" : : : "memory", "cc")
  145. /*
  146. * Disable FIQs
  147. */
  148. #define arch_fiq_disable() __asm volatile("cpsid f" : : : "memory", "cc")
  149. #endif /* __CONFIG_OS_RTTHREAD */
  150. #else
  151. #error "Compiler not supported."
  152. #endif
  153. #endif /* _SYS_INTERRUPT_H_ */