1 /**************************************************************************//**
2 * @file cmsis_armclang.h
3 * @brief CMSIS compiler specific macros, functions, instructions
6 ******************************************************************************/
8 * Copyright (c) 2009-2017 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 #ifndef __ARM_COMPAT_H
31 #include <arm_compat.h> /* Compatibility header for ARM Compiler 5 intrinsics */
34 /* CMSIS compiler specific defines */
39 #define __INLINE __inline
42 #define __FORCEINLINE __attribute__((always_inline))
44 #ifndef __STATIC_INLINE
45 #define __STATIC_INLINE static __inline
47 #ifndef __STATIC_FORCEINLINE
48 #define __STATIC_FORCEINLINE __attribute__((always_inline)) static __inline
51 #define __NO_RETURN __attribute__((__noreturn__))
54 #define __DEPRECATED __attribute__((deprecated))
57 #define __USED __attribute__((used))
60 #define __WEAK __attribute__((weak))
63 #define __PACKED __attribute__((packed, aligned(1)))
65 #ifndef __PACKED_STRUCT
66 #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
68 #ifndef __UNALIGNED_UINT16_WRITE
69 #pragma clang diagnostic push
70 #pragma clang diagnostic ignored "-Wpacked"
71 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
72 __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
73 #pragma clang diagnostic pop
74 #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
76 #ifndef __UNALIGNED_UINT16_READ
77 #pragma clang diagnostic push
78 #pragma clang diagnostic ignored "-Wpacked"
79 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
80 __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
81 #pragma clang diagnostic pop
82 #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
84 #ifndef __UNALIGNED_UINT32_WRITE
85 #pragma clang diagnostic push
86 #pragma clang diagnostic ignored "-Wpacked"
87 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
88 __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
89 #pragma clang diagnostic pop
90 #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
92 #ifndef __UNALIGNED_UINT32_READ
93 #pragma clang diagnostic push
94 #pragma clang diagnostic ignored "-Wpacked"
95 __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
96 #pragma clang diagnostic pop
97 #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
100 #define __ALIGNED(x) __attribute__((aligned(x)))
103 #define __PACKED __attribute__((packed))
106 /* ########################## Core Instruction Access ######################### */
110 #define __NOP __builtin_arm_nop
113 \brief Wait For Interrupt
115 #define __WFI __builtin_arm_wfi
118 \brief Wait For Event
120 #define __WFE __builtin_arm_wfe
125 #define __SEV __builtin_arm_sev
128 \brief Instruction Synchronization Barrier
130 #define __ISB() do {\
131 __schedule_barrier();\
132 __builtin_arm_isb(0xF);\
133 __schedule_barrier();\
137 \brief Data Synchronization Barrier
139 #define __DSB() do {\
140 __schedule_barrier();\
141 __builtin_arm_dsb(0xF);\
142 __schedule_barrier();\
146 \brief Data Memory Barrier
148 #define __DMB() do {\
149 __schedule_barrier();\
150 __builtin_arm_dmb(0xF);\
151 __schedule_barrier();\
155 \brief Reverse byte order (32 bit)
156 \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
157 \param [in] value Value to reverse
158 \return Reversed value
160 #define __REV(value) __builtin_bswap32(value)
163 \brief Reverse byte order (16 bit)
164 \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
165 \param [in] value Value to reverse
166 \return Reversed value
168 #define __REV16(value) __ROR(__REV(value), 16)
172 \brief Reverse byte order (16 bit)
173 \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
174 \param [in] value Value to reverse
175 \return Reversed value
177 #define __REVSH(value) (int16_t)__builtin_bswap16(value)
181 \brief Rotate Right in unsigned value (32 bit)
182 \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
183 \param [in] op1 Value to rotate
184 \param [in] op2 Number of Bits to rotate
185 \return Rotated value
187 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
194 return (op1 >> op2) | (op1 << (32U - op2));
200 \param [in] value is ignored by the processor.
201 If required, a debugger can use it to store additional information about the breakpoint.
203 #define __BKPT(value) __ASM volatile ("bkpt "#value)
206 \brief Reverse bit order of value
207 \param [in] value Value to reverse
208 \return Reversed value
210 #define __RBIT __builtin_arm_rbit
213 \brief Count leading zeros
214 \param [in] value Value to count the leading zeros
215 \return number of leading zeros in value
217 #define __CLZ (uint8_t)__builtin_clz
220 \brief LDR Exclusive (8 bit)
221 \details Executes a exclusive LDR instruction for 8 bit value.
222 \param [in] ptr Pointer to data
223 \return value of type uint8_t at (*ptr)
225 #define __LDREXB (uint8_t)__builtin_arm_ldrex
229 \brief LDR Exclusive (16 bit)
230 \details Executes a exclusive LDR instruction for 16 bit values.
231 \param [in] ptr Pointer to data
232 \return value of type uint16_t at (*ptr)
234 #define __LDREXH (uint16_t)__builtin_arm_ldrex
237 \brief LDR Exclusive (32 bit)
238 \details Executes a exclusive LDR instruction for 32 bit values.
239 \param [in] ptr Pointer to data
240 \return value of type uint32_t at (*ptr)
242 #define __LDREXW (uint32_t)__builtin_arm_ldrex
245 \brief STR Exclusive (8 bit)
246 \details Executes a exclusive STR instruction for 8 bit values.
247 \param [in] value Value to store
248 \param [in] ptr Pointer to location
249 \return 0 Function succeeded
250 \return 1 Function failed
252 #define __STREXB (uint32_t)__builtin_arm_strex
255 \brief STR Exclusive (16 bit)
256 \details Executes a exclusive STR instruction for 16 bit values.
257 \param [in] value Value to store
258 \param [in] ptr Pointer to location
259 \return 0 Function succeeded
260 \return 1 Function failed
262 #define __STREXH (uint32_t)__builtin_arm_strex
265 \brief STR Exclusive (32 bit)
266 \details Executes a exclusive STR instruction for 32 bit values.
267 \param [in] value Value to store
268 \param [in] ptr Pointer to location
269 \return 0 Function succeeded
270 \return 1 Function failed
272 #define __STREXW (uint32_t)__builtin_arm_strex
275 \brief Remove the exclusive lock
276 \details Removes the exclusive lock which is created by LDREX.
278 #define __CLREX __builtin_arm_clrex
281 \brief Signed Saturate
282 \details Saturates a signed value.
283 \param [in] value Value to be saturated
284 \param [in] sat Bit position to saturate to (1..32)
285 \return Saturated value
287 #define __SSAT __builtin_arm_ssat
290 \brief Unsigned Saturate
291 \details Saturates an unsigned value.
292 \param [in] value Value to be saturated
293 \param [in] sat Bit position to saturate to (0..31)
294 \return Saturated value
296 #define __USAT __builtin_arm_usat
299 /* ########################### Core Function Access ########################### */
303 \details Returns the current value of the Floating Point Status/Control register.
304 \return Floating Point Status/Control register value
306 #define __get_FPSCR __builtin_arm_get_fpscr
310 \details Assigns the given value to the Floating Point Status/Control register.
311 \param [in] fpscr Floating Point Status/Control value to set
313 #define __set_FPSCR __builtin_arm_set_fpscr
315 /** \brief Get CPSR Register
316 \return CPSR Register value
318 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
321 __ASM volatile("MRS %0, cpsr" : "=r" (result) );
325 /** \brief Set CPSR Register
326 \param [in] cpsr CPSR value to set
328 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
330 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "memory");
334 \return Processor Mode
336 __STATIC_FORCEINLINE uint32_t __get_mode(void)
338 return (__get_CPSR() & 0x1FU);
342 \param [in] mode Mode value to set
344 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
346 __ASM volatile("MSR cpsr_c, %0" : : "r" (mode) : "memory");
349 /** \brief Get Stack Pointer
350 \return Stack Pointer value
352 __STATIC_FORCEINLINE uint32_t __get_SP()
355 __ASM volatile("MOV %0, sp" : "=r" (result) : : "memory");
359 /** \brief Set Stack Pointer
360 \param [in] stack Stack Pointer value to set
362 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
364 __ASM volatile("MOV sp, %0" : : "r" (stack) : "memory");
367 /** \brief Get USR/SYS Stack Pointer
368 \return USR/SYS Stack Pointer value
370 __STATIC_FORCEINLINE uint32_t __get_SP_usr()
376 "CPS #0x1F \n" // no effect in USR mode
378 "MSR cpsr_c, %2 \n" // no effect in USR mode
379 "ISB" : "=r"(cpsr), "=r"(result) : "r"(cpsr) : "memory"
384 /** \brief Set USR/SYS Stack Pointer
385 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
387 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
392 "CPS #0x1F \n" // no effect in USR mode
394 "MSR cpsr_c, %2 \n" // no effect in USR mode
395 "ISB" : "=r"(cpsr) : "r" (topOfProcStack), "r"(cpsr) : "memory"
400 \return Floating Point Exception Control register value
402 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
404 #if (__FPU_PRESENT == 1)
406 __ASM volatile("VMRS %0, fpexc" : "=r" (result) : : "memory");
414 \param [in] fpexc Floating Point Exception Control value to set
416 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
418 #if (__FPU_PRESENT == 1)
419 __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
424 * Include common core functions to access Coprocessor 15 registers
427 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
428 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
429 #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : "=r" (Rt) : : "memory" )
430 #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : : "r" (Rt) : "memory" )
432 #include "cmsis_cp15.h"
434 /** \brief Enable Floating Point Unit
436 Critical section, called from undef handler, so systick is disabled
438 __STATIC_INLINE void __FPU_Enable(void)
441 //Permit access to VFP/NEON, registers by modifying CPACR
442 " MRC p15,0,R1,c1,c0,2 \n"
443 " ORR R1,R1,#0x00F00000 \n"
444 " MCR p15,0,R1,c1,c0,2 \n"
446 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
451 " ORR R1,R1,#0x40000000 \n"
454 //Initialise VFP/NEON registers to 0
457 //Initialise D16 registers to 0
476 //Initialise D32 registers to 0
495 //Initialise FPSCR to a known state
497 " LDR R3,=0x00086060 \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
503 #endif /* __CMSIS_ARMCLANG_H */