cmsis_armcc.h 16 KB

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