core_cmInstr.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /**************************************************************************//**
  2. * @file core_cmInstr.h
  3. * @brief CMSIS Cortex-M Core Instruction Access Header File
  4. * @version V3.20
  5. * @date 05. March 2013
  6. *
  7. * @note
  8. *
  9. ******************************************************************************/
  10. /* Copyright (c) 2009 - 2013 ARM LIMITED
  11. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions are met:
  14. - Redistributions of source code must retain the above copyright
  15. notice, this list of conditions and the following disclaimer.
  16. - Redistributions in binary form must reproduce the above copyright
  17. notice, this list of conditions and the following disclaimer in the
  18. documentation and/or other materials provided with the distribution.
  19. - Neither the name of ARM nor the names of its contributors may be used
  20. to endorse or promote products derived from this software without
  21. specific prior written permission.
  22. *
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. POSSIBILITY OF SUCH DAMAGE.
  34. ---------------------------------------------------------------------------*/
  35. #ifndef __CORE_CMINSTR_H
  36. #define __CORE_CMINSTR_H
  37. /* ########################## Core Instruction Access ######################### */
  38. /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface
  39. Access to dedicated instructions
  40. @{
  41. */
  42. #if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/
  43. /* ARM armcc specific functions */
  44. #if (__ARMCC_VERSION < 400677)
  45. #error "Please use ARM Compiler Toolchain V4.0.677 or later!"
  46. #endif
  47. /** \brief No Operation
  48. No Operation does nothing. This instruction can be used for code alignment purposes.
  49. */
  50. #define __NOP __nop
  51. /** \brief Wait For Interrupt
  52. Wait For Interrupt is a hint instruction that suspends execution
  53. until one of a number of events occurs.
  54. */
  55. #define __WFI __wfi
  56. /** \brief Wait For Event
  57. Wait For Event is a hint instruction that permits the processor to enter
  58. a low-power state until one of a number of events occurs.
  59. */
  60. #define __WFE __wfe
  61. /** \brief Send Event
  62. Send Event is a hint instruction. It causes an event to be signaled to the CPU.
  63. */
  64. #define __SEV __sev
  65. /** \brief Instruction Synchronization Barrier
  66. Instruction Synchronization Barrier flushes the pipeline in the processor,
  67. so that all instructions following the ISB are fetched from cache or
  68. memory, after the instruction has been completed.
  69. */
  70. #define __ISB() __isb(0xF)
  71. /** \brief Data Synchronization Barrier
  72. This function acts as a special kind of Data Memory Barrier.
  73. It completes when all explicit memory accesses before this instruction complete.
  74. */
  75. #define __DSB() __dsb(0xF)
  76. /** \brief Data Memory Barrier
  77. This function ensures the apparent order of the explicit memory operations before
  78. and after the instruction, without ensuring their completion.
  79. */
  80. #define __DMB() __dmb(0xF)
  81. /** \brief Reverse byte order (32 bit)
  82. This function reverses the byte order in integer value.
  83. \param [in] value Value to reverse
  84. \return Reversed value
  85. */
  86. #define __REV __rev
  87. /** \brief Reverse byte order (16 bit)
  88. This function reverses the byte order in two unsigned short values.
  89. \param [in] value Value to reverse
  90. \return Reversed value
  91. */
  92. #ifndef __NO_EMBEDDED_ASM
  93. __attribute__((section(".rev16_text"))) __STATIC_INLINE __ASM uint32_t __REV16(uint32_t value)
  94. {
  95. rev16 r0, r0
  96. bx lr
  97. }
  98. #endif
  99. /** \brief Reverse byte order in signed short value
  100. This function reverses the byte order in a signed short value with sign extension to integer.
  101. \param [in] value Value to reverse
  102. \return Reversed value
  103. */
  104. #ifndef __NO_EMBEDDED_ASM
  105. __attribute__((section(".revsh_text"))) __STATIC_INLINE __ASM int32_t __REVSH(int32_t value)
  106. {
  107. revsh r0, r0
  108. bx lr
  109. }
  110. #endif
  111. /** \brief Rotate Right in unsigned value (32 bit)
  112. This function Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  113. \param [in] value Value to rotate
  114. \param [in] value Number of Bits to rotate
  115. \return Rotated value
  116. */
  117. #define __ROR __ror
  118. /** \brief Breakpoint
  119. This function causes the processor to enter Debug state.
  120. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  121. \param [in] value is ignored by the processor.
  122. If required, a debugger can use it to store additional information about the breakpoint.
  123. */
  124. #define __BKPT(value) __breakpoint(value)
  125. #if (__CORTEX_M >= 0x03)
  126. /** \brief Reverse bit order of value
  127. This function reverses the bit order of the given value.
  128. \param [in] value Value to reverse
  129. \return Reversed value
  130. */
  131. #define __RBIT __rbit
  132. /** \brief LDR Exclusive (8 bit)
  133. This function performs a exclusive LDR command for 8 bit value.
  134. \param [in] ptr Pointer to data
  135. \return value of type uint8_t at (*ptr)
  136. */
  137. #define __LDREXB(ptr) ((uint8_t ) __ldrex(ptr))
  138. /** \brief LDR Exclusive (16 bit)
  139. This function performs a exclusive LDR command for 16 bit values.
  140. \param [in] ptr Pointer to data
  141. \return value of type uint16_t at (*ptr)
  142. */
  143. #define __LDREXH(ptr) ((uint16_t) __ldrex(ptr))
  144. /** \brief LDR Exclusive (32 bit)
  145. This function performs a exclusive LDR command for 32 bit values.
  146. \param [in] ptr Pointer to data
  147. \return value of type uint32_t at (*ptr)
  148. */
  149. #define __LDREXW(ptr) ((uint32_t ) __ldrex(ptr))
  150. /** \brief STR Exclusive (8 bit)
  151. This function performs a exclusive STR command for 8 bit values.
  152. \param [in] value Value to store
  153. \param [in] ptr Pointer to location
  154. \return 0 Function succeeded
  155. \return 1 Function failed
  156. */
  157. #define __STREXB(value, ptr) __strex(value, ptr)
  158. /** \brief STR Exclusive (16 bit)
  159. This function performs a exclusive STR command for 16 bit values.
  160. \param [in] value Value to store
  161. \param [in] ptr Pointer to location
  162. \return 0 Function succeeded
  163. \return 1 Function failed
  164. */
  165. #define __STREXH(value, ptr) __strex(value, ptr)
  166. /** \brief STR Exclusive (32 bit)
  167. This function performs a exclusive STR command for 32 bit values.
  168. \param [in] value Value to store
  169. \param [in] ptr Pointer to location
  170. \return 0 Function succeeded
  171. \return 1 Function failed
  172. */
  173. #define __STREXW(value, ptr) __strex(value, ptr)
  174. /** \brief Remove the exclusive lock
  175. This function removes the exclusive lock which is created by LDREX.
  176. */
  177. #define __CLREX __clrex
  178. /** \brief Signed Saturate
  179. This function saturates a signed value.
  180. \param [in] value Value to be saturated
  181. \param [in] sat Bit position to saturate to (1..32)
  182. \return Saturated value
  183. */
  184. #define __SSAT __ssat
  185. /** \brief Unsigned Saturate
  186. This function saturates an unsigned value.
  187. \param [in] value Value to be saturated
  188. \param [in] sat Bit position to saturate to (0..31)
  189. \return Saturated value
  190. */
  191. #define __USAT __usat
  192. /** \brief Count leading zeros
  193. This function counts the number of leading zeros of a data value.
  194. \param [in] value Value to count the leading zeros
  195. \return number of leading zeros in value
  196. */
  197. #define __CLZ __clz
  198. #endif /* (__CORTEX_M >= 0x03) */
  199. #elif defined ( __ICCARM__ ) /*------------------ ICC Compiler -------------------*/
  200. /* IAR iccarm specific functions */
  201. #include <cmsis_iar.h>
  202. #elif defined ( __TMS470__ ) /*---------------- TI CCS Compiler ------------------*/
  203. /* TI CCS specific functions */
  204. #include <cmsis_ccs.h>
  205. #elif defined ( __GNUC__ ) /*------------------ GNU Compiler ---------------------*/
  206. /* GNU gcc specific functions */
  207. /* Define macros for porting to both thumb1 and thumb2.
  208. * For thumb1, use low register (r0-r7), specified by constrant "l"
  209. * Otherwise, use general registers, specified by constrant "r" */
  210. #if defined (__thumb__) && !defined (__thumb2__)
  211. #define __CMSIS_GCC_OUT_REG(r) "=l" (r)
  212. #define __CMSIS_GCC_USE_REG(r) "l" (r)
  213. #else
  214. #define __CMSIS_GCC_OUT_REG(r) "=r" (r)
  215. #define __CMSIS_GCC_USE_REG(r) "r" (r)
  216. #endif
  217. /** \brief No Operation
  218. No Operation does nothing. This instruction can be used for code alignment purposes.
  219. */
  220. __attribute__( ( always_inline ) ) __STATIC_INLINE void __NOP(void)
  221. {
  222. __ASM volatile ("nop");
  223. }
  224. /** \brief Wait For Interrupt
  225. Wait For Interrupt is a hint instruction that suspends execution
  226. until one of a number of events occurs.
  227. */
  228. __attribute__( ( always_inline ) ) __STATIC_INLINE void __WFI(void)
  229. {
  230. __ASM volatile ("wfi");
  231. }
  232. /** \brief Wait For Event
  233. Wait For Event is a hint instruction that permits the processor to enter
  234. a low-power state until one of a number of events occurs.
  235. */
  236. __attribute__( ( always_inline ) ) __STATIC_INLINE void __WFE(void)
  237. {
  238. __ASM volatile ("wfe");
  239. }
  240. /** \brief Send Event
  241. Send Event is a hint instruction. It causes an event to be signaled to the CPU.
  242. */
  243. __attribute__( ( always_inline ) ) __STATIC_INLINE void __SEV(void)
  244. {
  245. __ASM volatile ("sev");
  246. }
  247. /** \brief Instruction Synchronization Barrier
  248. Instruction Synchronization Barrier flushes the pipeline in the processor,
  249. so that all instructions following the ISB are fetched from cache or
  250. memory, after the instruction has been completed.
  251. */
  252. __attribute__( ( always_inline ) ) __STATIC_INLINE void __ISB(void)
  253. {
  254. __ASM volatile ("isb");
  255. }
  256. /** \brief Data Synchronization Barrier
  257. This function acts as a special kind of Data Memory Barrier.
  258. It completes when all explicit memory accesses before this instruction complete.
  259. */
  260. __attribute__( ( always_inline ) ) __STATIC_INLINE void __DSB(void)
  261. {
  262. __ASM volatile ("dsb");
  263. }
  264. /** \brief Data Memory Barrier
  265. This function ensures the apparent order of the explicit memory operations before
  266. and after the instruction, without ensuring their completion.
  267. */
  268. __attribute__( ( always_inline ) ) __STATIC_INLINE void __DMB(void)
  269. {
  270. __ASM volatile ("dmb");
  271. }
  272. /** \brief Reverse byte order (32 bit)
  273. This function reverses the byte order in integer value.
  274. \param [in] value Value to reverse
  275. \return Reversed value
  276. */
  277. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __REV(uint32_t value)
  278. {
  279. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
  280. return __builtin_bswap32(value);
  281. #else
  282. uint32_t result;
  283. __ASM volatile ("rev %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  284. return(result);
  285. #endif
  286. }
  287. /** \brief Reverse byte order (16 bit)
  288. This function reverses the byte order in two unsigned short values.
  289. \param [in] value Value to reverse
  290. \return Reversed value
  291. */
  292. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __REV16(uint32_t value)
  293. {
  294. uint32_t result;
  295. __ASM volatile ("rev16 %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  296. return(result);
  297. }
  298. /** \brief Reverse byte order in signed short value
  299. This function reverses the byte order in a signed short value with sign extension to integer.
  300. \param [in] value Value to reverse
  301. \return Reversed value
  302. */
  303. __attribute__( ( always_inline ) ) __STATIC_INLINE int32_t __REVSH(int32_t value)
  304. {
  305. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  306. return (short)__builtin_bswap16(value);
  307. #else
  308. uint32_t result;
  309. __ASM volatile ("revsh %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  310. return(result);
  311. #endif
  312. }
  313. /** \brief Rotate Right in unsigned value (32 bit)
  314. This function Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  315. \param [in] value Value to rotate
  316. \param [in] value Number of Bits to rotate
  317. \return Rotated value
  318. */
  319. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
  320. {
  321. return (op1 >> op2) | (op1 << (32 - op2));
  322. }
  323. /** \brief Breakpoint
  324. This function causes the processor to enter Debug state.
  325. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  326. \param [in] value is ignored by the processor.
  327. If required, a debugger can use it to store additional information about the breakpoint.
  328. */
  329. #define __BKPT(value) __ASM volatile ("bkpt "#value)
  330. #if (__CORTEX_M >= 0x03)
  331. /** \brief Reverse bit order of value
  332. This function reverses the bit order of the given value.
  333. \param [in] value Value to reverse
  334. \return Reversed value
  335. */
  336. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __RBIT(uint32_t value)
  337. {
  338. uint32_t result;
  339. __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
  340. return(result);
  341. }
  342. /** \brief LDR Exclusive (8 bit)
  343. This function performs a exclusive LDR command for 8 bit value.
  344. \param [in] ptr Pointer to data
  345. \return value of type uint8_t at (*ptr)
  346. */
  347. __attribute__( ( always_inline ) ) __STATIC_INLINE uint8_t __LDREXB(volatile uint8_t *addr)
  348. {
  349. uint32_t result;
  350. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  351. __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
  352. #else
  353. /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
  354. accepted by assembler. So has to use following less efficient pattern.
  355. */
  356. __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
  357. #endif
  358. return(result);
  359. }
  360. /** \brief LDR Exclusive (16 bit)
  361. This function performs a exclusive LDR command for 16 bit values.
  362. \param [in] ptr Pointer to data
  363. \return value of type uint16_t at (*ptr)
  364. */
  365. __attribute__( ( always_inline ) ) __STATIC_INLINE uint16_t __LDREXH(volatile uint16_t *addr)
  366. {
  367. uint32_t result;
  368. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  369. __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
  370. #else
  371. /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
  372. accepted by assembler. So has to use following less efficient pattern.
  373. */
  374. __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
  375. #endif
  376. return(result);
  377. }
  378. /** \brief LDR Exclusive (32 bit)
  379. This function performs a exclusive LDR command for 32 bit values.
  380. \param [in] ptr Pointer to data
  381. \return value of type uint32_t at (*ptr)
  382. */
  383. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __LDREXW(volatile uint32_t *addr)
  384. {
  385. uint32_t result;
  386. __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
  387. return(result);
  388. }
  389. /** \brief STR Exclusive (8 bit)
  390. This function performs a exclusive STR command for 8 bit values.
  391. \param [in] value Value to store
  392. \param [in] ptr Pointer to location
  393. \return 0 Function succeeded
  394. \return 1 Function failed
  395. */
  396. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
  397. {
  398. uint32_t result;
  399. __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
  400. return(result);
  401. }
  402. /** \brief STR Exclusive (16 bit)
  403. This function performs a exclusive STR command for 16 bit values.
  404. \param [in] value Value to store
  405. \param [in] ptr Pointer to location
  406. \return 0 Function succeeded
  407. \return 1 Function failed
  408. */
  409. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
  410. {
  411. uint32_t result;
  412. __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
  413. return(result);
  414. }
  415. /** \brief STR Exclusive (32 bit)
  416. This function performs a exclusive STR command for 32 bit values.
  417. \param [in] value Value to store
  418. \param [in] ptr Pointer to location
  419. \return 0 Function succeeded
  420. \return 1 Function failed
  421. */
  422. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
  423. {
  424. uint32_t result;
  425. __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
  426. return(result);
  427. }
  428. /** \brief Remove the exclusive lock
  429. This function removes the exclusive lock which is created by LDREX.
  430. */
  431. __attribute__( ( always_inline ) ) __STATIC_INLINE void __CLREX(void)
  432. {
  433. __ASM volatile ("clrex" ::: "memory");
  434. }
  435. /** \brief Signed Saturate
  436. This function saturates a signed value.
  437. \param [in] value Value to be saturated
  438. \param [in] sat Bit position to saturate to (1..32)
  439. \return Saturated value
  440. */
  441. #define __SSAT(ARG1,ARG2) \
  442. ({ \
  443. uint32_t __RES, __ARG1 = (ARG1); \
  444. __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
  445. __RES; \
  446. })
  447. /** \brief Unsigned Saturate
  448. This function saturates an unsigned value.
  449. \param [in] value Value to be saturated
  450. \param [in] sat Bit position to saturate to (0..31)
  451. \return Saturated value
  452. */
  453. #define __USAT(ARG1,ARG2) \
  454. ({ \
  455. uint32_t __RES, __ARG1 = (ARG1); \
  456. __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
  457. __RES; \
  458. })
  459. /** \brief Count leading zeros
  460. This function counts the number of leading zeros of a data value.
  461. \param [in] value Value to count the leading zeros
  462. \return number of leading zeros in value
  463. */
  464. __attribute__( ( always_inline ) ) __STATIC_INLINE uint8_t __CLZ(uint32_t value)
  465. {
  466. uint32_t result;
  467. __ASM volatile ("clz %0, %1" : "=r" (result) : "r" (value) );
  468. return(result);
  469. }
  470. #endif /* (__CORTEX_M >= 0x03) */
  471. #elif defined ( __TASKING__ ) /*------------------ TASKING Compiler --------------*/
  472. /* TASKING carm specific functions */
  473. /*
  474. * The CMSIS functions have been implemented as intrinsics in the compiler.
  475. * Please use "carm -?i" to get an up to date list of all intrinsics,
  476. * Including the CMSIS ones.
  477. */
  478. #endif
  479. /*@}*/ /* end of group CMSIS_Core_InstructionInterface */
  480. #endif /* __CORE_CMINSTR_H */