cmsis_gcc.h 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /**************************************************************************//**
  2. * @file cmsis_gcc.h
  3. * @brief CMSIS compiler GCC header file
  4. * @version V6.0.0
  5. * @date 27. July 2024
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2009-2023 Arm Limited. All rights reserved.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the License); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #ifndef __CMSIS_GCC_H
  25. #define __CMSIS_GCC_H
  26. #pragma GCC system_header /* treat file as system include file */
  27. #include <arm_acle.h>
  28. /* Fallback for __has_builtin */
  29. #ifndef __has_builtin
  30. #define __has_builtin(x) (0)
  31. #endif
  32. /* CMSIS compiler specific defines */
  33. #ifndef __ASM
  34. #define __ASM __asm
  35. #endif
  36. #ifndef __INLINE
  37. #define __INLINE inline
  38. #endif
  39. #ifndef __STATIC_INLINE
  40. #define __STATIC_INLINE static inline
  41. #endif
  42. #ifndef __STATIC_FORCEINLINE
  43. #define __STATIC_FORCEINLINE __attribute__((always_inline)) static inline
  44. #endif
  45. #ifndef __NO_RETURN
  46. #define __NO_RETURN __attribute__((__noreturn__))
  47. #endif
  48. #ifndef CMSIS_DEPRECATED
  49. #define CMSIS_DEPRECATED __attribute__((deprecated))
  50. #endif
  51. #ifndef __USED
  52. #define __USED __attribute__((used))
  53. #endif
  54. #ifndef __WEAK
  55. #define __WEAK __attribute__((weak))
  56. #endif
  57. #ifndef __PACKED
  58. #define __PACKED __attribute__((packed, aligned(1)))
  59. #endif
  60. #ifndef __PACKED_STRUCT
  61. #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
  62. #endif
  63. #ifndef __PACKED_UNION
  64. #define __PACKED_UNION union __attribute__((packed, aligned(1)))
  65. #endif
  66. #ifndef __UNALIGNED_UINT16_WRITE
  67. #pragma GCC diagnostic push
  68. #pragma GCC diagnostic ignored "-Wpacked"
  69. #pragma GCC diagnostic ignored "-Wattributes"
  70. __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
  71. #pragma GCC diagnostic pop
  72. #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
  73. #endif
  74. #ifndef __UNALIGNED_UINT16_READ
  75. #pragma GCC diagnostic push
  76. #pragma GCC diagnostic ignored "-Wpacked"
  77. #pragma GCC diagnostic ignored "-Wattributes"
  78. __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
  79. #pragma GCC diagnostic pop
  80. #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
  81. #endif
  82. #ifndef __UNALIGNED_UINT32_WRITE
  83. #pragma GCC diagnostic push
  84. #pragma GCC diagnostic ignored "-Wpacked"
  85. #pragma GCC diagnostic ignored "-Wattributes"
  86. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  87. #pragma GCC diagnostic pop
  88. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  89. #endif
  90. #ifndef __UNALIGNED_UINT32_READ
  91. #pragma GCC diagnostic push
  92. #pragma GCC diagnostic ignored "-Wpacked"
  93. #pragma GCC diagnostic ignored "-Wattributes"
  94. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  95. #pragma GCC diagnostic pop
  96. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  97. #endif
  98. #ifndef __ALIGNED
  99. #define __ALIGNED(x) __attribute__((aligned(x)))
  100. #endif
  101. #ifndef __RESTRICT
  102. #define __RESTRICT __restrict
  103. #endif
  104. #ifndef __COMPILER_BARRIER
  105. #define __COMPILER_BARRIER() __ASM volatile("":::"memory")
  106. #endif
  107. #ifndef __NO_INIT
  108. #define __NO_INIT __attribute__ ((section (".noinit")))
  109. #endif
  110. #ifndef __ALIAS
  111. #define __ALIAS(x) __attribute__ ((alias(x)))
  112. #endif
  113. /* ########################## Core Instruction Access ######################### */
  114. /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface
  115. Access to dedicated instructions
  116. @{
  117. */
  118. /* Define macros for porting to both thumb1 and thumb2.
  119. * For thumb1, use low register (r0-r7), specified by constraint "l"
  120. * Otherwise, use general registers, specified by constraint "r" */
  121. #if defined (__thumb__) && !defined (__thumb2__)
  122. #define __CMSIS_GCC_OUT_REG(r) "=l" (r)
  123. #define __CMSIS_GCC_RW_REG(r) "+l" (r)
  124. #define __CMSIS_GCC_USE_REG(r) "l" (r)
  125. #else
  126. #define __CMSIS_GCC_OUT_REG(r) "=r" (r)
  127. #define __CMSIS_GCC_RW_REG(r) "+r" (r)
  128. #define __CMSIS_GCC_USE_REG(r) "r" (r)
  129. #endif
  130. /**
  131. \brief No Operation
  132. \details No Operation does nothing. This instruction can be used for code alignment purposes.
  133. */
  134. #define __NOP() __ASM volatile ("nop")
  135. /**
  136. \brief Wait For Interrupt
  137. \details Wait For Interrupt is a hint instruction that suspends execution until one of a number of events occurs.
  138. */
  139. #define __WFI() __ASM volatile ("wfi":::"memory")
  140. /**
  141. \brief Wait For Event
  142. \details Wait For Event is a hint instruction that permits the processor to enter
  143. a low-power state until one of a number of events occurs.
  144. */
  145. #define __WFE() __ASM volatile ("wfe":::"memory")
  146. /**
  147. \brief Send Event
  148. \details Send Event is a hint instruction. It causes an event to be signaled to the CPU.
  149. */
  150. #define __SEV() __ASM volatile ("sev")
  151. /**
  152. \brief Instruction Synchronization Barrier
  153. \details Instruction Synchronization Barrier flushes the pipeline in the processor,
  154. so that all instructions following the ISB are fetched from cache or memory,
  155. after the instruction has been completed.
  156. */
  157. __STATIC_FORCEINLINE void __ISB(void)
  158. {
  159. __ASM volatile ("isb 0xF":::"memory");
  160. }
  161. /**
  162. \brief Data Synchronization Barrier
  163. \details Acts as a special kind of Data Memory Barrier.
  164. It completes when all explicit memory accesses before this instruction complete.
  165. */
  166. __STATIC_FORCEINLINE void __DSB(void)
  167. {
  168. __ASM volatile ("dsb 0xF":::"memory");
  169. }
  170. /**
  171. \brief Data Memory Barrier
  172. \details Ensures the apparent order of the explicit memory operations before
  173. and after the instruction, without ensuring their completion.
  174. */
  175. __STATIC_FORCEINLINE void __DMB(void)
  176. {
  177. __ASM volatile ("dmb 0xF":::"memory");
  178. }
  179. /**
  180. \brief Reverse byte order (32 bit)
  181. \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
  182. \param [in] value Value to reverse
  183. \return Reversed value
  184. */
  185. __STATIC_FORCEINLINE uint32_t __REV(uint32_t value)
  186. {
  187. return __builtin_bswap32(value);
  188. }
  189. /**
  190. \brief Reverse byte order (16 bit)
  191. \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
  192. \param [in] value Value to reverse
  193. \return Reversed value
  194. */
  195. __STATIC_FORCEINLINE uint32_t __REV16(uint32_t value)
  196. {
  197. uint32_t result;
  198. __ASM ("rev16 %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  199. return (result);
  200. }
  201. /**
  202. \brief Reverse byte order (16 bit)
  203. \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
  204. \param [in] value Value to reverse
  205. \return Reversed value
  206. */
  207. __STATIC_FORCEINLINE int16_t __REVSH(int16_t value)
  208. {
  209. return (int16_t)__builtin_bswap16(value);
  210. }
  211. /**
  212. \brief Rotate Right in unsigned value (32 bit)
  213. \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  214. \param [in] op1 Value to rotate
  215. \param [in] op2 Number of Bits to rotate
  216. \return Rotated value
  217. */
  218. __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
  219. {
  220. op2 %= 32U;
  221. if (op2 == 0U)
  222. {
  223. return op1;
  224. }
  225. return (op1 >> op2) | (op1 << (32U - op2));
  226. }
  227. /**
  228. \brief Breakpoint
  229. \details Causes the processor to enter Debug state.
  230. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  231. \param [in] value is ignored by the processor.
  232. If required, a debugger can use it to store additional information about the breakpoint.
  233. */
  234. #define __BKPT(value) __ASM volatile ("bkpt "#value)
  235. /**
  236. \brief Reverse bit order of value
  237. \details Reverses the bit order of the given value.
  238. \param [in] value Value to reverse
  239. \return Reversed value
  240. */
  241. __STATIC_FORCEINLINE uint32_t __RBIT(uint32_t value)
  242. {
  243. uint32_t result;
  244. #if (__ARM_ARCH_ISA_THUMB >= 2)
  245. __ASM ("rbit %0, %1" : "=r" (result) : "r" (value) );
  246. #else
  247. uint32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
  248. result = value; /* r will be reversed bits of v; first get LSB of v */
  249. for (value >>= 1U; value != 0U; value >>= 1U)
  250. {
  251. result <<= 1U;
  252. result |= value & 1U;
  253. s--;
  254. }
  255. result <<= s; /* shift when v's highest bits are zero */
  256. #endif
  257. return (result);
  258. }
  259. /**
  260. \brief Count leading zeros
  261. \details Counts the number of leading zeros of a data value.
  262. \param [in] value Value to count the leading zeros
  263. \return number of leading zeros in value
  264. */
  265. __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
  266. {
  267. /* Even though __builtin_clz produces a CLZ instruction on ARM, formally
  268. __builtin_clz(0) is undefined behaviour, so handle this case specially.
  269. This guarantees ARM-compatible results if happening to compile on a non-ARM
  270. target, and ensures the compiler doesn't decide to activate any
  271. optimisations using the logic "value was passed to __builtin_clz, so it
  272. is non-zero".
  273. ARM GCC 7.3 and possibly earlier will optimise this test away, leaving a
  274. single CLZ instruction.
  275. */
  276. if (value == 0U)
  277. {
  278. return 32U;
  279. }
  280. return __builtin_clz(value);
  281. }
  282. #if (__ARM_FEATURE_SAT >= 1)
  283. /**
  284. \brief Signed Saturate
  285. \details Saturates a signed value.
  286. \param [in] value Value to be saturated
  287. \param [in] sat Bit position to saturate to (1..32)
  288. \return Saturated value
  289. */
  290. #define __SSAT(value, sat) __ssat(value, sat)
  291. /**
  292. \brief Unsigned Saturate
  293. \details Saturates an unsigned value.
  294. \param [in] value Value to be saturated
  295. \param [in] sat Bit position to saturate to (0..31)
  296. \return Saturated value
  297. */
  298. #define __USAT(value, sat) __usat(value, sat)
  299. #else /* (__ARM_FEATURE_SAT >= 1) */
  300. /**
  301. \brief Signed Saturate
  302. \details Saturates a signed value.
  303. \param [in] value Value to be saturated
  304. \param [in] sat Bit position to saturate to (1..32)
  305. \return Saturated value
  306. */
  307. __STATIC_FORCEINLINE int32_t __SSAT(int32_t val, uint32_t sat)
  308. {
  309. if ((sat >= 1U) && (sat <= 32U))
  310. {
  311. const int32_t max = (int32_t)((1U << (sat - 1U)) - 1U);
  312. const int32_t min = -1 - max ;
  313. if (val > max)
  314. {
  315. return (max);
  316. }
  317. else if (val < min)
  318. {
  319. return (min);
  320. }
  321. }
  322. return (val);
  323. }
  324. /**
  325. \brief Unsigned Saturate
  326. \details Saturates an unsigned value.
  327. \param [in] value Value to be saturated
  328. \param [in] sat Bit position to saturate to (0..31)
  329. \return Saturated value
  330. */
  331. __STATIC_FORCEINLINE uint32_t __USAT(int32_t val, uint32_t sat)
  332. {
  333. if (sat <= 31U)
  334. {
  335. const uint32_t max = ((1U << sat) - 1U);
  336. if (val > (int32_t)max)
  337. {
  338. return (max);
  339. }
  340. else if (val < 0)
  341. {
  342. return (0U);
  343. }
  344. }
  345. return ((uint32_t)val);
  346. }
  347. #endif /* (__ARM_FEATURE_SAT >= 1) */
  348. #if (__ARM_FEATURE_LDREX >= 1)
  349. /**
  350. \brief Remove the exclusive lock
  351. \details Removes the exclusive lock which is created by LDREX.
  352. */
  353. __STATIC_FORCEINLINE void __CLREX(void)
  354. {
  355. __ASM volatile ("clrex" ::: "memory");
  356. }
  357. /**
  358. \brief LDR Exclusive (8 bit)
  359. \details Executes a exclusive LDR instruction for 8 bit value.
  360. \param [in] ptr Pointer to data
  361. \return value of type uint8_t at (*ptr)
  362. */
  363. __STATIC_FORCEINLINE uint8_t __LDREXB(volatile uint8_t *addr)
  364. {
  365. uint32_t result;
  366. __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
  367. return ((uint8_t) result); /* Add explicit type cast here */
  368. }
  369. /**
  370. \brief STR Exclusive (8 bit)
  371. \details Executes a exclusive STR instruction for 8 bit values.
  372. \param [in] value Value to store
  373. \param [in] ptr Pointer to location
  374. \return 0 Function succeeded
  375. \return 1 Function failed
  376. */
  377. __STATIC_FORCEINLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
  378. {
  379. uint32_t result;
  380. __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
  381. return (result);
  382. }
  383. #endif /* (__ARM_FEATURE_LDREX >= 1) */
  384. #if (__ARM_FEATURE_LDREX >= 2)
  385. /**
  386. \brief LDR Exclusive (16 bit)
  387. \details Executes a exclusive LDR instruction for 16 bit values.
  388. \param [in] ptr Pointer to data
  389. \return value of type uint16_t at (*ptr)
  390. */
  391. __STATIC_FORCEINLINE uint16_t __LDREXH(volatile uint16_t *addr)
  392. {
  393. uint32_t result;
  394. __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
  395. return ((uint16_t)result); /* Add explicit type cast here */
  396. }
  397. /**
  398. \brief STR Exclusive (16 bit)
  399. \details Executes a exclusive STR instruction for 16 bit values.
  400. \param [in] value Value to store
  401. \param [in] ptr Pointer to location
  402. \return 0 Function succeeded
  403. \return 1 Function failed
  404. */
  405. __STATIC_FORCEINLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
  406. {
  407. uint32_t result;
  408. __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
  409. return (result);
  410. }
  411. #endif /* (__ARM_FEATURE_LDREX >= 2) */
  412. #if (__ARM_FEATURE_LDREX >= 4)
  413. /**
  414. \brief LDR Exclusive (32 bit)
  415. \details Executes a exclusive LDR instruction for 32 bit values.
  416. \param [in] ptr Pointer to data
  417. \return value of type uint32_t at (*ptr)
  418. */
  419. __STATIC_FORCEINLINE uint32_t __LDREXW(volatile uint32_t *addr)
  420. {
  421. uint32_t result;
  422. __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
  423. return (result);
  424. }
  425. /**
  426. \brief STR Exclusive (32 bit)
  427. \details Executes a exclusive STR instruction for 32 bit values.
  428. \param [in] value Value to store
  429. \param [in] ptr Pointer to location
  430. \return 0 Function succeeded
  431. \return 1 Function failed
  432. */
  433. __STATIC_FORCEINLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
  434. {
  435. uint32_t result;
  436. __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
  437. return (result);
  438. }
  439. #endif /* (__ARM_FEATURE_LDREX >= 4) */
  440. #if (__ARM_ARCH_ISA_THUMB >= 2)
  441. /**
  442. \brief Rotate Right with Extend (32 bit)
  443. \details Moves each bit of a bitstring right by one bit.
  444. The carry input is shifted in at the left end of the bitstring.
  445. \param [in] value Value to rotate
  446. \return Rotated value
  447. */
  448. __STATIC_FORCEINLINE uint32_t __RRX(uint32_t value)
  449. {
  450. uint32_t result;
  451. __ASM volatile ("rrx %0, %1" : "=r" (result) : "r" (value));
  452. return (result);
  453. }
  454. /**
  455. \brief LDRT Unprivileged (8 bit)
  456. \details Executes a Unprivileged LDRT instruction for 8 bit value.
  457. \param [in] ptr Pointer to data
  458. \return value of type uint8_t at (*ptr)
  459. */
  460. __STATIC_FORCEINLINE uint8_t __LDRBT(volatile uint8_t *ptr)
  461. {
  462. uint32_t result;
  463. __ASM volatile ("ldrbt %0, %1" : "=r" (result) : "Q" (*ptr) );
  464. return ((uint8_t)result); /* Add explicit type cast here */
  465. }
  466. /**
  467. \brief LDRT Unprivileged (16 bit)
  468. \details Executes a Unprivileged LDRT instruction for 16 bit values.
  469. \param [in] ptr Pointer to data
  470. \return value of type uint16_t at (*ptr)
  471. */
  472. __STATIC_FORCEINLINE uint16_t __LDRHT(volatile uint16_t *ptr)
  473. {
  474. uint32_t result;
  475. __ASM volatile ("ldrht %0, %1" : "=r" (result) : "Q" (*ptr) );
  476. return ((uint16_t)result); /* Add explicit type cast here */
  477. }
  478. /**
  479. \brief LDRT Unprivileged (32 bit)
  480. \details Executes a Unprivileged LDRT instruction for 32 bit values.
  481. \param [in] ptr Pointer to data
  482. \return value of type uint32_t at (*ptr)
  483. */
  484. __STATIC_FORCEINLINE uint32_t __LDRT(volatile uint32_t *ptr)
  485. {
  486. uint32_t result;
  487. __ASM volatile ("ldrt %0, %1" : "=r" (result) : "Q" (*ptr) );
  488. return (result);
  489. }
  490. /**
  491. \brief STRT Unprivileged (8 bit)
  492. \details Executes a Unprivileged STRT instruction for 8 bit values.
  493. \param [in] value Value to store
  494. \param [in] ptr Pointer to location
  495. */
  496. __STATIC_FORCEINLINE void __STRBT(uint8_t value, volatile uint8_t *ptr)
  497. {
  498. __ASM volatile ("strbt %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) );
  499. }
  500. /**
  501. \brief STRT Unprivileged (16 bit)
  502. \details Executes a Unprivileged STRT instruction for 16 bit values.
  503. \param [in] value Value to store
  504. \param [in] ptr Pointer to location
  505. */
  506. __STATIC_FORCEINLINE void __STRHT(uint16_t value, volatile uint16_t *ptr)
  507. {
  508. __ASM volatile ("strht %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) );
  509. }
  510. /**
  511. \brief STRT Unprivileged (32 bit)
  512. \details Executes a Unprivileged STRT instruction for 32 bit values.
  513. \param [in] value Value to store
  514. \param [in] ptr Pointer to location
  515. */
  516. __STATIC_FORCEINLINE void __STRT(uint32_t value, volatile uint32_t *ptr)
  517. {
  518. __ASM volatile ("strt %1, %0" : "=Q" (*ptr) : "r" (value) );
  519. }
  520. #endif /* (__ARM_ARCH_ISA_THUMB >= 2) */
  521. #if (__ARM_ARCH >= 8)
  522. /**
  523. \brief Load-Acquire (8 bit)
  524. \details Executes a LDAB instruction for 8 bit value.
  525. \param [in] ptr Pointer to data
  526. \return value of type uint8_t at (*ptr)
  527. */
  528. __STATIC_FORCEINLINE uint8_t __LDAB(volatile uint8_t *ptr)
  529. {
  530. uint32_t result;
  531. __ASM volatile ("ldab %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  532. return ((uint8_t)result); /* Add explicit type cast here */
  533. }
  534. /**
  535. \brief Load-Acquire (16 bit)
  536. \details Executes a LDAH instruction for 16 bit values.
  537. \param [in] ptr Pointer to data
  538. \return value of type uint16_t at (*ptr)
  539. */
  540. __STATIC_FORCEINLINE uint16_t __LDAH(volatile uint16_t *ptr)
  541. {
  542. uint32_t result;
  543. __ASM volatile ("ldah %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  544. return ((uint16_t)result); /* Add explicit type cast here */
  545. }
  546. /**
  547. \brief Load-Acquire (32 bit)
  548. \details Executes a LDA instruction for 32 bit values.
  549. \param [in] ptr Pointer to data
  550. \return value of type uint32_t at (*ptr)
  551. */
  552. __STATIC_FORCEINLINE uint32_t __LDA(volatile uint32_t *ptr)
  553. {
  554. uint32_t result;
  555. __ASM volatile ("lda %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  556. return (result);
  557. }
  558. /**
  559. \brief Store-Release (8 bit)
  560. \details Executes a STLB instruction for 8 bit values.
  561. \param [in] value Value to store
  562. \param [in] ptr Pointer to location
  563. */
  564. __STATIC_FORCEINLINE void __STLB(uint8_t value, volatile uint8_t *ptr)
  565. {
  566. __ASM volatile ("stlb %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  567. }
  568. /**
  569. \brief Store-Release (16 bit)
  570. \details Executes a STLH instruction for 16 bit values.
  571. \param [in] value Value to store
  572. \param [in] ptr Pointer to location
  573. */
  574. __STATIC_FORCEINLINE void __STLH(uint16_t value, volatile uint16_t *ptr)
  575. {
  576. __ASM volatile ("stlh %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  577. }
  578. /**
  579. \brief Store-Release (32 bit)
  580. \details Executes a STL instruction for 32 bit values.
  581. \param [in] value Value to store
  582. \param [in] ptr Pointer to location
  583. */
  584. __STATIC_FORCEINLINE void __STL(uint32_t value, volatile uint32_t *ptr)
  585. {
  586. __ASM volatile ("stl %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  587. }
  588. /**
  589. \brief Load-Acquire Exclusive (8 bit)
  590. \details Executes a LDAB exclusive instruction for 8 bit value.
  591. \param [in] ptr Pointer to data
  592. \return value of type uint8_t at (*ptr)
  593. */
  594. __STATIC_FORCEINLINE uint8_t __LDAEXB(volatile uint8_t *ptr)
  595. {
  596. uint32_t result;
  597. __ASM volatile ("ldaexb %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  598. return ((uint8_t)result); /* Add explicit type cast here */
  599. }
  600. /**
  601. \brief Load-Acquire Exclusive (16 bit)
  602. \details Executes a LDAH exclusive instruction for 16 bit values.
  603. \param [in] ptr Pointer to data
  604. \return value of type uint16_t at (*ptr)
  605. */
  606. __STATIC_FORCEINLINE uint16_t __LDAEXH(volatile uint16_t *ptr)
  607. {
  608. uint32_t result;
  609. __ASM volatile ("ldaexh %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  610. return ((uint16_t)result); /* Add explicit type cast here */
  611. }
  612. /**
  613. \brief Load-Acquire Exclusive (32 bit)
  614. \details Executes a LDA exclusive instruction for 32 bit values.
  615. \param [in] ptr Pointer to data
  616. \return value of type uint32_t at (*ptr)
  617. */
  618. __STATIC_FORCEINLINE uint32_t __LDAEX(volatile uint32_t *ptr)
  619. {
  620. uint32_t result;
  621. __ASM volatile ("ldaex %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  622. return (result);
  623. }
  624. /**
  625. \brief Store-Release Exclusive (8 bit)
  626. \details Executes a STLB exclusive instruction for 8 bit values.
  627. \param [in] value Value to store
  628. \param [in] ptr Pointer to location
  629. \return 0 Function succeeded
  630. \return 1 Function failed
  631. */
  632. __STATIC_FORCEINLINE uint32_t __STLEXB(uint8_t value, volatile uint8_t *ptr)
  633. {
  634. uint32_t result;
  635. __ASM volatile ("stlexb %0, %2, %1" : "=&r" (result), "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  636. return (result);
  637. }
  638. /**
  639. \brief Store-Release Exclusive (16 bit)
  640. \details Executes a STLH exclusive instruction for 16 bit values.
  641. \param [in] value Value to store
  642. \param [in] ptr Pointer to location
  643. \return 0 Function succeeded
  644. \return 1 Function failed
  645. */
  646. __STATIC_FORCEINLINE uint32_t __STLEXH(uint16_t value, volatile uint16_t *ptr)
  647. {
  648. uint32_t result;
  649. __ASM volatile ("stlexh %0, %2, %1" : "=&r" (result), "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  650. return (result);
  651. }
  652. /**
  653. \brief Store-Release Exclusive (32 bit)
  654. \details Executes a STL exclusive instruction for 32 bit values.
  655. \param [in] value Value to store
  656. \param [in] ptr Pointer to location
  657. \return 0 Function succeeded
  658. \return 1 Function failed
  659. */
  660. __STATIC_FORCEINLINE uint32_t __STLEX(uint32_t value, volatile uint32_t *ptr)
  661. {
  662. uint32_t result;
  663. __ASM volatile ("stlex %0, %2, %1" : "=&r" (result), "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  664. return (result);
  665. }
  666. #endif /* (__ARM_ARCH >= 8) */
  667. /** @}*/ /* end of group CMSIS_Core_InstructionInterface */
  668. /* ########################### Core Function Access ########################### */
  669. /** \ingroup CMSIS_Core_FunctionInterface
  670. \defgroup CMSIS_Core_RegAccFunctions CMSIS Core Register Access Functions
  671. @{
  672. */
  673. /**
  674. \brief Enable IRQ Interrupts
  675. \details Enables IRQ interrupts by clearing special-purpose register PRIMASK.
  676. Can only be executed in Privileged modes.
  677. */
  678. __STATIC_FORCEINLINE void __enable_irq(void)
  679. {
  680. __ASM volatile ("cpsie i" : : : "memory");
  681. }
  682. /**
  683. \brief Disable IRQ Interrupts
  684. \details Disables IRQ interrupts by setting special-purpose register PRIMASK.
  685. Can only be executed in Privileged modes.
  686. */
  687. __STATIC_FORCEINLINE void __disable_irq(void)
  688. {
  689. __ASM volatile ("cpsid i" : : : "memory");
  690. }
  691. #if (__ARM_ARCH_ISA_THUMB >= 2)
  692. /**
  693. \brief Enable FIQ
  694. \details Enables FIQ interrupts by clearing special-purpose register FAULTMASK.
  695. Can only be executed in Privileged modes.
  696. */
  697. __STATIC_FORCEINLINE void __enable_fault_irq(void)
  698. {
  699. __ASM volatile ("cpsie f" : : : "memory");
  700. }
  701. /**
  702. \brief Disable FIQ
  703. \details Disables FIQ interrupts by setting special-purpose register FAULTMASK.
  704. Can only be executed in Privileged modes.
  705. */
  706. __STATIC_FORCEINLINE void __disable_fault_irq(void)
  707. {
  708. __ASM volatile ("cpsid f" : : : "memory");
  709. }
  710. #endif
  711. /**
  712. \brief Get FPSCR
  713. \details Returns the current value of the Floating Point Status/Control register.
  714. \return Floating Point Status/Control register value
  715. */
  716. __STATIC_FORCEINLINE uint32_t __get_FPSCR(void)
  717. {
  718. #if (defined(__ARM_FP) && (__ARM_FP >= 1))
  719. return (__builtin_arm_get_fpscr());
  720. #else
  721. return (0U);
  722. #endif
  723. }
  724. /**
  725. \brief Set FPSCR
  726. \details Assigns the given value to the Floating Point Status/Control register.
  727. \param [in] fpscr Floating Point Status/Control value to set
  728. */
  729. __STATIC_FORCEINLINE void __set_FPSCR(uint32_t fpscr)
  730. {
  731. #if (defined(__ARM_FP) && (__ARM_FP >= 1))
  732. __builtin_arm_set_fpscr(fpscr);
  733. #else
  734. (void)fpscr;
  735. #endif
  736. }
  737. /** @} end of CMSIS_Core_RegAccFunctions */
  738. /* ################### Compiler specific Intrinsics ########################### */
  739. /** \defgroup CMSIS_SIMD_intrinsics CMSIS SIMD Intrinsics
  740. Access to dedicated SIMD instructions
  741. @{
  742. */
  743. #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
  744. #define __SADD8 __sadd8
  745. #define __QADD8 __qadd8
  746. #define __SHADD8 __shadd8
  747. #define __UADD8 __uadd8
  748. #define __UQADD8 __uqadd8
  749. #define __UHADD8 __uhadd8
  750. #define __SSUB8 __ssub8
  751. #define __QSUB8 __qsub8
  752. #define __SHSUB8 __shsub8
  753. #define __USUB8 __usub8
  754. #define __UQSUB8 __uqsub8
  755. #define __UHSUB8 __uhsub8
  756. #define __SADD16 __sadd16
  757. #define __QADD16 __qadd16
  758. #define __SHADD16 __shadd16
  759. #define __UADD16 __uadd16
  760. #define __UQADD16 __uqadd16
  761. #define __UHADD16 __uhadd16
  762. #define __SSUB16 __ssub16
  763. #define __QSUB16 __qsub16
  764. #define __SHSUB16 __shsub16
  765. #define __USUB16 __usub16
  766. #define __UQSUB16 __uqsub16
  767. #define __UHSUB16 __uhsub16
  768. #define __SASX __sasx
  769. #define __QASX __qasx
  770. #define __SHASX __shasx
  771. #define __UASX __uasx
  772. #define __UQASX __uqasx
  773. #define __UHASX __uhasx
  774. #define __SSAX __ssax
  775. #define __QSAX __qsax
  776. #define __SHSAX __shsax
  777. #define __USAX __usax
  778. #define __UQSAX __uqsax
  779. #define __UHSAX __uhsax
  780. #define __USAD8 __usad8
  781. #define __USADA8 __usada8
  782. #define __SSAT16 __ssat16
  783. #define __USAT16 __usat16
  784. #define __UXTB16 __uxtb16
  785. #define __UXTAB16 __uxtab16
  786. #define __SXTB16 __sxtb16
  787. #define __SXTAB16 __sxtab16
  788. #define __SMUAD __smuad
  789. #define __SMUADX __smuadx
  790. #define __SMLAD __smlad
  791. #define __SMLADX __smladx
  792. #define __SMLALD __smlald
  793. #define __SMLALDX __smlaldx
  794. #define __SMUSD __smusd
  795. #define __SMUSDX __smusdx
  796. #define __SMLSD __smlsd
  797. #define __SMLSDX __smlsdx
  798. #define __SMLSLD __smlsld
  799. #define __SMLSLDX __smlsldx
  800. #define __SEL __sel
  801. #define __QADD __qadd
  802. #define __QSUB __qsub
  803. #define __PKHBT(ARG1,ARG2,ARG3) \
  804. __extension__ \
  805. ({ \
  806. uint32_t __RES, __ARG1 = (ARG1), __ARG2 = (ARG2); \
  807. __ASM ("pkhbt %0, %1, %2, lsl %3" : "=r" (__RES) : "r" (__ARG1), "r" (__ARG2), "I" (ARG3) ); \
  808. __RES; \
  809. })
  810. #define __PKHTB(ARG1,ARG2,ARG3) \
  811. __extension__ \
  812. ({ \
  813. uint32_t __RES, __ARG1 = (ARG1), __ARG2 = (ARG2); \
  814. if (ARG3 == 0) \
  815. __ASM ("pkhtb %0, %1, %2" : "=r" (__RES) : "r" (__ARG1), "r" (__ARG2) ); \
  816. else \
  817. __ASM ("pkhtb %0, %1, %2, asr %3" : "=r" (__RES) : "r" (__ARG1), "r" (__ARG2), "I" (ARG3) ); \
  818. __RES; \
  819. })
  820. __STATIC_FORCEINLINE uint32_t __SXTB16_RORn(uint32_t op1, uint32_t rotate)
  821. {
  822. uint32_t result;
  823. if (__builtin_constant_p(rotate) && ((rotate == 8U) || (rotate == 16U) || (rotate == 24U)))
  824. {
  825. __ASM volatile("sxtb16 %0, %1, ROR %2" : "=r"(result) : "r"(op1), "i"(rotate));
  826. }
  827. else
  828. {
  829. result = __SXTB16(__ROR(op1, rotate));
  830. }
  831. return result;
  832. }
  833. __STATIC_FORCEINLINE uint32_t __SXTAB16_RORn(uint32_t op1, uint32_t op2, uint32_t rotate)
  834. {
  835. uint32_t result;
  836. if (__builtin_constant_p(rotate) && ((rotate == 8U) || (rotate == 16U) || (rotate == 24U)))
  837. {
  838. __ASM volatile("sxtab16 %0, %1, %2, ROR %3" : "=r"(result) : "r"(op1), "r"(op2), "i"(rotate));
  839. }
  840. else
  841. {
  842. result = __SXTAB16(op1, __ROR(op2, rotate));
  843. }
  844. return result;
  845. }
  846. __STATIC_FORCEINLINE int32_t __SMMLA (int32_t op1, int32_t op2, int32_t op3)
  847. {
  848. int32_t result;
  849. __ASM volatile ("smmla %0, %1, %2, %3" : "=r" (result): "r" (op1), "r" (op2), "r" (op3) );
  850. return (result);
  851. }
  852. #endif /* (__ARM_FEATURE_DSP == 1) */
  853. /** @} end of group CMSIS_SIMD_intrinsics */
  854. // Include the profile specific settings:
  855. #if __ARM_ARCH_PROFILE == 'A'
  856. #include "a-profile/cmsis_gcc_a.h"
  857. #elif __ARM_ARCH_PROFILE == 'R'
  858. #include "r-profile/cmsis_gcc_r.h"
  859. #elif __ARM_ARCH_PROFILE == 'M'
  860. #include "m-profile/cmsis_gcc_m.h"
  861. #else
  862. #error "Unknown Arm architecture profile"
  863. #endif
  864. #endif /* __CMSIS_GCC_H */