1 /**************************************************************************//**
\r
2 * @file core_cmInstr.h
\r
3 * @brief CMSIS Cortex-M Core Instruction Access Header File
\r
5 * @date 19. July 2011
\r
8 * Copyright (C) 2009-2011 ARM Limited. All rights reserved.
\r
11 * ARM Limited (ARM) is supplying this software for use with Cortex-M
\r
12 * processor based microcontrollers. This file can be freely distributed
\r
13 * within development tools that are supporting such ARM based processors.
\r
16 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
\r
17 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
\r
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
\r
19 * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
\r
20 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
\r
22 ******************************************************************************/
\r
24 #ifndef __CORE_CMINSTR_H
\r
25 #define __CORE_CMINSTR_H
\r
28 /* ########################## Core Instruction Access ######################### */
\r
29 /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface
\r
30 Access to dedicated instructions
\r
34 #if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/
\r
35 /* ARM armcc specific functions */
\r
37 #if (__ARMCC_VERSION < 400677)
\r
38 #error "Please use ARM Compiler Toolchain V4.0.677 or later!"
\r
42 /** \brief No Operation
\r
44 No Operation does nothing. This instruction can be used for code alignment purposes.
\r
49 /** \brief Wait For Interrupt
\r
51 Wait For Interrupt is a hint instruction that suspends execution
\r
52 until one of a number of events occurs.
\r
57 /** \brief Wait For Event
\r
59 Wait For Event is a hint instruction that permits the processor to enter
\r
60 a low-power state until one of a number of events occurs.
\r
65 /** \brief Send Event
\r
67 Send Event is a hint instruction. It causes an event to be signaled to the CPU.
\r
72 /** \brief Instruction Synchronization Barrier
\r
74 Instruction Synchronization Barrier flushes the pipeline in the processor,
\r
75 so that all instructions following the ISB are fetched from cache or
\r
76 memory, after the instruction has been completed.
\r
78 #define __ISB() __isb(0xF)
\r
81 /** \brief Data Synchronization Barrier
\r
83 This function acts as a special kind of Data Memory Barrier.
\r
84 It completes when all explicit memory accesses before this instruction complete.
\r
86 #define __DSB() __dsb(0xF)
\r
89 /** \brief Data Memory Barrier
\r
91 This function ensures the apparent order of the explicit memory operations before
\r
92 and after the instruction, without ensuring their completion.
\r
94 #define __DMB() __dmb(0xF)
\r
97 /** \brief Reverse byte order (32 bit)
\r
99 This function reverses the byte order in integer value.
\r
101 \param [in] value Value to reverse
\r
102 \return Reversed value
\r
104 #define __REV __rev
\r
107 /** \brief Reverse byte order (16 bit)
\r
109 This function reverses the byte order in two unsigned short values.
\r
111 \param [in] value Value to reverse
\r
112 \return Reversed value
\r
114 static __INLINE __ASM uint32_t __REV16(uint32_t value)
\r
121 /** \brief Reverse byte order in signed short value
\r
123 This function reverses the byte order in a signed short value with sign extension to integer.
\r
125 \param [in] value Value to reverse
\r
126 \return Reversed value
\r
128 static __INLINE __ASM int32_t __REVSH(int32_t value)
\r
135 #if (__CORTEX_M >= 0x03)
\r
137 /** \brief Reverse bit order of value
\r
139 This function reverses the bit order of the given value.
\r
141 \param [in] value Value to reverse
\r
142 \return Reversed value
\r
144 #define __RBIT __rbit
\r
147 /** \brief LDR Exclusive (8 bit)
\r
149 This function performs a exclusive LDR command for 8 bit value.
\r
151 \param [in] ptr Pointer to data
\r
152 \return value of type uint8_t at (*ptr)
\r
154 #define __LDREXB(ptr) ((uint8_t ) __ldrex(ptr))
\r
157 /** \brief LDR Exclusive (16 bit)
\r
159 This function performs a exclusive LDR command for 16 bit values.
\r
161 \param [in] ptr Pointer to data
\r
162 \return value of type uint16_t at (*ptr)
\r
164 #define __LDREXH(ptr) ((uint16_t) __ldrex(ptr))
\r
167 /** \brief LDR Exclusive (32 bit)
\r
169 This function performs a exclusive LDR command for 32 bit values.
\r
171 \param [in] ptr Pointer to data
\r
172 \return value of type uint32_t at (*ptr)
\r
174 #define __LDREXW(ptr) ((uint32_t ) __ldrex(ptr))
\r
177 /** \brief STR Exclusive (8 bit)
\r
179 This function performs a exclusive STR command for 8 bit values.
\r
181 \param [in] value Value to store
\r
182 \param [in] ptr Pointer to location
\r
183 \return 0 Function succeeded
\r
184 \return 1 Function failed
\r
186 #define __STREXB(value, ptr) __strex(value, ptr)
\r
189 /** \brief STR Exclusive (16 bit)
\r
191 This function performs a exclusive STR command for 16 bit values.
\r
193 \param [in] value Value to store
\r
194 \param [in] ptr Pointer to location
\r
195 \return 0 Function succeeded
\r
196 \return 1 Function failed
\r
198 #define __STREXH(value, ptr) __strex(value, ptr)
\r
201 /** \brief STR Exclusive (32 bit)
\r
203 This function performs a exclusive STR command for 32 bit values.
\r
205 \param [in] value Value to store
\r
206 \param [in] ptr Pointer to location
\r
207 \return 0 Function succeeded
\r
208 \return 1 Function failed
\r
210 #define __STREXW(value, ptr) __strex(value, ptr)
\r
213 /** \brief Remove the exclusive lock
\r
215 This function removes the exclusive lock which is created by LDREX.
\r
218 #define __CLREX __clrex
\r
221 /** \brief Signed Saturate
\r
223 This function saturates a signed value.
\r
225 \param [in] value Value to be saturated
\r
226 \param [in] sat Bit position to saturate to (1..32)
\r
227 \return Saturated value
\r
229 #define __SSAT __ssat
\r
232 /** \brief Unsigned Saturate
\r
234 This function saturates an unsigned value.
\r
236 \param [in] value Value to be saturated
\r
237 \param [in] sat Bit position to saturate to (0..31)
\r
238 \return Saturated value
\r
240 #define __USAT __usat
\r
243 /** \brief Count leading zeros
\r
245 This function counts the number of leading zeros of a data value.
\r
247 \param [in] value Value to count the leading zeros
\r
248 \return number of leading zeros in value
\r
250 #define __CLZ __clz
\r
252 #endif /* (__CORTEX_M >= 0x03) */
\r
256 #elif defined ( __ICCARM__ ) /*------------------ ICC Compiler -------------------*/
\r
257 /* IAR iccarm specific functions */
\r
259 #include <cmsis_iar.h>
\r
262 #elif defined ( __GNUC__ ) /*------------------ GNU Compiler ---------------------*/
\r
263 /* GNU gcc specific functions */
\r
265 /** \brief No Operation
\r
267 No Operation does nothing. This instruction can be used for code alignment purposes.
\r
269 __attribute__( ( always_inline ) ) static __INLINE void __NOP(void)
\r
271 __ASM volatile ("nop");
\r
275 /** \brief Wait For Interrupt
\r
277 Wait For Interrupt is a hint instruction that suspends execution
\r
278 until one of a number of events occurs.
\r
280 __attribute__( ( always_inline ) ) static __INLINE void __WFI(void)
\r
282 __ASM volatile ("wfi");
\r
286 /** \brief Wait For Event
\r
288 Wait For Event is a hint instruction that permits the processor to enter
\r
289 a low-power state until one of a number of events occurs.
\r
291 __attribute__( ( always_inline ) ) static __INLINE void __WFE(void)
\r
293 __ASM volatile ("wfe");
\r
297 /** \brief Send Event
\r
299 Send Event is a hint instruction. It causes an event to be signaled to the CPU.
\r
301 __attribute__( ( always_inline ) ) static __INLINE void __SEV(void)
\r
303 __ASM volatile ("sev");
\r
307 /** \brief Instruction Synchronization Barrier
\r
309 Instruction Synchronization Barrier flushes the pipeline in the processor,
\r
310 so that all instructions following the ISB are fetched from cache or
\r
311 memory, after the instruction has been completed.
\r
313 __attribute__( ( always_inline ) ) static __INLINE void __ISB(void)
\r
315 __ASM volatile ("isb");
\r
319 /** \brief Data Synchronization Barrier
\r
321 This function acts as a special kind of Data Memory Barrier.
\r
322 It completes when all explicit memory accesses before this instruction complete.
\r
324 __attribute__( ( always_inline ) ) static __INLINE void __DSB(void)
\r
326 __ASM volatile ("dsb");
\r
330 /** \brief Data Memory Barrier
\r
332 This function ensures the apparent order of the explicit memory operations before
\r
333 and after the instruction, without ensuring their completion.
\r
335 __attribute__( ( always_inline ) ) static __INLINE void __DMB(void)
\r
337 __ASM volatile ("dmb");
\r
341 /** \brief Reverse byte order (32 bit)
\r
343 This function reverses the byte order in integer value.
\r
345 \param [in] value Value to reverse
\r
346 \return Reversed value
\r
348 __attribute__( ( always_inline ) ) static __INLINE uint32_t __REV(uint32_t value)
\r
352 __ASM volatile ("rev %0, %1" : "=r" (result) : "r" (value) );
\r
357 /** \brief Reverse byte order (16 bit)
\r
359 This function reverses the byte order in two unsigned short values.
\r
361 \param [in] value Value to reverse
\r
362 \return Reversed value
\r
364 __attribute__( ( always_inline ) ) static __INLINE uint32_t __REV16(uint32_t value)
\r
368 __ASM volatile ("rev16 %0, %1" : "=r" (result) : "r" (value) );
\r
373 /** \brief Reverse byte order in signed short value
\r
375 This function reverses the byte order in a signed short value with sign extension to integer.
\r
377 \param [in] value Value to reverse
\r
378 \return Reversed value
\r
380 __attribute__( ( always_inline ) ) static __INLINE int32_t __REVSH(int32_t value)
\r
384 __ASM volatile ("revsh %0, %1" : "=r" (result) : "r" (value) );
\r
389 #if (__CORTEX_M >= 0x03)
\r
391 /** \brief Reverse bit order of value
\r
393 This function reverses the bit order of the given value.
\r
395 \param [in] value Value to reverse
\r
396 \return Reversed value
\r
398 __attribute__( ( always_inline ) ) static __INLINE uint32_t __RBIT(uint32_t value)
\r
402 __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
\r
407 /** \brief LDR Exclusive (8 bit)
\r
409 This function performs a exclusive LDR command for 8 bit value.
\r
411 \param [in] ptr Pointer to data
\r
412 \return value of type uint8_t at (*ptr)
\r
414 __attribute__( ( always_inline ) ) static __INLINE uint8_t __LDREXB(volatile uint8_t *addr)
\r
418 __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) );
\r
423 /** \brief LDR Exclusive (16 bit)
\r
425 This function performs a exclusive LDR command for 16 bit values.
\r
427 \param [in] ptr Pointer to data
\r
428 \return value of type uint16_t at (*ptr)
\r
430 __attribute__( ( always_inline ) ) static __INLINE uint16_t __LDREXH(volatile uint16_t *addr)
\r
434 __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) );
\r
439 /** \brief LDR Exclusive (32 bit)
\r
441 This function performs a exclusive LDR command for 32 bit values.
\r
443 \param [in] ptr Pointer to data
\r
444 \return value of type uint32_t at (*ptr)
\r
446 __attribute__( ( always_inline ) ) static __INLINE uint32_t __LDREXW(volatile uint32_t *addr)
\r
450 __ASM volatile ("ldrex %0, [%1]" : "=r" (result) : "r" (addr) );
\r
455 /** \brief STR Exclusive (8 bit)
\r
457 This function performs a exclusive STR command for 8 bit values.
\r
459 \param [in] value Value to store
\r
460 \param [in] ptr Pointer to location
\r
461 \return 0 Function succeeded
\r
462 \return 1 Function failed
\r
464 __attribute__( ( always_inline ) ) static __INLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
\r
468 __ASM volatile ("strexb %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
\r
473 /** \brief STR Exclusive (16 bit)
\r
475 This function performs a exclusive STR command for 16 bit values.
\r
477 \param [in] value Value to store
\r
478 \param [in] ptr Pointer to location
\r
479 \return 0 Function succeeded
\r
480 \return 1 Function failed
\r
482 __attribute__( ( always_inline ) ) static __INLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
\r
486 __ASM volatile ("strexh %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
\r
491 /** \brief STR Exclusive (32 bit)
\r
493 This function performs a exclusive STR command for 32 bit values.
\r
495 \param [in] value Value to store
\r
496 \param [in] ptr Pointer to location
\r
497 \return 0 Function succeeded
\r
498 \return 1 Function failed
\r
500 __attribute__( ( always_inline ) ) static __INLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
\r
504 __ASM volatile ("strex %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
\r
509 /** \brief Remove the exclusive lock
\r
511 This function removes the exclusive lock which is created by LDREX.
\r
514 __attribute__( ( always_inline ) ) static __INLINE void __CLREX(void)
\r
516 __ASM volatile ("clrex");
\r
520 /** \brief Signed Saturate
\r
522 This function saturates a signed value.
\r
524 \param [in] value Value to be saturated
\r
525 \param [in] sat Bit position to saturate to (1..32)
\r
526 \return Saturated value
\r
528 #define __SSAT(ARG1,ARG2) \
\r
530 uint32_t __RES, __ARG1 = (ARG1); \
\r
531 __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
\r
536 /** \brief Unsigned Saturate
\r
538 This function saturates an unsigned value.
\r
540 \param [in] value Value to be saturated
\r
541 \param [in] sat Bit position to saturate to (0..31)
\r
542 \return Saturated value
\r
544 #define __USAT(ARG1,ARG2) \
\r
546 uint32_t __RES, __ARG1 = (ARG1); \
\r
547 __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
\r
552 /** \brief Count leading zeros
\r
554 This function counts the number of leading zeros of a data value.
\r
556 \param [in] value Value to count the leading zeros
\r
557 \return number of leading zeros in value
\r
559 __attribute__( ( always_inline ) ) static __INLINE uint8_t __CLZ(uint32_t value)
\r
563 __ASM volatile ("clz %0, %1" : "=r" (result) : "r" (value) );
\r
567 #endif /* (__CORTEX_M >= 0x03) */
\r
572 #elif defined ( __TASKING__ ) /*------------------ TASKING Compiler --------------*/
\r
573 /* TASKING carm specific functions */
\r
576 * The CMSIS functions have been implemented as intrinsics in the compiler.
\r
577 * Please use "carm -?i" to get an up to date list of all intrinsics,
\r
578 * Including the CMSIS ones.
\r
583 /*@}*/ /* end of group CMSIS_Core_InstructionInterface */
\r
585 #endif /* __CORE_CMINSTR_H */
\r