core_cmInstr.h 16 KB

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