cmsis_armclang.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**************************************************************************//**
  2. * @file cmsis_armclang.h
  3. * @brief CMSIS compiler specific macros, functions, instructions
  4. * @version V1.0.2
  5. * @date 10. January 2018
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2009-2018 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_ARMCLANG_H
  25. #define __CMSIS_ARMCLANG_H
  26. #pragma clang system_header /* treat file as system include file */
  27. #ifndef __ARM_COMPAT_H
  28. #include <arm_compat.h> /* Compatibility header for Arm Compiler 5 intrinsics */
  29. #endif
  30. /* CMSIS compiler specific defines */
  31. #ifndef __ASM
  32. #define __ASM __asm
  33. #endif
  34. #ifndef __INLINE
  35. #define __INLINE __inline
  36. #endif
  37. #ifndef __FORCEINLINE
  38. #define __FORCEINLINE __attribute__((always_inline))
  39. #endif
  40. #ifndef __STATIC_INLINE
  41. #define __STATIC_INLINE static __inline
  42. #endif
  43. #ifndef __STATIC_FORCEINLINE
  44. #define __STATIC_FORCEINLINE __attribute__((always_inline)) static __inline
  45. #endif
  46. #ifndef __NO_RETURN
  47. #define __NO_RETURN __attribute__((__noreturn__))
  48. #endif
  49. #ifndef CMSIS_DEPRECATED
  50. #define CMSIS_DEPRECATED __attribute__((deprecated))
  51. #endif
  52. #ifndef __USED
  53. #define __USED __attribute__((used))
  54. #endif
  55. #ifndef __WEAK
  56. #define __WEAK __attribute__((weak))
  57. #endif
  58. #ifndef __PACKED
  59. #define __PACKED __attribute__((packed, aligned(1)))
  60. #endif
  61. #ifndef __PACKED_STRUCT
  62. #define __PACKED_STRUCT struct __attribute__((packed, aligned(1)))
  63. #endif
  64. #ifndef __UNALIGNED_UINT16_WRITE
  65. #pragma clang diagnostic push
  66. #pragma clang diagnostic ignored "-Wpacked"
  67. /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
  68. __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
  69. #pragma clang diagnostic pop
  70. #define __UNALIGNED_UINT16_WRITE(addr, val) (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
  71. #endif
  72. #ifndef __UNALIGNED_UINT16_READ
  73. #pragma clang diagnostic push
  74. #pragma clang diagnostic ignored "-Wpacked"
  75. /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
  76. __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
  77. #pragma clang diagnostic pop
  78. #define __UNALIGNED_UINT16_READ(addr) (((const struct T_UINT16_READ *)(const void *)(addr))->v)
  79. #endif
  80. #ifndef __UNALIGNED_UINT32_WRITE
  81. #pragma clang diagnostic push
  82. #pragma clang diagnostic ignored "-Wpacked"
  83. /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
  84. __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
  85. #pragma clang diagnostic pop
  86. #define __UNALIGNED_UINT32_WRITE(addr, val) (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
  87. #endif
  88. #ifndef __UNALIGNED_UINT32_READ
  89. #pragma clang diagnostic push
  90. #pragma clang diagnostic ignored "-Wpacked"
  91. __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
  92. #pragma clang diagnostic pop
  93. #define __UNALIGNED_UINT32_READ(addr) (((const struct T_UINT32_READ *)(const void *)(addr))->v)
  94. #endif
  95. #ifndef __ALIGNED
  96. #define __ALIGNED(x) __attribute__((aligned(x)))
  97. #endif
  98. #ifndef __PACKED
  99. #define __PACKED __attribute__((packed))
  100. #endif
  101. /* ########################## Core Instruction Access ######################### */
  102. /**
  103. \brief No Operation
  104. */
  105. #define __NOP __builtin_arm_nop
  106. /**
  107. \brief Wait For Interrupt
  108. */
  109. #define __WFI __builtin_arm_wfi
  110. /**
  111. \brief Wait For Event
  112. */
  113. #define __WFE __builtin_arm_wfe
  114. /**
  115. \brief Send Event
  116. */
  117. #define __SEV __builtin_arm_sev
  118. /**
  119. \brief Instruction Synchronization Barrier
  120. */
  121. #define __ISB() do {\
  122. __schedule_barrier();\
  123. __builtin_arm_isb(0xF);\
  124. __schedule_barrier();\
  125. } while (0U)
  126. /**
  127. \brief Data Synchronization Barrier
  128. */
  129. #define __DSB() do {\
  130. __schedule_barrier();\
  131. __builtin_arm_dsb(0xF);\
  132. __schedule_barrier();\
  133. } while (0U)
  134. /**
  135. \brief Data Memory Barrier
  136. */
  137. #define __DMB() do {\
  138. __schedule_barrier();\
  139. __builtin_arm_dmb(0xF);\
  140. __schedule_barrier();\
  141. } while (0U)
  142. /**
  143. \brief Reverse byte order (32 bit)
  144. \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
  145. \param [in] value Value to reverse
  146. \return Reversed value
  147. */
  148. #define __REV(value) __builtin_bswap32(value)
  149. /**
  150. \brief Reverse byte order (16 bit)
  151. \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
  152. \param [in] value Value to reverse
  153. \return Reversed value
  154. */
  155. #define __REV16(value) __ROR(__REV(value), 16)
  156. /**
  157. \brief Reverse byte order (16 bit)
  158. \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
  159. \param [in] value Value to reverse
  160. \return Reversed value
  161. */
  162. #define __REVSH(value) (int16_t)__builtin_bswap16(value)
  163. /**
  164. \brief Rotate Right in unsigned value (32 bit)
  165. \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
  166. \param [in] op1 Value to rotate
  167. \param [in] op2 Number of Bits to rotate
  168. \return Rotated value
  169. */
  170. __STATIC_FORCEINLINE uint32_t __ROR(uint32_t op1, uint32_t op2)
  171. {
  172. op2 %= 32U;
  173. if (op2 == 0U)
  174. {
  175. return op1;
  176. }
  177. return (op1 >> op2) | (op1 << (32U - op2));
  178. }
  179. /**
  180. \brief Breakpoint
  181. \param [in] value is ignored by the processor.
  182. If required, a debugger can use it to store additional information about the breakpoint.
  183. */
  184. #define __BKPT(value) __ASM volatile ("bkpt "#value)
  185. /**
  186. \brief Reverse bit order of value
  187. \param [in] value Value to reverse
  188. \return Reversed value
  189. */
  190. #define __RBIT __builtin_arm_rbit
  191. /**
  192. \brief Count leading zeros
  193. \param [in] value Value to count the leading zeros
  194. \return number of leading zeros in value
  195. */
  196. #define __CLZ (uint8_t)__builtin_clz
  197. /**
  198. \brief LDR Exclusive (8 bit)
  199. \details Executes a exclusive LDR instruction for 8 bit value.
  200. \param [in] ptr Pointer to data
  201. \return value of type uint8_t at (*ptr)
  202. */
  203. #define __LDREXB (uint8_t)__builtin_arm_ldrex
  204. /**
  205. \brief LDR Exclusive (16 bit)
  206. \details Executes a exclusive LDR instruction for 16 bit values.
  207. \param [in] ptr Pointer to data
  208. \return value of type uint16_t at (*ptr)
  209. */
  210. #define __LDREXH (uint16_t)__builtin_arm_ldrex
  211. /**
  212. \brief LDR Exclusive (32 bit)
  213. \details Executes a exclusive LDR instruction for 32 bit values.
  214. \param [in] ptr Pointer to data
  215. \return value of type uint32_t at (*ptr)
  216. */
  217. #define __LDREXW (uint32_t)__builtin_arm_ldrex
  218. /**
  219. \brief STR Exclusive (8 bit)
  220. \details Executes a exclusive STR instruction for 8 bit values.
  221. \param [in] value Value to store
  222. \param [in] ptr Pointer to location
  223. \return 0 Function succeeded
  224. \return 1 Function failed
  225. */
  226. #define __STREXB (uint32_t)__builtin_arm_strex
  227. /**
  228. \brief STR Exclusive (16 bit)
  229. \details Executes a exclusive STR instruction for 16 bit values.
  230. \param [in] value Value to store
  231. \param [in] ptr Pointer to location
  232. \return 0 Function succeeded
  233. \return 1 Function failed
  234. */
  235. #define __STREXH (uint32_t)__builtin_arm_strex
  236. /**
  237. \brief STR Exclusive (32 bit)
  238. \details Executes a exclusive STR instruction for 32 bit values.
  239. \param [in] value Value to store
  240. \param [in] ptr Pointer to location
  241. \return 0 Function succeeded
  242. \return 1 Function failed
  243. */
  244. #define __STREXW (uint32_t)__builtin_arm_strex
  245. /**
  246. \brief Remove the exclusive lock
  247. \details Removes the exclusive lock which is created by LDREX.
  248. */
  249. #define __CLREX __builtin_arm_clrex
  250. /**
  251. \brief Signed Saturate
  252. \details Saturates a signed value.
  253. \param [in] value Value to be saturated
  254. \param [in] sat Bit position to saturate to (1..32)
  255. \return Saturated value
  256. */
  257. #define __SSAT __builtin_arm_ssat
  258. /**
  259. \brief Unsigned Saturate
  260. \details Saturates an unsigned value.
  261. \param [in] value Value to be saturated
  262. \param [in] sat Bit position to saturate to (0..31)
  263. \return Saturated value
  264. */
  265. #define __USAT __builtin_arm_usat
  266. /* ########################### Core Function Access ########################### */
  267. /**
  268. \brief Get FPSCR
  269. \details Returns the current value of the Floating Point Status/Control register.
  270. \return Floating Point Status/Control register value
  271. */
  272. #define __get_FPSCR __builtin_arm_get_fpscr
  273. /**
  274. \brief Set FPSCR
  275. \details Assigns the given value to the Floating Point Status/Control register.
  276. \param [in] fpscr Floating Point Status/Control value to set
  277. */
  278. #define __set_FPSCR __builtin_arm_set_fpscr
  279. /** \brief Get CPSR Register
  280. \return CPSR Register value
  281. */
  282. __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
  283. {
  284. uint32_t result;
  285. __ASM volatile("MRS %0, cpsr" : "=r" (result) );
  286. return(result);
  287. }
  288. /** \brief Set CPSR Register
  289. \param [in] cpsr CPSR value to set
  290. */
  291. __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
  292. {
  293. __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "memory");
  294. }
  295. /** \brief Get Mode
  296. \return Processor Mode
  297. */
  298. __STATIC_FORCEINLINE uint32_t __get_mode(void)
  299. {
  300. return (__get_CPSR() & 0x1FU);
  301. }
  302. /** \brief Set Mode
  303. \param [in] mode Mode value to set
  304. */
  305. __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
  306. {
  307. __ASM volatile("MSR cpsr_c, %0" : : "r" (mode) : "memory");
  308. }
  309. /** \brief Get Stack Pointer
  310. \return Stack Pointer value
  311. */
  312. __STATIC_FORCEINLINE uint32_t __get_SP()
  313. {
  314. uint32_t result;
  315. __ASM volatile("MOV %0, sp" : "=r" (result) : : "memory");
  316. return result;
  317. }
  318. /** \brief Set Stack Pointer
  319. \param [in] stack Stack Pointer value to set
  320. */
  321. __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
  322. {
  323. __ASM volatile("MOV sp, %0" : : "r" (stack) : "memory");
  324. }
  325. /** \brief Get USR/SYS Stack Pointer
  326. \return USR/SYS Stack Pointer value
  327. */
  328. __STATIC_FORCEINLINE uint32_t __get_SP_usr()
  329. {
  330. uint32_t cpsr;
  331. uint32_t result;
  332. __ASM volatile(
  333. "MRS %0, cpsr \n"
  334. "CPS #0x1F \n" // no effect in USR mode
  335. "MOV %1, sp \n"
  336. "MSR cpsr_c, %2 \n" // no effect in USR mode
  337. "ISB" : "=r"(cpsr), "=r"(result) : "r"(cpsr) : "memory"
  338. );
  339. return result;
  340. }
  341. /** \brief Set USR/SYS Stack Pointer
  342. \param [in] topOfProcStack USR/SYS Stack Pointer value to set
  343. */
  344. __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
  345. {
  346. uint32_t cpsr;
  347. __ASM volatile(
  348. "MRS %0, cpsr \n"
  349. "CPS #0x1F \n" // no effect in USR mode
  350. "MOV sp, %1 \n"
  351. "MSR cpsr_c, %2 \n" // no effect in USR mode
  352. "ISB" : "=r"(cpsr) : "r" (topOfProcStack), "r"(cpsr) : "memory"
  353. );
  354. }
  355. /** \brief Get FPEXC
  356. \return Floating Point Exception Control register value
  357. */
  358. __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
  359. {
  360. #if (__FPU_PRESENT == 1)
  361. uint32_t result;
  362. __ASM volatile("VMRS %0, fpexc" : "=r" (result) : : "memory");
  363. return(result);
  364. #else
  365. return(0);
  366. #endif
  367. }
  368. /** \brief Set FPEXC
  369. \param [in] fpexc Floating Point Exception Control value to set
  370. */
  371. __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
  372. {
  373. #if (__FPU_PRESENT == 1)
  374. __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
  375. #endif
  376. }
  377. /*
  378. * Include common core functions to access Coprocessor 15 registers
  379. */
  380. #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
  381. #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
  382. #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : "=r" (Rt) : : "memory" )
  383. #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm : : "r" (Rt) : "memory" )
  384. #include "cmsis_cp15.h"
  385. /** \brief Enable Floating Point Unit
  386. Critical section, called from undef handler, so systick is disabled
  387. */
  388. __STATIC_INLINE void __FPU_Enable(void)
  389. {
  390. __ASM volatile(
  391. //Permit access to VFP/NEON, registers by modifying CPACR
  392. " MRC p15,0,R1,c1,c0,2 \n"
  393. " ORR R1,R1,#0x00F00000 \n"
  394. " MCR p15,0,R1,c1,c0,2 \n"
  395. //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
  396. " ISB \n"
  397. //Enable VFP/NEON
  398. " VMRS R1,FPEXC \n"
  399. " ORR R1,R1,#0x40000000 \n"
  400. " VMSR FPEXC,R1 \n"
  401. //Initialise VFP/NEON registers to 0
  402. " MOV R2,#0 \n"
  403. //Initialise D16 registers to 0
  404. " VMOV D0, R2,R2 \n"
  405. " VMOV D1, R2,R2 \n"
  406. " VMOV D2, R2,R2 \n"
  407. " VMOV D3, R2,R2 \n"
  408. " VMOV D4, R2,R2 \n"
  409. " VMOV D5, R2,R2 \n"
  410. " VMOV D6, R2,R2 \n"
  411. " VMOV D7, R2,R2 \n"
  412. " VMOV D8, R2,R2 \n"
  413. " VMOV D9, R2,R2 \n"
  414. " VMOV D10,R2,R2 \n"
  415. " VMOV D11,R2,R2 \n"
  416. " VMOV D12,R2,R2 \n"
  417. " VMOV D13,R2,R2 \n"
  418. " VMOV D14,R2,R2 \n"
  419. " VMOV D15,R2,R2 \n"
  420. #if __ARM_NEON == 1
  421. //Initialise D32 registers to 0
  422. " VMOV D16,R2,R2 \n"
  423. " VMOV D17,R2,R2 \n"
  424. " VMOV D18,R2,R2 \n"
  425. " VMOV D19,R2,R2 \n"
  426. " VMOV D20,R2,R2 \n"
  427. " VMOV D21,R2,R2 \n"
  428. " VMOV D22,R2,R2 \n"
  429. " VMOV D23,R2,R2 \n"
  430. " VMOV D24,R2,R2 \n"
  431. " VMOV D25,R2,R2 \n"
  432. " VMOV D26,R2,R2 \n"
  433. " VMOV D27,R2,R2 \n"
  434. " VMOV D28,R2,R2 \n"
  435. " VMOV D29,R2,R2 \n"
  436. " VMOV D30,R2,R2 \n"
  437. " VMOV D31,R2,R2 \n"
  438. #endif
  439. //Initialise FPSCR to a known state
  440. " VMRS R2,FPSCR \n"
  441. " LDR R3,=0x00086060 \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
  442. " AND R2,R2,R3 \n"
  443. " VMSR FPSCR,R2 "
  444. );
  445. }
  446. #endif /* __CMSIS_ARMCLANG_H */