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.
25 #ifndef __CMSIS_ARMCC_H
26 #define __CMSIS_ARMCC_H
28 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 400677)
29 #error "Please use ARM Compiler Toolchain V4.0.677 or later!"
32 /* CMSIS compiler control architecture macros */
33 #if (defined (__TARGET_ARCH_7_A ) && (__TARGET_ARCH_7_A == 1))
34 #define __ARM_ARCH_7A__ 1
37 /* CMSIS compiler specific defines */
42 #define __INLINE __inline
45 #define __FORCEINLINE __forceinline
47 #ifndef __STATIC_INLINE
48 #define __STATIC_INLINE static __inline
50 #ifndef __STATIC_FORCEINLINE
51 #define __STATIC_FORCEINLINE static __forceinline
54 #define __NO_RETURN __declspec(noreturn)
56 #ifndef CMSIS_DEPRECATED
57 #define CMSIS_DEPRECATED __attribute__((deprecated))
60 #define __USED __attribute__((used))
63 #define __WEAK __attribute__((weak))
66 #define __PACKED __attribute__((packed))
68 #ifndef __PACKED_STRUCT
69 #define __PACKED_STRUCT __packed struct
71 #ifndef __UNALIGNED_UINT16_WRITE
72 #define __UNALIGNED_UINT16_WRITE(addr, val) ((*((__packed uint16_t *)(addr))) = (val))
74 #ifndef __UNALIGNED_UINT16_READ
75 #define __UNALIGNED_UINT16_READ(addr) (*((const __packed uint16_t *)(addr)))
77 #ifndef __UNALIGNED_UINT32_WRITE
78 #define __UNALIGNED_UINT32_WRITE(addr, val) ((*((__packed uint32_t *)(addr))) = (val))
80 #ifndef __UNALIGNED_UINT32_READ
81 #define __UNALIGNED_UINT32_READ(addr) (*((const __packed uint32_t *)(addr)))
84 #define __ALIGNED(x) __attribute__((aligned(x)))
87 #define __PACKED __attribute__((packed))
90 /* ########################## Core Instruction Access ######################### */
97 \brief Wait For Interrupt
102 \brief Wait For Event
112 \brief Instruction Synchronization Barrier
114 #define __ISB() do {\
115 __schedule_barrier();\
117 __schedule_barrier();\
121 \brief Data Synchronization Barrier
123 #define __DSB() do {\
124 __schedule_barrier();\
126 __schedule_barrier();\
130 \brief Data Memory Barrier
132 #define __DMB() do {\
133 __schedule_barrier();\
135 __schedule_barrier();\
139 \brief Reverse byte order (32 bit)
140 \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
141 \param [in] value Value to reverse
142 \return Reversed value
147 \brief Reverse byte order (16 bit)
148 \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
149 \param [in] value Value to reverse
150 \return Reversed value
152 #ifndef __NO_EMBEDDED_ASM
153 __attribute__((section(".rev16_text"))) __STATIC_INLINE __ASM uint32_t __REV16(uint32_t value)
161 \brief Reverse byte order (16 bit)
162 \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
163 \param [in] value Value to reverse
164 \return Reversed value
166 #ifndef __NO_EMBEDDED_ASM
167 __attribute__((section(".revsh_text"))) __STATIC_INLINE __ASM int16_t __REVSH(int16_t value)
175 \brief Rotate Right in unsigned value (32 bit)
176 \param [in] op1 Value to rotate
177 \param [in] op2 Number of Bits to rotate
178 \return Rotated value
184 \param [in] value is ignored by the processor.
185 If required, a debugger can use it to store additional information about the breakpoint.
187 #define __BKPT(value) __breakpoint(value)
190 \brief Reverse bit order of value
191 \param [in] value Value to reverse
192 \return Reversed value
194 #define __RBIT __rbit
197 \brief Count leading zeros
198 \param [in] value Value to count the leading zeros
199 \return number of leading zeros in value
204 \brief LDR Exclusive (8 bit)
205 \details Executes a exclusive LDR instruction for 8 bit value.
206 \param [in] ptr Pointer to data
207 \return value of type uint8_t at (*ptr)
209 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
210 #define __LDREXB(ptr) ((uint8_t ) __ldrex(ptr))
212 #define __LDREXB(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint8_t ) __ldrex(ptr)) _Pragma("pop")
216 \brief LDR Exclusive (16 bit)
217 \details Executes a exclusive LDR instruction for 16 bit values.
218 \param [in] ptr Pointer to data
219 \return value of type uint16_t at (*ptr)
221 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
222 #define __LDREXH(ptr) ((uint16_t) __ldrex(ptr))
224 #define __LDREXH(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint16_t) __ldrex(ptr)) _Pragma("pop")
228 \brief LDR Exclusive (32 bit)
229 \details Executes a exclusive LDR instruction for 32 bit values.
230 \param [in] ptr Pointer to data
231 \return value of type uint32_t at (*ptr)
233 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
234 #define __LDREXW(ptr) ((uint32_t ) __ldrex(ptr))
236 #define __LDREXW(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint32_t ) __ldrex(ptr)) _Pragma("pop")
240 \brief STR Exclusive (8 bit)
241 \details Executes a exclusive STR instruction for 8 bit values.
242 \param [in] value Value to store
243 \param [in] ptr Pointer to location
244 \return 0 Function succeeded
245 \return 1 Function failed
247 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
248 #define __STREXB(value, ptr) __strex(value, ptr)
250 #define __STREXB(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
254 \brief STR Exclusive (16 bit)
255 \details Executes a exclusive STR instruction for 16 bit values.
256 \param [in] value Value to store
257 \param [in] ptr Pointer to location
258 \return 0 Function succeeded
259 \return 1 Function failed
261 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
262 #define __STREXH(value, ptr) __strex(value, ptr)
264 #define __STREXH(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
268 \brief STR Exclusive (32 bit)
269 \details Executes a exclusive STR instruction for 32 bit values.
270 \param [in] value Value to store
271 \param [in] ptr Pointer to location
272 \return 0 Function succeeded
273 \return 1 Function failed
275 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
276 #define __STREXW(value, ptr) __strex(value, ptr)
278 #define __STREXW(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
282 \brief Remove the exclusive lock
283 \details Removes the exclusive lock which is created by LDREX.
285 #define __CLREX __clrex
289 \brief Signed Saturate
290 \details Saturates a signed value.
291 \param [in] value Value to be saturated
292 \param [in] sat Bit position to saturate to (1..32)
293 \return Saturated value
295 #define __SSAT __ssat
298 \brief Unsigned Saturate
299 \details Saturates an unsigned value.
300 \param [in] value Value to be saturated
301 \param [in] sat Bit position to saturate to (0..31)
302 \return Saturated value
304 #define __USAT __usat
306 /* ########################### Core Function Access ########################### */
309 \brief Get FPSCR (Floating Point Status/Control)
310 \return Floating Point Status/Control register value
312 __STATIC_INLINE uint32_t __get_FPSCR(void)
314 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
315 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
316 register uint32_t __regfpscr __ASM("fpscr");
324 \brief Set FPSCR (Floating Point Status/Control)
325 \param [in] fpscr Floating Point Status/Control value to set
327 __STATIC_INLINE void __set_FPSCR(uint32_t fpscr)
329 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
330 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
331 register uint32_t __regfpscr __ASM("fpscr");
332 __regfpscr = (fpscr);
338 /** \brief Get CPSR (Current Program Status Register)
339 \return CPSR Register value
341 __STATIC_INLINE uint32_t __get_CPSR(void)
343 register uint32_t __regCPSR __ASM("cpsr");
348 /** \brief Set CPSR (Current Program Status Register)
349 \param [in] cpsr CPSR value to set
351 __STATIC_INLINE void __set_CPSR(uint32_t cpsr)
353 register uint32_t __regCPSR __ASM("cpsr");
358 \return Processor Mode
360 __STATIC_INLINE uint32_t __get_mode(void)
362 return (__get_CPSR() & 0x1FU);
366 \param [in] mode Mode value to set
368 __STATIC_INLINE __ASM void __set_mode(uint32_t mode)
375 /** \brief Get Stack Pointer
376 \return Stack Pointer
378 __STATIC_INLINE __ASM uint32_t __get_SP(void)
384 /** \brief Set Stack Pointer
385 \param [in] stack Stack Pointer value to set
387 __STATIC_INLINE __ASM void __set_SP(uint32_t stack)
394 /** \brief Get USR/SYS Stack Pointer
395 \return USR/SYSStack Pointer
397 __STATIC_INLINE __ASM uint32_t __get_SP_usr(void)
403 CPS #0x1F ;no effect in USR mode
405 MSR CPSR_c, R1 ;no effect in USR mode
410 /** \brief Set USR/SYS Stack Pointer
411 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
413 __STATIC_INLINE __ASM void __set_SP_usr(uint32_t topOfProcStack)
419 CPS #0x1F ;no effect in USR mode
421 MSR CPSR_c, R1 ;no effect in USR mode
426 /** \brief Get FPEXC (Floating Point Exception Control Register)
427 \return Floating Point Exception Control Register value
429 __STATIC_INLINE uint32_t __get_FPEXC(void)
431 #if (__FPU_PRESENT == 1)
432 register uint32_t __regfpexc __ASM("fpexc");
439 /** \brief Set FPEXC (Floating Point Exception Control Register)
440 \param [in] fpexc Floating Point Exception Control value to set
442 __STATIC_INLINE void __set_FPEXC(uint32_t fpexc)
444 #if (__FPU_PRESENT == 1)
445 register uint32_t __regfpexc __ASM("fpexc");
446 __regfpexc = (fpexc);
451 * Include common core functions to access Coprocessor 15 registers
454 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) do { register uint32_t tmp __ASM("cp" # cp ":" # op1 ":c" # CRn ":c" # CRm ":" # op2); (Rt) = tmp; } while(0)
455 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) do { register uint32_t tmp __ASM("cp" # cp ":" # op1 ":c" # CRn ":c" # CRm ":" # op2); tmp = (Rt); } while(0)
456 #define __get_CP64(cp, op1, Rt, CRm) \
458 uint32_t ltmp, htmp; \
459 __ASM volatile("MRRC p" # cp ", " # op1 ", ltmp, htmp, c" # CRm); \
460 (Rt) = ((((uint64_t)htmp) << 32U) | ((uint64_t)ltmp)); \
463 #define __set_CP64(cp, op1, Rt, CRm) \
465 const uint64_t tmp = (Rt); \
466 const uint32_t ltmp = (uint32_t)(tmp); \
467 const uint32_t htmp = (uint32_t)(tmp >> 32U); \
468 __ASM volatile("MCRR p" # cp ", " # op1 ", ltmp, htmp, c" # CRm); \
471 #include "cmsis_cp15.h"
473 /** \brief Enable Floating Point Unit
475 Critical section, called from undef handler, so systick is disabled
477 __STATIC_INLINE __ASM void __FPU_Enable(void)
481 //Permit access to VFP/NEON, registers by modifying CPACR
483 ORR R1,R1,#0x00F00000
486 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
491 ORR R1,R1,#0x40000000
494 //Initialise VFP/NEON registers to 0
497 //Initialise D16 registers to 0
515 IF {TARGET_FEATURE_EXTENSION_REGISTER_COUNT} == 32
516 //Initialise D32 registers to 0
535 //Initialise FPSCR to a known state
537 LDR R3,=0x00086060 //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
544 #endif /* __CMSIS_ARMCC_H */