core_cmInstr.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /**************************************************************************//**
  2. * @file core_cmInstr.h
  3. * @brief CMSIS Cortex-M Core Instruction Access Header File
  4. * @version V4.10
  5. * @date 18. March 2015
  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() do {\
  71. __schedule_barrier();\
  72. __isb(0xF);\
  73. __schedule_barrier();\
  74. } while (0)
  75. /** \brief Data Synchronization Barrier
  76. This function acts as a special kind of Data Memory Barrier.
  77. It completes when all explicit memory accesses before this instruction complete.
  78. */
  79. #define __DSB() do {\
  80. __schedule_barrier();\
  81. __dsb(0xF);\
  82. __schedule_barrier();\
  83. } while (0)
  84. /** \brief Data Memory Barrier
  85. This function ensures the apparent order of the explicit memory operations before
  86. and after the instruction, without ensuring their completion.
  87. */
  88. #define __DMB() do {\
  89. __schedule_barrier();\
  90. __dmb(0xF);\
  91. __schedule_barrier();\
  92. } while (0)
  93. /** \brief Reverse byte order (32 bit)
  94. This function reverses the byte order in integer value.
  95. \param [in] value Value to reverse
  96. \return Reversed value
  97. */
  98. #define __REV __rev
  99. /** \brief Reverse byte order (16 bit)
  100. This function reverses the byte order in two unsigned short values.
  101. \param [in] value Value to reverse
  102. \return Reversed value
  103. */
  104. #ifndef __NO_EMBEDDED_ASM
  105. __attribute__((section(".rev16_text"))) __STATIC_INLINE __ASM uint32_t __REV16(uint32_t value)
  106. {
  107. rev16 r0, r0
  108. bx lr
  109. }
  110. #endif
  111. /** \brief Reverse byte order in signed short value
  112. This function reverses the byte order in a signed short value with sign extension to integer.
  113. \param [in] value Value to reverse
  114. \return Reversed value
  115. */
  116. #ifndef __NO_EMBEDDED_ASM
  117. __attribute__((section(".revsh_text"))) __STATIC_INLINE __ASM int32_t __REVSH(int32_t value)
  118. {
  119. revsh r0, r0
  120. bx lr
  121. }
  122. #endif
  123. /** \brief Rotate Right in unsigned value (32 bit)
  124. This function Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  125. \param [in] value Value to rotate
  126. \param [in] value Number of Bits to rotate
  127. \return Rotated value
  128. */
  129. #define __ROR __ror
  130. /** \brief Breakpoint
  131. This function causes the processor to enter Debug state.
  132. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  133. \param [in] value is ignored by the processor.
  134. If required, a debugger can use it to store additional information about the breakpoint.
  135. */
  136. #define __BKPT(value) __breakpoint(value)
  137. /** \brief Reverse bit order of value
  138. This function reverses the bit order of the given value.
  139. \param [in] value Value to reverse
  140. \return Reversed value
  141. */
  142. #if (__CORTEX_M >= 0x03) || (__CORTEX_SC >= 300)
  143. #define __RBIT __rbit
  144. #else
  145. __attribute__((always_inline)) __STATIC_INLINE uint32_t __RBIT(uint32_t value)
  146. {
  147. uint32_t result;
  148. int32_t s = 4 /*sizeof(v)*/ * 8 - 1; // extra shift needed at end
  149. result = value; // r will be reversed bits of v; first get LSB of v
  150. for (value >>= 1; value; value >>= 1)
  151. {
  152. result <<= 1;
  153. result |= value & 1;
  154. s--;
  155. }
  156. result <<= s; // shift when v's highest bits are zero
  157. return(result);
  158. }
  159. #endif
  160. /** \brief Count leading zeros
  161. This function counts the number of leading zeros of a data value.
  162. \param [in] value Value to count the leading zeros
  163. \return number of leading zeros in value
  164. */
  165. #define __CLZ __clz
  166. #if (__CORTEX_M >= 0x03) || (__CORTEX_SC >= 300)
  167. /** \brief LDR Exclusive (8 bit)
  168. This function executes a exclusive LDR instruction for 8 bit value.
  169. \param [in] ptr Pointer to data
  170. \return value of type uint8_t at (*ptr)
  171. */
  172. #define __LDREXB(ptr) ((uint8_t ) __ldrex(ptr))
  173. /** \brief LDR Exclusive (16 bit)
  174. This function executes a exclusive LDR instruction for 16 bit values.
  175. \param [in] ptr Pointer to data
  176. \return value of type uint16_t at (*ptr)
  177. */
  178. #define __LDREXH(ptr) ((uint16_t) __ldrex(ptr))
  179. /** \brief LDR Exclusive (32 bit)
  180. This function executes a exclusive LDR instruction for 32 bit values.
  181. \param [in] ptr Pointer to data
  182. \return value of type uint32_t at (*ptr)
  183. */
  184. #define __LDREXW(ptr) ((uint32_t ) __ldrex(ptr))
  185. /** \brief STR Exclusive (8 bit)
  186. This function executes a exclusive STR instruction for 8 bit values.
  187. \param [in] value Value to store
  188. \param [in] ptr Pointer to location
  189. \return 0 Function succeeded
  190. \return 1 Function failed
  191. */
  192. #define __STREXB(value, ptr) __strex(value, ptr)
  193. /** \brief STR Exclusive (16 bit)
  194. This function executes a exclusive STR instruction for 16 bit values.
  195. \param [in] value Value to store
  196. \param [in] ptr Pointer to location
  197. \return 0 Function succeeded
  198. \return 1 Function failed
  199. */
  200. #define __STREXH(value, ptr) __strex(value, ptr)
  201. /** \brief STR Exclusive (32 bit)
  202. This function executes a exclusive STR instruction for 32 bit values.
  203. \param [in] value Value to store
  204. \param [in] ptr Pointer to location
  205. \return 0 Function succeeded
  206. \return 1 Function failed
  207. */
  208. #define __STREXW(value, ptr) __strex(value, ptr)
  209. /** \brief Remove the exclusive lock
  210. This function removes the exclusive lock which is created by LDREX.
  211. */
  212. #define __CLREX __clrex
  213. /** \brief Signed Saturate
  214. This function saturates a signed value.
  215. \param [in] value Value to be saturated
  216. \param [in] sat Bit position to saturate to (1..32)
  217. \return Saturated value
  218. */
  219. #define __SSAT __ssat
  220. /** \brief Unsigned Saturate
  221. This function saturates an unsigned value.
  222. \param [in] value Value to be saturated
  223. \param [in] sat Bit position to saturate to (0..31)
  224. \return Saturated value
  225. */
  226. #define __USAT __usat
  227. /** \brief Rotate Right with Extend (32 bit)
  228. This function moves each bit of a bitstring right by one bit.
  229. The carry input is shifted in at the left end of the bitstring.
  230. \param [in] value Value to rotate
  231. \return Rotated value
  232. */
  233. #ifndef __NO_EMBEDDED_ASM
  234. __attribute__((section(".rrx_text"))) __STATIC_INLINE __ASM uint32_t __RRX(uint32_t value)
  235. {
  236. rrx r0, r0
  237. bx lr
  238. }
  239. #endif
  240. /** \brief LDRT Unprivileged (8 bit)
  241. This function executes a Unprivileged LDRT instruction for 8 bit value.
  242. \param [in] ptr Pointer to data
  243. \return value of type uint8_t at (*ptr)
  244. */
  245. #define __LDRBT(ptr) ((uint8_t ) __ldrt(ptr))
  246. /** \brief LDRT Unprivileged (16 bit)
  247. This function executes a Unprivileged LDRT instruction for 16 bit values.
  248. \param [in] ptr Pointer to data
  249. \return value of type uint16_t at (*ptr)
  250. */
  251. #define __LDRHT(ptr) ((uint16_t) __ldrt(ptr))
  252. /** \brief LDRT Unprivileged (32 bit)
  253. This function executes a Unprivileged LDRT instruction for 32 bit values.
  254. \param [in] ptr Pointer to data
  255. \return value of type uint32_t at (*ptr)
  256. */
  257. #define __LDRT(ptr) ((uint32_t ) __ldrt(ptr))
  258. /** \brief STRT Unprivileged (8 bit)
  259. This function executes a Unprivileged STRT instruction for 8 bit values.
  260. \param [in] value Value to store
  261. \param [in] ptr Pointer to location
  262. */
  263. #define __STRBT(value, ptr) __strt(value, ptr)
  264. /** \brief STRT Unprivileged (16 bit)
  265. This function executes a Unprivileged STRT instruction for 16 bit values.
  266. \param [in] value Value to store
  267. \param [in] ptr Pointer to location
  268. */
  269. #define __STRHT(value, ptr) __strt(value, ptr)
  270. /** \brief STRT Unprivileged (32 bit)
  271. This function executes a Unprivileged STRT instruction for 32 bit values.
  272. \param [in] value Value to store
  273. \param [in] ptr Pointer to location
  274. */
  275. #define __STRT(value, ptr) __strt(value, ptr)
  276. #endif /* (__CORTEX_M >= 0x03) || (__CORTEX_SC >= 300) */
  277. #elif defined ( __GNUC__ ) /*------------------ GNU Compiler ---------------------*/
  278. /* GNU gcc specific functions */
  279. /* Define macros for porting to both thumb1 and thumb2.
  280. * For thumb1, use low register (r0-r7), specified by constrant "l"
  281. * Otherwise, use general registers, specified by constrant "r" */
  282. #if defined (__thumb__) && !defined (__thumb2__)
  283. #define __CMSIS_GCC_OUT_REG(r) "=l" (r)
  284. #define __CMSIS_GCC_USE_REG(r) "l" (r)
  285. #else
  286. #define __CMSIS_GCC_OUT_REG(r) "=r" (r)
  287. #define __CMSIS_GCC_USE_REG(r) "r" (r)
  288. #endif
  289. /** \brief No Operation
  290. No Operation does nothing. This instruction can be used for code alignment purposes.
  291. */
  292. __attribute__((always_inline)) __STATIC_INLINE void __NOP(void)
  293. {
  294. __ASM volatile ("nop");
  295. }
  296. /** \brief Wait For Interrupt
  297. Wait For Interrupt is a hint instruction that suspends execution
  298. until one of a number of events occurs.
  299. */
  300. __attribute__((always_inline)) __STATIC_INLINE void __WFI(void)
  301. {
  302. __ASM volatile ("wfi");
  303. }
  304. /** \brief Wait For Event
  305. Wait For Event is a hint instruction that permits the processor to enter
  306. a low-power state until one of a number of events occurs.
  307. */
  308. __attribute__((always_inline)) __STATIC_INLINE void __WFE(void)
  309. {
  310. __ASM volatile ("wfe");
  311. }
  312. /** \brief Send Event
  313. Send Event is a hint instruction. It causes an event to be signaled to the CPU.
  314. */
  315. __attribute__((always_inline)) __STATIC_INLINE void __SEV(void)
  316. {
  317. __ASM volatile ("sev");
  318. }
  319. /** \brief Instruction Synchronization Barrier
  320. Instruction Synchronization Barrier flushes the pipeline in the processor,
  321. so that all instructions following the ISB are fetched from cache or
  322. memory, after the instruction has been completed.
  323. */
  324. __attribute__((always_inline)) __STATIC_INLINE void __ISB(void)
  325. {
  326. __ASM volatile ("isb 0xF":::"memory");
  327. }
  328. /** \brief Data Synchronization Barrier
  329. This function acts as a special kind of Data Memory Barrier.
  330. It completes when all explicit memory accesses before this instruction complete.
  331. */
  332. __attribute__((always_inline)) __STATIC_INLINE void __DSB(void)
  333. {
  334. __ASM volatile ("dsb 0xF":::"memory");
  335. }
  336. /** \brief Data Memory Barrier
  337. This function ensures the apparent order of the explicit memory operations before
  338. and after the instruction, without ensuring their completion.
  339. */
  340. __attribute__((always_inline)) __STATIC_INLINE void __DMB(void)
  341. {
  342. __ASM volatile ("dmb 0xF":::"memory");
  343. }
  344. /** \brief Reverse byte order (32 bit)
  345. This function reverses the byte order in integer value.
  346. \param [in] value Value to reverse
  347. \return Reversed value
  348. */
  349. __attribute__((always_inline)) __STATIC_INLINE uint32_t __REV(uint32_t value)
  350. {
  351. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
  352. return __builtin_bswap32(value);
  353. #else
  354. uint32_t result;
  355. __ASM volatile ("rev %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  356. return(result);
  357. #endif
  358. }
  359. /** \brief Reverse byte order (16 bit)
  360. This function reverses the byte order in two unsigned short values.
  361. \param [in] value Value to reverse
  362. \return Reversed value
  363. */
  364. __attribute__((always_inline)) __STATIC_INLINE uint32_t __REV16(uint32_t value)
  365. {
  366. uint32_t result;
  367. __ASM volatile ("rev16 %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  368. return(result);
  369. }
  370. /** \brief Reverse byte order in signed short value
  371. This function reverses the byte order in a signed short value with sign extension to integer.
  372. \param [in] value Value to reverse
  373. \return Reversed value
  374. */
  375. __attribute__((always_inline)) __STATIC_INLINE int32_t __REVSH(int32_t value)
  376. {
  377. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  378. return (short)__builtin_bswap16(value);
  379. #else
  380. uint32_t result;
  381. __ASM volatile ("revsh %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  382. return(result);
  383. #endif
  384. }
  385. /** \brief Rotate Right in unsigned value (32 bit)
  386. This function Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  387. \param [in] value Value to rotate
  388. \param [in] value Number of Bits to rotate
  389. \return Rotated value
  390. */
  391. __attribute__((always_inline)) __STATIC_INLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
  392. {
  393. return (op1 >> op2) | (op1 << (32 - op2));
  394. }
  395. /** \brief Breakpoint
  396. This function causes the processor to enter Debug state.
  397. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  398. \param [in] value is ignored by the processor.
  399. If required, a debugger can use it to store additional information about the breakpoint.
  400. */
  401. #define __BKPT(value) __ASM volatile ("bkpt "#value)
  402. /** \brief Reverse bit order of value
  403. This function reverses the bit order of the given value.
  404. \param [in] value Value to reverse
  405. \return Reversed value
  406. */
  407. __attribute__((always_inline)) __STATIC_INLINE uint32_t __RBIT(uint32_t value)
  408. {
  409. uint32_t result;
  410. #if (__CORTEX_M >= 0x03) || (__CORTEX_SC >= 300)
  411. __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
  412. #else
  413. int32_t s = 4 /*sizeof(v)*/ * 8 - 1; // extra shift needed at end
  414. result = value; // r will be reversed bits of v; first get LSB of v
  415. for (value >>= 1; value; value >>= 1)
  416. {
  417. result <<= 1;
  418. result |= value & 1;
  419. s--;
  420. }
  421. result <<= s; // shift when v's highest bits are zero
  422. #endif
  423. return(result);
  424. }
  425. /** \brief Count leading zeros
  426. This function counts the number of leading zeros of a data value.
  427. \param [in] value Value to count the leading zeros
  428. \return number of leading zeros in value
  429. */
  430. #define __CLZ __builtin_clz
  431. #if (__CORTEX_M >= 0x03) || (__CORTEX_SC >= 300)
  432. /** \brief LDR Exclusive (8 bit)
  433. This function executes a exclusive LDR instruction for 8 bit value.
  434. \param [in] ptr Pointer to data
  435. \return value of type uint8_t at (*ptr)
  436. */
  437. __attribute__((always_inline)) __STATIC_INLINE uint8_t __LDREXB(volatile uint8_t *addr)
  438. {
  439. uint32_t result;
  440. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  441. __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
  442. #else
  443. /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
  444. accepted by assembler. So has to use following less efficient pattern.
  445. */
  446. __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
  447. #endif
  448. return ((uint8_t) result); /* Add explicit type cast here */
  449. }
  450. /** \brief LDR Exclusive (16 bit)
  451. This function executes a exclusive LDR instruction for 16 bit values.
  452. \param [in] ptr Pointer to data
  453. \return value of type uint16_t at (*ptr)
  454. */
  455. __attribute__((always_inline)) __STATIC_INLINE uint16_t __LDREXH(volatile uint16_t *addr)
  456. {
  457. uint32_t result;
  458. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  459. __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
  460. #else
  461. /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
  462. accepted by assembler. So has to use following less efficient pattern.
  463. */
  464. __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
  465. #endif
  466. return ((uint16_t) result); /* Add explicit type cast here */
  467. }
  468. /** \brief LDR Exclusive (32 bit)
  469. This function executes a exclusive LDR instruction for 32 bit values.
  470. \param [in] ptr Pointer to data
  471. \return value of type uint32_t at (*ptr)
  472. */
  473. __attribute__((always_inline)) __STATIC_INLINE uint32_t __LDREXW(volatile uint32_t *addr)
  474. {
  475. uint32_t result;
  476. __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
  477. return(result);
  478. }
  479. /** \brief STR Exclusive (8 bit)
  480. This function executes a exclusive STR instruction for 8 bit values.
  481. \param [in] value Value to store
  482. \param [in] ptr Pointer to location
  483. \return 0 Function succeeded
  484. \return 1 Function failed
  485. */
  486. __attribute__((always_inline)) __STATIC_INLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
  487. {
  488. uint32_t result;
  489. __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
  490. return(result);
  491. }
  492. /** \brief STR Exclusive (16 bit)
  493. This function executes a exclusive STR instruction for 16 bit values.
  494. \param [in] value Value to store
  495. \param [in] ptr Pointer to location
  496. \return 0 Function succeeded
  497. \return 1 Function failed
  498. */
  499. __attribute__((always_inline)) __STATIC_INLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
  500. {
  501. uint32_t result;
  502. __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
  503. return(result);
  504. }
  505. /** \brief STR Exclusive (32 bit)
  506. This function executes a exclusive STR instruction for 32 bit values.
  507. \param [in] value Value to store
  508. \param [in] ptr Pointer to location
  509. \return 0 Function succeeded
  510. \return 1 Function failed
  511. */
  512. __attribute__((always_inline)) __STATIC_INLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
  513. {
  514. uint32_t result;
  515. __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
  516. return(result);
  517. }
  518. /** \brief Remove the exclusive lock
  519. This function removes the exclusive lock which is created by LDREX.
  520. */
  521. __attribute__((always_inline)) __STATIC_INLINE void __CLREX(void)
  522. {
  523. __ASM volatile ("clrex" ::: "memory");
  524. }
  525. /** \brief Signed Saturate
  526. This function saturates a signed value.
  527. \param [in] value Value to be saturated
  528. \param [in] sat Bit position to saturate to (1..32)
  529. \return Saturated value
  530. */
  531. #define __SSAT(ARG1,ARG2) \
  532. ({ \
  533. uint32_t __RES, __ARG1 = (ARG1); \
  534. __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
  535. __RES; \
  536. })
  537. /** \brief Unsigned Saturate
  538. This function saturates an unsigned value.
  539. \param [in] value Value to be saturated
  540. \param [in] sat Bit position to saturate to (0..31)
  541. \return Saturated value
  542. */
  543. #define __USAT(ARG1,ARG2) \
  544. ({ \
  545. uint32_t __RES, __ARG1 = (ARG1); \
  546. __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \
  547. __RES; \
  548. })
  549. /** \brief Rotate Right with Extend (32 bit)
  550. This function moves each bit of a bitstring right by one bit.
  551. The carry input is shifted in at the left end of the bitstring.
  552. \param [in] value Value to rotate
  553. \return Rotated value
  554. */
  555. __attribute__((always_inline)) __STATIC_INLINE uint32_t __RRX(uint32_t value)
  556. {
  557. uint32_t result;
  558. __ASM volatile ("rrx %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  559. return(result);
  560. }
  561. /** \brief LDRT Unprivileged (8 bit)
  562. This function executes a Unprivileged LDRT instruction for 8 bit value.
  563. \param [in] ptr Pointer to data
  564. \return value of type uint8_t at (*ptr)
  565. */
  566. __attribute__((always_inline)) __STATIC_INLINE uint8_t __LDRBT(volatile uint8_t *addr)
  567. {
  568. uint32_t result;
  569. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  570. __ASM volatile ("ldrbt %0, %1" : "=r" (result) : "Q" (*addr) );
  571. #else
  572. /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
  573. accepted by assembler. So has to use following less efficient pattern.
  574. */
  575. __ASM volatile ("ldrbt %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
  576. #endif
  577. return ((uint8_t) result); /* Add explicit type cast here */
  578. }
  579. /** \brief LDRT Unprivileged (16 bit)
  580. This function executes a Unprivileged LDRT instruction for 16 bit values.
  581. \param [in] ptr Pointer to data
  582. \return value of type uint16_t at (*ptr)
  583. */
  584. __attribute__((always_inline)) __STATIC_INLINE uint16_t __LDRHT(volatile uint16_t *addr)
  585. {
  586. uint32_t result;
  587. #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
  588. __ASM volatile ("ldrht %0, %1" : "=r" (result) : "Q" (*addr) );
  589. #else
  590. /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
  591. accepted by assembler. So has to use following less efficient pattern.
  592. */
  593. __ASM volatile ("ldrht %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
  594. #endif
  595. return ((uint16_t) result); /* Add explicit type cast here */
  596. }
  597. /** \brief LDRT Unprivileged (32 bit)
  598. This function executes a Unprivileged LDRT instruction for 32 bit values.
  599. \param [in] ptr Pointer to data
  600. \return value of type uint32_t at (*ptr)
  601. */
  602. __attribute__((always_inline)) __STATIC_INLINE uint32_t __LDRT(volatile uint32_t *addr)
  603. {
  604. uint32_t result;
  605. __ASM volatile ("ldrt %0, %1" : "=r" (result) : "Q" (*addr) );
  606. return(result);
  607. }
  608. /** \brief STRT Unprivileged (8 bit)
  609. This function executes a Unprivileged STRT instruction for 8 bit values.
  610. \param [in] value Value to store
  611. \param [in] ptr Pointer to location
  612. */
  613. __attribute__((always_inline)) __STATIC_INLINE void __STRBT(uint8_t value, volatile uint8_t *addr)
  614. {
  615. __ASM volatile ("strbt %1, %0" : "=Q" (*addr) : "r" ((uint32_t)value) );
  616. }
  617. /** \brief STRT Unprivileged (16 bit)
  618. This function executes a Unprivileged STRT instruction for 16 bit values.
  619. \param [in] value Value to store
  620. \param [in] ptr Pointer to location
  621. */
  622. __attribute__((always_inline)) __STATIC_INLINE void __STRHT(uint16_t value, volatile uint16_t *addr)
  623. {
  624. __ASM volatile ("strht %1, %0" : "=Q" (*addr) : "r" ((uint32_t)value) );
  625. }
  626. /** \brief STRT Unprivileged (32 bit)
  627. This function executes a Unprivileged STRT instruction for 32 bit values.
  628. \param [in] value Value to store
  629. \param [in] ptr Pointer to location
  630. */
  631. __attribute__((always_inline)) __STATIC_INLINE void __STRT(uint32_t value, volatile uint32_t *addr)
  632. {
  633. __ASM volatile ("strt %1, %0" : "=Q" (*addr) : "r" (value) );
  634. }
  635. #endif /* (__CORTEX_M >= 0x03) || (__CORTEX_SC >= 300) */
  636. #elif defined ( __ICCARM__ ) /*------------------ ICC Compiler -------------------*/
  637. /* IAR iccarm specific functions */
  638. #include <cmsis_iar.h>
  639. #elif defined ( __TMS470__ ) /*---------------- TI CCS Compiler ------------------*/
  640. /* TI CCS specific functions */
  641. #include <cmsis_ccs.h>
  642. #elif defined ( __TASKING__ ) /*------------------ TASKING Compiler --------------*/
  643. /* TASKING carm specific functions */
  644. /*
  645. * The CMSIS functions have been implemented as intrinsics in the compiler.
  646. * Please use "carm -?i" to get an up to date list of all intrinsics,
  647. * Including the CMSIS ones.
  648. */
  649. #elif defined ( __CSMC__ ) /*------------------ COSMIC Compiler -------------------*/
  650. /* Cosmic specific functions */
  651. #include <cmsis_csm.h>
  652. #endif
  653. /*@}*/ /* end of group CMSIS_Core_InstructionInterface */
  654. #endif /* __CORE_CMINSTR_H */