cmsis_armcc.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. /**************************************************************************//**
  2. * @file cmsis_armcc.h
  3. * @brief CMSIS compiler specific macros, functions, instructions
  4. * @version V1.00
  5. * @date 22. Feb 2017
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2009-2017 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 __STATIC_INLINE
  41. #define __STATIC_INLINE static __inline
  42. #endif
  43. #ifndef __NO_RETURN
  44. #define __NO_RETURN __declspec(noreturn)
  45. #endif
  46. #ifndef __USED
  47. #define __USED __attribute__((used))
  48. #endif
  49. #ifndef __WEAK
  50. #define __WEAK __attribute__((weak))
  51. #endif
  52. #ifndef __PACKED
  53. #define __PACKED __attribute__((packed))
  54. #endif
  55. #ifndef __PACKED_STRUCT
  56. #define __PACKED_STRUCT __packed struct
  57. #endif
  58. #ifndef __UNALIGNED_UINT16_WRITE
  59. #define __UNALIGNED_UINT16_WRITE(addr, val) ((*((__packed uint16_t *)(addr))) = (val))
  60. #endif
  61. #ifndef __UNALIGNED_UINT16_READ
  62. #define __UNALIGNED_UINT16_READ(addr) (*((const __packed uint16_t *)(addr)))
  63. #endif
  64. #ifndef __UNALIGNED_UINT32_WRITE
  65. #define __UNALIGNED_UINT32_WRITE(addr, val) ((*((__packed uint32_t *)(addr))) = (val))
  66. #endif
  67. #ifndef __UNALIGNED_UINT32_READ
  68. #define __UNALIGNED_UINT32_READ(addr) (*((const __packed uint32_t *)(addr)))
  69. #endif
  70. #ifndef __ALIGNED
  71. #define __ALIGNED(x) __attribute__((aligned(x)))
  72. #endif
  73. #ifndef __PACKED
  74. #define __PACKED __attribute__((packed))
  75. #endif
  76. /* ########################### Core Function Access ########################### */
  77. /**
  78. \brief Get FPSCR
  79. \return Floating Point Status/Control register value
  80. */
  81. __STATIC_INLINE uint32_t __get_FPSCR(void)
  82. {
  83. #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
  84. (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
  85. register uint32_t __regfpscr __ASM("fpscr");
  86. return(__regfpscr);
  87. #else
  88. return(0U);
  89. #endif
  90. }
  91. /**
  92. \brief Set FPSCR
  93. \param [in] fpscr Floating Point Status/Control value to set
  94. */
  95. __STATIC_INLINE void __set_FPSCR(uint32_t fpscr)
  96. {
  97. #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
  98. (defined (__FPU_USED ) && (__FPU_USED == 1U)) )
  99. register uint32_t __regfpscr __ASM("fpscr");
  100. __regfpscr = (fpscr);
  101. #else
  102. (void)fpscr;
  103. #endif
  104. }
  105. /* ########################## Core Instruction Access ######################### */
  106. /**
  107. \brief No Operation
  108. */
  109. #define __NOP __nop
  110. /**
  111. \brief Wait For Interrupt
  112. */
  113. #define __WFI __wfi
  114. /**
  115. \brief Wait For Event
  116. */
  117. #define __WFE __wfe
  118. /**
  119. \brief Send Event
  120. */
  121. #define __SEV __sev
  122. /**
  123. \brief Instruction Synchronization Barrier
  124. */
  125. #define __ISB() do {\
  126. __schedule_barrier();\
  127. __isb(0xF);\
  128. __schedule_barrier();\
  129. } while (0U)
  130. /**
  131. \brief Data Synchronization Barrier
  132. */
  133. #define __DSB() do {\
  134. __schedule_barrier();\
  135. __dsb(0xF);\
  136. __schedule_barrier();\
  137. } while (0U)
  138. /**
  139. \brief Data Memory Barrier
  140. */
  141. #define __DMB() do {\
  142. __schedule_barrier();\
  143. __dmb(0xF);\
  144. __schedule_barrier();\
  145. } while (0U)
  146. /**
  147. \brief Reverse byte order (32 bit)
  148. \param [in] value Value to reverse
  149. \return Reversed value
  150. */
  151. #define __REV __rev
  152. /**
  153. \brief Reverse byte order (16 bit)
  154. \param [in] value Value to reverse
  155. \return Reversed value
  156. */
  157. #ifndef __NO_EMBEDDED_ASM
  158. __attribute__((section(".rev16_text"))) __STATIC_INLINE __ASM uint32_t __REV16(uint32_t value)
  159. {
  160. rev16 r0, r0
  161. bx lr
  162. }
  163. #endif
  164. /**
  165. \brief Reverse byte order in signed short value
  166. \param [in] value Value to reverse
  167. \return Reversed value
  168. */
  169. #ifndef __NO_EMBEDDED_ASM
  170. __attribute__((section(".revsh_text"))) __STATIC_INLINE __ASM int32_t __REVSH(int32_t value)
  171. {
  172. revsh r0, r0
  173. bx lr
  174. }
  175. #endif
  176. /**
  177. \brief Rotate Right in unsigned value (32 bit)
  178. \param [in] op1 Value to rotate
  179. \param [in] op2 Number of Bits to rotate
  180. \return Rotated value
  181. */
  182. #define __ROR __ror
  183. /**
  184. \brief Breakpoint
  185. \param [in] value is ignored by the processor.
  186. If required, a debugger can use it to store additional information about the breakpoint.
  187. */
  188. #define __BKPT(value) __breakpoint(value)
  189. /**
  190. \brief Reverse bit order of value
  191. \param [in] value Value to reverse
  192. \return Reversed value
  193. */
  194. #define __RBIT __rbit
  195. /**
  196. \brief Count leading zeros
  197. \param [in] value Value to count the leading zeros
  198. \return number of leading zeros in value
  199. */
  200. #define __CLZ __clz
  201. /**
  202. \brief LDR Exclusive (8 bit)
  203. \details Executes a exclusive LDR instruction for 8 bit value.
  204. \param [in] ptr Pointer to data
  205. \return value of type uint8_t at (*ptr)
  206. */
  207. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
  208. #define __LDREXB(ptr) ((uint8_t ) __ldrex(ptr))
  209. #else
  210. #define __LDREXB(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint8_t ) __ldrex(ptr)) _Pragma("pop")
  211. #endif
  212. /**
  213. \brief LDR Exclusive (16 bit)
  214. \details Executes a exclusive LDR instruction for 16 bit values.
  215. \param [in] ptr Pointer to data
  216. \return value of type uint16_t at (*ptr)
  217. */
  218. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
  219. #define __LDREXH(ptr) ((uint16_t) __ldrex(ptr))
  220. #else
  221. #define __LDREXH(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint16_t) __ldrex(ptr)) _Pragma("pop")
  222. #endif
  223. /**
  224. \brief LDR Exclusive (32 bit)
  225. \details Executes a exclusive LDR instruction for 32 bit values.
  226. \param [in] ptr Pointer to data
  227. \return value of type uint32_t at (*ptr)
  228. */
  229. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
  230. #define __LDREXW(ptr) ((uint32_t ) __ldrex(ptr))
  231. #else
  232. #define __LDREXW(ptr) _Pragma("push") _Pragma("diag_suppress 3731") ((uint32_t ) __ldrex(ptr)) _Pragma("pop")
  233. #endif
  234. /**
  235. \brief STR Exclusive (8 bit)
  236. \details Executes a exclusive STR instruction for 8 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 __STREXB(value, ptr) __strex(value, ptr)
  244. #else
  245. #define __STREXB(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
  246. #endif
  247. /**
  248. \brief STR Exclusive (16 bit)
  249. \details Executes a exclusive STR instruction for 16 bit values.
  250. \param [in] value Value to store
  251. \param [in] ptr Pointer to location
  252. \return 0 Function succeeded
  253. \return 1 Function failed
  254. */
  255. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
  256. #define __STREXH(value, ptr) __strex(value, ptr)
  257. #else
  258. #define __STREXH(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
  259. #endif
  260. /**
  261. \brief STR Exclusive (32 bit)
  262. \details Executes a exclusive STR instruction for 32 bit values.
  263. \param [in] value Value to store
  264. \param [in] ptr Pointer to location
  265. \return 0 Function succeeded
  266. \return 1 Function failed
  267. */
  268. #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION < 5060020)
  269. #define __STREXW(value, ptr) __strex(value, ptr)
  270. #else
  271. #define __STREXW(value, ptr) _Pragma("push") _Pragma("diag_suppress 3731") __strex(value, ptr) _Pragma("pop")
  272. #endif
  273. /**
  274. \brief Remove the exclusive lock
  275. \details Removes the exclusive lock which is created by LDREX.
  276. */
  277. #define __CLREX __clrex
  278. /** \brief Get CPSR Register
  279. \return CPSR Register value
  280. */
  281. __STATIC_INLINE uint32_t __get_CPSR(void)
  282. {
  283. register uint32_t __regCPSR __ASM("cpsr");
  284. return(__regCPSR);
  285. }
  286. /** \brief Set CPSR Register
  287. \param [in] cpsr CPSR value to set
  288. */
  289. __STATIC_INLINE void __set_CPSR(uint32_t cpsr)
  290. {
  291. register uint32_t __regCPSR __ASM("cpsr");
  292. __regCPSR = cpsr;
  293. }
  294. /** \brief Get Mode
  295. \return Processor Mode
  296. */
  297. __STATIC_INLINE uint32_t __get_mode(void) {
  298. return (__get_CPSR() & 0x1FU);
  299. }
  300. /** \brief Set Mode
  301. \param [in] mode Mode value to set
  302. */
  303. __STATIC_INLINE __ASM void __set_mode(uint32_t mode) {
  304. MOV r1, lr
  305. MSR CPSR_C, r0
  306. BX r1
  307. }
  308. /** \brief Set Stack Pointer
  309. \param [in] stack Stack Pointer value to set
  310. */
  311. __STATIC_INLINE __ASM void __set_SP(uint32_t stack)
  312. {
  313. MOV sp, r0
  314. BX lr
  315. }
  316. /** \brief Set USR/SYS Stack Pointer
  317. \param [in] topOfProcStack USR/SYS Stack Pointer value to set
  318. */
  319. __STATIC_INLINE __ASM void __set_SP_usr(uint32_t topOfProcStack)
  320. {
  321. ARM
  322. PRESERVE8
  323. BIC R0, R0, #7 ;ensure stack is 8-byte aligned
  324. MRS R1, CPSR
  325. CPS #0x1F ;no effect in USR mode
  326. MOV SP, R0
  327. MSR CPSR_c, R1 ;no effect in USR mode
  328. ISB
  329. BX LR
  330. }
  331. /** \brief Get FPEXC register
  332. \return Floating Point Exception Control register value
  333. */
  334. __STATIC_INLINE uint32_t __get_FPEXC(void)
  335. {
  336. #if (__FPU_PRESENT == 1)
  337. register uint32_t __regfpexc __ASM("fpexc");
  338. return(__regfpexc);
  339. #else
  340. return(0);
  341. #endif
  342. }
  343. /** \brief Set FPEXC register
  344. \param [in] fpexc Floating Point Exception Control value to set
  345. */
  346. __STATIC_INLINE void __set_FPEXC(uint32_t fpexc)
  347. {
  348. #if (__FPU_PRESENT == 1)
  349. register uint32_t __regfpexc __ASM("fpexc");
  350. __regfpexc = (fpexc);
  351. #endif
  352. }
  353. /** \brief Get ACTLR register
  354. \return Auxiliary Control register value
  355. */
  356. __STATIC_INLINE uint32_t __get_ACTLR(void)
  357. {
  358. register uint32_t __regACTLR __ASM("cp15:0:c1:c0:1");
  359. return __regACTLR;
  360. }
  361. /** \brief Set ACTLR register
  362. \param [in] actlr Auxiliary Control value to set
  363. */
  364. __STATIC_INLINE void __set_ACTLR(uint32_t actlr)
  365. {
  366. register uint32_t __regACTLR __ASM("cp15:0:c1:c0:1");
  367. __regACTLR = actlr;
  368. }
  369. /** \brief Get CPACR register
  370. \return Coprocessor Access Control register value
  371. */
  372. __STATIC_INLINE uint32_t __get_CPACR(void)
  373. {
  374. register uint32_t __regCPACR __ASM("cp15:0:c1:c0:2");
  375. return __regCPACR;
  376. }
  377. /** \brief Set CPACR register
  378. \param [in] cpacr Coprocessor Access Control value to set
  379. */
  380. __STATIC_INLINE void __set_CPACR(uint32_t cpacr)
  381. {
  382. register uint32_t __regCPACR __ASM("cp15:0:c1:c0:2");
  383. __regCPACR = cpacr;
  384. }
  385. /** \brief Get CBAR register
  386. \return Configuration Base Address register value
  387. */
  388. __STATIC_INLINE uint32_t __get_CBAR() {
  389. register uint32_t __regCBAR __ASM("cp15:4:c15:c0:0");
  390. return(__regCBAR);
  391. }
  392. /** \brief Get TTBR0 register
  393. This function returns the value of the Translation Table Base Register 0.
  394. \return Translation Table Base Register 0 value
  395. */
  396. __STATIC_INLINE uint32_t __get_TTBR0() {
  397. register uint32_t __regTTBR0 __ASM("cp15:0:c2:c0:0");
  398. return(__regTTBR0);
  399. }
  400. /** \brief Set TTBR0 register
  401. This function assigns the given value to the Translation Table Base Register 0.
  402. \param [in] ttbr0 Translation Table Base Register 0 value to set
  403. */
  404. __STATIC_INLINE void __set_TTBR0(uint32_t ttbr0) {
  405. register uint32_t __regTTBR0 __ASM("cp15:0:c2:c0:0");
  406. __regTTBR0 = ttbr0;
  407. }
  408. /** \brief Get DACR register
  409. This function returns the value of the Domain Access Control Register.
  410. \return Domain Access Control Register value
  411. */
  412. __STATIC_INLINE uint32_t __get_DACR() {
  413. register uint32_t __regDACR __ASM("cp15:0:c3:c0:0");
  414. return(__regDACR);
  415. }
  416. /** \brief Set DACR register
  417. This function assigns the given value to the Domain Access Control Register.
  418. \param [in] dacr Domain Access Control Register value to set
  419. */
  420. __STATIC_INLINE void __set_DACR(uint32_t dacr) {
  421. register uint32_t __regDACR __ASM("cp15:0:c3:c0:0");
  422. __regDACR = dacr;
  423. }
  424. /** \brief Set the \ref CMSIS_SCTLR "System Control Register" register.
  425. \param [in] sctlr System Control Register value to set
  426. */
  427. __STATIC_INLINE void __set_SCTLR(uint32_t sctlr)
  428. {
  429. register uint32_t __regSCTLR __ASM("cp15:0:c1:c0:0");
  430. __regSCTLR = sctlr;
  431. }
  432. /** \brief Get SCTLR register
  433. \return System Control Register value
  434. */
  435. __STATIC_INLINE uint32_t __get_SCTLR() {
  436. register uint32_t __regSCTLR __ASM("cp15:0:c1:c0:0");
  437. return(__regSCTLR);
  438. }
  439. /** \brief Set ACTRL register
  440. \param [in] actrl Auxiliary Control Register value to set
  441. */
  442. __STATIC_INLINE void __set_ACTRL(uint32_t actrl)
  443. {
  444. register uint32_t __regACTRL __ASM("cp15:0:c1:c0:1");
  445. __regACTRL = actrl;
  446. }
  447. /** \brief Get ACTRL register
  448. \return Auxiliary Control Register value
  449. */
  450. __STATIC_INLINE uint32_t __get_ACTRL(void)
  451. {
  452. register uint32_t __regACTRL __ASM("cp15:0:c1:c0:1");
  453. return(__regACTRL);
  454. }
  455. /** \brief Get MPIDR register
  456. \return Multiprocessor Affinity Register value
  457. */
  458. __STATIC_INLINE uint32_t __get_MPIDR(void)
  459. {
  460. register uint32_t __regMPIDR __ASM("cp15:0:c0:c0:5");
  461. return(__regMPIDR);
  462. }
  463. /** \brief Get VBAR register
  464. \return Vector Base Address Register
  465. */
  466. __STATIC_INLINE uint32_t __get_VBAR(void)
  467. {
  468. register uint32_t __regVBAR __ASM("cp15:0:c12:c0:0");
  469. return(__regVBAR);
  470. }
  471. /** \brief Set VBAR register
  472. \param [in] vbar Vector Base Address Register value to set
  473. */
  474. __STATIC_INLINE void __set_VBAR(uint32_t vbar)
  475. {
  476. register uint32_t __regVBAR __ASM("cp15:0:c12:c0:0");
  477. __regVBAR = vbar;
  478. }
  479. /** \brief Set CNTFRQ register
  480. \param [in] value CNTFRQ Register value to set
  481. */
  482. __STATIC_INLINE void __set_CNTFRQ(uint32_t value) {
  483. register uint32_t __regCNTFRQ __ASM("cp15:0:c14:c0:0");
  484. __regCNTFRQ = value;
  485. }
  486. /** \brief Set CNTP_TVAL register
  487. \param [in] value CNTP_TVAL Register value to set
  488. */
  489. __STATIC_INLINE void __set_CNTP_TVAL(uint32_t value) {
  490. register uint32_t __regCNTP_TVAL __ASM("cp15:0:c14:c2:0");
  491. __regCNTP_TVAL = value;
  492. }
  493. /** \brief Get CNTP_TVAL register
  494. \return CNTP_TVAL Register value
  495. */
  496. __STATIC_INLINE uint32_t __get_CNTP_TVAL() {
  497. register uint32_t __regCNTP_TVAL __ASM("cp15:0:c14:c2:0");
  498. return(__regCNTP_TVAL);
  499. }
  500. /** \brief Set CNTP_CTL register
  501. \param [in] value CNTP_CTL Register value to set
  502. */
  503. __STATIC_INLINE void __set_CNTP_CTL(uint32_t value) {
  504. register uint32_t __regCNTP_CTL __ASM("cp15:0:c14:c2:1");
  505. __regCNTP_CTL = value;
  506. }
  507. /** \brief Set TLBIALL register
  508. */
  509. __STATIC_INLINE void __set_TLBIALL(uint32_t value) {
  510. register uint32_t __TLBIALL __ASM("cp15:0:c8:c7:0");
  511. __TLBIALL = value;
  512. }
  513. /** \brief Set Branch Predictor Invalidate All register.
  514. * \param [in] value New register value to be set.
  515. */
  516. __STATIC_INLINE void __set_BPIALL(uint32_t value) {
  517. register uint32_t __BPIALL __ASM("cp15:0:c7:c5:6");
  518. __BPIALL = value;
  519. }
  520. /** \brief Set ICIALLU register
  521. */
  522. __STATIC_INLINE void __set_ICIALLU(uint32_t value) {
  523. register uint32_t __ICIALLU __ASM("cp15:0:c7:c5:0");
  524. __ICIALLU = value;
  525. }
  526. /** \brief Set DCCMVAC register
  527. */
  528. __STATIC_INLINE void __set_DCCMVAC(uint32_t value) {
  529. register uint32_t __DCCMVAC __ASM("cp15:0:c7:c10:1");
  530. __DCCMVAC = value;
  531. }
  532. /** \brief Set DCIMVAC register
  533. */
  534. __STATIC_INLINE void __set_DCIMVAC(uint32_t value) {
  535. register uint32_t __DCIMVAC __ASM("cp15:0:c7:c6:1");
  536. __DCIMVAC = value;
  537. }
  538. /** \brief Set DCCIMVAC register
  539. */
  540. __STATIC_INLINE void __set_DCCIMVAC(uint32_t value) {
  541. register uint32_t __DCCIMVAC __ASM("cp15:0:c7:c14:1");
  542. __DCCIMVAC = value;
  543. }
  544. /** \brief Clean and Invalidate the entire data or unified cache
  545. * \param op 0 - invalidate, 1 - clean, otherwise - invalidate and clean
  546. */
  547. __STATIC_INLINE __ASM void __L1C_CleanInvalidateCache(uint32_t op) {
  548. ARM
  549. PUSH {R4-R11}
  550. MRC p15, 1, R6, c0, c0, 1 // Read CLIDR
  551. ANDS R3, R6, #0x07000000 // Extract coherency level
  552. MOV R3, R3, LSR #23 // Total cache levels << 1
  553. BEQ Finished // If 0, no need to clean
  554. MOV R10, #0 // R10 holds current cache level << 1
  555. Loop1 ADD R2, R10, R10, LSR #1 // R2 holds cache "Set" position
  556. MOV R1, R6, LSR R2 // Bottom 3 bits are the Cache-type for this level
  557. AND R1, R1, #7 // Isolate those lower 3 bits
  558. CMP R1, #2
  559. BLT Skip // No cache or only instruction cache at this level
  560. MCR p15, 2, R10, c0, c0, 0 // Write the Cache Size selection register
  561. ISB // ISB to sync the change to the CacheSizeID reg
  562. MRC p15, 1, R1, c0, c0, 0 // Reads current Cache Size ID register
  563. AND R2, R1, #7 // Extract the line length field
  564. ADD R2, R2, #4 // Add 4 for the line length offset (log2 16 bytes)
  565. LDR R4, =0x3FF
  566. ANDS R4, R4, R1, LSR #3 // R4 is the max number on the way size (right aligned)
  567. CLZ R5, R4 // R5 is the bit position of the way size increment
  568. LDR R7, =0x7FFF
  569. ANDS R7, R7, R1, LSR #13 // R7 is the max number of the index size (right aligned)
  570. Loop2 MOV R9, R4 // R9 working copy of the max way size (right aligned)
  571. Loop3 ORR R11, R10, R9, LSL R5 // Factor in the Way number and cache number into R11
  572. ORR R11, R11, R7, LSL R2 // Factor in the Set number
  573. CMP R0, #0
  574. BNE Dccsw
  575. MCR p15, 0, R11, c7, c6, 2 // DCISW. Invalidate by Set/Way
  576. B cont
  577. Dccsw CMP R0, #1
  578. BNE Dccisw
  579. MCR p15, 0, R11, c7, c10, 2 // DCCSW. Clean by Set/Way
  580. B cont
  581. Dccisw MCR p15, 0, R11, c7, c14, 2 // DCCISW. Clean and Invalidate by Set/Way
  582. cont SUBS R9, R9, #1 // Decrement the Way number
  583. BGE Loop3
  584. SUBS R7, R7, #1 // Decrement the Set number
  585. BGE Loop2
  586. Skip ADD R10, R10, #2 // Increment the cache number
  587. CMP R3, R10
  588. BGT Loop1
  589. Finished
  590. DSB
  591. POP {R4-R11}
  592. BX lr
  593. }
  594. /** \brief Enable Floating Point Unit
  595. Critical section, called from undef handler, so systick is disabled
  596. */
  597. __STATIC_INLINE __ASM void __FPU_Enable(void) {
  598. ARM
  599. //Permit access to VFP/NEON, registers by modifying CPACR
  600. MRC p15,0,R1,c1,c0,2
  601. ORR R1,R1,#0x00F00000
  602. MCR p15,0,R1,c1,c0,2
  603. //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
  604. ISB
  605. //Enable VFP/NEON
  606. VMRS R1,FPEXC
  607. ORR R1,R1,#0x40000000
  608. VMSR FPEXC,R1
  609. //Initialise VFP/NEON registers to 0
  610. MOV R2,#0
  611. IF {TARGET_FEATURE_EXTENSION_REGISTER_COUNT} >= 16
  612. //Initialise D16 registers to 0
  613. VMOV D0, R2,R2
  614. VMOV D1, R2,R2
  615. VMOV D2, R2,R2
  616. VMOV D3, R2,R2
  617. VMOV D4, R2,R2
  618. VMOV D5, R2,R2
  619. VMOV D6, R2,R2
  620. VMOV D7, R2,R2
  621. VMOV D8, R2,R2
  622. VMOV D9, R2,R2
  623. VMOV D10,R2,R2
  624. VMOV D11,R2,R2
  625. VMOV D12,R2,R2
  626. VMOV D13,R2,R2
  627. VMOV D14,R2,R2
  628. VMOV D15,R2,R2
  629. ENDIF
  630. IF {TARGET_FEATURE_EXTENSION_REGISTER_COUNT} == 32
  631. //Initialise D32 registers to 0
  632. VMOV D16,R2,R2
  633. VMOV D17,R2,R2
  634. VMOV D18,R2,R2
  635. VMOV D19,R2,R2
  636. VMOV D20,R2,R2
  637. VMOV D21,R2,R2
  638. VMOV D22,R2,R2
  639. VMOV D23,R2,R2
  640. VMOV D24,R2,R2
  641. VMOV D25,R2,R2
  642. VMOV D26,R2,R2
  643. VMOV D27,R2,R2
  644. VMOV D28,R2,R2
  645. VMOV D29,R2,R2
  646. VMOV D30,R2,R2
  647. VMOV D31,R2,R2
  648. ENDIF
  649. //Initialise FPSCR to a known state
  650. VMRS R2,FPSCR
  651. LDR R3,=0x00086060 //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
  652. AND R2,R2,R3
  653. VMSR FPSCR,R2
  654. BX LR
  655. }
  656. #endif /* __CMSIS_ARMCC_H */