1 /**************************************************************************//**
3 * @brief CMSIS compiler specific macros, functions, instructions
5 * @date 20. December 2018
6 ******************************************************************************/
8 * Copyright (c) 2009-2018 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 */
44 #define __INLINE inline
47 #define __FORCEINLINE __attribute__((always_inline))
49 #ifndef __STATIC_INLINE
50 #define __STATIC_INLINE static inline
52 #ifndef __STATIC_FORCEINLINE
53 #define __STATIC_FORCEINLINE __attribute__((always_inline)) static inline
56 #define __NO_RETURN __attribute__((__noreturn__))
58 #ifndef CMSIS_DEPRECATED
59 #define CMSIS_DEPRECATED __attribute__((deprecated))
62 #define __USED __attribute__((used))
65 #define __WEAK __attribute__((weak))
68 #define __PACKED __attribute__((packed, aligned(1)))
70 #ifndef __PACKED_STRUCT
71 #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
73 #ifndef __UNALIGNED_UINT16_WRITE
74 #pragma GCC diagnostic push
75 #pragma GCC diagnostic ignored "-Wpacked"
76 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
77 __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
78 #pragma GCC diagnostic pop
79 #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
81 #ifndef __UNALIGNED_UINT16_READ
82 #pragma GCC diagnostic push
83 #pragma GCC diagnostic ignored "-Wpacked"
84 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
85 __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
86 #pragma GCC diagnostic pop
87 #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
89 #ifndef __UNALIGNED_UINT32_WRITE
90 #pragma GCC diagnostic push
91 #pragma GCC diagnostic ignored "-Wpacked"
92 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
93 __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
94 #pragma GCC diagnostic pop
95 #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
97 #ifndef __UNALIGNED_UINT32_READ
98 #pragma GCC diagnostic push
99 #pragma GCC diagnostic ignored "-Wpacked"
100 __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
101 #pragma GCC diagnostic pop
102 #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
105 #define __ALIGNED(x) __attribute__((aligned(x)))
107 #ifndef __COMPILER_BARRIER
108 #define __COMPILER_BARRIER() __ASM volatile("":::"memory")
111 /* ########################## Core Instruction Access ######################### */
115 #define __NOP() __ASM volatile ("nop")
118 \brief Wait For Interrupt
120 #define __WFI() __ASM volatile ("wfi")
123 \brief Wait For Event
125 #define __WFE() __ASM volatile ("wfe")
130 #define __SEV() __ASM volatile ("sev")
133 \brief Instruction Synchronization Barrier
134 \details Instruction Synchronization Barrier flushes the pipeline in the processor,
135 so that all instructions following the ISB are fetched from cache or memory,
136 after the instruction has been completed.
138 __STATIC_FORCEINLINE void __ISB(void)
140 __ASM volatile ("isb 0xF":::"memory");
145 \brief Data Synchronization Barrier
146 \details Acts as a special kind of Data Memory Barrier.
147 It completes when all explicit memory accesses before this instruction complete.
149 __STATIC_FORCEINLINE void __DSB(void)
151 __ASM volatile ("dsb 0xF":::"memory");
155 \brief Data Memory Barrier
156 \details Ensures the apparent order of the explicit memory operations before
157 and after the instruction, without ensuring their completion.
159 __STATIC_FORCEINLINE void __DMB(void)
161 __ASM volatile ("dmb 0xF":::"memory");
165 \brief Reverse byte order (32 bit)
166 \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
167 \param [in] value Value to reverse
168 \return Reversed value
170 __STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
172 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
173 return __builtin_bswap32(value);
177 __ASM volatile ("rev %0, %1" : "=r" (result) : "r" (value) );
183 \brief Reverse byte order (16 bit)
184 \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
185 \param [in] value Value to reverse
186 \return Reversed value
188 #ifndef __NO_EMBEDDED_ASM
189 __attribute__((section(".rev16_text"))) __STATIC_INLINE uint32_t __REV16(uint32_t value)
192 __ASM volatile("rev16 %0, %1" : "=r" (result) : "r" (value));
198 \brief Reverse byte order (16 bit)
199 \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
200 \param [in] value Value to reverse
201 \return Reversed value
203 __STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
205 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
206 return (int16_t)__builtin_bswap16(value);
210 __ASM volatile ("revsh %0, %1" : "=r" (result) : "r" (value) );
216 \brief Rotate Right in unsigned value (32 bit)
217 \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
218 \param [in] op1 Value to rotate
219 \param [in] op2 Number of Bits to rotate
220 \return Rotated value
222 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
228 return (op1 >> op2) | (op1 << (32U - op2));
234 \param [in] value is ignored by the processor.
235 If required, a debugger can use it to store additional information about the breakpoint.
237 #define __BKPT(value) __ASM volatile ("bkpt "#value)
240 \brief Reverse bit order of value
241 \details Reverses the bit order of the given value.
242 \param [in] value Value to reverse
243 \return Reversed value
245 __STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
249 #if ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
250 (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
251 (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) )
252 __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
254 int32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
256 result = value; /* r will be reversed bits of v; first get LSB of v */
257 for (value >>= 1U; value; value >>= 1U)
260 result |= value & 1U;
263 result <<= s; /* shift when v's highest bits are zero */
269 \brief Count leading zeros
270 \param [in] value Value to count the leading zeros
271 \return number of leading zeros in value
273 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
275 /* Even though __builtin_clz produces a CLZ instruction on ARM, formally
276 __builtin_clz(0) is undefined behaviour, so handle this case specially.
277 This guarantees ARM-compatible results if happening to compile on a non-ARM
278 target, and ensures the compiler doesn't decide to activate any
279 optimisations using the logic "value was passed to __builtin_clz, so it
281 ARM GCC 7.3 and possibly earlier will optimise this test away, leaving a
282 single CLZ instruction.
288 return __builtin_clz(value);
292 \brief LDR Exclusive (8 bit)
293 \details Executes a exclusive LDR instruction for 8 bit value.
294 \param [in] ptr Pointer to data
295 \return value of type uint8_t at (*ptr)
297 __STATIC_FORCEINLINE uint8_t __LDREXB(volatile uint8_t *addr)
301 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
302 __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
304 /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
305 accepted by assembler. So has to use following less efficient pattern.
307 __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
309 return ((uint8_t) result); /* Add explicit type cast here */
314 \brief LDR Exclusive (16 bit)
315 \details Executes a exclusive LDR instruction for 16 bit values.
316 \param [in] ptr Pointer to data
317 \return value of type uint16_t at (*ptr)
319 __STATIC_FORCEINLINE uint16_t __LDREXH(volatile uint16_t *addr)
323 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
324 __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
326 /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
327 accepted by assembler. So has to use following less efficient pattern.
329 __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
331 return ((uint16_t) result); /* Add explicit type cast here */
336 \brief LDR Exclusive (32 bit)
337 \details Executes a exclusive LDR instruction for 32 bit values.
338 \param [in] ptr Pointer to data
339 \return value of type uint32_t at (*ptr)
341 __STATIC_FORCEINLINE uint32_t __LDREXW(volatile uint32_t *addr)
345 __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
351 \brief STR Exclusive (8 bit)
352 \details Executes a exclusive STR instruction for 8 bit values.
353 \param [in] value Value to store
354 \param [in] ptr Pointer to location
355 \return 0 Function succeeded
356 \return 1 Function failed
358 __STATIC_FORCEINLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
362 __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
368 \brief STR Exclusive (16 bit)
369 \details Executes a exclusive STR instruction for 16 bit values.
370 \param [in] value Value to store
371 \param [in] ptr Pointer to location
372 \return 0 Function succeeded
373 \return 1 Function failed
375 __STATIC_FORCEINLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
379 __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
385 \brief STR Exclusive (32 bit)
386 \details Executes a exclusive STR instruction for 32 bit values.
387 \param [in] value Value to store
388 \param [in] ptr Pointer to location
389 \return 0 Function succeeded
390 \return 1 Function failed
392 __STATIC_FORCEINLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
396 __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
402 \brief Remove the exclusive lock
403 \details Removes the exclusive lock which is created by LDREX.
405 __STATIC_FORCEINLINE void __CLREX(void)
407 __ASM volatile ("clrex" ::: "memory");
411 \brief Signed Saturate
412 \details Saturates a signed value.
413 \param [in] value Value to be saturated
414 \param [in] sat Bit position to saturate to (1..32)
415 \return Saturated value
417 #define __SSAT(ARG1,ARG2) \
420 int32_t __RES, __ARG1 = (ARG1); \
421 __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
427 \brief Unsigned Saturate
428 \details Saturates an unsigned value.
429 \param [in] value Value to be saturated
430 \param [in] sat Bit position to saturate to (0..31)
431 \return Saturated value
433 #define __USAT(ARG1,ARG2) \
436 uint32_t __RES, __ARG1 = (ARG1); \
437 __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
441 /* ########################### Core Function Access ########################### */
444 \brief Enable IRQ Interrupts
445 \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
446 Can only be executed in Privileged modes.
448 __STATIC_FORCEINLINE void __enable_irq(void)
450 __ASM volatile ("cpsie i" : : : "memory");
454 \brief Disable IRQ Interrupts
455 \details Disables IRQ interrupts by setting the I-bit in the CPSR.
456 Can only be executed in Privileged modes.
458 __STATIC_FORCEINLINE void __disable_irq(void)
460 __ASM volatile ("cpsid i" : : : "memory");
465 \details Returns the current value of the Floating Point Status/Control register.
466 \return Floating Point Status/Control register value
468 __STATIC_FORCEINLINE uint32_t __get_FPSCR(void)
470 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
471 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
472 #if __has_builtin(__builtin_arm_get_fpscr)
473 // Re-enable using built-in when GCC has been fixed
474 // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
475 /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
476 return __builtin_arm_get_fpscr();
480 __ASM volatile ("VMRS %0, fpscr" : "=r" (result) );
490 \details Assigns the given value to the Floating Point Status/Control register.
491 \param [in] fpscr Floating Point Status/Control value to set
493 __STATIC_FORCEINLINE void __set_FPSCR(uint32_t fpscr)
495 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
496 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
497 #if __has_builtin(__builtin_arm_set_fpscr)
498 // Re-enable using built-in when GCC has been fixed
499 // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
500 /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
501 __builtin_arm_set_fpscr(fpscr);
503 __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc", "memory");
510 /** \brief Get CPSR Register
511 \return CPSR Register value
513 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
516 __ASM volatile("MRS %0, cpsr" : "=r" (result) );
520 /** \brief Set CPSR Register
521 \param [in] cpsr CPSR value to set
523 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
525 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "memory");
529 \return Processor Mode
531 __STATIC_FORCEINLINE uint32_t __get_mode(void)
533 return (__get_CPSR() & 0x1FU);
537 \param [in] mode Mode value to set
539 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
541 __ASM volatile("MSR cpsr_c, %0" : : "r" (mode) : "memory");
544 /** \brief Get Stack Pointer
545 \return Stack Pointer value
547 __STATIC_FORCEINLINE uint32_t __get_SP(void)
550 __ASM volatile("MOV %0, sp" : "=r" (result) : : "memory");
554 /** \brief Set Stack Pointer
555 \param [in] stack Stack Pointer value to set
557 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
559 __ASM volatile("MOV sp, %0" : : "r" (stack) : "memory");
562 /** \brief Get USR/SYS Stack Pointer
563 \return USR/SYS Stack Pointer value
565 __STATIC_FORCEINLINE uint32_t __get_SP_usr(void)
567 uint32_t cpsr = __get_CPSR();
571 "MOV %0, sp " : "=r"(result) : : "memory"
578 /** \brief Set USR/SYS Stack Pointer
579 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
581 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
583 uint32_t cpsr = __get_CPSR();
586 "MOV sp, %0 " : : "r" (topOfProcStack) : "memory"
593 \return Floating Point Exception Control register value
595 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
597 #if (__FPU_PRESENT == 1)
599 __ASM volatile("VMRS %0, fpexc" : "=r" (result) );
607 \param [in] fpexc Floating Point Exception Control value to set
609 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
611 #if (__FPU_PRESENT == 1)
612 __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
617 * Include common core functions to access Coprocessor 15 registers
620 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
621 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
622 #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : "=r" (Rt) : : "memory" )
623 #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : : "r" (Rt) : "memory" )
625 #include "cmsis_cp15.h"
627 /** \brief Enable Floating Point Unit
629 Critical section, called from undef handler, so systick is disabled
631 __STATIC_INLINE void __FPU_Enable(void)
634 //Permit access to VFP/NEON, registers by modifying CPACR
635 " MRC p15,0,R1,c1,c0,2 \n"
636 " ORR R1,R1,#0x00F00000 \n"
637 " MCR p15,0,R1,c1,c0,2 \n"
639 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
644 " ORR R1,R1,#0x40000000 \n"
647 //Initialise VFP/NEON registers to 0
650 //Initialise D16 registers to 0
668 #if (defined(__ARM_NEON) && (__ARM_NEON == 1))
669 //Initialise D32 registers to 0
688 //Initialise FPSCR to a known state
690 " LDR R3,=0x00086060 \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
696 #pragma GCC diagnostic pop
698 #endif /* __CMSIS_GCC_H */