cmsis_armclang.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. /**************************************************************************//**
  2. * @file cmsis_armclang.h
  3. * @brief CMSIS compiler armclang (Arm Compiler 6) header file
  4. * @version V5.3.2
  5. * @date 16. December 2020
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2009-2020 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. /*lint -esym(9058, IRQn)*/ /* disable MISRA 2012 Rule 2.4 for IRQn */
  25. #ifndef __CMSIS_ARMCLANG_H
  26. #define __CMSIS_ARMCLANG_H
  27. #pragma clang system_header /* treat file as system include file */
  28. #ifndef __ARM_COMPAT_H
  29. #include <arm_compat.h> /* Compatibility header for Arm Compiler 5 intrinsics */
  30. #endif
  31. /* CMSIS compiler specific defines */
  32. #ifndef __ASM
  33. #define __ASM __asm
  34. #endif
  35. #ifndef __INLINE
  36. #define __INLINE __inline
  37. #endif
  38. #ifndef __STATIC_INLINE
  39. #define __STATIC_INLINE static __inline
  40. #endif
  41. #ifndef __STATIC_FORCEINLINE
  42. #define __STATIC_FORCEINLINE __attribute__((always_inline)) static __inline
  43. #endif
  44. #ifndef __NO_RETURN
  45. #define __NO_RETURN __attribute__((__noreturn__))
  46. #endif
  47. #ifndef __USED
  48. #define __USED __attribute__((used))
  49. #endif
  50. #ifndef __WEAK
  51. #define __WEAK __attribute__((weak))
  52. #endif
  53. #ifndef __PACKED
  54. #define __PACKED __attribute__((packed, aligned(1)))
  55. #endif
  56. #ifndef __PACKED_STRUCT
  57. #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
  58. #endif
  59. #ifndef __PACKED_UNION
  60. #define __PACKED_UNION union __attribute__((packed, aligned(1)))
  61. #endif
  62. #ifndef __UNALIGNED_UINT32 /* deprecated */
  63. #pragma clang diagnostic push
  64. #pragma clang diagnostic ignored "-Wpacked"
  65. /*lint -esym(9058, T_UINT32)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32 */
  66. struct __attribute__((packed)) T_UINT32 { uint32_t v; };
  67. #pragma clang diagnostic pop
  68. #define __UNALIGNED_UINT32(x) (((struct T_UINT32 *)(x))->v)
  69. #endif
  70. #ifndef __UNALIGNED_UINT16_WRITE
  71. #pragma clang diagnostic push
  72. #pragma clang diagnostic ignored "-Wpacked"
  73. /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
  74. __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
  75. #pragma clang diagnostic pop
  76. #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
  77. #endif
  78. #ifndef __UNALIGNED_UINT16_READ
  79. #pragma clang diagnostic push
  80. #pragma clang diagnostic ignored "-Wpacked"
  81. /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
  82. __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
  83. #pragma clang diagnostic pop
  84. #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
  85. #endif
  86. #ifndef __UNALIGNED_UINT32_WRITE
  87. #pragma clang diagnostic push
  88. #pragma clang diagnostic ignored "-Wpacked"
  89. /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
  90. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  91. #pragma clang diagnostic pop
  92. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  93. #endif
  94. #ifndef __UNALIGNED_UINT32_READ
  95. #pragma clang diagnostic push
  96. #pragma clang diagnostic ignored "-Wpacked"
  97. /*lint -esym(9058, T_UINT32_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_READ */
  98. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  99. #pragma clang diagnostic pop
  100. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  101. #endif
  102. #ifndef __ALIGNED
  103. #define __ALIGNED(x) __attribute__((aligned(x)))
  104. #endif
  105. #ifndef __RESTRICT
  106. #define __RESTRICT __restrict
  107. #endif
  108. #ifndef __COMPILER_BARRIER
  109. #define __COMPILER_BARRIER() __ASM volatile("":::"memory")
  110. #endif
  111. /* ######################### Startup and Lowlevel Init ######################## */
  112. #ifndef __PROGRAM_START
  113. #define __PROGRAM_START __main
  114. #endif
  115. #ifndef __INITIAL_SP
  116. #define __INITIAL_SP Image$$ARM_LIB_STACK$$ZI$$Limit
  117. #endif
  118. #ifndef __STACK_LIMIT
  119. #define __STACK_LIMIT Image$$ARM_LIB_STACK$$ZI$$Base
  120. #endif
  121. #ifndef __VECTOR_TABLE
  122. #define __VECTOR_TABLE __Vectors
  123. #endif
  124. #ifndef __VECTOR_TABLE_ATTRIBUTE
  125. #define __VECTOR_TABLE_ATTRIBUTE __attribute__((used, section("RESET")))
  126. #endif
  127. #if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
  128. #ifndef __STACK_SEAL
  129. #define __STACK_SEAL Image$$STACKSEAL$$ZI$$Base
  130. #endif
  131. #ifndef __TZ_STACK_SEAL_SIZE
  132. #define __TZ_STACK_SEAL_SIZE 8U
  133. #endif
  134. #ifndef __TZ_STACK_SEAL_VALUE
  135. #define __TZ_STACK_SEAL_VALUE 0xFEF5EDA5FEF5EDA5ULL
  136. #endif
  137. __STATIC_FORCEINLINE void __TZ_set_STACKSEAL_S (uint32_t* stackTop) {
  138. *((uint64_t *)stackTop) = __TZ_STACK_SEAL_VALUE;
  139. }
  140. #endif
  141. /* ########################### Core Function Access ########################### */
  142. /** \ingroup CMSIS_Core_FunctionInterface
  143. \defgroup CMSIS_Core_RegAccFunctions CMSIS Core Register Access Functions
  144. @{
  145. */
  146. /**
  147. \brief Enable IRQ Interrupts
  148. \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
  149. Can only be executed in Privileged modes.
  150. */
  151. /* intrinsic void __enable_irq(); see arm_compat.h */
  152. /**
  153. \brief Disable IRQ Interrupts
  154. \details Disables IRQ interrupts by setting the I-bit in the CPSR.
  155. Can only be executed in Privileged modes.
  156. */
  157. /* intrinsic void __disable_irq(); see arm_compat.h */
  158. /**
  159. \brief Get Control Register
  160. \details Returns the content of the Control Register.
  161. \return Control Register value
  162. */
  163. __STATIC_FORCEINLINE uint32_t __get_CONTROL(void)
  164. {
  165. uint32_t result;
  166. __ASM volatile ("MRS %0, control" : "=r" (result) );
  167. return(result);
  168. }
  169. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  170. /**
  171. \brief Get Control Register (non-secure)
  172. \details Returns the content of the non-secure Control Register when in secure mode.
  173. \return non-secure Control Register value
  174. */
  175. __STATIC_FORCEINLINE uint32_t __TZ_get_CONTROL_NS(void)
  176. {
  177. uint32_t result;
  178. __ASM volatile ("MRS %0, control_ns" : "=r" (result) );
  179. return(result);
  180. }
  181. #endif
  182. /**
  183. \brief Set Control Register
  184. \details Writes the given value to the Control Register.
  185. \param [in] control Control Register value to set
  186. */
  187. __STATIC_FORCEINLINE void __set_CONTROL(uint32_t control)
  188. {
  189. __ASM volatile ("MSR control, %0" : : "r" (control) : "memory");
  190. }
  191. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  192. /**
  193. \brief Set Control Register (non-secure)
  194. \details Writes the given value to the non-secure Control Register when in secure state.
  195. \param [in] control Control Register value to set
  196. */
  197. __STATIC_FORCEINLINE void __TZ_set_CONTROL_NS(uint32_t control)
  198. {
  199. __ASM volatile ("MSR control_ns, %0" : : "r" (control) : "memory");
  200. }
  201. #endif
  202. /**
  203. \brief Get IPSR Register
  204. \details Returns the content of the IPSR Register.
  205. \return IPSR Register value
  206. */
  207. __STATIC_FORCEINLINE uint32_t __get_IPSR(void)
  208. {
  209. uint32_t result;
  210. __ASM volatile ("MRS %0, ipsr" : "=r" (result) );
  211. return(result);
  212. }
  213. /**
  214. \brief Get APSR Register
  215. \details Returns the content of the APSR Register.
  216. \return APSR Register value
  217. */
  218. __STATIC_FORCEINLINE uint32_t __get_APSR(void)
  219. {
  220. uint32_t result;
  221. __ASM volatile ("MRS %0, apsr" : "=r" (result) );
  222. return(result);
  223. }
  224. /**
  225. \brief Get xPSR Register
  226. \details Returns the content of the xPSR Register.
  227. \return xPSR Register value
  228. */
  229. __STATIC_FORCEINLINE uint32_t __get_xPSR(void)
  230. {
  231. uint32_t result;
  232. __ASM volatile ("MRS %0, xpsr" : "=r" (result) );
  233. return(result);
  234. }
  235. /**
  236. \brief Get Process Stack Pointer
  237. \details Returns the current value of the Process Stack Pointer (PSP).
  238. \return PSP Register value
  239. */
  240. __STATIC_FORCEINLINE uint32_t __get_PSP(void)
  241. {
  242. uint32_t result;
  243. __ASM volatile ("MRS %0, psp" : "=r" (result) );
  244. return(result);
  245. }
  246. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  247. /**
  248. \brief Get Process Stack Pointer (non-secure)
  249. \details Returns the current value of the non-secure Process Stack Pointer (PSP) when in secure state.
  250. \return PSP Register value
  251. */
  252. __STATIC_FORCEINLINE uint32_t __TZ_get_PSP_NS(void)
  253. {
  254. uint32_t result;
  255. __ASM volatile ("MRS %0, psp_ns" : "=r" (result) );
  256. return(result);
  257. }
  258. #endif
  259. /**
  260. \brief Set Process Stack Pointer
  261. \details Assigns the given value to the Process Stack Pointer (PSP).
  262. \param [in] topOfProcStack Process Stack Pointer value to set
  263. */
  264. __STATIC_FORCEINLINE void __set_PSP(uint32_t topOfProcStack)
  265. {
  266. __ASM volatile ("MSR psp, %0" : : "r" (topOfProcStack) : );
  267. }
  268. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  269. /**
  270. \brief Set Process Stack Pointer (non-secure)
  271. \details Assigns the given value to the non-secure Process Stack Pointer (PSP) when in secure state.
  272. \param [in] topOfProcStack Process Stack Pointer value to set
  273. */
  274. __STATIC_FORCEINLINE void __TZ_set_PSP_NS(uint32_t topOfProcStack)
  275. {
  276. __ASM volatile ("MSR psp_ns, %0" : : "r" (topOfProcStack) : );
  277. }
  278. #endif
  279. /**
  280. \brief Get Main Stack Pointer
  281. \details Returns the current value of the Main Stack Pointer (MSP).
  282. \return MSP Register value
  283. */
  284. __STATIC_FORCEINLINE uint32_t __get_MSP(void)
  285. {
  286. uint32_t result;
  287. __ASM volatile ("MRS %0, msp" : "=r" (result) );
  288. return(result);
  289. }
  290. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  291. /**
  292. \brief Get Main Stack Pointer (non-secure)
  293. \details Returns the current value of the non-secure Main Stack Pointer (MSP) when in secure state.
  294. \return MSP Register value
  295. */
  296. __STATIC_FORCEINLINE uint32_t __TZ_get_MSP_NS(void)
  297. {
  298. uint32_t result;
  299. __ASM volatile ("MRS %0, msp_ns" : "=r" (result) );
  300. return(result);
  301. }
  302. #endif
  303. /**
  304. \brief Set Main Stack Pointer
  305. \details Assigns the given value to the Main Stack Pointer (MSP).
  306. \param [in] topOfMainStack Main Stack Pointer value to set
  307. */
  308. __STATIC_FORCEINLINE void __set_MSP(uint32_t topOfMainStack)
  309. {
  310. __ASM volatile ("MSR msp, %0" : : "r" (topOfMainStack) : );
  311. }
  312. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  313. /**
  314. \brief Set Main Stack Pointer (non-secure)
  315. \details Assigns the given value to the non-secure Main Stack Pointer (MSP) when in secure state.
  316. \param [in] topOfMainStack Main Stack Pointer value to set
  317. */
  318. __STATIC_FORCEINLINE void __TZ_set_MSP_NS(uint32_t topOfMainStack)
  319. {
  320. __ASM volatile ("MSR msp_ns, %0" : : "r" (topOfMainStack) : );
  321. }
  322. #endif
  323. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  324. /**
  325. \brief Get Stack Pointer (non-secure)
  326. \details Returns the current value of the non-secure Stack Pointer (SP) when in secure state.
  327. \return SP Register value
  328. */
  329. __STATIC_FORCEINLINE uint32_t __TZ_get_SP_NS(void)
  330. {
  331. uint32_t result;
  332. __ASM volatile ("MRS %0, sp_ns" : "=r" (result) );
  333. return(result);
  334. }
  335. /**
  336. \brief Set Stack Pointer (non-secure)
  337. \details Assigns the given value to the non-secure Stack Pointer (SP) when in secure state.
  338. \param [in] topOfStack Stack Pointer value to set
  339. */
  340. __STATIC_FORCEINLINE void __TZ_set_SP_NS(uint32_t topOfStack)
  341. {
  342. __ASM volatile ("MSR sp_ns, %0" : : "r" (topOfStack) : );
  343. }
  344. #endif
  345. /**
  346. \brief Get Priority Mask
  347. \details Returns the current state of the priority mask bit from the Priority Mask Register.
  348. \return Priority Mask value
  349. */
  350. __STATIC_FORCEINLINE uint32_t __get_PRIMASK(void)
  351. {
  352. uint32_t result;
  353. __ASM volatile ("MRS %0, primask" : "=r" (result) );
  354. return(result);
  355. }
  356. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  357. /**
  358. \brief Get Priority Mask (non-secure)
  359. \details Returns the current state of the non-secure priority mask bit from the Priority Mask Register when in secure state.
  360. \return Priority Mask value
  361. */
  362. __STATIC_FORCEINLINE uint32_t __TZ_get_PRIMASK_NS(void)
  363. {
  364. uint32_t result;
  365. __ASM volatile ("MRS %0, primask_ns" : "=r" (result) );
  366. return(result);
  367. }
  368. #endif
  369. /**
  370. \brief Set Priority Mask
  371. \details Assigns the given value to the Priority Mask Register.
  372. \param [in] priMask Priority Mask
  373. */
  374. __STATIC_FORCEINLINE void __set_PRIMASK(uint32_t priMask)
  375. {
  376. __ASM volatile ("MSR primask, %0" : : "r" (priMask) : "memory");
  377. }
  378. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  379. /**
  380. \brief Set Priority Mask (non-secure)
  381. \details Assigns the given value to the non-secure Priority Mask Register when in secure state.
  382. \param [in] priMask Priority Mask
  383. */
  384. __STATIC_FORCEINLINE void __TZ_set_PRIMASK_NS(uint32_t priMask)
  385. {
  386. __ASM volatile ("MSR primask_ns, %0" : : "r" (priMask) : "memory");
  387. }
  388. #endif
  389. #if ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
  390. (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
  391. (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  392. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) )
  393. /**
  394. \brief Enable FIQ
  395. \details Enables FIQ interrupts by clearing the F-bit in the CPSR.
  396. Can only be executed in Privileged modes.
  397. */
  398. #define __enable_fault_irq __enable_fiq /* see arm_compat.h */
  399. /**
  400. \brief Disable FIQ
  401. \details Disables FIQ interrupts by setting the F-bit in the CPSR.
  402. Can only be executed in Privileged modes.
  403. */
  404. #define __disable_fault_irq __disable_fiq /* see arm_compat.h */
  405. /**
  406. \brief Get Base Priority
  407. \details Returns the current value of the Base Priority register.
  408. \return Base Priority register value
  409. */
  410. __STATIC_FORCEINLINE uint32_t __get_BASEPRI(void)
  411. {
  412. uint32_t result;
  413. __ASM volatile ("MRS %0, basepri" : "=r" (result) );
  414. return(result);
  415. }
  416. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  417. /**
  418. \brief Get Base Priority (non-secure)
  419. \details Returns the current value of the non-secure Base Priority register when in secure state.
  420. \return Base Priority register value
  421. */
  422. __STATIC_FORCEINLINE uint32_t __TZ_get_BASEPRI_NS(void)
  423. {
  424. uint32_t result;
  425. __ASM volatile ("MRS %0, basepri_ns" : "=r" (result) );
  426. return(result);
  427. }
  428. #endif
  429. /**
  430. \brief Set Base Priority
  431. \details Assigns the given value to the Base Priority register.
  432. \param [in] basePri Base Priority value to set
  433. */
  434. __STATIC_FORCEINLINE void __set_BASEPRI(uint32_t basePri)
  435. {
  436. __ASM volatile ("MSR basepri, %0" : : "r" (basePri) : "memory");
  437. }
  438. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  439. /**
  440. \brief Set Base Priority (non-secure)
  441. \details Assigns the given value to the non-secure Base Priority register when in secure state.
  442. \param [in] basePri Base Priority value to set
  443. */
  444. __STATIC_FORCEINLINE void __TZ_set_BASEPRI_NS(uint32_t basePri)
  445. {
  446. __ASM volatile ("MSR basepri_ns, %0" : : "r" (basePri) : "memory");
  447. }
  448. #endif
  449. /**
  450. \brief Set Base Priority with condition
  451. \details Assigns the given value to the Base Priority register only if BASEPRI masking is disabled,
  452. or the new value increases the BASEPRI priority level.
  453. \param [in] basePri Base Priority value to set
  454. */
  455. __STATIC_FORCEINLINE void __set_BASEPRI_MAX(uint32_t basePri)
  456. {
  457. __ASM volatile ("MSR basepri_max, %0" : : "r" (basePri) : "memory");
  458. }
  459. /**
  460. \brief Get Fault Mask
  461. \details Returns the current value of the Fault Mask register.
  462. \return Fault Mask register value
  463. */
  464. __STATIC_FORCEINLINE uint32_t __get_FAULTMASK(void)
  465. {
  466. uint32_t result;
  467. __ASM volatile ("MRS %0, faultmask" : "=r" (result) );
  468. return(result);
  469. }
  470. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  471. /**
  472. \brief Get Fault Mask (non-secure)
  473. \details Returns the current value of the non-secure Fault Mask register when in secure state.
  474. \return Fault Mask register value
  475. */
  476. __STATIC_FORCEINLINE uint32_t __TZ_get_FAULTMASK_NS(void)
  477. {
  478. uint32_t result;
  479. __ASM volatile ("MRS %0, faultmask_ns" : "=r" (result) );
  480. return(result);
  481. }
  482. #endif
  483. /**
  484. \brief Set Fault Mask
  485. \details Assigns the given value to the Fault Mask register.
  486. \param [in] faultMask Fault Mask value to set
  487. */
  488. __STATIC_FORCEINLINE void __set_FAULTMASK(uint32_t faultMask)
  489. {
  490. __ASM volatile ("MSR faultmask, %0" : : "r" (faultMask) : "memory");
  491. }
  492. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  493. /**
  494. \brief Set Fault Mask (non-secure)
  495. \details Assigns the given value to the non-secure Fault Mask register when in secure state.
  496. \param [in] faultMask Fault Mask value to set
  497. */
  498. __STATIC_FORCEINLINE void __TZ_set_FAULTMASK_NS(uint32_t faultMask)
  499. {
  500. __ASM volatile ("MSR faultmask_ns, %0" : : "r" (faultMask) : "memory");
  501. }
  502. #endif
  503. #endif /* ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
  504. (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
  505. (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  506. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) */
  507. #if ((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  508. (defined (__ARM_ARCH_8M_BASE__ ) && (__ARM_ARCH_8M_BASE__ == 1)) || \
  509. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) )
  510. /**
  511. \brief Get Process Stack Pointer Limit
  512. Devices without ARMv8-M Main Extensions (i.e. Cortex-M23) lack the non-secure
  513. Stack Pointer Limit register hence zero is returned always in non-secure
  514. mode.
  515. \details Returns the current value of the Process Stack Pointer Limit (PSPLIM).
  516. \return PSPLIM Register value
  517. */
  518. __STATIC_FORCEINLINE uint32_t __get_PSPLIM(void)
  519. {
  520. #if (!((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  521. (defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) && \
  522. (!defined (__ARM_FEATURE_CMSE) || (__ARM_FEATURE_CMSE < 3)))
  523. // without main extensions, the non-secure PSPLIM is RAZ/WI
  524. return 0U;
  525. #else
  526. uint32_t result;
  527. __ASM volatile ("MRS %0, psplim" : "=r" (result) );
  528. return result;
  529. #endif
  530. }
  531. #if (defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3))
  532. /**
  533. \brief Get Process Stack Pointer Limit (non-secure)
  534. Devices without ARMv8-M Main Extensions (i.e. Cortex-M23) lack the non-secure
  535. Stack Pointer Limit register hence zero is returned always in non-secure
  536. mode.
  537. \details Returns the current value of the non-secure Process Stack Pointer Limit (PSPLIM) when in secure state.
  538. \return PSPLIM Register value
  539. */
  540. __STATIC_FORCEINLINE uint32_t __TZ_get_PSPLIM_NS(void)
  541. {
  542. #if (!((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  543. (defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) )
  544. // without main extensions, the non-secure PSPLIM is RAZ/WI
  545. return 0U;
  546. #else
  547. uint32_t result;
  548. __ASM volatile ("MRS %0, psplim_ns" : "=r" (result) );
  549. return result;
  550. #endif
  551. }
  552. #endif
  553. /**
  554. \brief Set Process Stack Pointer Limit
  555. Devices without ARMv8-M Main Extensions (i.e. Cortex-M23) lack the non-secure
  556. Stack Pointer Limit register hence the write is silently ignored in non-secure
  557. mode.
  558. \details Assigns the given value to the Process Stack Pointer Limit (PSPLIM).
  559. \param [in] ProcStackPtrLimit Process Stack Pointer Limit value to set
  560. */
  561. __STATIC_FORCEINLINE void __set_PSPLIM(uint32_t ProcStackPtrLimit)
  562. {
  563. #if (!((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  564. (defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) && \
  565. (!defined (__ARM_FEATURE_CMSE) || (__ARM_FEATURE_CMSE < 3)))
  566. // without main extensions, the non-secure PSPLIM is RAZ/WI
  567. (void)ProcStackPtrLimit;
  568. #else
  569. __ASM volatile ("MSR psplim, %0" : : "r" (ProcStackPtrLimit));
  570. #endif
  571. }
  572. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  573. /**
  574. \brief Set Process Stack Pointer (non-secure)
  575. Devices without ARMv8-M Main Extensions (i.e. Cortex-M23) lack the non-secure
  576. Stack Pointer Limit register hence the write is silently ignored in non-secure
  577. mode.
  578. \details Assigns the given value to the non-secure Process Stack Pointer Limit (PSPLIM) when in secure state.
  579. \param [in] ProcStackPtrLimit Process Stack Pointer Limit value to set
  580. */
  581. __STATIC_FORCEINLINE void __TZ_set_PSPLIM_NS(uint32_t ProcStackPtrLimit)
  582. {
  583. #if (!((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  584. (defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) )
  585. // without main extensions, the non-secure PSPLIM is RAZ/WI
  586. (void)ProcStackPtrLimit;
  587. #else
  588. __ASM volatile ("MSR psplim_ns, %0\n" : : "r" (ProcStackPtrLimit));
  589. #endif
  590. }
  591. #endif
  592. /**
  593. \brief Get Main Stack Pointer Limit
  594. Devices without ARMv8-M Main Extensions (i.e. Cortex-M23) lack the non-secure
  595. Stack Pointer Limit register hence zero is returned always.
  596. \details Returns the current value of the Main Stack Pointer Limit (MSPLIM).
  597. \return MSPLIM Register value
  598. */
  599. __STATIC_FORCEINLINE uint32_t __get_MSPLIM(void)
  600. {
  601. #if (!((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  602. (defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) && \
  603. (!defined (__ARM_FEATURE_CMSE) || (__ARM_FEATURE_CMSE < 3)))
  604. // without main extensions, the non-secure MSPLIM is RAZ/WI
  605. return 0U;
  606. #else
  607. uint32_t result;
  608. __ASM volatile ("MRS %0, msplim" : "=r" (result) );
  609. return result;
  610. #endif
  611. }
  612. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  613. /**
  614. \brief Get Main Stack Pointer Limit (non-secure)
  615. Devices without ARMv8-M Main Extensions (i.e. Cortex-M23) lack the non-secure
  616. Stack Pointer Limit register hence zero is returned always.
  617. \details Returns the current value of the non-secure Main Stack Pointer Limit(MSPLIM) when in secure state.
  618. \return MSPLIM Register value
  619. */
  620. __STATIC_FORCEINLINE uint32_t __TZ_get_MSPLIM_NS(void)
  621. {
  622. #if (!((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  623. (defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) )
  624. // without main extensions, the non-secure MSPLIM is RAZ/WI
  625. return 0U;
  626. #else
  627. uint32_t result;
  628. __ASM volatile ("MRS %0, msplim_ns" : "=r" (result) );
  629. return result;
  630. #endif
  631. }
  632. #endif
  633. /**
  634. \brief Set Main Stack Pointer Limit
  635. Devices without ARMv8-M Main Extensions (i.e. Cortex-M23) lack the non-secure
  636. Stack Pointer Limit register hence the write is silently ignored.
  637. \details Assigns the given value to the Main Stack Pointer Limit (MSPLIM).
  638. \param [in] MainStackPtrLimit Main Stack Pointer Limit value to set
  639. */
  640. __STATIC_FORCEINLINE void __set_MSPLIM(uint32_t MainStackPtrLimit)
  641. {
  642. #if (!((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  643. (defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) && \
  644. (!defined (__ARM_FEATURE_CMSE) || (__ARM_FEATURE_CMSE < 3)))
  645. // without main extensions, the non-secure MSPLIM is RAZ/WI
  646. (void)MainStackPtrLimit;
  647. #else
  648. __ASM volatile ("MSR msplim, %0" : : "r" (MainStackPtrLimit));
  649. #endif
  650. }
  651. #if (defined (__ARM_FEATURE_CMSE ) && (__ARM_FEATURE_CMSE == 3))
  652. /**
  653. \brief Set Main Stack Pointer Limit (non-secure)
  654. Devices without ARMv8-M Main Extensions (i.e. Cortex-M23) lack the non-secure
  655. Stack Pointer Limit register hence the write is silently ignored.
  656. \details Assigns the given value to the non-secure Main Stack Pointer Limit (MSPLIM) when in secure state.
  657. \param [in] MainStackPtrLimit Main Stack Pointer value to set
  658. */
  659. __STATIC_FORCEINLINE void __TZ_set_MSPLIM_NS(uint32_t MainStackPtrLimit)
  660. {
  661. #if (!((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  662. (defined (__ARM_ARCH_8_1M_MAIN__ ) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) )
  663. // without main extensions, the non-secure MSPLIM is RAZ/WI
  664. (void)MainStackPtrLimit;
  665. #else
  666. __ASM volatile ("MSR msplim_ns, %0" : : "r" (MainStackPtrLimit));
  667. #endif
  668. }
  669. #endif
  670. #endif /* ((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  671. (defined (__ARM_ARCH_8M_BASE__ ) && (__ARM_ARCH_8M_BASE__ == 1)) || \
  672. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) */
  673. /**
  674. \brief Get FPSCR
  675. \details Returns the current value of the Floating Point Status/Control register.
  676. \return Floating Point Status/Control register value
  677. */
  678. #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
  679. (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
  680. #define __get_FPSCR (uint32_t)__builtin_arm_get_fpscr
  681. #else
  682. #define __get_FPSCR() ((uint32_t)0U)
  683. #endif
  684. /**
  685. \brief Set FPSCR
  686. \details Assigns the given value to the Floating Point Status/Control register.
  687. \param [in] fpscr Floating Point Status/Control value to set
  688. */
  689. #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
  690. (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
  691. #define __set_FPSCR __builtin_arm_set_fpscr
  692. #else
  693. #define __set_FPSCR(x) ((void)(x))
  694. #endif
  695. /*@} end of CMSIS_Core_RegAccFunctions */
  696. /* ########################## Core Instruction Access ######################### */
  697. /** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface
  698. Access to dedicated instructions
  699. @{
  700. */
  701. /* Define macros for porting to both thumb1 and thumb2.
  702. * For thumb1, use low register (r0-r7), specified by constraint "l"
  703. * Otherwise, use general registers, specified by constraint "r" */
  704. #if defined (__thumb__) && !defined (__thumb2__)
  705. #define __CMSIS_GCC_OUT_REG(r) "=l" (r)
  706. #define __CMSIS_GCC_RW_REG(r) "+l" (r)
  707. #define __CMSIS_GCC_USE_REG(r) "l" (r)
  708. #else
  709. #define __CMSIS_GCC_OUT_REG(r) "=r" (r)
  710. #define __CMSIS_GCC_RW_REG(r) "+r" (r)
  711. #define __CMSIS_GCC_USE_REG(r) "r" (r)
  712. #endif
  713. /**
  714. \brief No Operation
  715. \details No Operation does nothing. This instruction can be used for code alignment purposes.
  716. */
  717. #define __NOP __builtin_arm_nop
  718. /**
  719. \brief Wait For Interrupt
  720. \details Wait For Interrupt is a hint instruction that suspends execution until one of a number of events occurs.
  721. */
  722. #define __WFI __builtin_arm_wfi
  723. /**
  724. \brief Wait For Event
  725. \details Wait For Event is a hint instruction that permits the processor to enter
  726. a low-power state until one of a number of events occurs.
  727. */
  728. #define __WFE __builtin_arm_wfe
  729. /**
  730. \brief Send Event
  731. \details Send Event is a hint instruction. It causes an event to be signaled to the CPU.
  732. */
  733. #define __SEV __builtin_arm_sev
  734. /**
  735. \brief Instruction Synchronization Barrier
  736. \details Instruction Synchronization Barrier flushes the pipeline in the processor,
  737. so that all instructions following the ISB are fetched from cache or memory,
  738. after the instruction has been completed.
  739. */
  740. #define __ISB() __builtin_arm_isb(0xF)
  741. /**
  742. \brief Data Synchronization Barrier
  743. \details Acts as a special kind of Data Memory Barrier.
  744. It completes when all explicit memory accesses before this instruction complete.
  745. */
  746. #define __DSB() __builtin_arm_dsb(0xF)
  747. /**
  748. \brief Data Memory Barrier
  749. \details Ensures the apparent order of the explicit memory operations before
  750. and after the instruction, without ensuring their completion.
  751. */
  752. #define __DMB() __builtin_arm_dmb(0xF)
  753. /**
  754. \brief Reverse byte order (32 bit)
  755. \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
  756. \param [in] value Value to reverse
  757. \return Reversed value
  758. */
  759. #define __REV(value) __builtin_bswap32(value)
  760. /**
  761. \brief Reverse byte order (16 bit)
  762. \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
  763. \param [in] value Value to reverse
  764. \return Reversed value
  765. */
  766. #define __REV16(value) __ROR(__REV(value), 16)
  767. /**
  768. \brief Reverse byte order (16 bit)
  769. \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
  770. \param [in] value Value to reverse
  771. \return Reversed value
  772. */
  773. #define __REVSH(value) (int16_t)__builtin_bswap16(value)
  774. /**
  775. \brief Rotate Right in unsigned value (32 bit)
  776. \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  777. \param [in] op1 Value to rotate
  778. \param [in] op2 Number of Bits to rotate
  779. \return Rotated value
  780. */
  781. __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
  782. {
  783. op2 %= 32U;
  784. if (op2 == 0U)
  785. {
  786. return op1;
  787. }
  788. return (op1 >> op2) | (op1 << (32U - op2));
  789. }
  790. /**
  791. \brief Breakpoint
  792. \details Causes the processor to enter Debug state.
  793. Debug tools can use this to investigate system state when the instruction at a particular address is reached.
  794. \param [in] value is ignored by the processor.
  795. If required, a debugger can use it to store additional information about the breakpoint.
  796. */
  797. #define __BKPT(value) __ASM volatile ("bkpt "#value)
  798. /**
  799. \brief Reverse bit order of value
  800. \details Reverses the bit order of the given value.
  801. \param [in] value Value to reverse
  802. \return Reversed value
  803. */
  804. #define __RBIT __builtin_arm_rbit
  805. /**
  806. \brief Count leading zeros
  807. \details Counts the number of leading zeros of a data value.
  808. \param [in] value Value to count the leading zeros
  809. \return number of leading zeros in value
  810. */
  811. __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
  812. {
  813. /* Even though __builtin_clz produces a CLZ instruction on ARM, formally
  814. __builtin_clz(0) is undefined behaviour, so handle this case specially.
  815. This guarantees ARM-compatible results if happening to compile on a non-ARM
  816. target, and ensures the compiler doesn't decide to activate any
  817. optimisations using the logic "value was passed to __builtin_clz, so it
  818. is non-zero".
  819. ARM Compiler 6.10 and possibly earlier will optimise this test away, leaving a
  820. single CLZ instruction.
  821. */
  822. if (value == 0U)
  823. {
  824. return 32U;
  825. }
  826. return __builtin_clz(value);
  827. }
  828. #if ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
  829. (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
  830. (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  831. (defined (__ARM_ARCH_8M_BASE__ ) && (__ARM_ARCH_8M_BASE__ == 1)) || \
  832. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) )
  833. /**
  834. \brief LDR Exclusive (8 bit)
  835. \details Executes a exclusive LDR instruction for 8 bit value.
  836. \param [in] ptr Pointer to data
  837. \return value of type uint8_t at (*ptr)
  838. */
  839. #define __LDREXB (uint8_t)__builtin_arm_ldrex
  840. /**
  841. \brief LDR Exclusive (16 bit)
  842. \details Executes a exclusive LDR instruction for 16 bit values.
  843. \param [in] ptr Pointer to data
  844. \return value of type uint16_t at (*ptr)
  845. */
  846. #define __LDREXH (uint16_t)__builtin_arm_ldrex
  847. /**
  848. \brief LDR Exclusive (32 bit)
  849. \details Executes a exclusive LDR instruction for 32 bit values.
  850. \param [in] ptr Pointer to data
  851. \return value of type uint32_t at (*ptr)
  852. */
  853. #define __LDREXW (uint32_t)__builtin_arm_ldrex
  854. /**
  855. \brief STR Exclusive (8 bit)
  856. \details Executes a exclusive STR instruction for 8 bit values.
  857. \param [in] value Value to store
  858. \param [in] ptr Pointer to location
  859. \return 0 Function succeeded
  860. \return 1 Function failed
  861. */
  862. #define __STREXB (uint32_t)__builtin_arm_strex
  863. /**
  864. \brief STR Exclusive (16 bit)
  865. \details Executes a exclusive STR instruction for 16 bit values.
  866. \param [in] value Value to store
  867. \param [in] ptr Pointer to location
  868. \return 0 Function succeeded
  869. \return 1 Function failed
  870. */
  871. #define __STREXH (uint32_t)__builtin_arm_strex
  872. /**
  873. \brief STR Exclusive (32 bit)
  874. \details Executes a exclusive STR instruction for 32 bit values.
  875. \param [in] value Value to store
  876. \param [in] ptr Pointer to location
  877. \return 0 Function succeeded
  878. \return 1 Function failed
  879. */
  880. #define __STREXW (uint32_t)__builtin_arm_strex
  881. /**
  882. \brief Remove the exclusive lock
  883. \details Removes the exclusive lock which is created by LDREX.
  884. */
  885. #define __CLREX __builtin_arm_clrex
  886. #endif /* ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
  887. (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
  888. (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  889. (defined (__ARM_ARCH_8M_BASE__ ) && (__ARM_ARCH_8M_BASE__ == 1)) || \
  890. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) */
  891. #if ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
  892. (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
  893. (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  894. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) )
  895. /**
  896. \brief Signed Saturate
  897. \details Saturates a signed value.
  898. \param [in] value Value to be saturated
  899. \param [in] sat Bit position to saturate to (1..32)
  900. \return Saturated value
  901. */
  902. #define __SSAT __builtin_arm_ssat
  903. /**
  904. \brief Unsigned Saturate
  905. \details Saturates an unsigned value.
  906. \param [in] value Value to be saturated
  907. \param [in] sat Bit position to saturate to (0..31)
  908. \return Saturated value
  909. */
  910. #define __USAT __builtin_arm_usat
  911. /**
  912. \brief Rotate Right with Extend (32 bit)
  913. \details Moves each bit of a bitstring right by one bit.
  914. The carry input is shifted in at the left end of the bitstring.
  915. \param [in] value Value to rotate
  916. \return Rotated value
  917. */
  918. __STATIC_FORCEINLINE uint32_t __RRX(uint32_t value)
  919. {
  920. uint32_t result;
  921. __ASM volatile ("rrx %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) );
  922. return(result);
  923. }
  924. /**
  925. \brief LDRT Unprivileged (8 bit)
  926. \details Executes a Unprivileged LDRT instruction for 8 bit value.
  927. \param [in] ptr Pointer to data
  928. \return value of type uint8_t at (*ptr)
  929. */
  930. __STATIC_FORCEINLINE uint8_t __LDRBT(volatile uint8_t *ptr)
  931. {
  932. uint32_t result;
  933. __ASM volatile ("ldrbt %0, %1" : "=r" (result) : "Q" (*ptr) );
  934. return ((uint8_t) result); /* Add explicit type cast here */
  935. }
  936. /**
  937. \brief LDRT Unprivileged (16 bit)
  938. \details Executes a Unprivileged LDRT instruction for 16 bit values.
  939. \param [in] ptr Pointer to data
  940. \return value of type uint16_t at (*ptr)
  941. */
  942. __STATIC_FORCEINLINE uint16_t __LDRHT(volatile uint16_t *ptr)
  943. {
  944. uint32_t result;
  945. __ASM volatile ("ldrht %0, %1" : "=r" (result) : "Q" (*ptr) );
  946. return ((uint16_t) result); /* Add explicit type cast here */
  947. }
  948. /**
  949. \brief LDRT Unprivileged (32 bit)
  950. \details Executes a Unprivileged LDRT instruction for 32 bit values.
  951. \param [in] ptr Pointer to data
  952. \return value of type uint32_t at (*ptr)
  953. */
  954. __STATIC_FORCEINLINE uint32_t __LDRT(volatile uint32_t *ptr)
  955. {
  956. uint32_t result;
  957. __ASM volatile ("ldrt %0, %1" : "=r" (result) : "Q" (*ptr) );
  958. return(result);
  959. }
  960. /**
  961. \brief STRT Unprivileged (8 bit)
  962. \details Executes a Unprivileged STRT instruction for 8 bit values.
  963. \param [in] value Value to store
  964. \param [in] ptr Pointer to location
  965. */
  966. __STATIC_FORCEINLINE void __STRBT(uint8_t value, volatile uint8_t *ptr)
  967. {
  968. __ASM volatile ("strbt %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) );
  969. }
  970. /**
  971. \brief STRT Unprivileged (16 bit)
  972. \details Executes a Unprivileged STRT instruction for 16 bit values.
  973. \param [in] value Value to store
  974. \param [in] ptr Pointer to location
  975. */
  976. __STATIC_FORCEINLINE void __STRHT(uint16_t value, volatile uint16_t *ptr)
  977. {
  978. __ASM volatile ("strht %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) );
  979. }
  980. /**
  981. \brief STRT Unprivileged (32 bit)
  982. \details Executes a Unprivileged STRT instruction for 32 bit values.
  983. \param [in] value Value to store
  984. \param [in] ptr Pointer to location
  985. */
  986. __STATIC_FORCEINLINE void __STRT(uint32_t value, volatile uint32_t *ptr)
  987. {
  988. __ASM volatile ("strt %1, %0" : "=Q" (*ptr) : "r" (value) );
  989. }
  990. #else /* ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
  991. (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
  992. (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  993. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) */
  994. /**
  995. \brief Signed Saturate
  996. \details Saturates a signed value.
  997. \param [in] value Value to be saturated
  998. \param [in] sat Bit position to saturate to (1..32)
  999. \return Saturated value
  1000. */
  1001. __STATIC_FORCEINLINE int32_t __SSAT(int32_t val, uint32_t sat)
  1002. {
  1003. if ((sat >= 1U) && (sat <= 32U))
  1004. {
  1005. const int32_t max = (int32_t)((1U << (sat - 1U)) - 1U);
  1006. const int32_t min = -1 - max ;
  1007. if (val > max)
  1008. {
  1009. return max;
  1010. }
  1011. else if (val < min)
  1012. {
  1013. return min;
  1014. }
  1015. }
  1016. return val;
  1017. }
  1018. /**
  1019. \brief Unsigned Saturate
  1020. \details Saturates an unsigned value.
  1021. \param [in] value Value to be saturated
  1022. \param [in] sat Bit position to saturate to (0..31)
  1023. \return Saturated value
  1024. */
  1025. __STATIC_FORCEINLINE uint32_t __USAT(int32_t val, uint32_t sat)
  1026. {
  1027. if (sat <= 31U)
  1028. {
  1029. const uint32_t max = ((1U << sat) - 1U);
  1030. if (val > (int32_t)max)
  1031. {
  1032. return max;
  1033. }
  1034. else if (val < 0)
  1035. {
  1036. return 0U;
  1037. }
  1038. }
  1039. return (uint32_t)val;
  1040. }
  1041. #endif /* ((defined (__ARM_ARCH_7M__ ) && (__ARM_ARCH_7M__ == 1)) || \
  1042. (defined (__ARM_ARCH_7EM__ ) && (__ARM_ARCH_7EM__ == 1)) || \
  1043. (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  1044. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) */
  1045. #if ((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  1046. (defined (__ARM_ARCH_8M_BASE__ ) && (__ARM_ARCH_8M_BASE__ == 1)) || \
  1047. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) )
  1048. /**
  1049. \brief Load-Acquire (8 bit)
  1050. \details Executes a LDAB instruction for 8 bit value.
  1051. \param [in] ptr Pointer to data
  1052. \return value of type uint8_t at (*ptr)
  1053. */
  1054. __STATIC_FORCEINLINE uint8_t __LDAB(volatile uint8_t *ptr)
  1055. {
  1056. uint32_t result;
  1057. __ASM volatile ("ldab %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  1058. return ((uint8_t) result);
  1059. }
  1060. /**
  1061. \brief Load-Acquire (16 bit)
  1062. \details Executes a LDAH instruction for 16 bit values.
  1063. \param [in] ptr Pointer to data
  1064. \return value of type uint16_t at (*ptr)
  1065. */
  1066. __STATIC_FORCEINLINE uint16_t __LDAH(volatile uint16_t *ptr)
  1067. {
  1068. uint32_t result;
  1069. __ASM volatile ("ldah %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  1070. return ((uint16_t) result);
  1071. }
  1072. /**
  1073. \brief Load-Acquire (32 bit)
  1074. \details Executes a LDA instruction for 32 bit values.
  1075. \param [in] ptr Pointer to data
  1076. \return value of type uint32_t at (*ptr)
  1077. */
  1078. __STATIC_FORCEINLINE uint32_t __LDA(volatile uint32_t *ptr)
  1079. {
  1080. uint32_t result;
  1081. __ASM volatile ("lda %0, %1" : "=r" (result) : "Q" (*ptr) : "memory" );
  1082. return(result);
  1083. }
  1084. /**
  1085. \brief Store-Release (8 bit)
  1086. \details Executes a STLB instruction for 8 bit values.
  1087. \param [in] value Value to store
  1088. \param [in] ptr Pointer to location
  1089. */
  1090. __STATIC_FORCEINLINE void __STLB(uint8_t value, volatile uint8_t *ptr)
  1091. {
  1092. __ASM volatile ("stlb %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  1093. }
  1094. /**
  1095. \brief Store-Release (16 bit)
  1096. \details Executes a STLH instruction for 16 bit values.
  1097. \param [in] value Value to store
  1098. \param [in] ptr Pointer to location
  1099. */
  1100. __STATIC_FORCEINLINE void __STLH(uint16_t value, volatile uint16_t *ptr)
  1101. {
  1102. __ASM volatile ("stlh %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  1103. }
  1104. /**
  1105. \brief Store-Release (32 bit)
  1106. \details Executes a STL instruction for 32 bit values.
  1107. \param [in] value Value to store
  1108. \param [in] ptr Pointer to location
  1109. */
  1110. __STATIC_FORCEINLINE void __STL(uint32_t value, volatile uint32_t *ptr)
  1111. {
  1112. __ASM volatile ("stl %1, %0" : "=Q" (*ptr) : "r" ((uint32_t)value) : "memory" );
  1113. }
  1114. /**
  1115. \brief Load-Acquire Exclusive (8 bit)
  1116. \details Executes a LDAB exclusive instruction for 8 bit value.
  1117. \param [in] ptr Pointer to data
  1118. \return value of type uint8_t at (*ptr)
  1119. */
  1120. #define __LDAEXB (uint8_t)__builtin_arm_ldaex
  1121. /**
  1122. \brief Load-Acquire Exclusive (16 bit)
  1123. \details Executes a LDAH exclusive instruction for 16 bit values.
  1124. \param [in] ptr Pointer to data
  1125. \return value of type uint16_t at (*ptr)
  1126. */
  1127. #define __LDAEXH (uint16_t)__builtin_arm_ldaex
  1128. /**
  1129. \brief Load-Acquire Exclusive (32 bit)
  1130. \details Executes a LDA exclusive instruction for 32 bit values.
  1131. \param [in] ptr Pointer to data
  1132. \return value of type uint32_t at (*ptr)
  1133. */
  1134. #define __LDAEX (uint32_t)__builtin_arm_ldaex
  1135. /**
  1136. \brief Store-Release Exclusive (8 bit)
  1137. \details Executes a STLB exclusive instruction for 8 bit values.
  1138. \param [in] value Value to store
  1139. \param [in] ptr Pointer to location
  1140. \return 0 Function succeeded
  1141. \return 1 Function failed
  1142. */
  1143. #define __STLEXB (uint32_t)__builtin_arm_stlex
  1144. /**
  1145. \brief Store-Release Exclusive (16 bit)
  1146. \details Executes a STLH exclusive instruction for 16 bit values.
  1147. \param [in] value Value to store
  1148. \param [in] ptr Pointer to location
  1149. \return 0 Function succeeded
  1150. \return 1 Function failed
  1151. */
  1152. #define __STLEXH (uint32_t)__builtin_arm_stlex
  1153. /**
  1154. \brief Store-Release Exclusive (32 bit)
  1155. \details Executes a STL exclusive instruction for 32 bit values.
  1156. \param [in] value Value to store
  1157. \param [in] ptr Pointer to location
  1158. \return 0 Function succeeded
  1159. \return 1 Function failed
  1160. */
  1161. #define __STLEX (uint32_t)__builtin_arm_stlex
  1162. #endif /* ((defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1)) || \
  1163. (defined (__ARM_ARCH_8M_BASE__ ) && (__ARM_ARCH_8M_BASE__ == 1)) || \
  1164. (defined (__ARM_ARCH_8_1M_MAIN__) && (__ARM_ARCH_8_1M_MAIN__ == 1)) ) */
  1165. /*@}*/ /* end of group CMSIS_Core_InstructionInterface */
  1166. /* ################### Compiler specific Intrinsics ########################### */
  1167. /** \defgroup CMSIS_SIMD_intrinsics CMSIS SIMD Intrinsics
  1168. Access to dedicated SIMD instructions
  1169. @{
  1170. */
  1171. #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
  1172. #define __SADD8 __builtin_arm_sadd8
  1173. #define __QADD8 __builtin_arm_qadd8
  1174. #define __SHADD8 __builtin_arm_shadd8
  1175. #define __UADD8 __builtin_arm_uadd8
  1176. #define __UQADD8 __builtin_arm_uqadd8
  1177. #define __UHADD8 __builtin_arm_uhadd8
  1178. #define __SSUB8 __builtin_arm_ssub8
  1179. #define __QSUB8 __builtin_arm_qsub8
  1180. #define __SHSUB8 __builtin_arm_shsub8
  1181. #define __USUB8 __builtin_arm_usub8
  1182. #define __UQSUB8 __builtin_arm_uqsub8
  1183. #define __UHSUB8 __builtin_arm_uhsub8
  1184. #define __SADD16 __builtin_arm_sadd16
  1185. #define __QADD16 __builtin_arm_qadd16
  1186. #define __SHADD16 __builtin_arm_shadd16
  1187. #define __UADD16 __builtin_arm_uadd16
  1188. #define __UQADD16 __builtin_arm_uqadd16
  1189. #define __UHADD16 __builtin_arm_uhadd16
  1190. #define __SSUB16 __builtin_arm_ssub16
  1191. #define __QSUB16 __builtin_arm_qsub16
  1192. #define __SHSUB16 __builtin_arm_shsub16
  1193. #define __USUB16 __builtin_arm_usub16
  1194. #define __UQSUB16 __builtin_arm_uqsub16
  1195. #define __UHSUB16 __builtin_arm_uhsub16
  1196. #define __SASX __builtin_arm_sasx
  1197. #define __QASX __builtin_arm_qasx
  1198. #define __SHASX __builtin_arm_shasx
  1199. #define __UASX __builtin_arm_uasx
  1200. #define __UQASX __builtin_arm_uqasx
  1201. #define __UHASX __builtin_arm_uhasx
  1202. #define __SSAX __builtin_arm_ssax
  1203. #define __QSAX __builtin_arm_qsax
  1204. #define __SHSAX __builtin_arm_shsax
  1205. #define __USAX __builtin_arm_usax
  1206. #define __UQSAX __builtin_arm_uqsax
  1207. #define __UHSAX __builtin_arm_uhsax
  1208. #define __USAD8 __builtin_arm_usad8
  1209. #define __USADA8 __builtin_arm_usada8
  1210. #define __SSAT16 __builtin_arm_ssat16
  1211. #define __USAT16 __builtin_arm_usat16
  1212. #define __UXTB16 __builtin_arm_uxtb16
  1213. #define __UXTAB16 __builtin_arm_uxtab16
  1214. #define __SXTB16 __builtin_arm_sxtb16
  1215. #define __SXTAB16 __builtin_arm_sxtab16
  1216. #define __SMUAD __builtin_arm_smuad
  1217. #define __SMUADX __builtin_arm_smuadx
  1218. #define __SMLAD __builtin_arm_smlad
  1219. #define __SMLADX __builtin_arm_smladx
  1220. #define __SMLALD __builtin_arm_smlald
  1221. #define __SMLALDX __builtin_arm_smlaldx
  1222. #define __SMUSD __builtin_arm_smusd
  1223. #define __SMUSDX __builtin_arm_smusdx
  1224. #define __SMLSD __builtin_arm_smlsd
  1225. #define __SMLSDX __builtin_arm_smlsdx
  1226. #define __SMLSLD __builtin_arm_smlsld
  1227. #define __SMLSLDX __builtin_arm_smlsldx
  1228. #define __SEL __builtin_arm_sel
  1229. #define __QADD __builtin_arm_qadd
  1230. #define __QSUB __builtin_arm_qsub
  1231. #define __PKHBT(ARG1,ARG2,ARG3) ( ((((uint32_t)(ARG1)) ) & 0x0000FFFFUL) | \
  1232. ((((uint32_t)(ARG2)) << (ARG3)) & 0xFFFF0000UL) )
  1233. #define __PKHTB(ARG1,ARG2,ARG3) ( ((((uint32_t)(ARG1)) ) & 0xFFFF0000UL) | \
  1234. ((((uint32_t)(ARG2)) >> (ARG3)) & 0x0000FFFFUL) )
  1235. #define __SXTB16_RORn(ARG1, ARG2) __SXTB16(__ROR(ARG1, ARG2))
  1236. __STATIC_FORCEINLINE int32_t __SMMLA (int32_t op1, int32_t op2, int32_t op3)
  1237. {
  1238. int32_t result;
  1239. __ASM volatile ("smmla %0, %1, %2, %3" : "=r" (result): "r" (op1), "r" (op2), "r" (op3) );
  1240. return(result);
  1241. }
  1242. #endif /* (__ARM_FEATURE_DSP == 1) */
  1243. /*@} end of group CMSIS_SIMD_intrinsics */
  1244. #endif /* __CMSIS_ARMCLANG_H */