core_cmInstr.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  1. /**************************************************************************//**
  2. * @file core_cmInstr.h
  3. * @brief CMSIS Cortex-M Core Instruction Access Header File
  4. * @version V3.30
  5. * @date 17. February 2014
  6. *
  7. * @note
  8. *
  9. ******************************************************************************/
  10. /* Copyright (c) 2009 - 2014 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 ( __GNUC__ ) /*------------------ GNU Compiler ---------------------*/
  200. /* GNU gcc specific functions */
  201. /* Define macros for porting to both thumb1 and thumb2.
  202. * For thumb1, use low register (r0-r7), specified by constrant "l"
  203. * Otherwise, use general registers, specified by constrant "r" */
  204. #if defined (__thumb__) && !defined (__thumb2__)
  205. #define __CMSIS_GCC_OUT_REG(r) "=l" (r)
  206. #define __CMSIS_GCC_USE_REG(r) "l" (r)
  207. #else
  208. #define __CMSIS_GCC_OUT_REG(r) "=r" (r)
  209. #define __CMSIS_GCC_USE_REG(r) "r" (r)
  210. #endif
  211. /** \brief No Operation
  212. No Operation does nothing. This instruction can be used for code alignment purposes.
  213. */
  214. __attribute__( ( always_inline ) ) __STATIC_INLINE void __NOP(void)
  215. {
  216. __ASM volatile ("nop");
  217. }
  218. /** \brief Wait For Interrupt
  219. Wait For Interrupt is a hint instruction that suspends execution
  220. until one of a number of events occurs.
  221. */
  222. __attribute__( ( always_inline ) ) __STATIC_INLINE void __WFI(void)
  223. {
  224. __ASM volatile ("wfi");
  225. }
  226. /** \brief Wait For Event
  227. Wait For Event is a hint instruction that permits the processor to enter
  228. a low-power state until one of a number of events occurs.
  229. */
  230. __attribute__( ( always_inline ) ) __STATIC_INLINE void __WFE(void)
  231. {
  232. __ASM volatile ("wfe");
  233. }
  234. /** \brief Send Event
  235. Send Event is a hint instruction. It causes an event to be signaled to the CPU.
  236. */
  237. __attribute__( ( always_inline ) ) __STATIC_INLINE void __SEV(void)
  238. {
  239. __ASM volatile ("sev");
  240. }
  241. /** \brief Instruction Synchronization Barrier
  242. Instruction Synchronization Barrier flushes the pipeline in the processor,
  243. so that all instructions following the ISB are fetched from cache or
  244. memory, after the instruction has been completed.
  245. */
  246. __attribute__( ( always_inline ) ) __STATIC_INLINE void __ISB(void)
  247. {
  248. __ASM volatile ("isb");
  249. }
  250. /** \brief Data Synchronization Barrier
  251. This function acts as a special kind of Data Memory Barrier.
  252. It completes when all explicit memory accesses before this instruction complete.
  253. */
  254. __attribute__( ( always_inline ) ) __STATIC_INLINE void __DSB(void)
  255. {
  256. __ASM volatile ("dsb");
  257. }
  258. /** \brief Data Memory Barrier
  259. This function ensures the apparent order of the explicit memory operations before
  260. and after the instruction, without ensuring their completion.
  261. */
  262. __attribute__( ( always_inline ) ) __STATIC_INLINE void __DMB(void)
  263. {
  264. __ASM volatile ("dmb");
  265. }
  266. /** \brief Reverse byte order (32 bit)
  267. This function reverses the byte order in integer value.
  268. \param [in] value Value to reverse
  269. \return Reversed value
  270. */
  271. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __REV(uint32_t value)
  272. {
  273. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
  274. return __builtin_bswap32(value);
  275. #else
  276. uint32_t result;
  277. __ASM volatile ("rev %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  278. return(result);
  279. #endif
  280. }
  281. /** \brief Reverse byte order (16 bit)
  282. This function reverses the byte order in two unsigned short values.
  283. \param [in] value Value to reverse
  284. \return Reversed value
  285. */
  286. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __REV16(uint32_t value)
  287. {
  288. uint32_t result;
  289. __ASM volatile ("rev16 %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  290. return(result);
  291. }
  292. /** \brief Reverse byte order in signed short value
  293. This function reverses the byte order in a signed short value with sign extension to integer.
  294. \param [in] value Value to reverse
  295. \return Reversed value
  296. */
  297. __attribute__( ( always_inline ) ) __STATIC_INLINE int32_t __REVSH(int32_t value)
  298. {
  299. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  300. return (short)__builtin_bswap16(value);
  301. #else
  302. uint32_t result;
  303. __ASM volatile ("revsh %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  304. return(result);
  305. #endif
  306. }
  307. /** \brief Rotate Right in unsigned value (32 bit)
  308. This function Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  309. \param [in] value Value to rotate
  310. \param [in] value Number of Bits to rotate
  311. \return Rotated value
  312. */
  313. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
  314. {
  315. return (op1 >> op2) | (op1 << (32 - op2));
  316. }
  317. /** \brief Breakpoint
  318. This function causes the processor to enter Debug state.
  319. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  320. \param [in] value is ignored by the processor.
  321. If required, a debugger can use it to store additional information about the breakpoint.
  322. */
  323. #define __BKPT(value) __ASM volatile ("bkpt "#value)
  324. #if (__CORTEX_M >= 0x03)
  325. /** \brief Reverse bit order of value
  326. This function reverses the bit order of the given value.
  327. \param [in] value Value to reverse
  328. \return Reversed value
  329. */
  330. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __RBIT(uint32_t value)
  331. {
  332. uint32_t result;
  333. __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
  334. return(result);
  335. }
  336. /** \brief LDR Exclusive (8 bit)
  337. This function performs a exclusive LDR command for 8 bit value.
  338. \param [in] ptr Pointer to data
  339. \return value of type uint8_t at (*ptr)
  340. */
  341. __attribute__( ( always_inline ) ) __STATIC_INLINE uint8_t __LDREXB(volatile uint8_t *addr)
  342. {
  343. uint32_t result;
  344. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  345. __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
  346. #else
  347. /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
  348. accepted by assembler. So has to use following less efficient pattern.
  349. */
  350. __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
  351. #endif
  352. return ((uint8_t) result); /* Add explicit type cast here */
  353. }
  354. /** \brief LDR Exclusive (16 bit)
  355. This function performs a exclusive LDR command for 16 bit values.
  356. \param [in] ptr Pointer to data
  357. \return value of type uint16_t at (*ptr)
  358. */
  359. __attribute__( ( always_inline ) ) __STATIC_INLINE uint16_t __LDREXH(volatile uint16_t *addr)
  360. {
  361. uint32_t result;
  362. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  363. __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
  364. #else
  365. /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
  366. accepted by assembler. So has to use following less efficient pattern.
  367. */
  368. __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
  369. #endif
  370. return ((uint16_t) result); /* Add explicit type cast here */
  371. }
  372. /** \brief LDR Exclusive (32 bit)
  373. This function performs a exclusive LDR command for 32 bit values.
  374. \param [in] ptr Pointer to data
  375. \return value of type uint32_t at (*ptr)
  376. */
  377. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __LDREXW(volatile uint32_t *addr)
  378. {
  379. uint32_t result;
  380. __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
  381. return(result);
  382. }
  383. /** \brief STR Exclusive (8 bit)
  384. This function performs a exclusive STR command for 8 bit values.
  385. \param [in] value Value to store
  386. \param [in] ptr Pointer to location
  387. \return 0 Function succeeded
  388. \return 1 Function failed
  389. */
  390. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
  391. {
  392. uint32_t result;
  393. __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
  394. return(result);
  395. }
  396. /** \brief STR Exclusive (16 bit)
  397. This function performs a exclusive STR command for 16 bit values.
  398. \param [in] value Value to store
  399. \param [in] ptr Pointer to location
  400. \return 0 Function succeeded
  401. \return 1 Function failed
  402. */
  403. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
  404. {
  405. uint32_t result;
  406. __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
  407. return(result);
  408. }
  409. /** \brief STR Exclusive (32 bit)
  410. This function performs a exclusive STR command for 32 bit values.
  411. \param [in] value Value to store
  412. \param [in] ptr Pointer to location
  413. \return 0 Function succeeded
  414. \return 1 Function failed
  415. */
  416. __attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
  417. {
  418. uint32_t result;
  419. __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
  420. return(result);
  421. }
  422. /** \brief Remove the exclusive lock
  423. This function removes the exclusive lock which is created by LDREX.
  424. */
  425. __attribute__( ( always_inline ) ) __STATIC_INLINE void __CLREX(void)
  426. {
  427. __ASM volatile ("clrex" ::: "memory");
  428. }
  429. /** \brief Signed Saturate
  430. This function saturates a signed value.
  431. \param [in] value Value to be saturated
  432. \param [in] sat Bit position to saturate to (1..32)
  433. \return Saturated value
  434. */
  435. #define __SSAT(ARG1,ARG2) \
  436. ({ \
  437. uint32_t __RES, __ARG1 = (ARG1); \
  438. __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
  439. __RES; \
  440. })
  441. /** \brief Unsigned Saturate
  442. This function saturates an unsigned value.
  443. \param [in] value Value to be saturated
  444. \param [in] sat Bit position to saturate to (0..31)
  445. \return Saturated value
  446. */
  447. #define __USAT(ARG1,ARG2) \
  448. ({ \
  449. uint32_t __RES, __ARG1 = (ARG1); \
  450. __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
  451. __RES; \
  452. })
  453. /** \brief Count leading zeros
  454. This function counts the number of leading zeros of a data value.
  455. \param [in] value Value to count the leading zeros
  456. \return number of leading zeros in value
  457. */
  458. __attribute__( ( always_inline ) ) __STATIC_INLINE uint8_t __CLZ(uint32_t value)
  459. {
  460. uint32_t result;
  461. __ASM volatile ("clz %0, %1" : "=r" (result) : "r" (value) );
  462. return ((uint8_t) result); /* Add explicit type cast here */
  463. }
  464. #endif /* (__CORTEX_M >= 0x03) */
  465. #elif defined ( __ICCARM__ ) /*------------------ ICC Compiler -------------------*/
  466. /* IAR iccarm specific functions */
  467. #include <cmsis_iar.h>
  468. #elif defined ( __TMS470__ ) /*---------------- TI CCS Compiler ------------------*/
  469. /* TI CCS specific functions */
  470. #include <cmsis_ccs.h>
  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. #elif defined ( __CSMC__ ) /*------------------ COSMIC Compiler -------------------*/
  479. /* Cosmic specific functions */
  480. #include <cmsis_csm.h>
  481. #endif
  482. /*@}*/ /* end of group CMSIS_Core_InstructionInterface */
  483. #endif /* __CORE_CMINSTR_H */