1 /**************************************************************************//**
3 * @brief CMSIS compiler specific macros, functions, instructions
5 * @date 10. January 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.
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))
89 #ifndef __COMPILER_BARRIER
90 #define __COMPILER_BARRIER() __memory_changed()
93 /* ########################## Core Instruction Access ######################### */
100 \brief Wait For Interrupt
105 \brief Wait For Event
115 \brief Instruction Synchronization Barrier
117 #define __ISB() do {\
118 __schedule_barrier();\
120 __schedule_barrier();\
124 \brief Data Synchronization Barrier
126 #define __DSB() do {\
127 __schedule_barrier();\
129 __schedule_barrier();\
133 \brief Data Memory Barrier
135 #define __DMB() do {\
136 __schedule_barrier();\
138 __schedule_barrier();\
142 \brief Reverse byte order (32 bit)
143 \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
144 \param [in] value Value to reverse
145 \return Reversed value
150 \brief Reverse byte order (16 bit)
151 \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
152 \param [in] value Value to reverse
153 \return Reversed value
155 #ifndef __NO_EMBEDDED_ASM
156 __attribute__((section(".rev16_text"))) __STATIC_INLINE __ASM uint32_t __REV16(uint32_t value)
164 \brief Reverse byte order (16 bit)
165 \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
166 \param [in] value Value to reverse
167 \return Reversed value
169 #ifndef __NO_EMBEDDED_ASM
170 __attribute__((section(".revsh_text"))) __STATIC_INLINE __ASM int16_t __REVSH(int16_t value)
178 \brief Rotate Right in unsigned value (32 bit)
179 \param [in] op1 Value to rotate
180 \param [in] op2 Number of Bits to rotate
181 \return Rotated value
187 \param [in] value is ignored by the processor.
188 If required, a debugger can use it to store additional information about the breakpoint.
190 #define __BKPT(value) __breakpoint(value)
193 \brief Reverse bit order of value
194 \param [in] value Value to reverse
195 \return Reversed value
197 #define __RBIT __rbit
200 \brief Count leading zeros
201 \param [in] value Value to count the leading zeros
202 \return number of leading zeros in value
207 \brief LDR Exclusive (8 bit)
208 \details Executes a exclusive LDR instruction for 8 bit value.
209 \param [in] ptr Pointer to data
210 \return value of type uint8_t at (*ptr)
212 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
213 #define __LDREXB(ptr) ((uint8_t ) __ldrex(ptr))
215 #define __LDREXB(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint8_t ) __ldrex(ptr)) _Pragma("pop")
219 \brief LDR Exclusive (16 bit)
220 \details Executes a exclusive LDR instruction for 16 bit values.
221 \param [in] ptr Pointer to data
222 \return value of type uint16_t at (*ptr)
224 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
225 #define __LDREXH(ptr) ((uint16_t) __ldrex(ptr))
227 #define __LDREXH(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint16_t) __ldrex(ptr)) _Pragma("pop")
231 \brief LDR Exclusive (32 bit)
232 \details Executes a exclusive LDR instruction for 32 bit values.
233 \param [in] ptr Pointer to data
234 \return value of type uint32_t at (*ptr)
236 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
237 #define __LDREXW(ptr) ((uint32_t ) __ldrex(ptr))
239 #define __LDREXW(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint32_t ) __ldrex(ptr)) _Pragma("pop")
243 \brief STR Exclusive (8 bit)
244 \details Executes a exclusive STR instruction for 8 bit values.
245 \param [in] value Value to store
246 \param [in] ptr Pointer to location
247 \return 0 Function succeeded
248 \return 1 Function failed
250 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
251 #define __STREXB(value, ptr) __strex(value, ptr)
253 #define __STREXB(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
257 \brief STR Exclusive (16 bit)
258 \details Executes a exclusive STR instruction for 16 bit values.
259 \param [in] value Value to store
260 \param [in] ptr Pointer to location
261 \return 0 Function succeeded
262 \return 1 Function failed
264 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
265 #define __STREXH(value, ptr) __strex(value, ptr)
267 #define __STREXH(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
271 \brief STR Exclusive (32 bit)
272 \details Executes a exclusive STR instruction for 32 bit values.
273 \param [in] value Value to store
274 \param [in] ptr Pointer to location
275 \return 0 Function succeeded
276 \return 1 Function failed
278 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
279 #define __STREXW(value, ptr) __strex(value, ptr)
281 #define __STREXW(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
285 \brief Remove the exclusive lock
286 \details Removes the exclusive lock which is created by LDREX.
288 #define __CLREX __clrex
292 \brief Signed Saturate
293 \details Saturates a signed value.
294 \param [in] value Value to be saturated
295 \param [in] sat Bit position to saturate to (1..32)
296 \return Saturated value
298 #define __SSAT __ssat
301 \brief Unsigned Saturate
302 \details Saturates an unsigned value.
303 \param [in] value Value to be saturated
304 \param [in] sat Bit position to saturate to (0..31)
305 \return Saturated value
307 #define __USAT __usat
309 /* ########################### Core Function Access ########################### */
312 \brief Get FPSCR (Floating Point Status/Control)
313 \return Floating Point Status/Control register value
315 __STATIC_INLINE uint32_t __get_FPSCR(void)
317 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
318 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
319 register uint32_t __regfpscr __ASM("fpscr");
327 \brief Set FPSCR (Floating Point Status/Control)
328 \param [in] fpscr Floating Point Status/Control value to set
330 __STATIC_INLINE void __set_FPSCR(uint32_t fpscr)
332 #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
333 (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
334 register uint32_t __regfpscr __ASM("fpscr");
335 __regfpscr = (fpscr);
341 /** \brief Get CPSR (Current Program Status Register)
342 \return CPSR Register value
344 __STATIC_INLINE uint32_t __get_CPSR(void)
346 register uint32_t __regCPSR __ASM("cpsr");
351 /** \brief Set CPSR (Current Program Status Register)
352 \param [in] cpsr CPSR value to set
354 __STATIC_INLINE void __set_CPSR(uint32_t cpsr)
356 register uint32_t __regCPSR __ASM("cpsr");
361 \return Processor Mode
363 __STATIC_INLINE uint32_t __get_mode(void)
365 return (__get_CPSR() & 0x1FU);
369 \param [in] mode Mode value to set
371 __STATIC_INLINE __ASM void __set_mode(uint32_t mode)
378 /** \brief Get Stack Pointer
379 \return Stack Pointer
381 __STATIC_INLINE __ASM uint32_t __get_SP(void)
387 /** \brief Set Stack Pointer
388 \param [in] stack Stack Pointer value to set
390 __STATIC_INLINE __ASM void __set_SP(uint32_t stack)
397 /** \brief Get USR/SYS Stack Pointer
398 \return USR/SYSStack Pointer
400 __STATIC_INLINE __ASM uint32_t __get_SP_usr(void)
406 CPS #0x1F ;no effect in USR mode
408 MSR CPSR_c, R1 ;no effect in USR mode
413 /** \brief Set USR/SYS Stack Pointer
414 \param [in] topOfProcStack USR/SYS Stack Pointer value to set
416 __STATIC_INLINE __ASM void __set_SP_usr(uint32_t topOfProcStack)
422 CPS #0x1F ;no effect in USR mode
424 MSR CPSR_c, R1 ;no effect in USR mode
429 /** \brief Get FPEXC (Floating Point Exception Control Register)
430 \return Floating Point Exception Control Register value
432 __STATIC_INLINE uint32_t __get_FPEXC(void)
434 #if (__FPU_PRESENT == 1)
435 register uint32_t __regfpexc __ASM("fpexc");
442 /** \brief Set FPEXC (Floating Point Exception Control Register)
443 \param [in] fpexc Floating Point Exception Control value to set
445 __STATIC_INLINE void __set_FPEXC(uint32_t fpexc)
447 #if (__FPU_PRESENT == 1)
448 register uint32_t __regfpexc __ASM("fpexc");
449 __regfpexc = (fpexc);
454 * Include common core functions to access Coprocessor 15 registers
457 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) do { register volatile uint32_t tmp __ASM("cp" # cp ":" # op1 ":c" # CRn ":c" # CRm ":" # op2); (Rt) = tmp; } while(0)
458 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) do { register volatile uint32_t tmp __ASM("cp" # cp ":" # op1 ":c" # CRn ":c" # CRm ":" # op2); tmp = (Rt); } while(0)
459 #define __get_CP64(cp, op1, Rt, CRm) \
461 uint32_t ltmp, htmp; \
462 __ASM volatile("MRRC p" # cp ", " # op1 ", ltmp, htmp, c" # CRm); \
463 (Rt) = ((((uint64_t)htmp) << 32U) | ((uint64_t)ltmp)); \
466 #define __set_CP64(cp, op1, Rt, CRm) \
468 const uint64_t tmp = (Rt); \
469 const uint32_t ltmp = (uint32_t)(tmp); \
470 const uint32_t htmp = (uint32_t)(tmp >> 32U); \
471 __ASM volatile("MCRR p" # cp ", " # op1 ", ltmp, htmp, c" # CRm); \
474 #include "cmsis_cp15.h"
476 /** \brief Enable Floating Point Unit
478 Critical section, called from undef handler, so systick is disabled
480 __STATIC_INLINE __ASM void __FPU_Enable(void)
484 //Permit access to VFP/NEON, registers by modifying CPACR
486 ORR R1,R1,#0x00F00000
489 //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
494 ORR R1,R1,#0x40000000
497 //Initialise VFP/NEON registers to 0
500 //Initialise D16 registers to 0
518 IF {TARGET_FEATURE_EXTENSION_REGISTER_COUNT} == 32
519 //Initialise D32 registers to 0
538 //Initialise FPSCR to a known state
540 LDR R3,=0x00086060 //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
547 #endif /* __CMSIS_ARMCC_H */