am_reg_macros_asm.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //*****************************************************************************
  2. //
  3. //! @file am_reg_macros_asm.h
  4. //!
  5. //! @brief Inline assembly macros. Initially for critical section handling in
  6. //! protecting hardware registers.
  7. //
  8. //*****************************************************************************
  9. //*****************************************************************************
  10. //
  11. // Copyright (c) 2017, Ambiq Micro
  12. // All rights reserved.
  13. //
  14. // Redistribution and use in source and binary forms, with or without
  15. // modification, are permitted provided that the following conditions are met:
  16. //
  17. // 1. Redistributions of source code must retain the above copyright notice,
  18. // this list of conditions and the following disclaimer.
  19. //
  20. // 2. Redistributions in binary form must reproduce the above copyright
  21. // notice, this list of conditions and the following disclaimer in the
  22. // documentation and/or other materials provided with the distribution.
  23. //
  24. // 3. Neither the name of the copyright holder nor the names of its
  25. // contributors may be used to endorse or promote products derived from this
  26. // software without specific prior written permission.
  27. //
  28. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  32. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. // POSSIBILITY OF SUCH DAMAGE.
  39. //
  40. // This is part of revision 1.2.11 of the AmbiqSuite Development Package.
  41. //
  42. //*****************************************************************************
  43. #ifndef AM_REG_MACROS_ASM_H
  44. #define AM_REG_MACROS_ASM_H
  45. #ifdef __cplusplus
  46. extern "C"
  47. {
  48. #endif
  49. //*****************************************************************************
  50. //
  51. // Critical section assembly macros
  52. //
  53. // These macros implement critical section protection using inline assembly
  54. // for various compilers. They are intended to be used in other register
  55. // macros or directly in sections of code.
  56. //
  57. // Important usage note: These macros create a local scope and therefore MUST
  58. // be used in pairs.
  59. //
  60. //*****************************************************************************
  61. #if defined(__GNUC_STDC_INLINE__)
  62. //
  63. // GCC macros.
  64. //
  65. #define AM_CRITICAL_BEGIN_ASM \
  66. if ( 1 ) \
  67. { \
  68. volatile uint32_t ui32Primask_04172010; \
  69. __asm(" mrs %0, PRIMASK" : "=r"(ui32Primask_04172010)); \
  70. __asm(" cpsid i");
  71. #define AM_CRITICAL_END_ASM \
  72. __asm(" msr PRIMASK, %0" : : "r"(ui32Primask_04172010)); \
  73. }
  74. #elif defined(__ARMCC_VERSION)
  75. //
  76. // ARM/Keil macros.
  77. //
  78. #define AM_CRITICAL_BEGIN_ASM \
  79. if ( 1 ) \
  80. { \
  81. volatile uint32_t ui32Primask_04172010; \
  82. __asm \
  83. { \
  84. mrs ui32Primask_04172010, PRIMASK; \
  85. cpsid i; \
  86. }
  87. #define AM_CRITICAL_END_ASM \
  88. __asm \
  89. { \
  90. msr PRIMASK, ui32Primask_04172010; \
  91. } \
  92. }
  93. #elif defined(__IAR_SYSTEMS_ICC__)
  94. //
  95. // IAR macros.
  96. //
  97. #define AM_CRITICAL_BEGIN_ASM \
  98. if ( 1 ) \
  99. { \
  100. volatile uint32_t ui32Primask_04172010; \
  101. __asm(" mrs %0, PRIMASK" : "=r"(ui32Primask_04172010)); \
  102. __asm(" cpsid i");
  103. #define AM_CRITICAL_END_ASM \
  104. __asm(" msr PRIMASK, %0" : : "r"(ui32Primask_04172010)); \
  105. }
  106. #endif
  107. //*****************************************************************************
  108. //
  109. // A collection of some common inline assembly instructions / intrinsics.
  110. //
  111. //*****************************************************************************
  112. //
  113. // AM_ASM_BKPT(n)
  114. //
  115. #if defined(__ARMCC_VERSION)
  116. #define AM_ASM_BKPT(n) __breakpoint(n)
  117. #elif defined(__IAR_SYSTEMS_ICC__)
  118. #define AM_ASM_BKPT(n) asm(" bkpt "#n);
  119. #else
  120. #define AM_ASM_BKPT(n) __asm(" bkpt "#n);
  121. #endif
  122. //
  123. // AM_ASM_WFI
  124. //
  125. #if defined(__ARMCC_VERSION)
  126. #define AM_ASM_WFI __wfi();
  127. #elif defined(__IAR_SYSTEMS_ICC__)
  128. #define AM_ASM_WFI asm(" wfi");
  129. #else
  130. #define AM_ASM_WFI __asm(" wfi");
  131. #endif
  132. //
  133. // AM_ASM_NOP
  134. //
  135. #if defined(__ARMCC_VERSION)
  136. #define AM_ASM_NOP __nop();
  137. #elif defined(__IAR_SYSTEMS_ICC__)
  138. #define AM_ASM_NOP asm(" nop");
  139. #else
  140. #define AM_ASM_NOP __asm(" nop");
  141. #endif
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif // AM_REG_MACROS_ASM_H