]> begriffs open source - cmsis/blob - CMSIS/Core_A/Include/cmsis_gcc.h
Add __COMPILER_BARRIER
[cmsis] / CMSIS / Core_A / Include / cmsis_gcc.h
1 /**************************************************************************//**
2  * @file     cmsis_gcc.h
3  * @brief    CMSIS compiler specific macros, functions, instructions
4  * @version  V1.1.0
5  * @date     20. December 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
25 #ifndef __CMSIS_GCC_H
26 #define __CMSIS_GCC_H
27
28 /* ignore some GCC warnings */
29 #pragma GCC diagnostic push
30 #pragma GCC diagnostic ignored "-Wsign-conversion"
31 #pragma GCC diagnostic ignored "-Wconversion"
32 #pragma GCC diagnostic ignored "-Wunused-parameter"
33
34 /* Fallback for __has_builtin */
35 #ifndef __has_builtin
36   #define __has_builtin(x) (0)
37 #endif
38
39 /* CMSIS compiler specific defines */
40 #ifndef   __ASM
41   #define __ASM                                  asm
42 #endif
43 #ifndef   __INLINE
44   #define __INLINE                               inline
45 #endif
46 #ifndef   __FORCEINLINE
47   #define __FORCEINLINE                          __attribute__((always_inline))
48 #endif
49 #ifndef   __STATIC_INLINE
50   #define __STATIC_INLINE                        static inline
51 #endif
52 #ifndef   __STATIC_FORCEINLINE
53   #define __STATIC_FORCEINLINE                   __attribute__((always_inline)) static inline
54 #endif
55 #ifndef   __NO_RETURN
56   #define __NO_RETURN                            __attribute__((__noreturn__))
57 #endif
58 #ifndef   CMSIS_DEPRECATED
59  #define  CMSIS_DEPRECATED                       __attribute__((deprecated))
60 #endif
61 #ifndef   __USED
62   #define __USED                                 __attribute__((used))
63 #endif
64 #ifndef   __WEAK
65   #define __WEAK                                 __attribute__((weak))
66 #endif
67 #ifndef   __PACKED
68   #define __PACKED                               __attribute__((packed, aligned(1)))
69 #endif
70 #ifndef   __PACKED_STRUCT
71   #define __PACKED_STRUCT                        struct __attribute__((packed, aligned(1)))
72 #endif
73 #ifndef   __UNALIGNED_UINT16_WRITE
74   #pragma GCC diagnostic push
75   #pragma GCC diagnostic ignored "-Wpacked"
76 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
77   __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
78   #pragma GCC diagnostic pop
79   #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
80 #endif
81 #ifndef   __UNALIGNED_UINT16_READ
82   #pragma GCC diagnostic push
83   #pragma GCC diagnostic ignored "-Wpacked"
84 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
85   __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
86   #pragma GCC diagnostic pop
87   #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
88 #endif
89 #ifndef   __UNALIGNED_UINT32_WRITE
90   #pragma GCC diagnostic push
91   #pragma GCC diagnostic ignored "-Wpacked"
92 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
93   __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
94   #pragma GCC diagnostic pop
95   #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
96 #endif
97 #ifndef   __UNALIGNED_UINT32_READ
98   #pragma GCC diagnostic push
99   #pragma GCC diagnostic ignored "-Wpacked"
100   __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
101   #pragma GCC diagnostic pop
102   #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
103 #endif
104 #ifndef   __ALIGNED
105   #define __ALIGNED(x)                           __attribute__((aligned(x)))
106 #endif
107 #ifndef   __COMPILER_BARRIER
108   #define __COMPILER_BARRIER()                   __ASM volatile("":::"memory")
109 #endif
110
111 /* ##########################  Core Instruction Access  ######################### */
112 /**
113   \brief   No Operation
114  */
115 #define __NOP()                             __ASM volatile ("nop")
116
117 /**
118   \brief   Wait For Interrupt
119  */
120 #define __WFI()                             __ASM volatile ("wfi")
121
122 /**
123   \brief   Wait For Event
124  */
125 #define __WFE()                             __ASM volatile ("wfe")
126
127 /**
128   \brief   Send Event
129  */
130 #define __SEV()                             __ASM volatile ("sev")
131
132 /**
133   \brief   Instruction Synchronization Barrier
134   \details Instruction Synchronization Barrier flushes the pipeline in the processor,
135            so that all instructions following the ISB are fetched from cache or memory,
136            after the instruction has been completed.
137  */
138 __STATIC_FORCEINLINE  void __ISB(void)
139 {
140   __ASM volatile ("isb 0xF":::"memory");
141 }
142
143
144 /**
145   \brief   Data Synchronization Barrier
146   \details Acts as a special kind of Data Memory Barrier.
147            It completes when all explicit memory accesses before this instruction complete.
148  */
149 __STATIC_FORCEINLINE  void __DSB(void)
150 {
151   __ASM volatile ("dsb 0xF":::"memory");
152 }
153
154 /**
155   \brief   Data Memory Barrier
156   \details Ensures the apparent order of the explicit memory operations before
157            and after the instruction, without ensuring their completion.
158  */
159 __STATIC_FORCEINLINE  void __DMB(void)
160 {
161   __ASM volatile ("dmb 0xF":::"memory");
162 }
163
164 /**
165   \brief   Reverse byte order (32 bit)
166   \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
167   \param [in]    value  Value to reverse
168   \return               Reversed value
169  */
170 __STATIC_FORCEINLINE  uint32_t __REV(uint32_t value)
171 {
172 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
173   return __builtin_bswap32(value);
174 #else
175   uint32_t result;
176
177   __ASM volatile ("rev %0, %1" : "=r" (result) : "r" (value) );
178   return result;
179 #endif
180 }
181
182 /**
183   \brief   Reverse byte order (16 bit)
184   \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
185   \param [in]    value  Value to reverse
186   \return               Reversed value
187  */
188 #ifndef __NO_EMBEDDED_ASM
189 __attribute__((section(".rev16_text"))) __STATIC_INLINE uint32_t __REV16(uint32_t value)
190 {
191   uint32_t result;
192   __ASM volatile("rev16 %0, %1" : "=r" (result) : "r" (value));
193   return result;
194 }
195 #endif
196
197 /**
198   \brief   Reverse byte order (16 bit)
199   \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
200   \param [in]    value  Value to reverse
201   \return               Reversed value
202  */
203 __STATIC_FORCEINLINE  int16_t __REVSH(int16_t value)
204 {
205 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
206   return (int16_t)__builtin_bswap16(value);
207 #else
208   int16_t result;
209
210   __ASM volatile ("revsh %0, %1" : "=r" (result) : "r" (value) );
211   return result;
212 #endif
213 }
214
215 /**
216   \brief   Rotate Right in unsigned value (32 bit)
217   \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
218   \param [in]    op1  Value to rotate
219   \param [in]    op2  Number of Bits to rotate
220   \return               Rotated value
221  */
222 __STATIC_FORCEINLINE  uint32_t __ROR(uint32_t op1, uint32_t op2)
223 {
224   op2 %= 32U;
225   if (op2 == 0U) {
226     return op1;
227   }
228   return (op1 >> op2) | (op1 << (32U - op2));
229 }
230
231
232 /**
233   \brief   Breakpoint
234   \param [in]    value  is ignored by the processor.
235                  If required, a debugger can use it to store additional information about the breakpoint.
236  */
237 #define __BKPT(value)                       __ASM volatile ("bkpt "#value)
238
239 /**
240   \brief   Reverse bit order of value
241   \details Reverses the bit order of the given value.
242   \param [in]    value  Value to reverse
243   \return               Reversed value
244  */
245 __STATIC_FORCEINLINE  uint32_t __RBIT(uint32_t value)
246 {
247   uint32_t result;
248
249 #if ((defined (__ARM_ARCH_7M__      ) && (__ARM_ARCH_7M__      == 1)) || \
250      (defined (__ARM_ARCH_7EM__     ) && (__ARM_ARCH_7EM__     == 1)) || \
251      (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1))    )
252    __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
253 #else
254   int32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
255
256   result = value;                      /* r will be reversed bits of v; first get LSB of v */
257   for (value >>= 1U; value; value >>= 1U)
258   {
259     result <<= 1U;
260     result |= value & 1U;
261     s--;
262   }
263   result <<= s;                        /* shift when v's highest bits are zero */
264 #endif
265   return result;
266 }
267
268 /**
269   \brief   Count leading zeros
270   \param [in]  value  Value to count the leading zeros
271   \return             number of leading zeros in value
272  */
273 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
274 {
275   /* Even though __builtin_clz produces a CLZ instruction on ARM, formally
276      __builtin_clz(0) is undefined behaviour, so handle this case specially.
277      This guarantees ARM-compatible results if happening to compile on a non-ARM
278      target, and ensures the compiler doesn't decide to activate any
279      optimisations using the logic "value was passed to __builtin_clz, so it
280      is non-zero".
281      ARM GCC 7.3 and possibly earlier will optimise this test away, leaving a
282      single CLZ instruction.
283    */
284   if (value == 0U)
285   {
286     return 32U;
287   }
288   return __builtin_clz(value);
289 }
290
291 /**
292   \brief   LDR Exclusive (8 bit)
293   \details Executes a exclusive LDR instruction for 8 bit value.
294   \param [in]    ptr  Pointer to data
295   \return             value of type uint8_t at (*ptr)
296  */
297 __STATIC_FORCEINLINE  uint8_t __LDREXB(volatile uint8_t *addr)
298 {
299     uint32_t result;
300
301 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
302    __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
303 #else
304     /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
305        accepted by assembler. So has to use following less efficient pattern.
306     */
307    __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
308 #endif
309    return ((uint8_t) result);    /* Add explicit type cast here */
310 }
311
312
313 /**
314   \brief   LDR Exclusive (16 bit)
315   \details Executes a exclusive LDR instruction for 16 bit values.
316   \param [in]    ptr  Pointer to data
317   \return        value of type uint16_t at (*ptr)
318  */
319 __STATIC_FORCEINLINE  uint16_t __LDREXH(volatile uint16_t *addr)
320 {
321     uint32_t result;
322
323 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
324    __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
325 #else
326     /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
327        accepted by assembler. So has to use following less efficient pattern.
328     */
329    __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
330 #endif
331    return ((uint16_t) result);    /* Add explicit type cast here */
332 }
333
334
335 /**
336   \brief   LDR Exclusive (32 bit)
337   \details Executes a exclusive LDR instruction for 32 bit values.
338   \param [in]    ptr  Pointer to data
339   \return        value of type uint32_t at (*ptr)
340  */
341 __STATIC_FORCEINLINE  uint32_t __LDREXW(volatile uint32_t *addr)
342 {
343     uint32_t result;
344
345    __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
346    return(result);
347 }
348
349
350 /**
351   \brief   STR Exclusive (8 bit)
352   \details Executes a exclusive STR instruction for 8 bit values.
353   \param [in]  value  Value to store
354   \param [in]    ptr  Pointer to location
355   \return          0  Function succeeded
356   \return          1  Function failed
357  */
358 __STATIC_FORCEINLINE  uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
359 {
360    uint32_t result;
361
362    __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
363    return(result);
364 }
365
366
367 /**
368   \brief   STR Exclusive (16 bit)
369   \details Executes a exclusive STR instruction for 16 bit values.
370   \param [in]  value  Value to store
371   \param [in]    ptr  Pointer to location
372   \return          0  Function succeeded
373   \return          1  Function failed
374  */
375 __STATIC_FORCEINLINE  uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
376 {
377    uint32_t result;
378
379    __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
380    return(result);
381 }
382
383
384 /**
385   \brief   STR Exclusive (32 bit)
386   \details Executes a exclusive STR instruction for 32 bit values.
387   \param [in]  value  Value to store
388   \param [in]    ptr  Pointer to location
389   \return          0  Function succeeded
390   \return          1  Function failed
391  */
392 __STATIC_FORCEINLINE  uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
393 {
394    uint32_t result;
395
396    __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
397    return(result);
398 }
399
400
401 /**
402   \brief   Remove the exclusive lock
403   \details Removes the exclusive lock which is created by LDREX.
404  */
405 __STATIC_FORCEINLINE  void __CLREX(void)
406 {
407   __ASM volatile ("clrex" ::: "memory");
408 }
409
410 /**
411   \brief   Signed Saturate
412   \details Saturates a signed value.
413   \param [in]  value  Value to be saturated
414   \param [in]    sat  Bit position to saturate to (1..32)
415   \return             Saturated value
416  */
417 #define __SSAT(ARG1,ARG2) \
418 __extension__ \
419 ({                          \
420   int32_t __RES, __ARG1 = (ARG1); \
421   __ASM ("ssat %0, %1, %2" : "=r" (__RES) :  "I" (ARG2), "r" (__ARG1) ); \
422   __RES; \
423  })
424
425
426 /**
427   \brief   Unsigned Saturate
428   \details Saturates an unsigned value.
429   \param [in]  value  Value to be saturated
430   \param [in]    sat  Bit position to saturate to (0..31)
431   \return             Saturated value
432  */
433 #define __USAT(ARG1,ARG2) \
434 __extension__ \
435 ({                          \
436   uint32_t __RES, __ARG1 = (ARG1); \
437   __ASM ("usat %0, %1, %2" : "=r" (__RES) :  "I" (ARG2), "r" (__ARG1) ); \
438   __RES; \
439  })
440
441 /* ###########################  Core Function Access  ########################### */
442
443 /**
444   \brief   Enable IRQ Interrupts
445   \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
446            Can only be executed in Privileged modes.
447  */
448 __STATIC_FORCEINLINE void __enable_irq(void)
449 {
450   __ASM volatile ("cpsie i" : : : "memory");
451 }
452
453 /**
454   \brief   Disable IRQ Interrupts
455   \details Disables IRQ interrupts by setting the I-bit in the CPSR.
456   Can only be executed in Privileged modes.
457  */
458 __STATIC_FORCEINLINE  void __disable_irq(void)
459 {
460   __ASM volatile ("cpsid i" : : : "memory");
461 }
462
463 /**
464   \brief   Get FPSCR
465   \details Returns the current value of the Floating Point Status/Control register.
466   \return Floating Point Status/Control register value
467 */
468 __STATIC_FORCEINLINE  uint32_t __get_FPSCR(void)
469 {
470   #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
471        (defined (__FPU_USED   ) && (__FPU_USED    == 1U))     )
472   #if __has_builtin(__builtin_arm_get_fpscr) 
473   // Re-enable using built-in when GCC has been fixed
474   // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
475     /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
476     return __builtin_arm_get_fpscr();
477   #else
478     uint32_t result;
479
480     __ASM volatile ("VMRS %0, fpscr" : "=r" (result) );
481     return(result);
482   #endif
483   #else
484     return(0U);
485   #endif
486 }
487
488 /**
489   \brief   Set FPSCR
490   \details Assigns the given value to the Floating Point Status/Control register.
491   \param [in] fpscr  Floating Point Status/Control value to set
492 */
493 __STATIC_FORCEINLINE void __set_FPSCR(uint32_t fpscr)
494 {
495   #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
496        (defined (__FPU_USED   ) && (__FPU_USED    == 1U))     )
497   #if __has_builtin(__builtin_arm_set_fpscr)
498   // Re-enable using built-in when GCC has been fixed
499   // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
500     /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
501     __builtin_arm_set_fpscr(fpscr);
502   #else
503     __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc", "memory");
504   #endif
505   #else
506     (void)fpscr;
507   #endif
508 }
509
510 /** \brief  Get CPSR Register
511     \return               CPSR Register value
512  */
513 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
514 {
515   uint32_t result;
516   __ASM volatile("MRS %0, cpsr" : "=r" (result) );
517   return(result);
518 }
519
520 /** \brief  Set CPSR Register
521     \param [in]    cpsr  CPSR value to set
522  */
523 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
524 {
525 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "memory");
526 }
527
528 /** \brief  Get Mode
529     \return                Processor Mode
530  */
531 __STATIC_FORCEINLINE uint32_t __get_mode(void)
532 {
533     return (__get_CPSR() & 0x1FU);
534 }
535
536 /** \brief  Set Mode
537     \param [in]    mode  Mode value to set
538  */
539 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
540 {
541   __ASM volatile("MSR  cpsr_c, %0" : : "r" (mode) : "memory");
542 }
543
544 /** \brief  Get Stack Pointer
545     \return Stack Pointer value
546  */
547 __STATIC_FORCEINLINE uint32_t __get_SP(void)
548 {
549   uint32_t result;
550   __ASM volatile("MOV  %0, sp" : "=r" (result) : : "memory");
551   return result;
552 }
553
554 /** \brief  Set Stack Pointer
555     \param [in]    stack  Stack Pointer value to set
556  */
557 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
558 {
559   __ASM volatile("MOV  sp, %0" : : "r" (stack) : "memory");
560 }
561
562 /** \brief  Get USR/SYS Stack Pointer
563     \return USR/SYS Stack Pointer value
564  */
565 __STATIC_FORCEINLINE uint32_t __get_SP_usr(void)
566 {
567   uint32_t cpsr = __get_CPSR();
568   uint32_t result;
569   __ASM volatile(
570     "CPS     #0x1F  \n"
571     "MOV     %0, sp   " : "=r"(result) : : "memory"
572    );
573   __set_CPSR(cpsr);
574   __ISB();
575   return result;
576 }
577
578 /** \brief  Set USR/SYS Stack Pointer
579     \param [in]    topOfProcStack  USR/SYS Stack Pointer value to set
580  */
581 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
582 {
583   uint32_t cpsr = __get_CPSR();
584   __ASM volatile(
585     "CPS     #0x1F  \n"
586     "MOV     sp, %0   " : : "r" (topOfProcStack) : "memory"
587    );
588   __set_CPSR(cpsr);
589   __ISB();
590 }
591
592 /** \brief  Get FPEXC
593     \return               Floating Point Exception Control register value
594  */
595 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
596 {
597 #if (__FPU_PRESENT == 1)
598   uint32_t result;
599   __ASM volatile("VMRS %0, fpexc" : "=r" (result) );
600   return(result);
601 #else
602   return(0);
603 #endif
604 }
605
606 /** \brief  Set FPEXC
607     \param [in]    fpexc  Floating Point Exception Control value to set
608  */
609 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
610 {
611 #if (__FPU_PRESENT == 1)
612   __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
613 #endif
614 }
615
616 /*
617  * Include common core functions to access Coprocessor 15 registers
618  */
619
620 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
621 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
622 #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm  : "=r" (Rt) : : "memory" )
623 #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm  : : "r" (Rt) : "memory" )
624
625 #include "cmsis_cp15.h"
626
627 /** \brief  Enable Floating Point Unit
628
629   Critical section, called from undef handler, so systick is disabled
630  */
631 __STATIC_INLINE void __FPU_Enable(void)
632 {
633   __ASM volatile(
634     //Permit access to VFP/NEON, registers by modifying CPACR
635     "        MRC     p15,0,R1,c1,c0,2  \n"
636     "        ORR     R1,R1,#0x00F00000 \n"
637     "        MCR     p15,0,R1,c1,c0,2  \n"
638
639     //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
640     "        ISB                       \n"
641
642     //Enable VFP/NEON
643     "        VMRS    R1,FPEXC          \n"
644     "        ORR     R1,R1,#0x40000000 \n"
645     "        VMSR    FPEXC,R1          \n"
646
647     //Initialise VFP/NEON registers to 0
648     "        MOV     R2,#0             \n"
649
650     //Initialise D16 registers to 0
651     "        VMOV    D0, R2,R2         \n"
652     "        VMOV    D1, R2,R2         \n"
653     "        VMOV    D2, R2,R2         \n"
654     "        VMOV    D3, R2,R2         \n"
655     "        VMOV    D4, R2,R2         \n"
656     "        VMOV    D5, R2,R2         \n"
657     "        VMOV    D6, R2,R2         \n"
658     "        VMOV    D7, R2,R2         \n"
659     "        VMOV    D8, R2,R2         \n"
660     "        VMOV    D9, R2,R2         \n"
661     "        VMOV    D10,R2,R2         \n"
662     "        VMOV    D11,R2,R2         \n"
663     "        VMOV    D12,R2,R2         \n"
664     "        VMOV    D13,R2,R2         \n"
665     "        VMOV    D14,R2,R2         \n"
666     "        VMOV    D15,R2,R2         \n"
667
668 #if (defined(__ARM_NEON) && (__ARM_NEON == 1))
669     //Initialise D32 registers to 0
670     "        VMOV    D16,R2,R2         \n"
671     "        VMOV    D17,R2,R2         \n"
672     "        VMOV    D18,R2,R2         \n"
673     "        VMOV    D19,R2,R2         \n"
674     "        VMOV    D20,R2,R2         \n"
675     "        VMOV    D21,R2,R2         \n"
676     "        VMOV    D22,R2,R2         \n"
677     "        VMOV    D23,R2,R2         \n"
678     "        VMOV    D24,R2,R2         \n"
679     "        VMOV    D25,R2,R2         \n"
680     "        VMOV    D26,R2,R2         \n"
681     "        VMOV    D27,R2,R2         \n"
682     "        VMOV    D28,R2,R2         \n"
683     "        VMOV    D29,R2,R2         \n"
684     "        VMOV    D30,R2,R2         \n"
685     "        VMOV    D31,R2,R2         \n"
686 #endif
687
688     //Initialise FPSCR to a known state
689     "        VMRS    R2,FPSCR          \n"
690     "        LDR     R3,=0x00086060    \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
691     "        AND     R2,R2,R3          \n"
692     "        VMSR    FPSCR,R2            "
693   );
694 }
695
696 #pragma GCC diagnostic pop
697
698 #endif /* __CMSIS_GCC_H */