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))
59 #define __USED __attribute__((used))
62 #define __WEAK __attribute__((weak))
65 #define __PACKED __attribute__((packed, aligned(1)))
67 #ifndef __PACKED_STRUCT
68 #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
70 #ifndef __UNALIGNED_UINT16_WRITE
71 #pragma GCC diagnostic push
72 #pragma GCC diagnostic ignored "-Wpacked"
73 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
74 __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
75 #pragma GCC diagnostic pop
76 #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
78 #ifndef __UNALIGNED_UINT16_READ
79 #pragma GCC diagnostic push
80 #pragma GCC diagnostic ignored "-Wpacked"
81 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
82 __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
83 #pragma GCC diagnostic pop
84 #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
86 #ifndef __UNALIGNED_UINT32_WRITE
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wpacked"
89 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
90 __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
91 #pragma GCC diagnostic pop
92 #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
94 #ifndef __UNALIGNED_UINT32_READ
95 #pragma GCC diagnostic push
96 #pragma GCC diagnostic ignored "-Wpacked"
97 __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
98 #pragma GCC diagnostic pop
99 #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
102 #define __ALIGNED(x) __attribute__((aligned(x)))
105 /* ########################## Core Instruction Access ######################### */
109 #define __NOP() __ASM volatile ("nop")
112 \brief Wait For Interrupt
114 #define __WFI() __ASM volatile ("wfi")
117 \brief Wait For Event
119 #define __WFE() __ASM volatile ("wfe")
124 #define __SEV() __ASM volatile ("sev")
127 \brief Instruction Synchronization Barrier
128 \details Instruction Synchronization Barrier flushes the pipeline in the processor,
129 so that all instructions following the ISB are fetched from cache or memory,
130 after the instruction has been completed.
132 __STATIC_FORCEINLINE void __ISB(void)
134 __ASM volatile ("isb 0xF":::"memory");
139 \brief Data Synchronization Barrier
140 \details Acts as a special kind of Data Memory Barrier.
141 It completes when all explicit memory accesses before this instruction complete.
143 __STATIC_FORCEINLINE void __DSB(void)
145 __ASM volatile ("dsb 0xF":::"memory");
149 \brief Data Memory Barrier
150 \details Ensures the apparent order of the explicit memory operations before
151 and after the instruction, without ensuring their completion.
153 __STATIC_FORCEINLINE void __DMB(void)
155 __ASM volatile ("dmb 0xF":::"memory");
159 \brief Reverse byte order (32 bit)
160 \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
161 \param [in] value Value to reverse
162 \return Reversed value
164 __STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
166 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
167 return __builtin_bswap32(value);
171 __ASM volatile ("rev %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
177 \brief Reverse byte order (16 bit)
178 \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
179 \param [in] value Value to reverse
180 \return Reversed value
182 #ifndef __NO_EMBEDDED_ASM
183 __attribute__((section(".rev16_text"))) __STATIC_INLINE uint32_t __REV16(uint32_t value)
186 __ASM volatile("rev16 %0, %1" : "=r" (result) : "r" (value));
192 \brief Reverse byte order (16 bit)
193 \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
194 \param [in] value Value to reverse
195 \return Reversed value
197 __STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
199 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
200 return (int16_t)__builtin_bswap16(value);
204 __ASM volatile ("revsh %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
210 \brief Rotate Right in unsigned value (32 bit)
211 \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
212 \param [in] op1 Value to rotate
213 \param [in] op2 Number of Bits to rotate
214 \return Rotated value
216 __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
222 return (op1 >> op2) | (op1 << (32U - op2));
228 \param [in] value is ignored by the processor.
229 If required, a debugger can use it to store additional information about the breakpoint.
231 #define __BKPT(value) __ASM volatile ("bkpt "#value)
234 \brief Reverse bit order of value
235 \details Reverses the bit order of the given value.
236 \param [in] value Value to reverse
237 \return Reversed value
239 __STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
243 #if ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
244 (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
245 (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) )
246 __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
248 int32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
250 result = value; /* r will be reversed bits of v; first get LSB of v */
251 for (value >>= 1U; value; value >>= 1U)
254 result |= value & 1U;
257 result <<= s; /* shift when v's highest bits are zero */
263 \brief Count leading zeros
264 \param [in] value Value to count the leading zeros
265 \return number of leading zeros in value
267 #define __CLZ __builtin_clz
270 \brief LDR Exclusive (8 bit)
271 \details Executes a exclusive LDR instruction for 8 bit value.
272 \param [in] ptr Pointer to data
273 \return value of type uint8_t at (*ptr)
275 __STATIC_FORCEINLINE uint8_t __LDREXB(volatile uint8_t *addr)
279 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
280 __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
282 /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
283 accepted by assembler. So has to use following less efficient pattern.
285 __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
287 return ((uint8_t) result); /* Add explicit type cast here */
292 \brief LDR Exclusive (16 bit)
293 \details Executes a exclusive LDR instruction for 16 bit values.
294 \param [in] ptr Pointer to data
295 \return value of type uint16_t at (*ptr)
297 __STATIC_FORCEINLINE uint16_t __LDREXH(volatile uint16_t *addr)
301 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
302 __ASM volatile ("ldrexh %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 ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
309 return ((uint16_t) result); /* Add explicit type cast here */
314 \brief LDR Exclusive (32 bit)
315 \details Executes a exclusive LDR instruction for 32 bit values.
316 \param [in] ptr Pointer to data
317 \return value of type uint32_t at (*ptr)
319 __STATIC_FORCEINLINE uint32_t __LDREXW(volatile uint32_t *addr)
323 __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
329 \brief STR Exclusive (8 bit)
330 \details Executes a exclusive STR instruction for 8 bit values.
331 \param [in] value Value to store
332 \param [in] ptr Pointer to location
333 \return 0 Function succeeded
334 \return 1 Function failed
336 __STATIC_FORCEINLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
340 __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
346 \brief STR Exclusive (16 bit)
347 \details Executes a exclusive STR instruction for 16 bit values.
348 \param [in] value Value to store
349 \param [in] ptr Pointer to location
350 \return 0 Function succeeded
351 \return 1 Function failed
353 __STATIC_FORCEINLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
357 __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
363 \brief STR Exclusive (32 bit)
364 \details Executes a exclusive STR instruction for 32 bit values.
365 \param [in] value Value to store
366 \param [in] ptr Pointer to location
367 \return 0 Function succeeded
368 \return 1 Function failed
370 __STATIC_FORCEINLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
374 __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
380 \brief Remove the exclusive lock
381 \details Removes the exclusive lock which is created by LDREX.
383 __STATIC_FORCEINLINE void __CLREX(void)
385 __ASM volatile ("clrex" ::: "memory");
389 \brief Signed Saturate
390 \details Saturates a signed value.
391 \param [in] value Value to be saturated
392 \param [in] sat Bit position to saturate to (1..32)
393 \return Saturated value
395 #define __SSAT(ARG1,ARG2) \
398 int32_t __RES, __ARG1 = (ARG1); \
399 __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
405 \brief Unsigned Saturate
406 \details Saturates an unsigned value.
407 \param [in] value Value to be saturated
408 \param [in] sat Bit position to saturate to (0..31)
409 \return Saturated value
411 #define __USAT(ARG1,ARG2) \
414 uint32_t __RES, __ARG1 = (ARG1); \
415 __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
419 /* ########################### Core Function Access ########################### */
422 \brief Enable IRQ Interrupts
423 \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
424 Can only be executed in Privileged modes.
426 __STATIC_FORCEINLINE void __enable_irq(void)
428 __ASM volatile ("cpsie i" : : : "memory");
432 \brief Disable IRQ Interrupts
433 \details Disables IRQ interrupts by setting the I-bit in the CPSR.
434 Can only be executed in Privileged modes.
436 __STATIC_FORCEINLINE void __disable_irq(void)
438 __ASM volatile ("cpsid i" : : : "memory");
443 \details Returns the current value of the Floating Point Status/Control register.
444 \return Floating Point Status/Control register value
446 __STATIC_FORCEINLINE uint32_t __get_FPSCR(void)
448 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
449 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
450 #if __has_builtin(__builtin_arm_get_fpscr) || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
451 /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
452 return __builtin_arm_get_fpscr();
456 __ASM volatile ("VMRS %0, fpscr" : "=r" (result) );
466 \details Assigns the given value to the Floating Point Status/Control register.
467 \param [in] fpscr Floating Point Status/Control value to set
469 __STATIC_FORCEINLINE void __set_FPSCR(uint32_t fpscr)
471 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
472 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
473 #if __has_builtin(__builtin_arm_set_fpscr) || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
474 /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
475 __builtin_arm_set_fpscr(fpscr);
477 __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc", "memory");
484 /** \brief Get CPSR Register
485 \return CPSR Register value
487 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
490 __ASM volatile("MRS %0, cpsr" : "=r" (result) );
494 /** \brief Set CPSR Register
495 \param [in] cpsr CPSR value to set
497 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
499 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "memory");
503 \return Processor Mode
505 __STATIC_FORCEINLINE uint32_t __get_mode(void) {
506 return (__get_CPSR() & 0x1FU);
510 \param [in] mode Mode value to set
512 __STATIC_FORCEINLINE void __set_mode(uint32_t mode) {
513 __ASM volatile("MSR cpsr_c, %0" : : "r" (mode) : "memory");
516 /** \brief Get Stack Pointer
517 \return Stack Pointer value
519 __STATIC_FORCEINLINE uint32_t __get_SP(void)
522 __ASM volatile("MOV %0, sp" : "=r" (result) : : "memory");
526 /** \brief Set Stack Pointer
527 \param [in] stack Stack Pointer value to set
529 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
531 __ASM volatile("MOV sp, %0" : : "r" (stack) : "memory");
534 /** \brief Get USR/SYS Stack Pointer
535 \return USR/SYS Stack Pointer value
537 __STATIC_FORCEINLINE uint32_t __get_SP_usr(void)
539 uint32_t cpsr = __get_CPSR();
543 "MOV %0, sp " : "=r"(result) : : "memory"
550 /** \brief Set USR/SYS Stack Pointer
551 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
553 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
555 uint32_t cpsr = __get_CPSR();
558 "MOV sp, %0 " : : "r" (topOfProcStack) : "memory"
565 \return Floating Point Exception Control register value
567 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
569 #if (__FPU_PRESENT == 1)
571 __ASM volatile("VMRS %0, fpexc" : "=r" (result) );
579 \param [in] fpexc Floating Point Exception Control value to set
581 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
583 #if (__FPU_PRESENT == 1)
584 __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
589 * Include common core functions to access Coprocessor 15 registers
592 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
593 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
594 #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : "=r" (Rt) : : "memory" )
595 #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : : "r" (Rt) : "memory" )
597 #include "cmsis_cp15.h"
599 __STATIC_FORCEINLINE int32_t log2_up(uint32_t n)
607 /* if n not power of 2 -> round up*/
608 if ( n & (n - 1) ) log++;
612 __STATIC_INLINE void __L1C_MaintainDCacheSetWay(uint32_t level, uint32_t maint)
614 register volatile uint32_t Dummy;
615 register volatile uint32_t ccsidr;
619 uint32_t log2_linesize;
620 uint32_t log2_num_ways;
623 /* set csselr, select ccsidr register */
625 /* get current ccsidr register */
626 ccsidr = __get_CCSIDR();
627 num_sets = ((ccsidr & 0x0FFFE000) >> 13) + 1;
628 num_ways = ((ccsidr & 0x00001FF8) >> 3) + 1;
629 log2_linesize = (ccsidr & 0x00000007) + 2 + 2;
630 log2_num_ways = log2_up(num_ways);
631 shift_way = 32 - log2_num_ways;
632 for(int way = num_ways-1; way >= 0; way--) {
633 for(int set = num_sets-1; set >= 0; set--) {
634 Dummy = (level << 1) | (set << log2_linesize) | (way << shift_way);
638 __ASM volatile("MCR p15, 0, %0, c7, c6, 2" : : "r"(Dummy) : "memory"); // DCISW. Invalidate by Set/Way
642 __ASM volatile("MCR p15, 0, %0, c7, c10, 2" : : "r"(Dummy) : "memory"); // DCCSW. Clean by Set/Way
646 __ASM volatile("MCR p15, 0, %0, c7, c14, 2" : : "r"(Dummy) : "memory"); // DCCISW. Clean and Invalidate by Set/Way
655 /** \brief Clean and Invalidate the entire data or unified cache
657 Generic mechanism for cleaning/invalidating the entire data or unified cache to the point of coherency
659 __STATIC_INLINE void __L1C_CleanInvalidateCache(uint32_t op) {
660 register volatile uint32_t clidr;
662 clidr = __get_CLIDR();
663 for(uint32_t i = 0; i<7; i++)
665 cache_type = (clidr >> i*3) & 0x7UL;
666 if ((cache_type >= 2) && (cache_type <= 4))
668 __L1C_MaintainDCacheSetWay(i, op);
674 /** \brief Enable Floating Point Unit
676 Critical section, called from undef handler, so systick is disabled
678 __STATIC_INLINE void __FPU_Enable(void) {
680 //Permit access to VFP/NEON, registers by modifying CPACR
681 " MRC p15,0,R1,c1,c0,2 \n"
682 " ORR R1,R1,#0x00F00000 \n"
683 " MCR p15,0,R1,c1,c0,2 \n"
685 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
690 " ORR R1,R1,#0x40000000 \n"
693 //Initialise VFP/NEON registers to 0
696 #if TARGET_FEATURE_EXTENSION_REGISTER_COUNT >= 16
697 //Initialise D16 registers to 0
716 #if TARGET_FEATURE_EXTENSION_REGISTER_COUNT == 32
717 //Initialise D32 registers to 0
735 //Initialise FPSCR to a known state
737 " LDR R3,=0x00086060 \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
743 #pragma GCC diagnostic pop
745 #endif /* __CMSIS_GCC_H */