core_cmInstr.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /**************************************************************************//**
  2. * @file core_cmInstr.h
  3. * @brief CMSIS Cortex-M Core Instruction Access Header File
  4. * @version V2.10
  5. * @date 19. July 2011
  6. *
  7. * @note
  8. * Copyright (C) 2009-2011 ARM Limited. All rights reserved.
  9. *
  10. * @par
  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.
  14. *
  15. * @par
  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.
  21. *
  22. ******************************************************************************/
  23. #ifndef __CORE_CMINSTR_H
  24. #define __CORE_CMINSTR_H
  25. /* ########################## Core Instruction Access ######################### */
  26. /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface
  27. Access to dedicated instructions
  28. @{
  29. */
  30. #if defined ( __CC_ARM ) /*------------------RealView Compiler -----------------*/
  31. /* ARM armcc specific functions */
  32. #if (__ARMCC_VERSION < 400677)
  33. #error "Please use ARM Compiler Toolchain V4.0.677 or later!"
  34. #endif
  35. /** \brief No Operation
  36. No Operation does nothing. This instruction can be used for code alignment purposes.
  37. */
  38. #define __NOP __nop
  39. /** \brief Wait For Interrupt
  40. Wait For Interrupt is a hint instruction that suspends execution
  41. until one of a number of events occurs.
  42. */
  43. #define __WFI __wfi
  44. /** \brief Wait For Event
  45. Wait For Event is a hint instruction that permits the processor to enter
  46. a low-power state until one of a number of events occurs.
  47. */
  48. #define __WFE __wfe
  49. /** \brief Send Event
  50. Send Event is a hint instruction. It causes an event to be signaled to the CPU.
  51. */
  52. #define __SEV __sev
  53. /** \brief Instruction Synchronization Barrier
  54. Instruction Synchronization Barrier flushes the pipeline in the processor,
  55. so that all instructions following the ISB are fetched from cache or
  56. memory, after the instruction has been completed.
  57. */
  58. #define __ISB() __isb(0xF)
  59. /** \brief Data Synchronization Barrier
  60. This function acts as a special kind of Data Memory Barrier.
  61. It completes when all explicit memory accesses before this instruction complete.
  62. */
  63. #define __DSB() __dsb(0xF)
  64. /** \brief Data Memory Barrier
  65. This function ensures the apparent order of the explicit memory operations before
  66. and after the instruction, without ensuring their completion.
  67. */
  68. #define __DMB() __dmb(0xF)
  69. /** \brief Reverse byte order (32 bit)
  70. This function reverses the byte order in integer value.
  71. \param [in] value Value to reverse
  72. \return Reversed value
  73. */
  74. #define __REV __rev
  75. /** \brief Reverse byte order (16 bit)
  76. This function reverses the byte order in two unsigned short values.
  77. \param [in] value Value to reverse
  78. \return Reversed value
  79. */
  80. static __INLINE __ASM uint32_t __REV16(uint32_t value)
  81. {
  82. rev16 r0, r0
  83. bx lr
  84. }
  85. /** \brief Reverse byte order in signed short value
  86. This function reverses the byte order in a signed short value with sign extension to integer.
  87. \param [in] value Value to reverse
  88. \return Reversed value
  89. */
  90. static __INLINE __ASM int32_t __REVSH(int32_t value)
  91. {
  92. revsh r0, r0
  93. bx lr
  94. }
  95. #if (__CORTEX_M >= 0x03)
  96. /** \brief Reverse bit order of value
  97. This function reverses the bit order of the given value.
  98. \param [in] value Value to reverse
  99. \return Reversed value
  100. */
  101. #define __RBIT __rbit
  102. /** \brief LDR Exclusive (8 bit)
  103. This function performs a exclusive LDR command for 8 bit value.
  104. \param [in] ptr Pointer to data
  105. \return value of type uint8_t at (*ptr)
  106. */
  107. #define __LDREXB(ptr) ((uint8_t ) __ldrex(ptr))
  108. /** \brief LDR Exclusive (16 bit)
  109. This function performs a exclusive LDR command for 16 bit values.
  110. \param [in] ptr Pointer to data
  111. \return value of type uint16_t at (*ptr)
  112. */
  113. #define __LDREXH(ptr) ((uint16_t) __ldrex(ptr))
  114. /** \brief LDR Exclusive (32 bit)
  115. This function performs a exclusive LDR command for 32 bit values.
  116. \param [in] ptr Pointer to data
  117. \return value of type uint32_t at (*ptr)
  118. */
  119. #define __LDREXW(ptr) ((uint32_t ) __ldrex(ptr))
  120. /** \brief STR Exclusive (8 bit)
  121. This function performs a exclusive STR command for 8 bit values.
  122. \param [in] value Value to store
  123. \param [in] ptr Pointer to location
  124. \return 0 Function succeeded
  125. \return 1 Function failed
  126. */
  127. #define __STREXB(value, ptr) __strex(value, ptr)
  128. /** \brief STR Exclusive (16 bit)
  129. This function performs a exclusive STR command for 16 bit values.
  130. \param [in] value Value to store
  131. \param [in] ptr Pointer to location
  132. \return 0 Function succeeded
  133. \return 1 Function failed
  134. */
  135. #define __STREXH(value, ptr) __strex(value, ptr)
  136. /** \brief STR Exclusive (32 bit)
  137. This function performs a exclusive STR command for 32 bit values.
  138. \param [in] value Value to store
  139. \param [in] ptr Pointer to location
  140. \return 0 Function succeeded
  141. \return 1 Function failed
  142. */
  143. #define __STREXW(value, ptr) __strex(value, ptr)
  144. /** \brief Remove the exclusive lock
  145. This function removes the exclusive lock which is created by LDREX.
  146. */
  147. #define __CLREX __clrex
  148. /** \brief Signed Saturate
  149. This function saturates a signed value.
  150. \param [in] value Value to be saturated
  151. \param [in] sat Bit position to saturate to (1..32)
  152. \return Saturated value
  153. */
  154. #define __SSAT __ssat
  155. /** \brief Unsigned Saturate
  156. This function saturates an unsigned value.
  157. \param [in] value Value to be saturated
  158. \param [in] sat Bit position to saturate to (0..31)
  159. \return Saturated value
  160. */
  161. #define __USAT __usat
  162. /** \brief Count leading zeros
  163. This function counts the number of leading zeros of a data value.
  164. \param [in] value Value to count the leading zeros
  165. \return number of leading zeros in value
  166. */
  167. #define __CLZ __clz
  168. #endif /* (__CORTEX_M >= 0x03) */
  169. #elif defined ( __ICCARM__ ) /*------------------ ICC Compiler -------------------*/
  170. /* IAR iccarm specific functions */
  171. #include <cmsis_iar.h>
  172. #elif defined ( __GNUC__ ) /*------------------ GNU Compiler ---------------------*/
  173. /* GNU gcc specific functions */
  174. /** \brief No Operation
  175. No Operation does nothing. This instruction can be used for code alignment purposes.
  176. */
  177. __attribute__( ( always_inline ) ) static __INLINE void __NOP(void)
  178. {
  179. __ASM volatile ("nop");
  180. }
  181. /** \brief Wait For Interrupt
  182. Wait For Interrupt is a hint instruction that suspends execution
  183. until one of a number of events occurs.
  184. */
  185. __attribute__( ( always_inline ) ) static __INLINE void __WFI(void)
  186. {
  187. __ASM volatile ("wfi");
  188. }
  189. /** \brief Wait For Event
  190. Wait For Event is a hint instruction that permits the processor to enter
  191. a low-power state until one of a number of events occurs.
  192. */
  193. __attribute__( ( always_inline ) ) static __INLINE void __WFE(void)
  194. {
  195. __ASM volatile ("wfe");
  196. }
  197. /** \brief Send Event
  198. Send Event is a hint instruction. It causes an event to be signaled to the CPU.
  199. */
  200. __attribute__( ( always_inline ) ) static __INLINE void __SEV(void)
  201. {
  202. __ASM volatile ("sev");
  203. }
  204. /** \brief Instruction Synchronization Barrier
  205. Instruction Synchronization Barrier flushes the pipeline in the processor,
  206. so that all instructions following the ISB are fetched from cache or
  207. memory, after the instruction has been completed.
  208. */
  209. __attribute__( ( always_inline ) ) static __INLINE void __ISB(void)
  210. {
  211. __ASM volatile ("isb");
  212. }
  213. /** \brief Data Synchronization Barrier
  214. This function acts as a special kind of Data Memory Barrier.
  215. It completes when all explicit memory accesses before this instruction complete.
  216. */
  217. __attribute__( ( always_inline ) ) static __INLINE void __DSB(void)
  218. {
  219. __ASM volatile ("dsb");
  220. }
  221. /** \brief Data Memory Barrier
  222. This function ensures the apparent order of the explicit memory operations before
  223. and after the instruction, without ensuring their completion.
  224. */
  225. __attribute__( ( always_inline ) ) static __INLINE void __DMB(void)
  226. {
  227. __ASM volatile ("dmb");
  228. }
  229. /** \brief Reverse byte order (32 bit)
  230. This function reverses the byte order in integer value.
  231. \param [in] value Value to reverse
  232. \return Reversed value
  233. */
  234. __attribute__( ( always_inline ) ) static __INLINE uint32_t __REV(uint32_t value)
  235. {
  236. uint32_t result;
  237. __ASM volatile ("rev %0, %1" : "=r" (result) : "r" (value) );
  238. return(result);
  239. }
  240. /** \brief Reverse byte order (16 bit)
  241. This function reverses the byte order in two unsigned short values.
  242. \param [in] value Value to reverse
  243. \return Reversed value
  244. */
  245. __attribute__( ( always_inline ) ) static __INLINE uint32_t __REV16(uint32_t value)
  246. {
  247. uint32_t result;
  248. __ASM volatile ("rev16 %0, %1" : "=r" (result) : "r" (value) );
  249. return(result);
  250. }
  251. /** \brief Reverse byte order in signed short value
  252. This function reverses the byte order in a signed short value with sign extension to integer.
  253. \param [in] value Value to reverse
  254. \return Reversed value
  255. */
  256. __attribute__( ( always_inline ) ) static __INLINE int32_t __REVSH(int32_t value)
  257. {
  258. uint32_t result;
  259. __ASM volatile ("revsh %0, %1" : "=r" (result) : "r" (value) );
  260. return(result);
  261. }
  262. #if (__CORTEX_M >= 0x03)
  263. /** \brief Reverse bit order of value
  264. This function reverses the bit order of the given value.
  265. \param [in] value Value to reverse
  266. \return Reversed value
  267. */
  268. __attribute__( ( always_inline ) ) static __INLINE uint32_t __RBIT(uint32_t value)
  269. {
  270. uint32_t result;
  271. __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
  272. return(result);
  273. }
  274. /** \brief LDR Exclusive (8 bit)
  275. This function performs a exclusive LDR command for 8 bit value.
  276. \param [in] ptr Pointer to data
  277. \return value of type uint8_t at (*ptr)
  278. */
  279. __attribute__( ( always_inline ) ) static __INLINE uint8_t __LDREXB(volatile uint8_t *addr)
  280. {
  281. uint8_t result;
  282. __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) );
  283. return(result);
  284. }
  285. /** \brief LDR Exclusive (16 bit)
  286. This function performs a exclusive LDR command for 16 bit values.
  287. \param [in] ptr Pointer to data
  288. \return value of type uint16_t at (*ptr)
  289. */
  290. __attribute__( ( always_inline ) ) static __INLINE uint16_t __LDREXH(volatile uint16_t *addr)
  291. {
  292. uint16_t result;
  293. __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) );
  294. return(result);
  295. }
  296. /** \brief LDR Exclusive (32 bit)
  297. This function performs a exclusive LDR command for 32 bit values.
  298. \param [in] ptr Pointer to data
  299. \return value of type uint32_t at (*ptr)
  300. */
  301. __attribute__( ( always_inline ) ) static __INLINE uint32_t __LDREXW(volatile uint32_t *addr)
  302. {
  303. uint32_t result;
  304. __ASM volatile ("ldrex %0, [%1]" : "=r" (result) : "r" (addr) );
  305. return(result);
  306. }
  307. /** \brief STR Exclusive (8 bit)
  308. This function performs a exclusive STR command for 8 bit values.
  309. \param [in] value Value to store
  310. \param [in] ptr Pointer to location
  311. \return 0 Function succeeded
  312. \return 1 Function failed
  313. */
  314. __attribute__( ( always_inline ) ) static __INLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
  315. {
  316. uint32_t result;
  317. __ASM volatile ("strexb %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
  318. return(result);
  319. }
  320. /** \brief STR Exclusive (16 bit)
  321. This function performs a exclusive STR command for 16 bit values.
  322. \param [in] value Value to store
  323. \param [in] ptr Pointer to location
  324. \return 0 Function succeeded
  325. \return 1 Function failed
  326. */
  327. __attribute__( ( always_inline ) ) static __INLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
  328. {
  329. uint32_t result;
  330. __ASM volatile ("strexh %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
  331. return(result);
  332. }
  333. /** \brief STR Exclusive (32 bit)
  334. This function performs a exclusive STR command for 32 bit values.
  335. \param [in] value Value to store
  336. \param [in] ptr Pointer to location
  337. \return 0 Function succeeded
  338. \return 1 Function failed
  339. */
  340. __attribute__( ( always_inline ) ) static __INLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
  341. {
  342. uint32_t result;
  343. __ASM volatile ("strex %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
  344. return(result);
  345. }
  346. /** \brief Remove the exclusive lock
  347. This function removes the exclusive lock which is created by LDREX.
  348. */
  349. __attribute__( ( always_inline ) ) static __INLINE void __CLREX(void)
  350. {
  351. __ASM volatile ("clrex");
  352. }
  353. /** \brief Signed Saturate
  354. This function saturates a signed value.
  355. \param [in] value Value to be saturated
  356. \param [in] sat Bit position to saturate to (1..32)
  357. \return Saturated value
  358. */
  359. #define __SSAT(ARG1,ARG2) \
  360. ({ \
  361. uint32_t __RES, __ARG1 = (ARG1); \
  362. __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
  363. __RES; \
  364. })
  365. /** \brief Unsigned Saturate
  366. This function saturates an unsigned value.
  367. \param [in] value Value to be saturated
  368. \param [in] sat Bit position to saturate to (0..31)
  369. \return Saturated value
  370. */
  371. #define __USAT(ARG1,ARG2) \
  372. ({ \
  373. uint32_t __RES, __ARG1 = (ARG1); \
  374. __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
  375. __RES; \
  376. })
  377. /** \brief Count leading zeros
  378. This function counts the number of leading zeros of a data value.
  379. \param [in] value Value to count the leading zeros
  380. \return number of leading zeros in value
  381. */
  382. __attribute__( ( always_inline ) ) static __INLINE uint8_t __CLZ(uint32_t value)
  383. {
  384. uint8_t result;
  385. __ASM volatile ("clz %0, %1" : "=r" (result) : "r" (value) );
  386. return(result);
  387. }
  388. #endif /* (__CORTEX_M >= 0x03) */
  389. #elif defined ( __TASKING__ ) /*------------------ TASKING Compiler --------------*/
  390. /* TASKING carm specific functions */
  391. /*
  392. * The CMSIS functions have been implemented as intrinsics in the compiler.
  393. * Please use "carm -?i" to get an up to date list of all intrinsics,
  394. * Including the CMSIS ones.
  395. */
  396. #endif
  397. /*@}*/ /* end of group CMSIS_Core_InstructionInterface */
  398. #endif /* __CORE_CMINSTR_H */