1 /**************************************************************************//**
2 * @file cmsis_armclang.h
3 * @brief CMSIS compiler specific macros, functions, instructions
6 ******************************************************************************/
8 * Copyright (c) 2009-2021 Arm Limited. All rights reserved.
10 * SPDX-License-Identifier: Apache-2.0
12 * Licensed under the Apache License, Version 2.0 (the License); you may
13 * not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
16 * www.apache.org/licenses/LICENSE-2.0
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
25 #ifndef __CMSIS_ARMCLANG_H
26 #define __CMSIS_ARMCLANG_H
28 #pragma clang system_header /* treat file as system include file */
30 /* CMSIS compiler specific defines */
35 #define __INLINE __inline
38 #define __FORCEINLINE __attribute__((always_inline))
40 #ifndef __STATIC_INLINE
41 #define __STATIC_INLINE static __inline
43 #ifndef __STATIC_FORCEINLINE
44 #define __STATIC_FORCEINLINE __attribute__((always_inline)) static __inline
47 #define __NO_RETURN __attribute__((__noreturn__))
49 #ifndef CMSIS_DEPRECATED
50 #define CMSIS_DEPRECATED __attribute__((deprecated))
53 #define __USED __attribute__((used))
56 #define __WEAK __attribute__((weak))
59 #define __PACKED __attribute__((packed, aligned(1)))
61 #ifndef __PACKED_STRUCT
62 #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
64 #ifndef __UNALIGNED_UINT16_WRITE
65 #pragma clang diagnostic push
66 #pragma clang diagnostic ignored "-Wpacked"
67 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
68 __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
69 #pragma clang diagnostic pop
70 #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
72 #ifndef __UNALIGNED_UINT16_READ
73 #pragma clang diagnostic push
74 #pragma clang diagnostic ignored "-Wpacked"
75 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
76 __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
77 #pragma clang diagnostic pop
78 #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
80 #ifndef __UNALIGNED_UINT32_WRITE
81 #pragma clang diagnostic push
82 #pragma clang diagnostic ignored "-Wpacked"
83 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
84 __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
85 #pragma clang diagnostic pop
86 #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
88 #ifndef __UNALIGNED_UINT32_READ
89 #pragma clang diagnostic push
90 #pragma clang diagnostic ignored "-Wpacked"
91 __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
92 #pragma clang diagnostic pop
93 #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
96 #define __ALIGNED(x) __attribute__((aligned(x)))
99 #define __PACKED __attribute__((packed))
101 #ifndef __COMPILER_BARRIER
102 #define __COMPILER_BARRIER() __ASM volatile("":::"memory")
105 /* ########################## Core Instruction Access ######################### */
109 #define __NOP __builtin_arm_nop
112 \brief Wait For Interrupt
114 #define __WFI __builtin_arm_wfi
117 \brief Wait For Event
119 #define __WFE __builtin_arm_wfe
124 #define __SEV __builtin_arm_sev
127 \brief Instruction Synchronization Barrier
129 #define __ISB() __builtin_arm_isb(0xF)
132 \brief Data Synchronization Barrier
134 #define __DSB() __builtin_arm_dsb(0xF)
137 \brief Data Memory Barrier
139 #define __DMB() __builtin_arm_dmb(0xF)
142 \brief Reverse byte order (32 bit)
143 \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
144 \param [in] value Value to reverse
145 \return Reversed value
147 #define __REV(value) __builtin_bswap32(value)
150 \brief Reverse byte order (16 bit)
151 \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
152 \param [in] value Value to reverse
153 \return Reversed value
155 #define __REV16(value) __ROR(__REV(value), 16)
159 \brief Reverse byte order (16 bit)
160 \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
161 \param [in] value Value to reverse
162 \return Reversed value
164 #define __REVSH(value) (int16_t)__builtin_bswap16(value)
168 \brief Rotate Right in unsigned value (32 bit)
169 \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
170 \param [in] op1 Value to rotate
171 \param [in] op2 Number of Bits to rotate
172 \return Rotated value
174 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
181 return (op1 >> op2) | (op1 << (32U - op2));
187 \param [in] value is ignored by the processor.
188 If required, a debugger can use it to store additional information about the breakpoint.
190 #define __BKPT(value) __ASM volatile ("bkpt "#value)
193 \brief Reverse bit order of value
194 \param [in] value Value to reverse
195 \return Reversed value
197 #define __RBIT __builtin_arm_rbit
200 \brief Count leading zeros
201 \param [in] value Value to count the leading zeros
202 \return number of leading zeros in value
204 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
206 /* Even though __builtin_clz produces a CLZ instruction on ARM, formally
207 __builtin_clz(0) is undefined behaviour, so handle this case specially.
208 This guarantees ARM-compatible results if happening to compile on a non-ARM
209 target, and ensures the compiler doesn't decide to activate any
210 optimisations using the logic "value was passed to __builtin_clz, so it
212 ARM Compiler 6.10 and possibly earlier will optimise this test away, leaving a
213 single CLZ instruction.
219 return __builtin_clz(value);
223 \brief LDR Exclusive (8 bit)
224 \details Executes a exclusive LDR instruction for 8 bit value.
225 \param [in] ptr Pointer to data
226 \return value of type uint8_t at (*ptr)
228 #define __LDREXB (uint8_t)__builtin_arm_ldrex
232 \brief LDR Exclusive (16 bit)
233 \details Executes a exclusive LDR instruction for 16 bit values.
234 \param [in] ptr Pointer to data
235 \return value of type uint16_t at (*ptr)
237 #define __LDREXH (uint16_t)__builtin_arm_ldrex
240 \brief LDR Exclusive (32 bit)
241 \details Executes a exclusive LDR instruction for 32 bit values.
242 \param [in] ptr Pointer to data
243 \return value of type uint32_t at (*ptr)
245 #define __LDREXW (uint32_t)__builtin_arm_ldrex
248 \brief STR Exclusive (8 bit)
249 \details Executes a exclusive STR instruction for 8 bit values.
250 \param [in] value Value to store
251 \param [in] ptr Pointer to location
252 \return 0 Function succeeded
253 \return 1 Function failed
255 #define __STREXB (uint32_t)__builtin_arm_strex
258 \brief STR Exclusive (16 bit)
259 \details Executes a exclusive STR instruction for 16 bit values.
260 \param [in] value Value to store
261 \param [in] ptr Pointer to location
262 \return 0 Function succeeded
263 \return 1 Function failed
265 #define __STREXH (uint32_t)__builtin_arm_strex
268 \brief STR Exclusive (32 bit)
269 \details Executes a exclusive STR instruction for 32 bit values.
270 \param [in] value Value to store
271 \param [in] ptr Pointer to location
272 \return 0 Function succeeded
273 \return 1 Function failed
275 #define __STREXW (uint32_t)__builtin_arm_strex
278 \brief Remove the exclusive lock
279 \details Removes the exclusive lock which is created by LDREX.
281 #define __CLREX __builtin_arm_clrex
284 \brief Signed Saturate
285 \details Saturates a signed value.
286 \param [in] value Value to be saturated
287 \param [in] sat Bit position to saturate to (1..32)
288 \return Saturated value
290 #define __SSAT __builtin_arm_ssat
293 \brief Unsigned Saturate
294 \details Saturates an unsigned value.
295 \param [in] value Value to be saturated
296 \param [in] sat Bit position to saturate to (0..31)
297 \return Saturated value
299 #define __USAT __builtin_arm_usat
301 /* ################### Compiler specific Intrinsics ########################### */
302 /** \defgroup CMSIS_SIMD_intrinsics CMSIS SIMD Intrinsics
303 Access to dedicated SIMD instructions
307 #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
309 #define __SADD8 __builtin_arm_sadd8
310 #define __SADD16 __builtin_arm_sadd16
311 #define __QADD8 __builtin_arm_qadd8
312 #define __QSUB8 __builtin_arm_qsub8
313 #define __QADD16 __builtin_arm_qadd16
314 #define __SHADD16 __builtin_arm_shadd16
315 #define __QSUB16 __builtin_arm_qsub16
316 #define __SHSUB16 __builtin_arm_shsub16
317 #define __QASX __builtin_arm_qasx
318 #define __SHASX __builtin_arm_shasx
319 #define __QSAX __builtin_arm_qsax
320 #define __SHSAX __builtin_arm_shsax
321 #define __SXTB16 __builtin_arm_sxtb16
322 #define __SMUAD __builtin_arm_smuad
323 #define __SMUADX __builtin_arm_smuadx
324 #define __SMLAD __builtin_arm_smlad
325 #define __SMLADX __builtin_arm_smladx
326 #define __SMLALD __builtin_arm_smlald
327 #define __SMLALDX __builtin_arm_smlaldx
328 #define __SMUSD __builtin_arm_smusd
329 #define __SMUSDX __builtin_arm_smusdx
330 #define __SMLSDX __builtin_arm_smlsdx
331 #define __USAT16 __builtin_arm_usat16
332 #define __SSUB8 __builtin_arm_ssub8
333 #define __SXTB16 __builtin_arm_sxtb16
334 #define __SXTAB16 __builtin_arm_sxtab16
337 __STATIC_FORCEINLINE int32_t __QADD( int32_t op1, int32_t op2)
341 __ASM volatile ("qadd %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
345 __STATIC_FORCEINLINE int32_t __QSUB( int32_t op1, int32_t op2)
349 __ASM volatile ("qsub %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
353 #define __PKHBT(ARG1,ARG2,ARG3) ( ((((uint32_t)(ARG1)) ) & 0x0000FFFFUL) | \
354 ((((uint32_t)(ARG2)) << (ARG3)) & 0xFFFF0000UL) )
356 #define __PKHTB(ARG1,ARG2,ARG3) ( ((((uint32_t)(ARG1)) ) & 0xFFFF0000UL) | \
357 ((((uint32_t)(ARG2)) >> (ARG3)) & 0x0000FFFFUL) )
359 __STATIC_FORCEINLINE int32_t __SMMLA (int32_t op1, int32_t op2, int32_t op3)
363 __ASM volatile ("smmla %0, %1, %2, %3" : "=r" (result): "r" (op1), "r" (op2), "r" (op3) );
367 #endif /* (__ARM_FEATURE_DSP == 1) */
369 /* ########################### Core Function Access ########################### */
372 \brief Enable IRQ Interrupts
373 \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
374 Can only be executed in Privileged modes.
376 __STATIC_FORCEINLINE void __enable_irq(void)
378 __ASM volatile ("cpsie i" : : : "memory");
382 \brief Disable IRQ Interrupts
383 \details Disables IRQ interrupts by setting the I-bit in the CPSR.
384 Can only be executed in Privileged modes.
386 __STATIC_FORCEINLINE void __disable_irq(void)
388 __ASM volatile ("cpsid i" : : : "memory");
393 \details Enables FIQ interrupts by clearing the F-bit in the CPSR.
394 Can only be executed in Privileged modes.
396 __STATIC_FORCEINLINE void __enable_fault_irq(void)
398 __ASM volatile ("cpsie f" : : : "memory");
403 \details Disables FIQ interrupts by setting the F-bit in the CPSR.
404 Can only be executed in Privileged modes.
406 __STATIC_FORCEINLINE void __disable_fault_irq(void)
408 __ASM volatile ("cpsid f" : : : "memory");
413 \details Returns the current value of the Floating Point Status/Control register.
414 \return Floating Point Status/Control register value
416 #define __get_FPSCR __builtin_arm_get_fpscr
420 \details Assigns the given value to the Floating Point Status/Control register.
421 \param [in] fpscr Floating Point Status/Control value to set
423 #define __set_FPSCR __builtin_arm_set_fpscr
425 /** \brief Get CPSR Register
426 \return CPSR Register value
428 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
431 __ASM volatile("MRS %0, cpsr" : "=r" (result) );
435 /** \brief Set CPSR Register
436 \param [in] cpsr CPSR value to set
438 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
440 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "cc", "memory");
444 \return Processor Mode
446 __STATIC_FORCEINLINE uint32_t __get_mode(void)
448 return (__get_CPSR() & 0x1FU);
452 \param [in] mode Mode value to set
454 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
456 __ASM volatile("MSR cpsr_c, %0" : : "r" (mode) : "memory");
459 /** \brief Get Stack Pointer
460 \return Stack Pointer value
462 __STATIC_FORCEINLINE uint32_t __get_SP(void)
465 __ASM volatile("MOV %0, sp" : "=r" (result) : : "memory");
469 /** \brief Set Stack Pointer
470 \param [in] stack Stack Pointer value to set
472 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
474 __ASM volatile("MOV sp, %0" : : "r" (stack) : "memory");
477 /** \brief Get USR/SYS Stack Pointer
478 \return USR/SYS Stack Pointer value
480 __STATIC_FORCEINLINE uint32_t __get_SP_usr(void)
486 "CPS #0x1F \n" // no effect in USR mode
488 "MSR cpsr_c, %0 \n" // no effect in USR mode
489 "ISB" : "=r"(cpsr), "=r"(result) : : "memory"
494 /** \brief Set USR/SYS Stack Pointer
495 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
497 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
502 "CPS #0x1F \n" // no effect in USR mode
504 "MSR cpsr_c, %0 \n" // no effect in USR mode
505 "ISB" : "=r"(cpsr) : "r" (topOfProcStack) : "memory"
510 \return Floating Point Exception Control register value
512 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
514 #if (__FPU_PRESENT == 1)
516 __ASM volatile("VMRS %0, fpexc" : "=r" (result) : : "memory");
524 \param [in] fpexc Floating Point Exception Control value to set
526 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
528 #if (__FPU_PRESENT == 1)
529 __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
534 * Include common core functions to access Coprocessor 15 registers
537 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
538 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
539 #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : "=r" (Rt) : : "memory" )
540 #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : : "r" (Rt) : "memory" )
542 #include "cmsis_cp15.h"
544 /** \brief Enable Floating Point Unit
546 Critical section, called from undef handler, so systick is disabled
548 __STATIC_INLINE void __FPU_Enable(void)
551 //Permit access to VFP/NEON, registers by modifying CPACR
552 " MRC p15,0,R1,c1,c0,2 \n"
553 " ORR R1,R1,#0x00F00000 \n"
554 " MCR p15,0,R1,c1,c0,2 \n"
556 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
561 " ORR R1,R1,#0x40000000 \n"
564 //Initialise VFP/NEON registers to 0
567 //Initialise D16 registers to 0
585 #if (defined(__ARM_NEON) && (__ARM_NEON == 1))
586 //Initialise D32 registers to 0
605 //Initialise FPSCR to a known state
607 " LDR R2,=0x00086060 \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
610 : : : "cc", "r1", "r2"
614 #endif /* __CMSIS_ARMCLANG_H */