1 /**************************************************************************//**
3 * @brief CMSIS compiler specific macros, functions, instructions
6 ******************************************************************************/
8 * Copyright (c) 2009-2019 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.
28 /* ignore some GCC warnings */
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wsign-conversion"
31 #pragma GCC diagnostic ignored "-Wconversion"
32 #pragma GCC diagnostic ignored "-Wunused-parameter"
34 /* Fallback for __has_builtin */
36 #define __has_builtin(x) (0)
39 /* CMSIS compiler specific defines */
45 #define __INLINE inline
48 #define __FORCEINLINE __attribute__((always_inline))
50 #ifndef __STATIC_INLINE
51 #define __STATIC_INLINE static inline
53 #ifndef __STATIC_FORCEINLINE
54 #define __STATIC_FORCEINLINE __attribute__((always_inline)) static inline
57 #define __NO_RETURN __attribute__((__noreturn__))
59 #ifndef CMSIS_DEPRECATED
60 #define CMSIS_DEPRECATED __attribute__((deprecated))
63 #define __USED __attribute__((used))
66 #define __WEAK __attribute__((weak))
69 #define __PACKED __attribute__((packed, aligned(1)))
71 #ifndef __PACKED_STRUCT
72 #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
74 #ifndef __UNALIGNED_UINT16_WRITE
75 #pragma GCC diagnostic push
76 #pragma GCC diagnostic ignored "-Wpacked"
77 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
78 __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
79 #pragma GCC diagnostic pop
80 #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
82 #ifndef __UNALIGNED_UINT16_READ
83 #pragma GCC diagnostic push
84 #pragma GCC diagnostic ignored "-Wpacked"
85 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
86 __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
87 #pragma GCC diagnostic pop
88 #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
90 #ifndef __UNALIGNED_UINT32_WRITE
91 #pragma GCC diagnostic push
92 #pragma GCC diagnostic ignored "-Wpacked"
93 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
94 __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
95 #pragma GCC diagnostic pop
96 #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
98 #ifndef __UNALIGNED_UINT32_READ
99 #pragma GCC diagnostic push
100 #pragma GCC diagnostic ignored "-Wpacked"
101 __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
102 #pragma GCC diagnostic pop
103 #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
106 #define __ALIGNED(x) __attribute__((aligned(x)))
108 #ifndef __COMPILER_BARRIER
109 #define __COMPILER_BARRIER() __ASM volatile("":::"memory")
113 __STATIC_FORCEINLINE uint32_t __QSUB16(uint32_t op1, uint32_t op2)
117 __ASM volatile ("qsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
122 __STATIC_FORCEINLINE uint32_t __QADD16(uint32_t op1, uint32_t op2)
126 __ASM volatile ("qadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
130 __STATIC_FORCEINLINE int32_t __QADD( int32_t op1, int32_t op2)
134 __ASM volatile ("qadd %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
138 __STATIC_FORCEINLINE uint64_t __SMLALD (uint32_t op1, uint32_t op2, uint64_t acc)
146 #ifndef __ARMEB__ /* Little endian */
147 __ASM volatile ("smlald %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) );
148 #else /* Big endian */
149 __ASM volatile ("smlald %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) );
155 __STATIC_FORCEINLINE int32_t __QSUB( int32_t op1, int32_t op2)
159 __ASM volatile ("qsub %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
163 __STATIC_FORCEINLINE uint32_t __SMUAD (uint32_t op1, uint32_t op2)
167 __ASM volatile ("smuad %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
173 #define __PKHBT(ARG1,ARG2,ARG3) ( ((((uint32_t)(ARG1)) ) & 0x0000FFFFUL) | \
174 ((((uint32_t)(ARG2)) << (ARG3)) & 0xFFFF0000UL) )
176 __STATIC_FORCEINLINE uint32_t __SMLAD (uint32_t op1, uint32_t op2, uint32_t op3)
180 __ASM volatile ("smlad %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) );
184 __STATIC_FORCEINLINE uint32_t __SMUADX (uint32_t op1, uint32_t op2)
188 __ASM volatile ("smuadx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
192 __STATIC_FORCEINLINE uint32_t __SMLADX (uint32_t op1, uint32_t op2, uint32_t op3)
196 __ASM volatile ("smladx %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) );
200 __STATIC_FORCEINLINE uint64_t __SMLALDX (uint32_t op1, uint32_t op2, uint64_t acc)
208 #ifndef __ARMEB__ /* Little endian */
209 __ASM volatile ("smlaldx %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) );
210 #else /* Big endian */
211 __ASM volatile ("smlaldx %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) );
217 __STATIC_FORCEINLINE int32_t __SMMLA (int32_t op1, int32_t op2, int32_t op3)
221 __ASM volatile ("smmla %0, %1, %2, %3" : "=r" (result): "r" (op1), "r" (op2), "r" (op3) );
228 /* ########################## Core Instruction Access ######################### */
232 #define __NOP() __ASM volatile ("nop")
235 \brief Wait For Interrupt
237 #define __WFI() __ASM volatile ("wfi":::"memory")
240 \brief Wait For Event
242 #define __WFE() __ASM volatile ("wfe":::"memory")
247 #define __SEV() __ASM volatile ("sev")
250 \brief Instruction Synchronization Barrier
251 \details Instruction Synchronization Barrier flushes the pipeline in the processor,
252 so that all instructions following the ISB are fetched from cache or memory,
253 after the instruction has been completed.
255 __STATIC_FORCEINLINE void __ISB(void)
257 __ASM volatile ("isb 0xF":::"memory");
262 \brief Data Synchronization Barrier
263 \details Acts as a special kind of Data Memory Barrier.
264 It completes when all explicit memory accesses before this instruction complete.
266 __STATIC_FORCEINLINE void __DSB(void)
268 __ASM volatile ("dsb 0xF":::"memory");
272 \brief Data Memory Barrier
273 \details Ensures the apparent order of the explicit memory operations before
274 and after the instruction, without ensuring their completion.
276 __STATIC_FORCEINLINE void __DMB(void)
278 __ASM volatile ("dmb 0xF":::"memory");
282 \brief Reverse byte order (32 bit)
283 \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
284 \param [in] value Value to reverse
285 \return Reversed value
287 __STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
289 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
290 return __builtin_bswap32(value);
294 __ASM ("rev %0, %1" : "=r" (result) : "r" (value) );
300 \brief Reverse byte order (16 bit)
301 \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
302 \param [in] value Value to reverse
303 \return Reversed value
305 __STATIC_FORCEINLINE uint32_t __REV16(uint32_t value)
308 __ASM ("rev16 %0, %1" : "=r" (result) : "r" (value));
313 \brief Reverse byte order (16 bit)
314 \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
315 \param [in] value Value to reverse
316 \return Reversed value
318 __STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
320 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
321 return (int16_t)__builtin_bswap16(value);
325 __ASM ("revsh %0, %1" : "=r" (result) : "r" (value) );
331 \brief Rotate Right in unsigned value (32 bit)
332 \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
333 \param [in] op1 Value to rotate
334 \param [in] op2 Number of Bits to rotate
335 \return Rotated value
337 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
343 return (op1 >> op2) | (op1 << (32U - op2));
349 \param [in] value is ignored by the processor.
350 If required, a debugger can use it to store additional information about the breakpoint.
352 #define __BKPT(value) __ASM volatile ("bkpt "#value)
355 \brief Reverse bit order of value
356 \details Reverses the bit order of the given value.
357 \param [in] value Value to reverse
358 \return Reversed value
360 __STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
364 #if ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
365 (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
366 (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) )
367 __ASM ("rbit %0, %1" : "=r" (result) : "r" (value) );
369 int32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
371 result = value; /* r will be reversed bits of v; first get LSB of v */
372 for (value >>= 1U; value; value >>= 1U)
375 result |= value & 1U;
378 result <<= s; /* shift when v's highest bits are zero */
384 \brief Count leading zeros
385 \param [in] value Value to count the leading zeros
386 \return number of leading zeros in value
388 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
390 /* Even though __builtin_clz produces a CLZ instruction on ARM, formally
391 __builtin_clz(0) is undefined behaviour, so handle this case specially.
392 This guarantees ARM-compatible results if happening to compile on a non-ARM
393 target, and ensures the compiler doesn't decide to activate any
394 optimisations using the logic "value was passed to __builtin_clz, so it
396 ARM GCC 7.3 and possibly earlier will optimise this test away, leaving a
397 single CLZ instruction.
403 return __builtin_clz(value);
407 \brief LDR Exclusive (8 bit)
408 \details Executes a exclusive LDR instruction for 8 bit value.
409 \param [in] ptr Pointer to data
410 \return value of type uint8_t at (*ptr)
412 __STATIC_FORCEINLINE uint8_t __LDREXB(volatile uint8_t *addr)
416 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
417 __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
419 /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
420 accepted by assembler. So has to use following less efficient pattern.
422 __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
424 return ((uint8_t) result); /* Add explicit type cast here */
429 \brief LDR Exclusive (16 bit)
430 \details Executes a exclusive LDR instruction for 16 bit values.
431 \param [in] ptr Pointer to data
432 \return value of type uint16_t at (*ptr)
434 __STATIC_FORCEINLINE uint16_t __LDREXH(volatile uint16_t *addr)
438 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
439 __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
441 /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
442 accepted by assembler. So has to use following less efficient pattern.
444 __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
446 return ((uint16_t) result); /* Add explicit type cast here */
451 \brief LDR Exclusive (32 bit)
452 \details Executes a exclusive LDR instruction for 32 bit values.
453 \param [in] ptr Pointer to data
454 \return value of type uint32_t at (*ptr)
456 __STATIC_FORCEINLINE uint32_t __LDREXW(volatile uint32_t *addr)
460 __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
466 \brief STR Exclusive (8 bit)
467 \details Executes a exclusive STR instruction for 8 bit values.
468 \param [in] value Value to store
469 \param [in] ptr Pointer to location
470 \return 0 Function succeeded
471 \return 1 Function failed
473 __STATIC_FORCEINLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
477 __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
483 \brief STR Exclusive (16 bit)
484 \details Executes a exclusive STR instruction for 16 bit values.
485 \param [in] value Value to store
486 \param [in] ptr Pointer to location
487 \return 0 Function succeeded
488 \return 1 Function failed
490 __STATIC_FORCEINLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
494 __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
500 \brief STR Exclusive (32 bit)
501 \details Executes a exclusive STR instruction for 32 bit values.
502 \param [in] value Value to store
503 \param [in] ptr Pointer to location
504 \return 0 Function succeeded
505 \return 1 Function failed
507 __STATIC_FORCEINLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
511 __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
517 \brief Remove the exclusive lock
518 \details Removes the exclusive lock which is created by LDREX.
520 __STATIC_FORCEINLINE void __CLREX(void)
522 __ASM volatile ("clrex" ::: "memory");
526 \brief Signed Saturate
527 \details Saturates a signed value.
528 \param [in] value Value to be saturated
529 \param [in] sat Bit position to saturate to (1..32)
530 \return Saturated value
532 #define __SSAT(ARG1, ARG2) \
535 int32_t __RES, __ARG1 = (ARG1); \
536 __ASM volatile ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) : "cc" ); \
542 \brief Unsigned Saturate
543 \details Saturates an unsigned value.
544 \param [in] value Value to be saturated
545 \param [in] sat Bit position to saturate to (0..31)
546 \return Saturated value
548 #define __USAT(ARG1, ARG2) \
551 uint32_t __RES, __ARG1 = (ARG1); \
552 __ASM volatile ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) : "cc" ); \
556 /* ########################### Core Function Access ########################### */
559 \brief Enable IRQ Interrupts
560 \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
561 Can only be executed in Privileged modes.
563 __STATIC_FORCEINLINE void __enable_irq(void)
565 __ASM volatile ("cpsie i" : : : "memory");
569 \brief Disable IRQ Interrupts
570 \details Disables IRQ interrupts by setting the I-bit in the CPSR.
571 Can only be executed in Privileged modes.
573 __STATIC_FORCEINLINE void __disable_irq(void)
575 __ASM volatile ("cpsid i" : : : "memory");
580 \details Returns the current value of the Floating Point Status/Control register.
581 \return Floating Point Status/Control register value
583 __STATIC_FORCEINLINE uint32_t __get_FPSCR(void)
585 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
586 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
587 #if __has_builtin(__builtin_arm_get_fpscr)
588 // Re-enable using built-in when GCC has been fixed
589 // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
590 /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
591 return __builtin_arm_get_fpscr();
595 __ASM volatile ("VMRS %0, fpscr" : "=r" (result) );
605 \details Assigns the given value to the Floating Point Status/Control register.
606 \param [in] fpscr Floating Point Status/Control value to set
608 __STATIC_FORCEINLINE void __set_FPSCR(uint32_t fpscr)
610 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
611 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
612 #if __has_builtin(__builtin_arm_set_fpscr)
613 // Re-enable using built-in when GCC has been fixed
614 // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
615 /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
616 __builtin_arm_set_fpscr(fpscr);
618 __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc", "memory");
625 /** \brief Get CPSR Register
626 \return CPSR Register value
628 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
631 __ASM volatile("MRS %0, cpsr" : "=r" (result) );
635 /** \brief Set CPSR Register
636 \param [in] cpsr CPSR value to set
638 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
640 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "cc", "memory");
644 \return Processor Mode
646 __STATIC_FORCEINLINE uint32_t __get_mode(void)
648 return (__get_CPSR() & 0x1FU);
652 \param [in] mode Mode value to set
654 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
656 __ASM volatile("MSR cpsr_c, %0" : : "r" (mode) : "memory");
659 /** \brief Get Stack Pointer
660 \return Stack Pointer value
662 __STATIC_FORCEINLINE uint32_t __get_SP(void)
665 __ASM volatile("MOV %0, sp" : "=r" (result) : : "memory");
669 /** \brief Set Stack Pointer
670 \param [in] stack Stack Pointer value to set
672 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
674 __ASM volatile("MOV sp, %0" : : "r" (stack) : "memory");
677 /** \brief Get USR/SYS Stack Pointer
678 \return USR/SYS Stack Pointer value
680 __STATIC_FORCEINLINE uint32_t __get_SP_usr(void)
682 uint32_t cpsr = __get_CPSR();
686 "MOV %0, sp " : "=r"(result) : : "memory"
693 /** \brief Set USR/SYS Stack Pointer
694 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
696 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
698 uint32_t cpsr = __get_CPSR();
701 "MOV sp, %0 " : : "r" (topOfProcStack) : "memory"
708 \return Floating Point Exception Control register value
710 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
712 #if (__FPU_PRESENT == 1)
714 __ASM volatile("VMRS %0, fpexc" : "=r" (result) );
722 \param [in] fpexc Floating Point Exception Control value to set
724 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
726 #if (__FPU_PRESENT == 1)
727 __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
732 * Include common core functions to access Coprocessor 15 registers
735 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
736 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
737 #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : "=r" (Rt) : : "memory" )
738 #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : : "r" (Rt) : "memory" )
740 #include "cmsis_cp15.h"
742 /** \brief Enable Floating Point Unit
744 Critical section, called from undef handler, so systick is disabled
746 __STATIC_INLINE void __FPU_Enable(void)
749 //Permit access to VFP/NEON, registers by modifying CPACR
750 " MRC p15,0,R1,c1,c0,2 \n"
751 " ORR R1,R1,#0x00F00000 \n"
752 " MCR p15,0,R1,c1,c0,2 \n"
754 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
759 " ORR R1,R1,#0x40000000 \n"
762 //Initialise VFP/NEON registers to 0
765 //Initialise D16 registers to 0
783 #if (defined(__ARM_NEON) && (__ARM_NEON == 1))
784 //Initialise D32 registers to 0
803 //Initialise FPSCR to a known state
805 " LDR R2,=0x00086060 \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
808 : : : "cc", "r1", "r2"
812 #pragma GCC diagnostic pop
814 #endif /* __CMSIS_GCC_H */