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 #ifndef __ARM_COMPAT_H
29 #include <arm_compat.h> /* Compatibility header for ARM Compiler 5 intrinsics */
32 /* CMSIS compiler specific defines */
37 #define __INLINE __inline
39 #ifndef __STATIC_INLINE
40 #define __STATIC_INLINE static __inline
43 #define __NO_RETURN __declspec(noreturn)
46 #define __USED __attribute__((used))
49 #define __WEAK __attribute__((weak))
52 #define __PACKED __attribute__((packed, aligned(1)))
54 #ifndef __PACKED_STRUCT
55 #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
57 #ifndef __UNALIGNED_UINT16_WRITE
58 #pragma clang diagnostic push
59 #pragma clang diagnostic ignored "-Wpacked"
60 /* lint -esym(9058, T_UINT16_WRITE) disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
61 __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
62 #pragma clang diagnostic pop
63 #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
65 #ifndef __UNALIGNED_UINT16_READ
66 #pragma clang diagnostic push
67 #pragma clang diagnostic ignored "-Wpacked"
68 /* lint -esym(9058, T_UINT16_READ) disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
69 __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
70 #pragma clang diagnostic pop
71 #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
73 #ifndef __UNALIGNED_UINT32_WRITE
74 #pragma clang diagnostic push
75 #pragma clang diagnostic ignored "-Wpacked"
76 /* lint -esym(9058, T_UINT32_WRITE) disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
77 __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
78 #pragma clang diagnostic pop
79 #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
81 #ifndef __UNALIGNED_UINT32_READ
82 #pragma clang diagnostic push
83 #pragma clang diagnostic ignored "-Wpacked"
84 __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
85 #pragma clang diagnostic pop
86 #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
89 #define __ALIGNED(x) __attribute__((aligned(x)))
92 #define __PACKED __attribute__((packed))
96 /* ########################### Core Function Access ########################### */
100 \return Floating Point Status/Control register value
102 __STATIC_INLINE uint32_t __get_FPSCR(void)
104 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
105 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
107 __ASM volatile("MRS %0, fpscr" : "=r" (result) );
116 \param [in] fpscr Floating Point Status/Control value to set
118 __STATIC_INLINE void __set_FPSCR(uint32_t fpscr)
120 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
121 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
122 __ASM volatile ("MSR fpscr, %0" : : "r" (fpscr) : "memory");
128 /* ########################## Core Instruction Access ######################### */
132 #define __NOP __builtin_arm_nop
135 \brief Wait For Interrupt
137 #define __WFI __builtin_arm_wfi
140 \brief Wait For Event
142 #define __WFE __builtin_arm_wfe
147 #define __SEV __builtin_arm_sev
150 \brief Instruction Synchronization Barrier
152 #define __ISB() do {\
153 __schedule_barrier();\
154 __builtin_arm_isb(0xF);\
155 __schedule_barrier();\
159 \brief Data Synchronization Barrier
161 #define __DSB() do {\
162 __schedule_barrier();\
163 __builtin_arm_dsb(0xF);\
164 __schedule_barrier();\
168 \brief Data Memory Barrier
170 #define __DMB() do {\
171 __schedule_barrier();\
172 __builtin_arm_dmb(0xF);\
173 __schedule_barrier();\
177 \brief Reverse byte order (32 bit)
178 \param [in] value Value to reverse
179 \return Reversed value
181 #define __REV __builtin_bswap32
184 \brief Reverse byte order (16 bit)
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 in signed short value
199 \param [in] value Value to reverse
200 \return Reversed value
202 #ifndef __NO_EMBEDDED_ASM
203 __attribute__((section(".revsh_text"))) __STATIC_INLINE int32_t __REVSH(int32_t value)
206 __ASM volatile("revsh %0, %1" : "=r" (result) : "r" (value));
212 \brief Rotate Right in unsigned value (32 bit)
213 \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
214 \param [in] op1 Value to rotate
215 \param [in] op2 Number of Bits to rotate
216 \return Rotated value
218 __attribute__((always_inline)) __STATIC_INLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
220 return (op1 >> op2) | (op1 << (32U - op2));
225 \param [in] value is ignored by the processor.
226 If required, a debugger can use it to store additional information about the breakpoint.
228 #define __BKPT(value) __ASM volatile ("bkpt "#value)
231 \brief Reverse bit order of value
232 \param [in] value Value to reverse
233 \return Reversed value
235 #define __RBIT __builtin_arm_rbit
238 \brief Count leading zeros
239 \param [in] value Value to count the leading zeros
240 \return number of leading zeros in value
242 #define __CLZ __builtin_clz
245 \brief LDR Exclusive (8 bit)
246 \details Executes a exclusive LDR instruction for 8 bit value.
247 \param [in] ptr Pointer to data
248 \return value of type uint8_t at (*ptr)
250 #define __LDREXB (uint8_t)__builtin_arm_ldrex
254 \brief LDR Exclusive (16 bit)
255 \details Executes a exclusive LDR instruction for 16 bit values.
256 \param [in] ptr Pointer to data
257 \return value of type uint16_t at (*ptr)
259 #define __LDREXH (uint16_t)__builtin_arm_ldrex
262 \brief LDR Exclusive (32 bit)
263 \details Executes a exclusive LDR instruction for 32 bit values.
264 \param [in] ptr Pointer to data
265 \return value of type uint32_t at (*ptr)
267 #define __LDREXW (uint32_t)__builtin_arm_ldrex
270 \brief STR Exclusive (8 bit)
271 \details Executes a exclusive STR instruction for 8 bit values.
272 \param [in] value Value to store
273 \param [in] ptr Pointer to location
274 \return 0 Function succeeded
275 \return 1 Function failed
277 #define __STREXB (uint32_t)__builtin_arm_strex
280 \brief STR Exclusive (16 bit)
281 \details Executes a exclusive STR instruction for 16 bit values.
282 \param [in] value Value to store
283 \param [in] ptr Pointer to location
284 \return 0 Function succeeded
285 \return 1 Function failed
287 #define __STREXH (uint32_t)__builtin_arm_strex
290 \brief STR Exclusive (32 bit)
291 \details Executes a exclusive STR instruction for 32 bit values.
292 \param [in] value Value to store
293 \param [in] ptr Pointer to location
294 \return 0 Function succeeded
295 \return 1 Function failed
297 #define __STREXW (uint32_t)__builtin_arm_strex
300 \brief Remove the exclusive lock
301 \details Removes the exclusive lock which is created by LDREX.
303 #define __CLREX __builtin_arm_clrex
305 /** \brief Get CPSR Register
306 \return CPSR Register value
308 __STATIC_INLINE uint32_t __get_CPSR(void)
311 __ASM volatile("MRS %0, cpsr" : "=r" (result) );
315 /** \brief Set CPSR Register
316 \param [in] cpsr CPSR value to set
318 __STATIC_INLINE void __set_CPSR(uint32_t cpsr)
320 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "memory");
324 \return Processor Mode
326 __STATIC_INLINE uint32_t __get_mode(void) {
327 return (__get_CPSR() & 0x1FU);
331 \param [in] mode Mode value to set
333 __STATIC_INLINE void __set_mode(uint32_t mode) {
334 __ASM volatile("MSR cpsr_c, %0" : : "r" (mode) : "memory");
337 /** \brief Set Stack Pointer
338 \param [in] stack Stack Pointer value to set
340 __STATIC_INLINE void __set_SP(uint32_t stack)
342 __ASM volatile("MOV sp, %0" : : "r" (stack) : "memory");
345 /** \brief Set USR/SYS Stack Pointer
346 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
348 __STATIC_INLINE void __set_SP_usr(uint32_t topOfProcStack)
352 "BIC r0, r0, #7 \n" // ensure stack is 8-byte aligned
354 "CPS #0x1F \n" // no effect in USR mode
356 "MSR cpsr_c, r1 \n" // no effect in USR mode
362 \return Floating Point Exception Control register value
364 __STATIC_INLINE uint32_t __get_FPEXC(void)
366 #if (__FPU_PRESENT == 1)
368 __ASM volatile("MRS %0, fpexc" : "=r" (result) );
376 \param [in] fpexc Floating Point Exception Control value to set
378 __STATIC_INLINE void __set_FPEXC(uint32_t fpexc)
380 #if (__FPU_PRESENT == 1)
381 __ASM volatile ("MSR fpexc, %0" : : "r" (fpexc) : "memory");
386 \return Auxiliary Control register value
388 __STATIC_INLINE uint32_t __get_ACTLR(void)
391 __ASM volatile("MRS %0, actlr" : "=r" (result) );
396 \param [in] actlr Auxiliary Control value to set
398 __STATIC_INLINE void __set_ACTLR(uint32_t actlr)
400 __ASM volatile ("MSR fpexc, %0" : : "r" (actlr) : "memory");
403 \return Coprocessor Access Control register value
405 __STATIC_INLINE uint32_t __get_CPACR(void)
408 __ASM volatile("MRC p15, 0, %0, c1, c0, 2" : "=r"(result));
413 \param [in] cpacr Coprocessor Access Control value to set
415 __STATIC_INLINE void __set_CPACR(uint32_t cpacr)
417 __ASM volatile("MCR p15, 0, %0, c1, c0, 2" : : "r"(cpacr) : "memory");
421 \return Configuration Base Address register value
423 __STATIC_INLINE uint32_t __get_CBAR() {
425 __ASM volatile("MRC p15, 4, %0, c15, c0, 0" : "=r"(result));
431 This function returns the value of the Translation Table Base Register 0.
433 \return Translation Table Base Register 0 value
435 __STATIC_INLINE uint32_t __get_TTBR0() {
437 __ASM volatile("MRC p15, 0, %0, c2, c0, 0" : "=r"(result));
443 This function assigns the given value to the Translation Table Base Register 0.
445 \param [in] ttbr0 Translation Table Base Register 0 value to set
447 __STATIC_INLINE void __set_TTBR0(uint32_t ttbr0) {
448 __ASM volatile("MCR p15, 0, %0, c2, c0, 0" : : "r"(ttbr0) : "memory");
453 This function returns the value of the Domain Access Control Register.
455 \return Domain Access Control Register value
457 __STATIC_INLINE uint32_t __get_DACR() {
459 __ASM volatile("MRC p15, 0, %0, c3, c0, 0" : "=r"(result));
465 This function assigns the given value to the Domain Access Control Register.
467 \param [in] dacr Domain Access Control Register value to set
469 __STATIC_INLINE void __set_DACR(uint32_t dacr) {
470 __ASM volatile("MCR p15, 0, %0, c3, c0, 0" : : "r"(dacr) : "memory");
475 This function assigns the given value to the System Control Register.
477 \param [in] sctlr System Control Register value to set
479 __STATIC_INLINE void __set_SCTLR(uint32_t sctlr)
481 __ASM volatile("MCR p15, 0, %0, c1, c0, 0" : : "r"(sctlr) : "memory");
485 \return System Control Register value
487 __STATIC_INLINE uint32_t __get_SCTLR() {
489 __ASM volatile("MRC p15, 0, %0, c1, c0, 0" : "=r"(result));
494 \param [in] actrl Auxiliary Control Register value to set
496 __STATIC_INLINE void __set_ACTRL(uint32_t actrl)
498 __ASM volatile("MCR p15, 0, %0, c1, c0, 1" : : "r"(actrl) : "memory");
502 \return Auxiliary Control Register value
504 __STATIC_INLINE uint32_t __get_ACTRL(void)
507 __ASM volatile("MRC p15, 0, %0, c1, c0, 1" : "=r"(result));
513 This function returns the value of the Multiprocessor Affinity Register.
515 \return Multiprocessor Affinity Register value
517 __STATIC_INLINE uint32_t __get_MPIDR(void)
520 __ASM volatile("MRC p15, 0, %0, c0, c0, 5" : "=r"(result));
526 This function returns the value of the Vector Base Address Register.
528 \return Vector Base Address Register
530 __STATIC_INLINE uint32_t __get_VBAR(void)
533 __ASM volatile("MRC p15, 0, %0, c12, c0, 0" : "=r"(result));
539 This function assigns the given value to the Vector Base Address Register.
541 \param [in] vbar Vector Base Address Register value to set
543 __STATIC_INLINE void __set_VBAR(uint32_t vbar)
545 __ASM volatile("MCR p15, 0, %0, c12, c0, 1" : : "r"(vbar) : "memory");
548 /** \brief Set CNTFRQ
550 This function assigns the given value to PL1 Physical Timer Counter Frequency Register (CNTFRQ).
552 \param [in] value CNTFRQ Register value to set
554 __STATIC_INLINE void __set_CNTFRQ(uint32_t value) {
555 __ASM volatile("MCR p15, 0, %0, c14, c0, 0" : : "r"(value) : "memory");
558 /** \brief Set CNTP_TVAL
560 This function assigns the given value to PL1 Physical Timer Value Register (CNTP_TVAL).
562 \param [in] value CNTP_TVAL Register value to set
564 __STATIC_INLINE void __set_CNTP_TVAL(uint32_t value) {
565 __ASM volatile("MCR p15, 0, %0, c14, c2, 0" : : "r"(value) : "memory");
568 /** \brief Get CNTP_TVAL
570 This function returns the value of the PL1 Physical Timer Value Register (CNTP_TVAL).
572 \return CNTP_TVAL Register value
574 __STATIC_INLINE uint32_t __get_CNTP_TVAL() {
576 __ASM volatile("MRC p15, 0, %0, c14, c2, 0" : "=r"(result));
580 /** \brief Set CNTP_CTL
582 This function assigns the given value to PL1 Physical Timer Control Register (CNTP_CTL).
584 \param [in] value CNTP_CTL Register value to set
586 __STATIC_INLINE void __set_CNTP_CTL(uint32_t value) {
587 __ASM volatile("MCR p15, 0, %0, c14, c2, 1" : : "r"(value) : "memory");
590 /** \brief Set TLBIALL
594 __STATIC_INLINE void __set_TLBIALL(uint32_t value) {
595 __ASM volatile("MCR p15, 0, %0, c8, c7, 0" : : "r"(value) : "memory");
598 /** \brief Set BPIALL.
600 Branch Predictor Invalidate All
602 __STATIC_INLINE void __set_BPIALL(uint32_t value) {
603 __ASM volatile("MCR p15, 0, %0, c7, c5, 6" : : "r"(value) : "memory");
606 /** \brief Set ICIALLU
608 Instruction Cache Invalidate All
610 __STATIC_INLINE void __set_ICIALLU(uint32_t value) {
611 __ASM volatile("MCR p15, 0, %0, c7, c5, 0" : : "r"(value) : "memory");
614 /** \brief Set DCCMVAC
618 __STATIC_INLINE void __set_DCCMVAC(uint32_t value) {
619 __ASM volatile("MCR p15, 0, %0, c7, c10, 1" : : "r"(value) : "memory");
622 /** \brief Set DCIMVAC
624 Data cache invalidate
626 __STATIC_INLINE void __set_DCIMVAC(uint32_t value) {
627 __ASM volatile("MCR p15, 0, %0, c7, c6, 1" : : "r"(value) : "memory");
630 /** \brief Set DCCIMVAC
632 Data cache clean and invalidate
634 __STATIC_INLINE void __set_DCCIMVAC(uint32_t value) {
635 __ASM volatile("MCR p15, 0, %0, c7, c14, 1" : : "r"(value) : "memory");
638 /** \brief Clean and Invalidate the entire data or unified cache
640 Generic mechanism for cleaning/invalidating the entire data or unified cache to the point of coherency
642 __STATIC_INLINE void __L1C_CleanInvalidateCache(uint32_t op) {
646 " MRC p15, 1, R6, c0, c0, 1 \n" // Read CLIDR
647 " ANDS R3, R6, #0x07000000 \n" // Extract coherency level
648 " MOV R3, R3, LSR #23 \n" // Total cache levels << 1
649 " BEQ Finished \n" // If 0, no need to clean
651 " MOV R10, #0 \n" // R10 holds current cache level << 1
652 "Loop1: ADD R2, R10, R10, LSR #1 \n" // R2 holds cache "Set" position
653 " MOV R1, R6, LSR R2 \n" // Bottom 3 bits are the Cache-type for this level
654 " AND R1, R1, #7 \n" // Isolate those lower 3 bits
656 " BLT Skip \n" // No cache or only instruction cache at this level
658 " MCR p15, 2, R10, c0, c0, 0 \n" // Write the Cache Size selection register
659 " ISB \n" // ISB to sync the change to the CacheSizeID reg
660 " MRC p15, 1, R1, c0, c0, 0 \n" // Reads current Cache Size ID register
661 " AND R2, R1, #7 \n" // Extract the line length field
662 " ADD R2, R2, #4 \n" // Add 4 for the line length offset (log2 16 bytes)
664 " ANDS R4, R4, R1, LSR #3 \n" // R4 is the max number on the way size (right aligned)
665 " CLZ R5, R4 \n" // R5 is the bit position of the way size increment
666 " LDR R7, =0x7FFF \n"
667 " ANDS R7, R7, R1, LSR #13 \n" // R7 is the max number of the index size (right aligned)
669 "Loop2: MOV R9, R4 \n" // R9 working copy of the max way size (right aligned)
671 "Loop3: ORR R11, R10, R9, LSL R5 \n" // Factor in the Way number and cache number into R11
672 " ORR R11, R11, R7, LSL R2 \n" // Factor in the Set number
675 " MCR p15, 0, R11, c7, c6, 2 \n" // DCISW. Invalidate by Set/Way
677 "Dccsw: CMP R0, #1 \n"
679 " MCR p15, 0, R11, c7, c10, 2 \n" // DCCSW. Clean by Set/Way
681 "Dccisw: MCR p15, 0, R11, c7, c14, 2 \n" // DCCISW. Clean and Invalidate by Set/Way
682 "cont: SUBS R9, R9, #1 \n" // Decrement the Way number
684 " SUBS R7, R7, #1 \n" // Decrement the Set number
686 "Skip: ADD R10, R10, #2 \n" // Increment the cache number
696 /** \brief Enable Floating Point Unit
698 Critical section, called from undef handler, so systick is disabled
700 __STATIC_INLINE void __FPU_Enable(void) {
702 //Permit access to VFP/NEON, registers by modifying CPACR
703 " MRC p15,0,R1,c1,c0,2 \n"
704 " ORR R1,R1,#0x00F00000 \n"
705 " MCR p15,0,R1,c1,c0,2 \n"
707 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
712 " ORR R1,R1,#0x40000000 \n"
715 //Initialise VFP/NEON registers to 0
717 #if 0 // TODO: Initialize FPU registers according to available register count
718 ".if {TARGET_FEATURE_EXTENSION_REGISTER_COUNT} >= 16 \n"
719 //Initialise D16 registers to 0
738 ".if {TARGET_FEATURE_EXTENSION_REGISTER_COUNT} == 32 \n"
739 //Initialise D32 registers to 0
758 //Initialise FPSCR to a known state
760 " LDR R3,=0x00086060 \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
766 #endif /* __CMSIS_ARMCC_H */