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