1 /**************************************************************************//**
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.
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)))
108 /* ########################## Core Instruction Access ######################### */
112 #define __NOP() __ASM volatile ("nop")
115 \brief Wait For Interrupt
117 #define __WFI() __ASM volatile ("wfi")
120 \brief Wait For Event
122 #define __WFE() __ASM volatile ("wfe")
127 #define __SEV() __ASM volatile ("sev")
130 \brief Instruction Synchronization Barrier
131 \details Instruction Synchronization Barrier flushes the pipeline in the processor,
132 so that all instructions following the ISB are fetched from cache or memory,
133 after the instruction has been completed.
135 __STATIC_FORCEINLINE void __ISB(void)
137 __ASM volatile ("isb 0xF":::"memory");
142 \brief Data Synchronization Barrier
143 \details Acts as a special kind of Data Memory Barrier.
144 It completes when all explicit memory accesses before this instruction complete.
146 __STATIC_FORCEINLINE void __DSB(void)
148 __ASM volatile ("dsb 0xF":::"memory");
152 \brief Data Memory Barrier
153 \details Ensures the apparent order of the explicit memory operations before
154 and after the instruction, without ensuring their completion.
156 __STATIC_FORCEINLINE void __DMB(void)
158 __ASM volatile ("dmb 0xF":::"memory");
162 \brief Reverse byte order (32 bit)
163 \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
164 \param [in] value Value to reverse
165 \return Reversed value
167 __STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
169 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
170 return __builtin_bswap32(value);
174 __ASM volatile ("rev %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
180 \brief Reverse byte order (16 bit)
181 \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
182 \param [in] value Value to reverse
183 \return Reversed value
185 #ifndef __NO_EMBEDDED_ASM
186 __attribute__((section(".rev16_text"))) __STATIC_INLINE uint32_t __REV16(uint32_t value)
189 __ASM volatile("rev16 %0, %1" : "=r" (result) : "r" (value));
195 \brief Reverse byte order (16 bit)
196 \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
197 \param [in] value Value to reverse
198 \return Reversed value
200 __STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
202 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
203 return (int16_t)__builtin_bswap16(value);
207 __ASM volatile ("revsh %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
213 \brief Rotate Right in unsigned value (32 bit)
214 \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
215 \param [in] op1 Value to rotate
216 \param [in] op2 Number of Bits to rotate
217 \return Rotated value
219 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
225 return (op1 >> op2) | (op1 << (32U - op2));
231 \param [in] value is ignored by the processor.
232 If required, a debugger can use it to store additional information about the breakpoint.
234 #define __BKPT(value) __ASM volatile ("bkpt "#value)
237 \brief Reverse bit order of value
238 \details Reverses the bit order of the given value.
239 \param [in] value Value to reverse
240 \return Reversed value
242 __STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
246 #if ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
247 (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
248 (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) )
249 __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
251 int32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
253 result = value; /* r will be reversed bits of v; first get LSB of v */
254 for (value >>= 1U; value; value >>= 1U)
257 result |= value & 1U;
260 result <<= s; /* shift when v's highest bits are zero */
266 \brief Count leading zeros
267 \param [in] value Value to count the leading zeros
268 \return number of leading zeros in value
270 #define __CLZ (uint8_t)__builtin_clz
273 \brief LDR Exclusive (8 bit)
274 \details Executes a exclusive LDR instruction for 8 bit value.
275 \param [in] ptr Pointer to data
276 \return value of type uint8_t at (*ptr)
278 __STATIC_FORCEINLINE uint8_t __LDREXB(volatile uint8_t *addr)
282 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
283 __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
285 /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
286 accepted by assembler. So has to use following less efficient pattern.
288 __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
290 return ((uint8_t) result); /* Add explicit type cast here */
295 \brief LDR Exclusive (16 bit)
296 \details Executes a exclusive LDR instruction for 16 bit values.
297 \param [in] ptr Pointer to data
298 \return value of type uint16_t at (*ptr)
300 __STATIC_FORCEINLINE uint16_t __LDREXH(volatile uint16_t *addr)
304 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
305 __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
307 /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
308 accepted by assembler. So has to use following less efficient pattern.
310 __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
312 return ((uint16_t) result); /* Add explicit type cast here */
317 \brief LDR Exclusive (32 bit)
318 \details Executes a exclusive LDR instruction for 32 bit values.
319 \param [in] ptr Pointer to data
320 \return value of type uint32_t at (*ptr)
322 __STATIC_FORCEINLINE uint32_t __LDREXW(volatile uint32_t *addr)
326 __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
332 \brief STR Exclusive (8 bit)
333 \details Executes a exclusive STR instruction for 8 bit values.
334 \param [in] value Value to store
335 \param [in] ptr Pointer to location
336 \return 0 Function succeeded
337 \return 1 Function failed
339 __STATIC_FORCEINLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
343 __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
349 \brief STR Exclusive (16 bit)
350 \details Executes a exclusive STR instruction for 16 bit values.
351 \param [in] value Value to store
352 \param [in] ptr Pointer to location
353 \return 0 Function succeeded
354 \return 1 Function failed
356 __STATIC_FORCEINLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
360 __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
366 \brief STR Exclusive (32 bit)
367 \details Executes a exclusive STR instruction for 32 bit values.
368 \param [in] value Value to store
369 \param [in] ptr Pointer to location
370 \return 0 Function succeeded
371 \return 1 Function failed
373 __STATIC_FORCEINLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
377 __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
383 \brief Remove the exclusive lock
384 \details Removes the exclusive lock which is created by LDREX.
386 __STATIC_FORCEINLINE void __CLREX(void)
388 __ASM volatile ("clrex" ::: "memory");
392 \brief Signed Saturate
393 \details Saturates a signed value.
394 \param [in] value Value to be saturated
395 \param [in] sat Bit position to saturate to (1..32)
396 \return Saturated value
398 #define __SSAT(ARG1,ARG2) \
401 int32_t __RES, __ARG1 = (ARG1); \
402 __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
408 \brief Unsigned Saturate
409 \details Saturates an unsigned value.
410 \param [in] value Value to be saturated
411 \param [in] sat Bit position to saturate to (0..31)
412 \return Saturated value
414 #define __USAT(ARG1,ARG2) \
417 uint32_t __RES, __ARG1 = (ARG1); \
418 __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
422 /* ########################### Core Function Access ########################### */
425 \brief Enable IRQ Interrupts
426 \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
427 Can only be executed in Privileged modes.
429 __STATIC_FORCEINLINE void __enable_irq(void)
431 __ASM volatile ("cpsie i" : : : "memory");
435 \brief Disable IRQ Interrupts
436 \details Disables IRQ interrupts by setting the I-bit in the CPSR.
437 Can only be executed in Privileged modes.
439 __STATIC_FORCEINLINE void __disable_irq(void)
441 __ASM volatile ("cpsid i" : : : "memory");
446 \details Returns the current value of the Floating Point Status/Control register.
447 \return Floating Point Status/Control register value
449 __STATIC_FORCEINLINE uint32_t __get_FPSCR(void)
451 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
452 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
453 #if __has_builtin(__builtin_arm_get_fpscr) || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
454 /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
455 return __builtin_arm_get_fpscr();
459 __ASM volatile ("VMRS %0, fpscr" : "=r" (result) );
469 \details Assigns the given value to the Floating Point Status/Control register.
470 \param [in] fpscr Floating Point Status/Control value to set
472 __STATIC_FORCEINLINE void __set_FPSCR(uint32_t fpscr)
474 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
475 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
476 #if __has_builtin(__builtin_arm_set_fpscr) || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
477 /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
478 __builtin_arm_set_fpscr(fpscr);
480 __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc", "memory");
487 /** \brief Get CPSR Register
488 \return CPSR Register value
490 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
493 __ASM volatile("MRS %0, cpsr" : "=r" (result) );
497 /** \brief Set CPSR Register
498 \param [in] cpsr CPSR value to set
500 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
502 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "memory");
506 \return Processor Mode
508 __STATIC_FORCEINLINE uint32_t __get_mode(void)
510 return (__get_CPSR() & 0x1FU);
514 \param [in] mode Mode value to set
516 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
518 __ASM volatile("MSR cpsr_c, %0" : : "r" (mode) : "memory");
521 /** \brief Get Stack Pointer
522 \return Stack Pointer value
524 __STATIC_FORCEINLINE uint32_t __get_SP(void)
527 __ASM volatile("MOV %0, sp" : "=r" (result) : : "memory");
531 /** \brief Set Stack Pointer
532 \param [in] stack Stack Pointer value to set
534 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
536 __ASM volatile("MOV sp, %0" : : "r" (stack) : "memory");
539 /** \brief Get USR/SYS Stack Pointer
540 \return USR/SYS Stack Pointer value
542 __STATIC_FORCEINLINE uint32_t __get_SP_usr(void)
544 uint32_t cpsr = __get_CPSR();
548 "MOV %0, sp " : "=r"(result) : : "memory"
555 /** \brief Set USR/SYS Stack Pointer
556 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
558 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
560 uint32_t cpsr = __get_CPSR();
563 "MOV sp, %0 " : : "r" (topOfProcStack) : "memory"
570 \return Floating Point Exception Control register value
572 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
574 #if (__FPU_PRESENT == 1)
576 __ASM volatile("VMRS %0, fpexc" : "=r" (result) );
584 \param [in] fpexc Floating Point Exception Control value to set
586 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
588 #if (__FPU_PRESENT == 1)
589 __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
594 * Include common core functions to access Coprocessor 15 registers
597 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
598 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
599 #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : "=r" (Rt) : : "memory" )
600 #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : : "r" (Rt) : "memory" )
602 #include "cmsis_cp15.h"
604 /** \brief Enable Floating Point Unit
606 Critical section, called from undef handler, so systick is disabled
608 __STATIC_INLINE void __FPU_Enable(void)
611 //Permit access to VFP/NEON, registers by modifying CPACR
612 " MRC p15,0,R1,c1,c0,2 \n"
613 " ORR R1,R1,#0x00F00000 \n"
614 " MCR p15,0,R1,c1,c0,2 \n"
616 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
621 " ORR R1,R1,#0x40000000 \n"
624 //Initialise VFP/NEON registers to 0
627 //Initialise D16 registers to 0
645 #if (defined(__ARM_NEON) && (__ARM_NEON == 1))
646 //Initialise D32 registers to 0
665 //Initialise FPSCR to a known state
667 " LDR R3,=0x00086060 \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
673 #pragma GCC diagnostic pop
675 #endif /* __CMSIS_GCC_H */