]> begriffs open source - cmsis/blob - CMSIS/Core_A/Include/cmsis_gcc.h
Updated v7-M and v8-M examples:
[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.3.0
5  * @date     17. December 2019
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2009-2019 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
41 #ifndef   __ASM
42   #define __ASM                                  __asm
43 #endif
44 #ifndef   __INLINE
45   #define __INLINE                               inline
46 #endif
47 #ifndef   __FORCEINLINE
48   #define __FORCEINLINE                          __attribute__((always_inline))
49 #endif
50 #ifndef   __STATIC_INLINE
51   #define __STATIC_INLINE                        static inline
52 #endif
53 #ifndef   __STATIC_FORCEINLINE
54   #define __STATIC_FORCEINLINE                   __attribute__((always_inline)) static inline
55 #endif
56 #ifndef   __NO_RETURN
57   #define __NO_RETURN                            __attribute__((__noreturn__))
58 #endif
59 #ifndef   CMSIS_DEPRECATED
60  #define  CMSIS_DEPRECATED                       __attribute__((deprecated))
61 #endif
62 #ifndef   __USED
63   #define __USED                                 __attribute__((used))
64 #endif
65 #ifndef   __WEAK
66   #define __WEAK                                 __attribute__((weak))
67 #endif
68 #ifndef   __PACKED
69   #define __PACKED                               __attribute__((packed, aligned(1)))
70 #endif
71 #ifndef   __PACKED_STRUCT
72   #define __PACKED_STRUCT                        struct __attribute__((packed, aligned(1)))
73 #endif
74 #ifndef   __UNALIGNED_UINT16_WRITE
75   #pragma GCC diagnostic push
76   #pragma GCC diagnostic ignored "-Wpacked"
77 /*lint -esym(9058, T_UINT16_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_WRITE */
78   __PACKED_STRUCT T_UINT16_WRITE { uint16_t v; };
79   #pragma GCC diagnostic pop
80   #define __UNALIGNED_UINT16_WRITE(addr, val)    (void)((((struct T_UINT16_WRITE *)(void *)(addr))->v) = (val))
81 #endif
82 #ifndef   __UNALIGNED_UINT16_READ
83   #pragma GCC diagnostic push
84   #pragma GCC diagnostic ignored "-Wpacked"
85 /*lint -esym(9058, T_UINT16_READ)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT16_READ */
86   __PACKED_STRUCT T_UINT16_READ { uint16_t v; };
87   #pragma GCC diagnostic pop
88   #define __UNALIGNED_UINT16_READ(addr)          (((const struct T_UINT16_READ *)(const void *)(addr))->v)
89 #endif
90 #ifndef   __UNALIGNED_UINT32_WRITE
91   #pragma GCC diagnostic push
92   #pragma GCC diagnostic ignored "-Wpacked"
93 /*lint -esym(9058, T_UINT32_WRITE)*/ /* disable MISRA 2012 Rule 2.4 for T_UINT32_WRITE */
94   __PACKED_STRUCT T_UINT32_WRITE { uint32_t v; };
95   #pragma GCC diagnostic pop
96   #define __UNALIGNED_UINT32_WRITE(addr, val)    (void)((((struct T_UINT32_WRITE *)(void *)(addr))->v) = (val))
97 #endif
98 #ifndef   __UNALIGNED_UINT32_READ
99   #pragma GCC diagnostic push
100   #pragma GCC diagnostic ignored "-Wpacked"
101   __PACKED_STRUCT T_UINT32_READ { uint32_t v; };
102   #pragma GCC diagnostic pop
103   #define __UNALIGNED_UINT32_READ(addr)          (((const struct T_UINT32_READ *)(const void *)(addr))->v)
104 #endif
105 #ifndef   __ALIGNED
106   #define __ALIGNED(x)                           __attribute__((aligned(x)))
107 #endif
108 #ifndef   __COMPILER_BARRIER
109   #define __COMPILER_BARRIER()                   __ASM volatile("":::"memory")
110 #endif
111
112
113 __STATIC_FORCEINLINE uint32_t __QSUB16(uint32_t op1, uint32_t op2)
114 {
115   uint32_t result;
116
117   __ASM volatile ("qsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
118   return(result);
119 }
120
121
122 __STATIC_FORCEINLINE uint32_t __QSUB8(uint32_t op1, uint32_t op2)
123 {
124   uint32_t result;
125
126   __ASM volatile ("qsub8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
127   return(result);
128 }
129
130
131 __STATIC_FORCEINLINE uint32_t __QADD16(uint32_t op1, uint32_t op2)
132 {
133   uint32_t result;
134
135   __ASM volatile ("qadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
136   return(result);
137 }
138
139 __STATIC_FORCEINLINE uint32_t __QADD8(uint32_t op1, uint32_t op2)
140 {
141   uint32_t result;
142
143   __ASM volatile ("qadd8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
144   return(result);
145 }
146
147 __STATIC_FORCEINLINE  int32_t __QADD( int32_t op1,  int32_t op2)
148 {
149   int32_t result;
150
151   __ASM volatile ("qadd %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
152   return(result);
153 }
154
155 __STATIC_FORCEINLINE uint32_t __QSAX(uint32_t op1, uint32_t op2)
156 {
157   uint32_t result;
158
159   __ASM ("qsax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
160   return(result);
161 }
162
163 __STATIC_FORCEINLINE uint32_t __SHSAX(uint32_t op1, uint32_t op2)
164 {
165   uint32_t result;
166
167   __ASM ("shsax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
168   return(result);
169 }
170
171 __STATIC_FORCEINLINE uint64_t __SMLALD (uint32_t op1, uint32_t op2, uint64_t acc)
172 {
173   union llreg_u{
174     uint32_t w32[2];
175     uint64_t w64;
176   } llr;
177   llr.w64 = acc;
178
179 #ifndef __ARMEB__   /* Little endian */
180   __ASM volatile ("smlald %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) );
181 #else               /* Big endian */
182   __ASM volatile ("smlald %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) );
183 #endif
184
185   return(llr.w64);
186 }
187
188 __STATIC_FORCEINLINE  int32_t __QSUB( int32_t op1,  int32_t op2)
189 {
190   int32_t result;
191
192   __ASM volatile ("qsub %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
193   return(result);
194 }
195
196 __STATIC_FORCEINLINE uint32_t __SXTB16(uint32_t op1)
197 {
198   uint32_t result;
199
200   __ASM ("sxtb16 %0, %1" : "=r" (result) : "r" (op1));
201   return(result);
202 }
203
204
205 __STATIC_FORCEINLINE uint32_t __SMUAD  (uint32_t op1, uint32_t op2)
206 {
207   uint32_t result;
208
209   __ASM volatile ("smuad %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
210   return(result);
211 }
212
213
214
215 #define __PKHBT(ARG1,ARG2,ARG3)          ( ((((uint32_t)(ARG1))          ) & 0x0000FFFFUL) |  \
216                                            ((((uint32_t)(ARG2)) << (ARG3)) & 0xFFFF0000UL)  )
217
218 #define __PKHTB(ARG1,ARG2,ARG3)          ( ((((uint32_t)(ARG1))          ) & 0xFFFF0000UL) |  \
219                                            ((((uint32_t)(ARG2)) >> (ARG3)) & 0x0000FFFFUL)  )
220
221 __STATIC_FORCEINLINE uint32_t __SMLAD (uint32_t op1, uint32_t op2, uint32_t op3)
222 {
223   uint32_t result;
224
225   __ASM volatile ("smlad %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) );
226   return(result);
227 }
228
229 __STATIC_FORCEINLINE uint32_t __SMUADX (uint32_t op1, uint32_t op2)
230 {
231   uint32_t result;
232
233   __ASM volatile ("smuadx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
234   return(result);
235 }
236
237 __STATIC_FORCEINLINE uint32_t __SMLADX (uint32_t op1, uint32_t op2, uint32_t op3)
238 {
239   uint32_t result;
240
241   __ASM volatile ("smladx %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) );
242   return(result);
243 }
244
245 __STATIC_FORCEINLINE uint64_t __SMLALDX (uint32_t op1, uint32_t op2, uint64_t acc)
246 {
247   union llreg_u{
248     uint32_t w32[2];
249     uint64_t w64;
250   } llr;
251   llr.w64 = acc;
252
253 #ifndef __ARMEB__   /* Little endian */
254   __ASM volatile ("smlaldx %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) );
255 #else               /* Big endian */
256   __ASM volatile ("smlaldx %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) );
257 #endif
258
259   return(llr.w64);
260 }
261
262 __STATIC_FORCEINLINE int32_t __SMMLA (int32_t op1, int32_t op2, int32_t op3)
263 {
264  int32_t result;
265
266  __ASM volatile ("smmla %0, %1, %2, %3" : "=r" (result): "r"  (op1), "r" (op2), "r" (op3) );
267  return(result);
268 }
269
270 __STATIC_FORCEINLINE uint32_t __SMUSD  (uint32_t op1, uint32_t op2)
271 {
272   uint32_t result;
273
274   __ASM volatile ("smusd %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
275   return(result);
276 }
277
278 __STATIC_FORCEINLINE uint32_t __SMUSDX (uint32_t op1, uint32_t op2)
279 {
280   uint32_t result;
281
282   __ASM volatile ("smusdx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
283   return(result);
284 }
285
286 __STATIC_FORCEINLINE uint32_t __QASX(uint32_t op1, uint32_t op2)
287 {
288   uint32_t result;
289
290   __ASM ("qasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
291   return(result);
292 }
293
294 __STATIC_FORCEINLINE uint32_t __SHADD16(uint32_t op1, uint32_t op2)
295 {
296   uint32_t result;
297
298   __ASM ("shadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
299   return(result);
300 }
301
302 __STATIC_FORCEINLINE uint32_t __SHSUB16(uint32_t op1, uint32_t op2)
303 {
304   uint32_t result;
305
306   __ASM ("shsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
307   return(result);
308 }
309
310 __STATIC_FORCEINLINE uint32_t __SHASX(uint32_t op1, uint32_t op2)
311 {
312   uint32_t result;
313
314   __ASM ("shasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) );
315   return(result);
316 }
317
318 __STATIC_FORCEINLINE uint32_t __SMLSDX (uint32_t op1, uint32_t op2, uint32_t op3)
319 {
320   uint32_t result;
321
322   __ASM volatile ("smlsdx %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) );
323   return(result);
324 }
325
326
327 /* ##########################  Core Instruction Access  ######################### */
328 /**
329   \brief   No Operation
330  */
331 #define __NOP()                             __ASM volatile ("nop")
332
333 /**
334   \brief   Wait For Interrupt
335  */
336 #define __WFI()                             __ASM volatile ("wfi":::"memory")
337
338 /**
339   \brief   Wait For Event
340  */
341 #define __WFE()                             __ASM volatile ("wfe":::"memory")
342
343 /**
344   \brief   Send Event
345  */
346 #define __SEV()                             __ASM volatile ("sev")
347
348 /**
349   \brief   Instruction Synchronization Barrier
350   \details Instruction Synchronization Barrier flushes the pipeline in the processor,
351            so that all instructions following the ISB are fetched from cache or memory,
352            after the instruction has been completed.
353  */
354 __STATIC_FORCEINLINE  void __ISB(void)
355 {
356   __ASM volatile ("isb 0xF":::"memory");
357 }
358
359
360 /**
361   \brief   Data Synchronization Barrier
362   \details Acts as a special kind of Data Memory Barrier.
363            It completes when all explicit memory accesses before this instruction complete.
364  */
365 __STATIC_FORCEINLINE  void __DSB(void)
366 {
367   __ASM volatile ("dsb 0xF":::"memory");
368 }
369
370 /**
371   \brief   Data Memory Barrier
372   \details Ensures the apparent order of the explicit memory operations before
373            and after the instruction, without ensuring their completion.
374  */
375 __STATIC_FORCEINLINE  void __DMB(void)
376 {
377   __ASM volatile ("dmb 0xF":::"memory");
378 }
379
380 /**
381   \brief   Reverse byte order (32 bit)
382   \details Reverses the byte order in unsigned integer value. For example, 0x12345678 becomes 0x78563412.
383   \param [in]    value  Value to reverse
384   \return               Reversed value
385  */
386 __STATIC_FORCEINLINE  uint32_t __REV(uint32_t value)
387 {
388 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
389   return __builtin_bswap32(value);
390 #else
391   uint32_t result;
392
393   __ASM ("rev %0, %1" : "=r" (result) : "r" (value) );
394   return result;
395 #endif
396 }
397
398 /**
399   \brief   Reverse byte order (16 bit)
400   \details Reverses the byte order within each halfword of a word. For example, 0x12345678 becomes 0x34127856.
401   \param [in]    value  Value to reverse
402   \return               Reversed value
403  */
404 __STATIC_FORCEINLINE uint32_t __REV16(uint32_t value)
405 {
406   uint32_t result;
407   __ASM ("rev16 %0, %1" : "=r" (result) : "r" (value));
408   return result;
409 }
410
411 /**
412   \brief   Reverse byte order (16 bit)
413   \details Reverses the byte order in a 16-bit value and returns the signed 16-bit result. For example, 0x0080 becomes 0x8000.
414   \param [in]    value  Value to reverse
415   \return               Reversed value
416  */
417 __STATIC_FORCEINLINE  int16_t __REVSH(int16_t value)
418 {
419 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
420   return (int16_t)__builtin_bswap16(value);
421 #else
422   int16_t result;
423
424   __ASM ("revsh %0, %1" : "=r" (result) : "r" (value) );
425   return result;
426 #endif
427 }
428
429 /**
430   \brief   Rotate Right in unsigned value (32 bit)
431   \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits.
432   \param [in]    op1  Value to rotate
433   \param [in]    op2  Number of Bits to rotate
434   \return               Rotated value
435  */
436 __STATIC_FORCEINLINE  uint32_t __ROR(uint32_t op1, uint32_t op2)
437 {
438   op2 %= 32U;
439   if (op2 == 0U) {
440     return op1;
441   }
442   return (op1 >> op2) | (op1 << (32U - op2));
443 }
444
445
446 /**
447   \brief   Breakpoint
448   \param [in]    value  is ignored by the processor.
449                  If required, a debugger can use it to store additional information about the breakpoint.
450  */
451 #define __BKPT(value)                       __ASM volatile ("bkpt "#value)
452
453 /**
454   \brief   Reverse bit order of value
455   \details Reverses the bit order of the given value.
456   \param [in]    value  Value to reverse
457   \return               Reversed value
458  */
459 __STATIC_FORCEINLINE  uint32_t __RBIT(uint32_t value)
460 {
461   uint32_t result;
462
463 #if ((defined (__ARM_ARCH_7M__      ) && (__ARM_ARCH_7M__      == 1)) || \
464      (defined (__ARM_ARCH_7EM__     ) && (__ARM_ARCH_7EM__     == 1)) || \
465      (defined (__ARM_ARCH_8M_MAIN__ ) && (__ARM_ARCH_8M_MAIN__ == 1))    )
466    __ASM ("rbit %0, %1" : "=r" (result) : "r" (value) );
467 #else
468   int32_t s = (4U /*sizeof(v)*/ * 8U) - 1U; /* extra shift needed at end */
469
470   result = value;                      /* r will be reversed bits of v; first get LSB of v */
471   for (value >>= 1U; value; value >>= 1U)
472   {
473     result <<= 1U;
474     result |= value & 1U;
475     s--;
476   }
477   result <<= s;                        /* shift when v's highest bits are zero */
478 #endif
479   return result;
480 }
481
482 /**
483   \brief   Count leading zeros
484   \param [in]  value  Value to count the leading zeros
485   \return             number of leading zeros in value
486  */
487 __STATIC_FORCEINLINE uint8_t __CLZ(uint32_t value)
488 {
489   /* Even though __builtin_clz produces a CLZ instruction on ARM, formally
490      __builtin_clz(0) is undefined behaviour, so handle this case specially.
491      This guarantees ARM-compatible results if happening to compile on a non-ARM
492      target, and ensures the compiler doesn't decide to activate any
493      optimisations using the logic "value was passed to __builtin_clz, so it
494      is non-zero".
495      ARM GCC 7.3 and possibly earlier will optimise this test away, leaving a
496      single CLZ instruction.
497    */
498   if (value == 0U)
499   {
500     return 32U;
501   }
502   return __builtin_clz(value);
503 }
504
505 /**
506   \brief   LDR Exclusive (8 bit)
507   \details Executes a exclusive LDR instruction for 8 bit value.
508   \param [in]    ptr  Pointer to data
509   \return             value of type uint8_t at (*ptr)
510  */
511 __STATIC_FORCEINLINE  uint8_t __LDREXB(volatile uint8_t *addr)
512 {
513     uint32_t result;
514
515 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
516    __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) );
517 #else
518     /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
519        accepted by assembler. So has to use following less efficient pattern.
520     */
521    __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
522 #endif
523    return ((uint8_t) result);    /* Add explicit type cast here */
524 }
525
526
527 /**
528   \brief   LDR Exclusive (16 bit)
529   \details Executes a exclusive LDR instruction for 16 bit values.
530   \param [in]    ptr  Pointer to data
531   \return        value of type uint16_t at (*ptr)
532  */
533 __STATIC_FORCEINLINE  uint16_t __LDREXH(volatile uint16_t *addr)
534 {
535     uint32_t result;
536
537 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8)
538    __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) );
539 #else
540     /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not
541        accepted by assembler. So has to use following less efficient pattern.
542     */
543    __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" );
544 #endif
545    return ((uint16_t) result);    /* Add explicit type cast here */
546 }
547
548
549 /**
550   \brief   LDR Exclusive (32 bit)
551   \details Executes a exclusive LDR instruction for 32 bit values.
552   \param [in]    ptr  Pointer to data
553   \return        value of type uint32_t at (*ptr)
554  */
555 __STATIC_FORCEINLINE  uint32_t __LDREXW(volatile uint32_t *addr)
556 {
557     uint32_t result;
558
559    __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) );
560    return(result);
561 }
562
563
564 /**
565   \brief   STR Exclusive (8 bit)
566   \details Executes a exclusive STR instruction for 8 bit values.
567   \param [in]  value  Value to store
568   \param [in]    ptr  Pointer to location
569   \return          0  Function succeeded
570   \return          1  Function failed
571  */
572 __STATIC_FORCEINLINE  uint32_t __STREXB(uint8_t value, volatile uint8_t *addr)
573 {
574    uint32_t result;
575
576    __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
577    return(result);
578 }
579
580
581 /**
582   \brief   STR Exclusive (16 bit)
583   \details Executes a exclusive STR instruction for 16 bit values.
584   \param [in]  value  Value to store
585   \param [in]    ptr  Pointer to location
586   \return          0  Function succeeded
587   \return          1  Function failed
588  */
589 __STATIC_FORCEINLINE  uint32_t __STREXH(uint16_t value, volatile uint16_t *addr)
590 {
591    uint32_t result;
592
593    __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) );
594    return(result);
595 }
596
597
598 /**
599   \brief   STR Exclusive (32 bit)
600   \details Executes a exclusive STR instruction for 32 bit values.
601   \param [in]  value  Value to store
602   \param [in]    ptr  Pointer to location
603   \return          0  Function succeeded
604   \return          1  Function failed
605  */
606 __STATIC_FORCEINLINE  uint32_t __STREXW(uint32_t value, volatile uint32_t *addr)
607 {
608    uint32_t result;
609
610    __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) );
611    return(result);
612 }
613
614
615 /**
616   \brief   Remove the exclusive lock
617   \details Removes the exclusive lock which is created by LDREX.
618  */
619 __STATIC_FORCEINLINE  void __CLREX(void)
620 {
621   __ASM volatile ("clrex" ::: "memory");
622 }
623
624 /**
625   \brief   Signed Saturate
626   \details Saturates a signed value.
627   \param [in]  value  Value to be saturated
628   \param [in]    sat  Bit position to saturate to (1..32)
629   \return             Saturated value
630  */
631 #define __SSAT(ARG1, ARG2) \
632 __extension__ \
633 ({                          \
634   int32_t __RES, __ARG1 = (ARG1); \
635   __ASM volatile ("ssat %0, %1, %2" : "=r" (__RES) :  "I" (ARG2), "r" (__ARG1) : "cc" ); \
636   __RES; \
637  })
638
639
640 /**
641   \brief   Unsigned Saturate
642   \details Saturates an unsigned value.
643   \param [in]  value  Value to be saturated
644   \param [in]    sat  Bit position to saturate to (0..31)
645   \return             Saturated value
646  */
647 #define __USAT(ARG1, ARG2) \
648 __extension__ \
649 ({                          \
650   uint32_t __RES, __ARG1 = (ARG1); \
651   __ASM volatile ("usat %0, %1, %2" : "=r" (__RES) :  "I" (ARG2), "r" (__ARG1) : "cc" ); \
652   __RES; \
653  })
654
655 /* ###########################  Core Function Access  ########################### */
656
657 /**
658   \brief   Enable IRQ Interrupts
659   \details Enables IRQ interrupts by clearing the I-bit in the CPSR.
660            Can only be executed in Privileged modes.
661  */
662 __STATIC_FORCEINLINE void __enable_irq(void)
663 {
664   __ASM volatile ("cpsie i" : : : "memory");
665 }
666
667 /**
668   \brief   Disable IRQ Interrupts
669   \details Disables IRQ interrupts by setting the I-bit in the CPSR.
670   Can only be executed in Privileged modes.
671  */
672 __STATIC_FORCEINLINE  void __disable_irq(void)
673 {
674   __ASM volatile ("cpsid i" : : : "memory");
675 }
676
677 /**
678   \brief   Get FPSCR
679   \details Returns the current value of the Floating Point Status/Control register.
680   \return Floating Point Status/Control register value
681 */
682 __STATIC_FORCEINLINE  uint32_t __get_FPSCR(void)
683 {
684   #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
685        (defined (__FPU_USED   ) && (__FPU_USED    == 1U))     )
686   #if __has_builtin(__builtin_arm_get_fpscr) 
687   // Re-enable using built-in when GCC has been fixed
688   // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
689     /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
690     return __builtin_arm_get_fpscr();
691   #else
692     uint32_t result;
693
694     __ASM volatile ("VMRS %0, fpscr" : "=r" (result) );
695     return(result);
696   #endif
697   #else
698     return(0U);
699   #endif
700 }
701
702 /**
703   \brief   Set FPSCR
704   \details Assigns the given value to the Floating Point Status/Control register.
705   \param [in] fpscr  Floating Point Status/Control value to set
706 */
707 __STATIC_FORCEINLINE void __set_FPSCR(uint32_t fpscr)
708 {
709   #if ((defined (__FPU_PRESENT) && (__FPU_PRESENT == 1U)) && \
710        (defined (__FPU_USED   ) && (__FPU_USED    == 1U))     )
711   #if __has_builtin(__builtin_arm_set_fpscr)
712   // Re-enable using built-in when GCC has been fixed
713   // || (__GNUC__ > 7) || (__GNUC__ == 7 && __GNUC_MINOR__ >= 2)
714     /* see https://gcc.gnu.org/ml/gcc-patches/2017-04/msg00443.html */
715     __builtin_arm_set_fpscr(fpscr);
716   #else
717     __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc", "memory");
718   #endif
719   #else
720     (void)fpscr;
721   #endif
722 }
723
724 /** \brief  Get CPSR Register
725     \return               CPSR Register value
726  */
727 __STATIC_FORCEINLINE uint32_t __get_CPSR(void)
728 {
729   uint32_t result;
730   __ASM volatile("MRS %0, cpsr" : "=r" (result) );
731   return(result);
732 }
733
734 /** \brief  Set CPSR Register
735     \param [in]    cpsr  CPSR value to set
736  */
737 __STATIC_FORCEINLINE void __set_CPSR(uint32_t cpsr)
738 {
739 __ASM volatile ("MSR cpsr, %0" : : "r" (cpsr) : "cc", "memory");
740 }
741
742 /** \brief  Get Mode
743     \return                Processor Mode
744  */
745 __STATIC_FORCEINLINE uint32_t __get_mode(void)
746 {
747     return (__get_CPSR() & 0x1FU);
748 }
749
750 /** \brief  Set Mode
751     \param [in]    mode  Mode value to set
752  */
753 __STATIC_FORCEINLINE void __set_mode(uint32_t mode)
754 {
755   __ASM volatile("MSR  cpsr_c, %0" : : "r" (mode) : "memory");
756 }
757
758 /** \brief  Get Stack Pointer
759     \return Stack Pointer value
760  */
761 __STATIC_FORCEINLINE uint32_t __get_SP(void)
762 {
763   uint32_t result;
764   __ASM volatile("MOV  %0, sp" : "=r" (result) : : "memory");
765   return result;
766 }
767
768 /** \brief  Set Stack Pointer
769     \param [in]    stack  Stack Pointer value to set
770  */
771 __STATIC_FORCEINLINE void __set_SP(uint32_t stack)
772 {
773   __ASM volatile("MOV  sp, %0" : : "r" (stack) : "memory");
774 }
775
776 /** \brief  Get USR/SYS Stack Pointer
777     \return USR/SYS Stack Pointer value
778  */
779 __STATIC_FORCEINLINE uint32_t __get_SP_usr(void)
780 {
781   uint32_t cpsr = __get_CPSR();
782   uint32_t result;
783   __ASM volatile(
784     "CPS     #0x1F  \n"
785     "MOV     %0, sp   " : "=r"(result) : : "memory"
786    );
787   __set_CPSR(cpsr);
788   __ISB();
789   return result;
790 }
791
792 /** \brief  Set USR/SYS Stack Pointer
793     \param [in]    topOfProcStack  USR/SYS Stack Pointer value to set
794  */
795 __STATIC_FORCEINLINE void __set_SP_usr(uint32_t topOfProcStack)
796 {
797   uint32_t cpsr = __get_CPSR();
798   __ASM volatile(
799     "CPS     #0x1F  \n"
800     "MOV     sp, %0   " : : "r" (topOfProcStack) : "memory"
801    );
802   __set_CPSR(cpsr);
803   __ISB();
804 }
805
806 /** \brief  Get FPEXC
807     \return               Floating Point Exception Control register value
808  */
809 __STATIC_FORCEINLINE uint32_t __get_FPEXC(void)
810 {
811 #if (__FPU_PRESENT == 1)
812   uint32_t result;
813   __ASM volatile("VMRS %0, fpexc" : "=r" (result) );
814   return(result);
815 #else
816   return(0);
817 #endif
818 }
819
820 /** \brief  Set FPEXC
821     \param [in]    fpexc  Floating Point Exception Control value to set
822  */
823 __STATIC_FORCEINLINE void __set_FPEXC(uint32_t fpexc)
824 {
825 #if (__FPU_PRESENT == 1)
826   __ASM volatile ("VMSR fpexc, %0" : : "r" (fpexc) : "memory");
827 #endif
828 }
829
830 /*
831  * Include common core functions to access Coprocessor 15 registers
832  */
833
834 #define __get_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MRC p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : "=r" (Rt) : : "memory" )
835 #define __set_CP(cp, op1, Rt, CRn, CRm, op2) __ASM volatile("MCR p" # cp ", " # op1 ", %0, c" # CRn ", c" # CRm ", " # op2 : : "r" (Rt) : "memory" )
836 #define __get_CP64(cp, op1, Rt, CRm) __ASM volatile("MRRC p" # cp ", " # op1 ", %Q0, %R0, c" # CRm  : "=r" (Rt) : : "memory" )
837 #define __set_CP64(cp, op1, Rt, CRm) __ASM volatile("MCRR p" # cp ", " # op1 ", %Q0, %R0, c" # CRm  : : "r" (Rt) : "memory" )
838
839 #include "cmsis_cp15.h"
840
841 /** \brief  Enable Floating Point Unit
842
843   Critical section, called from undef handler, so systick is disabled
844  */
845 __STATIC_INLINE void __FPU_Enable(void)
846 {
847   __ASM volatile(
848     //Permit access to VFP/NEON, registers by modifying CPACR
849     "        MRC     p15,0,R1,c1,c0,2  \n"
850     "        ORR     R1,R1,#0x00F00000 \n"
851     "        MCR     p15,0,R1,c1,c0,2  \n"
852
853     //Ensure that subsequent instructions occur in the context of VFP/NEON access permitted
854     "        ISB                       \n"
855
856     //Enable VFP/NEON
857     "        VMRS    R1,FPEXC          \n"
858     "        ORR     R1,R1,#0x40000000 \n"
859     "        VMSR    FPEXC,R1          \n"
860
861     //Initialise VFP/NEON registers to 0
862     "        MOV     R2,#0             \n"
863
864     //Initialise D16 registers to 0
865     "        VMOV    D0, R2,R2         \n"
866     "        VMOV    D1, R2,R2         \n"
867     "        VMOV    D2, R2,R2         \n"
868     "        VMOV    D3, R2,R2         \n"
869     "        VMOV    D4, R2,R2         \n"
870     "        VMOV    D5, R2,R2         \n"
871     "        VMOV    D6, R2,R2         \n"
872     "        VMOV    D7, R2,R2         \n"
873     "        VMOV    D8, R2,R2         \n"
874     "        VMOV    D9, R2,R2         \n"
875     "        VMOV    D10,R2,R2         \n"
876     "        VMOV    D11,R2,R2         \n"
877     "        VMOV    D12,R2,R2         \n"
878     "        VMOV    D13,R2,R2         \n"
879     "        VMOV    D14,R2,R2         \n"
880     "        VMOV    D15,R2,R2         \n"
881
882 #if (defined(__ARM_NEON) && (__ARM_NEON == 1))
883     //Initialise D32 registers to 0
884     "        VMOV    D16,R2,R2         \n"
885     "        VMOV    D17,R2,R2         \n"
886     "        VMOV    D18,R2,R2         \n"
887     "        VMOV    D19,R2,R2         \n"
888     "        VMOV    D20,R2,R2         \n"
889     "        VMOV    D21,R2,R2         \n"
890     "        VMOV    D22,R2,R2         \n"
891     "        VMOV    D23,R2,R2         \n"
892     "        VMOV    D24,R2,R2         \n"
893     "        VMOV    D25,R2,R2         \n"
894     "        VMOV    D26,R2,R2         \n"
895     "        VMOV    D27,R2,R2         \n"
896     "        VMOV    D28,R2,R2         \n"
897     "        VMOV    D29,R2,R2         \n"
898     "        VMOV    D30,R2,R2         \n"
899     "        VMOV    D31,R2,R2         \n"
900 #endif
901
902     //Initialise FPSCR to a known state
903     "        VMRS    R1,FPSCR          \n"
904     "        LDR     R2,=0x00086060    \n" //Mask off all bits that do not have to be preserved. Non-preserved bits can/should be zero.
905     "        AND     R1,R1,R2          \n"
906     "        VMSR    FPSCR,R1            "
907     : : : "cc", "r1", "r2"
908   );
909 }
910
911 #pragma GCC diagnostic pop
912
913 #endif /* __CMSIS_GCC_H */