1 /**************************************************************************//**
3 * @brief CMSIS Cortex-M Core Instruction Access Header File
5 * @date 29. August 2012
8 * Copyright (C) 2009-2012 ARM Limited. All rights reserved.
11 * ARM Limited (ARM) is supplying this software for use with Cortex-M
12 * processor based microcontrollers. This file can be freely distributed
13 * within development tools that are supporting such ARM based processors.
16 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
17 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
19 * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
20 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
22 ******************************************************************************/
24 #ifndef __CORE_CMINSTR_H
25 #define __CORE_CMINSTR_H
28 /* ########################## Core Instruction Access ######################### */
29 /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface
30 Access to dedicated instructions
34 #if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/
35 /* ARM armcc specific functions */
37 #if (__ARMCC_VERSION < 400677)
38 #error "Please use ARM Compiler Toolchain V4.0.677 or later!"
42 /** \brief No Operation
44 No Operation does nothing. This instruction can be used for code alignment purposes.
49 /** \brief Wait For Interrupt
51 Wait For Interrupt is a hint instruction that suspends execution
52 until one of a number of events occurs.
57 /** \brief Wait For Event
59 Wait For Event is a hint instruction that permits the processor to enter
60 a low-power state until one of a number of events occurs.
67 Send Event is a hint instruction. It causes an event to be signaled to the CPU.
72 /** \brief Instruction Synchronization Barrier
74 Instruction Synchronization Barrier flushes the pipeline in the processor,
75 so that all instructions following the ISB are fetched from cache or
76 memory, after the instruction has been completed.
78 #define __ISB() __isb(0xF)
81 /** \brief Data Synchronization Barrier
83 This function acts as a special kind of Data Memory Barrier.
84 It completes when all explicit memory accesses before this instruction complete.
86 #define __DSB() __dsb(0xF)
89 /** \brief Data Memory Barrier
91 This function ensures the apparent order of the explicit memory operations before
92 and after the instruction, without ensuring their completion.
94 #define __DMB() __dmb(0xF)
97 /** \brief Reverse byte order (32 bit)
99 This function reverses the byte order in integer value.
101 \param [in] value Value to reverse
102 \return Reversed value
107 /** \brief Reverse byte order (16 bit)
109 This function reverses the byte order in two unsigned short values.
111 \param [in] value Value to reverse
112 \return Reversed value
114 #ifndef __NO_EMBEDDED_ASM
115 __attribute__((section(".rev16_text"))) __STATIC_INLINE __ASM
uint32_t __REV16(uint32_t value
)
122 /** \brief Reverse byte order in signed short value
124 This function reverses the byte order in a signed short value with sign extension to integer.
126 \param [in] value Value to reverse
127 \return Reversed value
129 #ifndef __NO_EMBEDDED_ASM
130 __attribute__((section(".revsh_text"))) __STATIC_INLINE __ASM
int32_t __REVSH(int32_t value
)
138 /** \brief Rotate Right in unsigned value (32 bit)
140 This function Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
142 \param [in] value Value to rotate
143 \param [in] value Number of Bits to rotate
144 \return Rotated value
149 /** \brief Breakpoint
151 This function causes the processor to enter Debug state.
152 Debug tools can use this to investigate system state when the instruction at a particular address is reached.
154 \param [in] value is ignored by the processor.
155 If required, a debugger can use it to store additional information about the breakpoint.
157 #define __BKPT(value) __breakpoint(value)
160 #if (__CORTEX_M >= 0x03)
162 /** \brief Reverse bit order of value
164 This function reverses the bit order of the given value.
166 \param [in] value Value to reverse
167 \return Reversed value
169 #define __RBIT __rbit
172 /** \brief LDR Exclusive (8 bit)
174 This function performs a exclusive LDR command for 8 bit value.
176 \param [in] ptr Pointer to data
177 \return value of type uint8_t at (*ptr)
179 #define __LDREXB(ptr) ((uint8_t ) __ldrex(ptr))
182 /** \brief LDR Exclusive (16 bit)
184 This function performs a exclusive LDR command for 16 bit values.
186 \param [in] ptr Pointer to data
187 \return value of type uint16_t at (*ptr)
189 #define __LDREXH(ptr) ((uint16_t) __ldrex(ptr))
192 /** \brief LDR Exclusive (32 bit)
194 This function performs a exclusive LDR command for 32 bit values.
196 \param [in] ptr Pointer to data
197 \return value of type uint32_t at (*ptr)
199 #define __LDREXW(ptr) ((uint32_t ) __ldrex(ptr))
202 /** \brief STR Exclusive (8 bit)
204 This function performs a exclusive STR command for 8 bit values.
206 \param [in] value Value to store
207 \param [in] ptr Pointer to location
208 \return 0 Function succeeded
209 \return 1 Function failed
211 #define __STREXB(value, ptr) __strex(value, ptr)
214 /** \brief STR Exclusive (16 bit)
216 This function performs a exclusive STR command for 16 bit values.
218 \param [in] value Value to store
219 \param [in] ptr Pointer to location
220 \return 0 Function succeeded
221 \return 1 Function failed
223 #define __STREXH(value, ptr) __strex(value, ptr)
226 /** \brief STR Exclusive (32 bit)
228 This function performs a exclusive STR command for 32 bit values.
230 \param [in] value Value to store
231 \param [in] ptr Pointer to location
232 \return 0 Function succeeded
233 \return 1 Function failed
235 #define __STREXW(value, ptr) __strex(value, ptr)
238 /** \brief Remove the exclusive lock
240 This function removes the exclusive lock which is created by LDREX.
243 #define __CLREX __clrex
246 /** \brief Signed Saturate
248 This function saturates a signed value.
250 \param [in] value Value to be saturated
251 \param [in] sat Bit position to saturate to (1..32)
252 \return Saturated value
254 #define __SSAT __ssat
257 /** \brief Unsigned Saturate
259 This function saturates an unsigned value.
261 \param [in] value Value to be saturated
262 \param [in] sat Bit position to saturate to (0..31)
263 \return Saturated value
265 #define __USAT __usat
268 /** \brief Count leading zeros
270 This function counts the number of leading zeros of a data value.
272 \param [in] value Value to count the leading zeros
273 \return number of leading zeros in value
277 #endif /* (__CORTEX_M >= 0x03) */
281 #elif defined ( __ICCARM__ ) /*------------------ ICC Compiler -------------------*/
282 /* IAR iccarm specific functions */
284 #include <cmsis_iar.h>
287 #elif defined ( __TMS470__ ) /*---------------- TI CCS Compiler ------------------*/
288 /* TI CCS specific functions */
290 #include <cmsis_ccs.h>
293 #elif defined ( __GNUC__ ) /*------------------ GNU Compiler ---------------------*/
294 /* GNU gcc specific functions */
296 /** \brief No Operation
298 No Operation does nothing. This instruction can be used for code alignment purposes.
300 __attribute__( ( always_inline
) ) __STATIC_INLINE
void __NOP(void)
302 __ASM
volatile ("nop");
306 /** \brief Wait For Interrupt
308 Wait For Interrupt is a hint instruction that suspends execution
309 until one of a number of events occurs.
311 __attribute__( ( always_inline
) ) __STATIC_INLINE
void __WFI(void)
313 __ASM
volatile ("wfi");
317 /** \brief Wait For Event
319 Wait For Event is a hint instruction that permits the processor to enter
320 a low-power state until one of a number of events occurs.
322 __attribute__( ( always_inline
) ) __STATIC_INLINE
void __WFE(void)
324 __ASM
volatile ("wfe");
328 /** \brief Send Event
330 Send Event is a hint instruction. It causes an event to be signaled to the CPU.
332 __attribute__( ( always_inline
) ) __STATIC_INLINE
void __SEV(void)
334 __ASM
volatile ("sev");
338 /** \brief Instruction Synchronization Barrier
340 Instruction Synchronization Barrier flushes the pipeline in the processor,
341 so that all instructions following the ISB are fetched from cache or
342 memory, after the instruction has been completed.
344 __attribute__( ( always_inline
) ) __STATIC_INLINE
void __ISB(void)
346 __ASM
volatile ("isb");
350 /** \brief Data Synchronization Barrier
352 This function acts as a special kind of Data Memory Barrier.
353 It completes when all explicit memory accesses before this instruction complete.
355 __attribute__( ( always_inline
) ) __STATIC_INLINE
void __DSB(void)
357 __ASM
volatile ("dsb");
361 /** \brief Data Memory Barrier
363 This function ensures the apparent order of the explicit memory operations before
364 and after the instruction, without ensuring their completion.
366 __attribute__( ( always_inline
) ) __STATIC_INLINE
void __DMB(void)
368 __ASM
volatile ("dmb");
372 /** \brief Reverse byte order (32 bit)
374 This function reverses the byte order in integer value.
376 \param [in] value Value to reverse
377 \return Reversed value
379 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint32_t __REV(uint32_t value
)
383 __ASM
volatile ("rev %0, %1" : "=r" (result
) : "r" (value
) );
388 /** \brief Reverse byte order (16 bit)
390 This function reverses the byte order in two unsigned short values.
392 \param [in] value Value to reverse
393 \return Reversed value
395 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint32_t __REV16(uint32_t value
)
399 __ASM
volatile ("rev16 %0, %1" : "=r" (result
) : "r" (value
) );
404 /** \brief Reverse byte order in signed short value
406 This function reverses the byte order in a signed short value with sign extension to integer.
408 \param [in] value Value to reverse
409 \return Reversed value
411 __attribute__( ( always_inline
) ) __STATIC_INLINE
int32_t __REVSH(int32_t value
)
415 __ASM
volatile ("revsh %0, %1" : "=r" (result
) : "r" (value
) );
420 /** \brief Rotate Right in unsigned value (32 bit)
422 This function Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
424 \param [in] value Value to rotate
425 \param [in] value Number of Bits to rotate
426 \return Rotated value
428 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint32_t __ROR(uint32_t op1
, uint32_t op2
)
431 __ASM
volatile ("ror %0, %0, %1" : "+r" (op1
) : "r" (op2
) );
436 /** \brief Breakpoint
438 This function causes the processor to enter Debug state.
439 Debug tools can use this to investigate system state when the instruction at a particular address is reached.
441 \param [in] value is ignored by the processor.
442 If required, a debugger can use it to store additional information about the breakpoint.
444 #define __BKPT(value) __ASM volatile ("bkpt "#value)
447 #if (__CORTEX_M >= 0x03)
449 /** \brief Reverse bit order of value
451 This function reverses the bit order of the given value.
453 \param [in] value Value to reverse
454 \return Reversed value
456 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint32_t __RBIT(uint32_t value
)
460 __ASM
volatile ("rbit %0, %1" : "=r" (result
) : "r" (value
) );
465 /** \brief LDR Exclusive (8 bit)
467 This function performs a exclusive LDR command for 8 bit value.
469 \param [in] ptr Pointer to data
470 \return value of type uint8_t at (*ptr)
472 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint8_t __LDREXB(volatile uint8_t *addr
)
476 __ASM
volatile ("ldrexb %0, [%1]" : "=r" (result
) : "r" (addr
) );
481 /** \brief LDR Exclusive (16 bit)
483 This function performs a exclusive LDR command for 16 bit values.
485 \param [in] ptr Pointer to data
486 \return value of type uint16_t at (*ptr)
488 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint16_t __LDREXH(volatile uint16_t *addr
)
492 __ASM
volatile ("ldrexh %0, [%1]" : "=r" (result
) : "r" (addr
) );
497 /** \brief LDR Exclusive (32 bit)
499 This function performs a exclusive LDR command for 32 bit values.
501 \param [in] ptr Pointer to data
502 \return value of type uint32_t at (*ptr)
504 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint32_t __LDREXW(volatile uint32_t *addr
)
508 __ASM
volatile ("ldrex %0, [%1]" : "=r" (result
) : "r" (addr
) );
513 /** \brief STR Exclusive (8 bit)
515 This function performs a exclusive STR command for 8 bit values.
517 \param [in] value Value to store
518 \param [in] ptr Pointer to location
519 \return 0 Function succeeded
520 \return 1 Function failed
522 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint32_t __STREXB(uint8_t value
, volatile uint8_t *addr
)
526 __ASM
volatile ("strexb %0, %2, [%1]" : "=&r" (result
) : "r" (addr
), "r" (value
) );
531 /** \brief STR Exclusive (16 bit)
533 This function performs a exclusive STR command for 16 bit values.
535 \param [in] value Value to store
536 \param [in] ptr Pointer to location
537 \return 0 Function succeeded
538 \return 1 Function failed
540 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint32_t __STREXH(uint16_t value
, volatile uint16_t *addr
)
544 __ASM
volatile ("strexh %0, %2, [%1]" : "=&r" (result
) : "r" (addr
), "r" (value
) );
549 /** \brief STR Exclusive (32 bit)
551 This function performs a exclusive STR command for 32 bit values.
553 \param [in] value Value to store
554 \param [in] ptr Pointer to location
555 \return 0 Function succeeded
556 \return 1 Function failed
558 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint32_t __STREXW(uint32_t value
, volatile uint32_t *addr
)
562 __ASM
volatile ("strex %0, %2, [%1]" : "=&r" (result
) : "r" (addr
), "r" (value
) );
567 /** \brief Remove the exclusive lock
569 This function removes the exclusive lock which is created by LDREX.
572 __attribute__( ( always_inline
) ) __STATIC_INLINE
void __CLREX(void)
574 __ASM
volatile ("clrex");
578 /** \brief Signed Saturate
580 This function saturates a signed value.
582 \param [in] value Value to be saturated
583 \param [in] sat Bit position to saturate to (1..32)
584 \return Saturated value
586 #define __SSAT(ARG1,ARG2) \
588 uint32_t __RES, __ARG1 = (ARG1); \
589 __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
594 /** \brief Unsigned Saturate
596 This function saturates an unsigned value.
598 \param [in] value Value to be saturated
599 \param [in] sat Bit position to saturate to (0..31)
600 \return Saturated value
602 #define __USAT(ARG1,ARG2) \
604 uint32_t __RES, __ARG1 = (ARG1); \
605 __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
610 /** \brief Count leading zeros
612 This function counts the number of leading zeros of a data value.
614 \param [in] value Value to count the leading zeros
615 \return number of leading zeros in value
617 __attribute__( ( always_inline
) ) __STATIC_INLINE
uint8_t __CLZ(uint32_t value
)
621 __ASM
volatile ("clz %0, %1" : "=r" (result
) : "r" (value
) );
625 #endif /* (__CORTEX_M >= 0x03) */
630 #elif defined ( __TASKING__ ) /*------------------ TASKING Compiler --------------*/
631 /* TASKING carm specific functions */
634 * The CMSIS functions have been implemented as intrinsics in the compiler.
635 * Please use "carm -?i" to get an up to date list of all intrinsics,
636 * Including the CMSIS ones.
641 /*@}*/ /* end of group CMSIS_Core_InstructionInterface */
643 #endif /* __CORE_CMINSTR_H */