cmsis_armcc.h 16 KB

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